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
This is the final post in a three-part series. Parts [1](/blog/dark-mode-theming-in-grommet-how-to-set-up-and-apply-a-theme) and [2](/blog/dark-mode-theming-in-grommet-adding-dark-and-light-theme-modes) introduced how to apply a theme, change the theme’s mode between light and dark, and, finally, provide an app’s user the ability to change the theme’s mode to their preference.
14
16
15
-
-[Part 1 - How to set up and apply a theme](/blog/dark-mode-theming-in-grommet-how-to-set-up-and-apply-a-theme)
16
-
-[Part 2 - Adding dark and light theme modes](/blog/dark-mode-theming-in-grommet-adding-dark-and-light-theme-modes)
17
-
- Part 3 - Theme color customization
17
+
*[Part 1 - How to set up and apply a theme](/blog/dark-mode-theming-in-grommet-how-to-set-up-and-apply-a-theme)
18
+
*[Part 2 - Adding dark and light theme modes](/blog/dark-mode-theming-in-grommet-adding-dark-and-light-theme-modes)
19
+
* Part 3 - Theme color customization
18
20
19
21
In this final post, I’ll demonstrate how to add custom light and dark mode colors to the theme. I will cover the following content:
20
-
- Creating a custom theme file
21
-
- Defining the color palette
22
-
- Mapping the color palette to component definitions in the theme
23
-
- Merging the customizations with an existing theme
22
+
23
+
* Creating a custom theme file
24
+
* Defining the color palette
25
+
* Mapping the color palette to component definitions in the theme
26
+
* Merging the customizations with an existing theme
24
27
28
+
25
29
## Customizing the Theme
30
+
26
31
Up to this point, we have been using Grommet’s theme. Let’s say, however, we’d like to tweak the Grommet theme by adding some custom colors for my fictional company, Acme, Inc.
27
-
32
+
28
33
To do this, continue modifying your app from where [Part 2](/blog/dark-mode-theming-in-grommet-adding-dark-and-light-theme-modes) concluded, or reference this [Codesandbox](https://codesandbox.io/s/grommet-theme-toggle-2addtogglebutton-txbux?file=/src/App.js) if you are catching up and joining midstream.
29
-
34
+
30
35
Acme’s brand colors are Ruby, Gold, and Amethyst, with some warm greys for backgrounds and text. The hex values for Acme’s color palette, plus values for the light and dark variants, are provided below.
To begin the custom Acme, Inc. theme, **create a theme file**, **define the color palette**, and then **map the color palette** to the Grommet components for which you want the colors to be applied.
36
-
40
+
37
41
## Create Theme File
42
+
38
43
In the `src` directory, create a theme file called `acme-theme.js` with the theme object `acme` as an export:
Define the color palette by naming each color, plus their dark and light variants. Colors are defined in the theme object’s `global.colors` property. For each color, the following structure is used:
44
48
49
+
Define the color palette by naming each color, plus their dark and light variants. Colors are defined in the theme object’s `global.colors` property. For each color, the following structure is used:
45
50
46
51
```markdown
47
52
uniqueColorName: {
@@ -56,9 +61,8 @@ Notice that any Grommet component with color as an attribute can accept either a
56
61
Additionally, by convention, a color name followed by a ‘!’ bang represents the color’s “true” value rather than a dark/light variant.
57
62
58
63
Add Acme, Inc. colors to `acme-theme.js` like so:
59
-
60
64
61
-
```javascript
65
+
```javascript
62
66
exportconstacme= {
63
67
global: {
64
68
colors: {
@@ -85,12 +89,13 @@ export const acme = {
85
89
}
86
90
};
87
91
```
92
+
88
93
## <aid="head3"></a>Map Colors to Grommet Namespaces and Components
94
+
89
95
Now that the colors are defined, it’s time to apply them. For the purposes of this tutorial, I will only map the colors to a handful of theme properties. This will demonstrate the process of implementation, but it is certainly not exhaustive. For the curious, inspecting [Grommet’s base theme](https://github.com/grommet/grommet/blob/master/src/js/themes/base.js) is a great place to take inventory of the many possibilities to fully customize your own theme.
90
96
91
97
Modify `acme-theme.js` by mapping the colors to background, brand, control, and anchor properties:
92
98
93
-
94
99
```javascript
95
100
exportconstacme= {
96
101
global: {
@@ -161,21 +166,20 @@ export const acme = {
161
166
```
162
167
163
168
## Merging a Custom Theme with an Existing Theme
169
+
164
170
In `App.js`, add the following imports:
171
+
172
+
*`import { deepMerge } from “grommet/utils”;` - A function allowing an existing theme to be customized or extended
-`import { DemoSection } from “./DemoSection”;` - A section of sample components to see how theme customizations are applied
175
+
*`import { DemoSection } from “./DemoSection”;` - A section of sample components to see how theme customizations are applied
171
176
172
-
Then, add the line `const theme = deepMerge(grommet, acme)`. The imported `deepMerge` function incorporates the `acme` specifications into the `grommet` theme, resulting in a new custom `theme`.
173
177
178
+
Then, add the line `const theme = deepMerge(grommet, acme)`. The imported `deepMerge` function incorporates the `acme` specifications into the `grommet` theme, resulting in a new custom `theme`.
@@ -184,27 +188,26 @@ import { DemoSection } from "./DemoSection";
184
188
consttheme=deepMerge(grommet, acme);
185
189
```
186
190
187
-
188
191
Finally, swap out the `grommet` theme with the newly created `theme`.
189
192
190
-
191
193
```javascript
192
194
<Grommet full theme={theme} themeMode={darkMode ?"dark":"light"}>
193
195
```
194
196
195
-
196
197
That concludes this tutorial. Your final code and resulting app should resemble this [Codesandbox](https://codesandbox.io/s/grommet-theme-toggle-3customizeourtheme-9wqfb?file=/src/App.js). I hope you have enjoyed this three-part tutorial.
197
198
198
199
As review, here’s how the app was modified:
199
200
200
-
- Created a custom theme file.
201
-
- Defined the color palette and its namespaces.
202
-
- Mapped the color namespaces to Grommet namespaces and component definitions.
203
-
- Finally, merged the theme customizations with Grommet’s theme.
201
+
* Created a custom theme file.
202
+
* Defined the color palette and its namespaces.
203
+
* Mapped the color namespaces to Grommet namespaces and component definitions.
204
+
* Finally, merged the theme customizations with Grommet’s theme.
204
205
206
+
205
207
## Next Steps for Exploration
208
+
206
209
Now that you have seen how easy it is to apply a theme to Grommet, set and toggle the theme’s light/dark modes, and even start applying custom colors to your own theme, here are some great next steps you can take:
207
210
208
-
- Check out [Grommet’s Theme Designer (Beta)](https://theme-designer.grommet.io/) and other [Grommet Tools](https://tools.grommet.io/).
209
-
- Explore Grommet’s other theme properties which can be customized.
210
-
- Create and apply your own theme to your own project, then share it on Grommet’s [#i-made-this](https://grommet.slack.com/archives/CG25TE0KZ) Slack channel for community members to enjoy.
211
+
* Check out [Grommet’s Theme Designer](https://theme-designer.grommet.io/)(Beta) and other [Grommet Resources](https://developer.hpe.com/platform/grommet/home/).
212
+
* Explore Grommet’s other theme properties which can be customized.
213
+
* Create and apply your own theme to your own project, then share it on Grommet’s [\#i-made-this](https://grommet.slack.com/archives/CG25TE0KZ) Slack channel for community members to enjoy.
0 commit comments