Skip to content

Commit 1e76148

Browse files
authored
Merge pull request #2234 from sulaymon-tajudeen-hpe/cms/sulaymon-tajudeen-hpe/hpe-dev-portal/blog/dark-mode-theming-in-grommet-how-to-set-up-and-apply-a-theme
Update Blog “dark-mode-theming-in-grommet-how-to-set-up-and-apply-a-theme”
2 parents bca3651 + 5ac8774 commit 1e76148

File tree

1 file changed

+44
-43
lines changed

1 file changed

+44
-43
lines changed
Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,100 @@
11
---
2-
title: "Dark Mode Theming in Grommet: How to set up and apply a theme"
3-
date: 2020-10-14T07:50:10.733Z
4-
author: Matt Glissmann
5-
tags: ["grommet","opensource"]
6-
authorimage: "/img/blogs/Avatar6.svg"
2+
title: "Dark Mode Theming in Grommet - Part 1: How to set up and apply a theme"
3+
date: 2023-12-15T15:45:24.933Z
74
featuredBlog: false
8-
priority:
9-
thumbnailimage:
5+
priority: null
6+
author: Matt Glissmann
7+
authorimage: /img/blogs/Avatar6.svg
8+
thumbnailimage: null
9+
tags:
10+
- grommet
11+
- opensource
1012
---
1113
![darkmode intro part1](https://hpe-developer-portal.s3.amazonaws.com/uploads/media/2020/9/darkmode-intro-part1-1603293464825.png)
1214

1315
This post is Part 1 of a three-part series.
1416

15-
- Part 1 - How to set up and apply a theme
16-
- Part 2 - Adding dark and light theme modes
17-
- Part 3 - Theme color customization
17+
* Part 1 - How to set up and apply a theme
18+
* [Part 2 - Adding dark and light theme modes](https://developer.hpe.com/blog/dark-mode-theming-in-grommet-adding-dark-and-light-theme-modes/)
19+
* [Part 3 - Theme color customization](https://developer.hpe.com/blog/dark-mode-theming-in-grommet-theme-color-customization/)
1820

1921
As a UI/UX developer at HPE, I use [Grommet](https://grommet.io) extensively. I am also a regular contributor on the [Grommet Slack](https://grommet.slack.com) channels where the Theming capabilities, especially Grommet’s support for light and dark modes, are a consistent topic of interest.
2022

2123
[Grommet](https://grommet.io) has robust support for light and dark themes. Due to the apparent interest in this topic, I thought I’d share my approach on how to get started with theme mode styling in [Grommet](https://grommet.io).
2224

2325
To illustrate the method I use, I’m going to create a simple application demonstrating the ability to toggle between light and dark modes. By the end of this 3-part blog series, I will have demonstrated how to:
2426

25-
- Apply a theme in a Grommet application
26-
- Incorporate dark and light theme modes
27-
- Modify a theme to apply custom colors in dark and light modes
27+
* Apply a theme in a Grommet application
28+
* Incorporate dark and light theme modes
29+
* Modify a theme to apply custom colors in dark and light modes
2830

2931
The final product will be a simple application with a custom theme applied and the ability for a user to toggle between the app’s light and dark modes.
3032

3133
<img src="https://hpe-developer-portal.s3.amazonaws.com/uploads/media/2020/9/themetutorialapp-1602698870239.gif" style="height:300px, width:300px" />
3234

3335
# Setup for the tutorial
36+
3437
## Prerequisites
38+
3539
This tutorial assumes familiarity with HTML, JavaScript, and React. However, even if any of these are new to you, you should be able to follow along and complete the exercise.
3640

3741
## Get the Starter Code
42+
3843
Open this [starting code](https://codesandbox.io/s/grommet-theme-toggle-0starter-1k1cv?file=/src/App.js) in your browser. Create your own copy by clicking 'Fork' from the menu in the upper right corner. The starter app will look like this:
3944

4045
<img src="https://hpe-developer-portal.s3.amazonaws.com/uploads/media/2020/9/picture-2-1602661773922.png" />
4146

4247
## Starter Code Orientation
48+
4349
### Development Environment
44-
For this tutorial, I’m using [Codesandbox](https://codesandbox.io/) as the development environment. [Codesandbox](https://codesandbox.io/) presents the user with:
4550

46-
- An interface for the project’s file and directory structure
47-
- A text editor for editing files
48-
- A browser window to view and interact with the application
51+
For this tutorial, I’m using [Codesandbox](https://codesandbox.io/) as the development environment. Codesandbox presents the user with:
52+
53+
* An interface for the project’s file and directory structure
54+
* A text editor for editing files
55+
* A browser window to view and interact with the application
4956

5057
### Project Structure and Dependencies
58+
5159
The project structure mimics a minimal Create React App (CRA) structure and is organized like so:
5260

53-
- /public
54-
- index.html (this gets loaded by the browser and is what renders src/index.js)
55-
- /src
56-
- index.js (This is the project’s root file. I won’t be modifying it.)
57-
- App.js
58-
- DemoSection.js (This is a component I will use later in the tutorial)
59-
- Package.json (Create React App ships with `react`, `react-dom`, and `react-scripts` as dependencies. Additionally, I’ve added `grommet` and its peer dependency, `styled-components`, to the starter project.)
61+
* /public
62+
63+
* index.html (this gets loaded by the browser and is what renders src/index.js)
64+
* /src
65+
66+
* index.js (This is the project’s root file. I won’t be modifying it.)
67+
* App.js
68+
* DemoSection.js (This is a component I will use later in the tutorial)
69+
* Package.json (Create React App ships with `react`, `react-dom`, and `react-scripts` as dependencies. Additionally, I’ve added `grommet` and its peer dependency, `styled-components`, to the starter project.)
6070

6171
### App.js
72+
6273
For this tutorial, most of the development will happen in App.js. The App.js file consists of three parts; imports, projectTasks array, and the App component.
6374

6475
Import of React and Grommet components:
6576

66-
6777
```javascript
6878
import React from "react";
6979
import { Grommet, Anchor, Box, List, Heading, Paragraph, Text } from "grommet";
7080
```
7181

7282
`projectTasks` array with the tasks to be implemented
7383

74-
7584
```javascript
7685
const projectTasks = [
7786
"Apply a Theme - Add an existing theme to provide some styling",
7887
`Add Theme Toggle Button - Add a button, which when clicked,
7988
will toggle the theme between light and dark modes`,
8089
"Customize Theme - Add custom colors to theme"
8190
];
82-
````
91+
```
8392

8493
The `App` is composed of the following Grommet components:
8594

86-
- `<Grommet>` is the top level Grommet container
87-
- `<Box>` for some basic page layout
88-
- For the page’s content, I have `<Heading>`,`< Paragraph>`, and `<List>` which iterates over the `projectTasks` array, returning a `<Text>` component for each task.
89-
95+
* `<Grommet>` is the top level Grommet container
96+
* `<Box>` for some basic page layout
97+
* For the page’s content, I have `<Heading>`,`< Paragraph>`, and `<List>` which iterates over the `projectTasks` array, returning a `<Text>` component for each task.
9098

9199
```javascript
92100
const App = () => {
@@ -115,36 +123,29 @@ export default App;
115123
```
116124

117125
# Applying a Theme
118-
To provide some initial styling, I’ll apply the `grommet` theme. In Part 3 of this series, I will show you how to customize and incorporate a custom theme.
126+
127+
To provide some initial styling, I’ll apply the `grommet` theme. In [Part 3](https://developer.hpe.com/blog/dark-mode-theming-in-grommet-theme-color-customization/) of this series, I will show you how to customize and incorporate a custom theme.
119128

120129
In App.js, import and apply the Grommet theme:
121130

122131
Import grommet theme
123132

124-
125-
```javascript
133+
```javascript
126134
import { grommet } from "grommet"; 
127-
````
135+
```
128136

129137
Apply it by adding `theme={grommet}` to the Grommet component.
130138

131-
132139
```javascript
133140
<Grommet full theme={grommet}>
134141
```
135142

136-
137-
138-
139143
![matt1-picture 3](https://hpe-developer-portal.s3.amazonaws.com/uploads/media/2020/9/picture-3-1602661789429.png)
140144

141-
142-
143145
![matt1-picture 4](https://hpe-developer-portal.s3.amazonaws.com/uploads/media/2020/9/picture-4-1602661802053.png)
144146

145147
Your code and resulting app should resemble this [Codesandbox](https://codesandbox.io/s/grommet-theme-toggle-1adding-theme-rg91i?file=/src/App.js), and the app’s UI should have updated with the applied theme.
146148

147149
At this point, you now have an understanding for how to apply a theme to a Grommet app. This is the foundation from which we will build custom theming. In my next post, I will show you how to add dark/light theming and give users control over toggling between theme modes. In my final post, I will wrap up with a how to customize a theme with custom dark and light mode colors.
148150

149-
Stay tuned to the [HPE DEV blog](/blog) to catch Parts 2 and 3 of this series. If you have any questions, please feel free to reach out to me and others in the Grommet group on our [Slack channel](https://app.slack.com/client/T04LMHMUT/C04LMHN59).
150-
151+
Stay tuned to the [HPE DEV blog](/blog) to catch Parts [2](https://developer.hpe.com/blog/dark-mode-theming-in-grommet-adding-dark-and-light-theme-modes/) and [3](https://developer.hpe.com/blog/dark-mode-theming-in-grommet-theme-color-customization/) of this series. If you have any questions, please feel free to reach out to me and others in the Grommet group on our [Slack channel](https://app.slack.com/client/T04LMHMUT/C04LMHN59).

0 commit comments

Comments
 (0)