You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
4
4
featuredBlog: false
5
5
priority: null
6
6
author: Matt Glissmann
@@ -16,46 +16,44 @@ tags:
16
16
17
17
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.
18
18
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/)
22
22
23
23
In this post, I’ll cover content regarding adding a theme toggle button, including:
24
-
24
+
25
25
* Introducing the `themeMode` prop, which allows specifying which version of the theme the app renders.
26
26
27
27
* Adding a button to the interface to serve as a control to toggle the value that gets passed to `themeMode`.
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
+
34
35
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.
39
36
37
+
In App.js:
40
38
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.
Below the `<List>`, add a theme toggle button with some formatting props and an `onClick` handler.
60
58
61
59
```javascript
@@ -67,23 +65,22 @@ Below the `<List>`, add a theme toggle button with some formatting props and an
67
65
onClick={() => {}}
68
66
/>
69
67
```
68
+
70
69
## Enable Toggling of ThemeMode’s State
70
+
71
71
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).
74
72
73
+
Create variable `darkMode` and its state using React’s [`useState` Hook](https://reactjs.org/docs/hooks-state.html).
Finally, to better demonstrate a changing theme, let’s add some more interesting visuals to the application.
110
107
111
108
Remove the following from App.js
112
109
113
-
114
110
```javascript
115
-
116
111
<Paragraph>We will be modifying this project to:</Paragraph>
117
112
<List data={projectTasks}>
118
113
{(task, index) => (
@@ -125,16 +120,13 @@ Remove the following from App.js
125
120
126
121
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.
127
122
128
-
129
123
```javascript
130
124
import { DemoSection } from"./DemoSection";
131
125
```
132
126
133
127
Then add DemoSection directly beneath this button.
134
128
135
-
136
129
```javascript
137
-
138
130
<Button
139
131
label="Toggle Theme"
140
132
primary
@@ -150,7 +142,7 @@ At this point, your code and resulting app should resemble what is shown in this
150
142

151
143
152
144
As quick review, here’s what we’ve done to modify the app:
153
-
145
+
154
146
* Added `themeMode` as a prop on the `<Grommet>` component. The value provided to `themeMode` specifies which mode of the theme to use.
155
147
156
148
* 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:
159
151
160
152
* Added a `<Button>` to serve as control to toggle the theme mode by toggling the state of `darkMode`.
161
153
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