Skip to content

Commit 6b08532

Browse files
authored
Merge pull request #2235 from sulaymon-tajudeen-hpe/cms/sulaymon-tajudeen-hpe/hpe-dev-portal/blog/dark-mode-theming-in-grommet-adding-dark-and-light-theme-modes
Update Blog “dark-mode-theming-in-grommet-adding-dark-and-light-theme-modes”
2 parents 1e76148 + eabc362 commit 6b08532

File tree

1 file changed

+21
-29
lines changed

1 file changed

+21
-29
lines changed
Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: "Dark Mode Theming in Grommet: Adding dark and light theme modes"
3-
date: 2020-10-21T12:49:17.679Z
2+
title: "Dark Mode Theming in Grommet - Part 2: Adding dark and light theme modes"
3+
date: 2023-12-15T15:46:57.782Z
44
featuredBlog: false
55
priority: null
66
author: Matt Glissmann
@@ -16,46 +16,44 @@ tags:
1616

1717
In [Part 1](/blog/dark-mode-theming-in-grommet-how-to-set-up-and-apply-a-theme) of this 3-part series on Grommet’s support for light and dark modes, I covered setting up a simple Grommet app and applying a theme to that app. Here in Part 2, I’ll guide you through the steps required to implement dark/light theme modes. At the conclusion of this post, the app will have some basic UI components and a control to toggle the interface between light and dark modes.
1818

