Skip to content

Commit a8aafb0

Browse files
authored
Merge pull request #2236 from sulaymon-tajudeen-hpe/cms/sulaymon-tajudeen-hpe/hpe-dev-portal/blog/dark-mode-theming-in-grommet-theme-color-customization
Update Blog “dark-mode-theming-in-grommet-theme-color-customization”
2 parents 6b08532 + 3f06ee3 commit a8aafb0

File tree

1 file changed

+43
-40
lines changed

1 file changed

+43
-40
lines changed

content/blog/dark-mode-theming-in-grommet-theme-color-customization.md

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,52 @@
11
---
2-
title: "Dark Mode Theming in Grommet: Theme color customization"
3-
date: 2020-10-28T13:39:20.480Z
4-
author: Matt Glissmann
5-
tags: ["grommet","opensource"]
6-
authorimage: "/img/blogs/Avatar4.svg"
2+
title: "Dark Mode Theming in Grommet - Part 3: Theme color customization"
3+
date: 2023-12-15T16:54:03.148Z
74
featuredBlog: false
8-
priority:
9-
thumbnailimage:
5+
priority: null
6+
author: Matt Glissmann
7+
authorimage: /img/blogs/Avatar4.svg
8+
thumbnailimage: null
9+
tags:
10+
- grommet
11+
- opensource
1012
---
1113
![g1lightdark3_1](https://hpe-developer-portal.s3.amazonaws.com/uploads/media/2020/9/g1lightdark3_1-1603892557138.png)
1214

1315
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.
1416

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
1820

1921
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
2427

28+
2529
## Customizing the Theme
30+
2631
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+
2833
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+
3035
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.
31-
3236

3337
![g3 table](https://hpe-developer-portal.s3.amazonaws.com/uploads/media/2020/9/g3-table-1603909712399.JPG)
3438

3539
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+
3741
## Create Theme File
42+
3843
In the `src` directory, create a theme file called `acme-theme.js` with the theme object `acme` as an export:
3944

4045
![g2part 3 image](https://hpe-developer-portal.s3.amazonaws.com/uploads/media/2020/9/g2part-3-image-1603892563485.png)
4146

4247
## <a id="head2"></a>Define the Color Palette
43-
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:
4448

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:
4550

4651
```markdown
4752
uniqueColorName: {
@@ -56,9 +61,8 @@ Notice that any Grommet component with color as an attribute can accept either a
5661
Additionally, by convention, a color name followed by a ‘!’ bang represents the color’s “true” value rather than a dark/light variant.
5762

5863
Add Acme, Inc. colors to `acme-theme.js` like so:
59-
6064

61-
``` javascript
65+
```javascript
6266
export const acme = {
6367
global: {
6468
colors: {
@@ -85,12 +89,13 @@ export const acme = {
8589
}
8690
};
8791
```
92+
8893
## <a id="head3"></a>Map Colors to Grommet Namespaces and Components
94+
8995
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.
9096

9197
Modify `acme-theme.js` by mapping the colors to background, brand, control, and anchor properties:
9298

93-
9499
```javascript
95100
export const acme = {
96101
global: {
@@ -161,21 +166,20 @@ export const acme = {
161166
```
162167

163168
## Merging a Custom Theme with an Existing Theme
169+
164170
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
173+
* `import { acme } from “./acme-theme”;` - Our custom theme file
165174

166-
- `import { deepMerge } from “grommet/utils”;` - A function allowing an existing theme to be customized or extended
167-
168-
- `import { acme } from “./acme-theme”;` - Our custom theme file
169-
170-
- `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
171176

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`.
173177

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`.
174179

175180
```javascript
176181
import React from "react";
177-
import { Grommet, Anchor, Box, Button, Heading, Paragraph } from "grommet";
178-
import { grommet } from "grommet";
182+
import { Grommet, grommet, Anchor, Box, Button, Heading, Paragraph } from "grommet";
179183
import { deepMerge } from "grommet/utils";
180184

181185
import { acme } from "./acme-theme";
@@ -184,27 +188,26 @@ import { DemoSection } from "./DemoSection";
184188
const theme = deepMerge(grommet, acme);
185189
```
186190

187-
188191
Finally, swap out the `grommet` theme with the newly created `theme`.
189192

190-
191193
```javascript
192194
<Grommet full theme={theme} themeMode={darkMode ? "dark" : "light"}>
193195
```
194196

195-
196197
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.
197198

198199
As review, here’s how the app was modified:
199200

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.
204205

206+
205207
## Next Steps for Exploration
208+
206209
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:
207210

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

Comments
 (0)