19-
- [Part 1 - How to set up and apply a Theme](/blog/dark-mode-theming-in-grommet-how-to-set-up-and-apply-a-theme)
20-
- Part 2 - Adding dark and light theme modes
21-
- Part 3 - Theme color customizations
19+
* [Part 1 - How to set up and apply a Theme](/blog/dark-mode-theming-in-grommet-how-to-set-up-and-apply-a-theme)
20+
* Part 2 - Adding dark and light theme modes
21+
* [Part 3 - Theme color customizations](https://developer.hpe.com/blog/dark-mode-theming-in-grommet-theme-color-customization/)
2222

2323
In this post, I’ll cover content regarding adding a theme toggle button, including:
24-
24+
2525
* Introducing the `themeMode` prop, which allows specifying which version of the theme the app renders.
2626

2727
* Adding a button to the interface to serve as a control to toggle the value that gets passed to `themeMode`.
2828

2929
![f10thememodetoggle](https://hpe-developer-portal.s3.amazonaws.com/uploads/media/2020/9/f10thememodetoggle-1603286872853.gif)
3030

3131
## Adding a Theme Toggle Button
32+
3233
For this exercise, you’ll continue modifying your app from the example you created in Part 1 of this series. If you are catching up and joining midstream, you can reference this [Codesandbox](https://codesandbox.io/s/grommet-theme-toggle-1adding-theme-rg91i?file=/src/App.js).
33-
34+
3435
First, I’m going to show you how to add a button that, when clicked, will toggle the theme between light and dark modes.
35-
36-
In App.js:
37-
38-
Add the [_**themeMode**_](https://v2.grommet.io/grommet#themeMode) prop to the `<Grommet>` component and set its value to `"dark"`. The value referenced by `themeMode` specifies whether Grommet should use the dark or light versions of the theme.
3936

37+
In App.js:
4038

39+
Add the ***[themeMode](https://v2.grommet.io/grommet#themeMode)*** prop to the `<Grommet>` component and set its value to `"dark"`. The value referenced by `themeMode` specifies whether Grommet should use the dark or light versions of the theme.
4140

4241
```javascript
43-
<Grommet full theme={theme} themeMode="dark">
42+
<Grommet full theme={grommet} themeMode="dark">
4443
```
4544

4645
This should result in:
4746

48-
4947
![f4part 2 toggle](https://hpe-developer-portal.s3.amazonaws.com/uploads/media/2020/9/f4part-2-toggle-1603286827841.png)
5048

5149
Next, add a button to serve as the toggle control.
52-
50+
5351
Import `Button` as a component from Grommet
54-
5552

5653
```javascript
5754
import { Grommet, Anchor, Box, Button, List, Heading, Paragraph, Text } from "grommet";
5855
```
56+
5957
Below the `<List>`, add a theme toggle button with some formatting props and an `onClick` handler.
6058

6159
```javascript
@@ -67,23 +65,22 @@ Below the `<List>`, add a theme toggle button with some formatting props and an
6765
onClick={() => {}}
6866
/>
6967
```
68+
7069
## Enable Toggling of ThemeMode’s State
70+
7171
Next, make the theme mode dynamic by adding a variable `darkMode` to hold the current theme mode, storing it in the component’s state, and adjusting the state each time the theme toggle button is clicked.
72-
73-
Create variable `darkMode` and its state using React’s [`useState` Hook](https://reactjs.org/docs/hooks-state.html).
7472

73+
Create variable `darkMode` and its state using React’s [`useState` Hook](https://reactjs.org/docs/hooks-state.html).
7574

7675
```javascript
7776
const App = () => {
7877
const [darkMode, setDarkMode] = React.useState(false);
7978
return (
8079
<Grommet full theme={grommet} themeMode="dark">
81-
8280
```
8381
8482
Modify the button’s `onClick` handler to toggle `darkMode` between `true` and `false`.
8583
86-
8784
```html
8885
<Button
8986
label="Toggle Theme"
@@ -92,27 +89,25 @@ Modify the button’s `onClick` handler to toggle `darkMode` between `true` and
9289
margin="large"
9390
onClick={() => setDarkMode(!darkMode)}
9491
/>
95-
9692
```
9793
9894
Next, replace `themeMode`’s value to be “dark” when `darkMode` is true, and “light” when `darkMode` is false.
9995
100-
```javascript
101-
<Grommet full theme={theme} themeMode={darkMode ? "dark" : "light"} >
96+
```javascript
97+
<Grommet full theme={grommet} themeMode={darkMode ? "dark" : "light"} >
10298
```
10399
104100
The theme mode toggling should be good to go. Give the toggle button a few clicks!
105101
106102
![f2thememodetogglemid](https://hpe-developer-portal.s3.amazonaws.com/uploads/media/2020/9/f2thememodetogglemid-1603286807584.gif)
107103
108104
## Incorporating More Visual Interest
105+
109106
Finally, to better demonstrate a changing theme, let’s add some more interesting visuals to the application.
110107
111108
Remove the following from App.js
112109
113-
114110
```javascript
115-
116111
<Paragraph>We will be modifying this project to:</Paragraph>
117112
<List data={projectTasks}>
118113
{(task, index) => (
@@ -125,16 +120,13 @@ Remove the following from App.js
125120
126121
Then, import the DemoSection from `DemoSection.js` and add it below the toggle button. DemoSection contains a sampling of Grommet components to better demonstrate the effect themeMode has across components.
127122
128-
129123
```javascript
130124
import { DemoSection } from "./DemoSection";
131125
```
132126
133127
Then add DemoSection directly beneath this button.
134128
135-
136129
```javascript
137-
138130
<Button
139131
label="Toggle Theme"
140132
primary
@@ -150,7 +142,7 @@ At this point, your code and resulting app should resemble what is shown in this
150142
![f14part 2 non animated white theme toggle](https://hpe-developer-portal.s3.amazonaws.com/uploads/media/2020/9/f14part-2-non-animated-white-theme-toggle-1603286900031.png)
151143
152144
As quick review, here’s what we’ve done to modify the app:
153-
145+
154146
* Added `themeMode` as a prop on the `<Grommet>` component. The value provided to `themeMode` specifies which mode of the theme to use.
155147
156148
* Created a state variable called `darkMode` to store whether the theme should currently be in dark mode.
@@ -159,4 +151,4 @@ As quick review, here’s what we’ve done to modify the app:
159151
160152
* Added a `<Button>` to serve as control to toggle the theme mode by toggling the state of `darkMode`.
161153
162-
* Lastly, made the app interface a bit more interesting to demonstrate how various components are affected by toggling the `themeMode`.That’s it for Part 2! In Part 3, I’ll demonstrate how to customize a theme with custom dark and light mode colors. Don’t forget to check back at the HPE DEV blog to catch Part 3 of this series. Again, if you have any questions, please feel free to reach out to me and others in the Grommet group on our Slack channel.
154+
* Lastly, made the app interface a bit more interesting to demonstrate how various components are affected by toggling the `themeMode`. That’s it for Part 2! In [Part 3](https://developer.hpe.com/blog/dark-mode-theming-in-grommet-theme-color-customization/), I’ll demonstrate how to customize a theme with custom dark and light mode colors. Don’t forget to check back at the HPE DEV blog to catch [Part 3](https://developer.hpe.com/blog/dark-mode-theming-in-grommet-theme-color-customization/) of this series. Again, if you have any questions, please feel free to reach out to me and others in the Grommet group on our Slack channel.

0 commit comments

Comments
 (0)