|
1 | 1 | ---
|
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 |
7 | 4 | 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 |
10 | 12 | ---
|
11 | 13 | 
|
12 | 14 |
|
13 | 15 | This post is Part 1 of a three-part series.
|
14 | 16 |
|
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/) |
18 | 20 |
|
19 | 21 | 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.
|
20 | 22 |
|
21 | 23 | [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).
|
22 | 24 |
|
23 | 25 | 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:
|
24 | 26 |
|
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 |
28 | 30 |
|
29 | 31 | 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.
|
30 | 32 |
|
31 | 33 | <img src="https://hpe-developer-portal.s3.amazonaws.com/uploads/media/2020/9/themetutorialapp-1602698870239.gif" style="height:300px, width:300px" />
|
32 | 34 |
|
33 | 35 | # Setup for the tutorial
|
| 36 | + |
34 | 37 | ## Prerequisites
|
| 38 | + |
35 | 39 | 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.
|
36 | 40 |
|
37 | 41 | ## Get the Starter Code
|
| 42 | + |
38 | 43 | 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:
|
39 | 44 |
|
40 | 45 | <img src="https://hpe-developer-portal.s3.amazonaws.com/uploads/media/2020/9/picture-2-1602661773922.png" />
|
41 | 46 |
|
42 | 47 | ## Starter Code Orientation
|
| 48 | + |
43 | 49 | ### 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: |
45 | 50 |
|
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 |
49 | 56 |
|
50 | 57 | ### Project Structure and Dependencies
|
| 58 | + |
51 | 59 | The project structure mimics a minimal Create React App (CRA) structure and is organized like so:
|
52 | 60 |
|
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.) |
60 | 70 |
|
61 | 71 | ### App.js
|
| 72 | + |
62 | 73 | 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.
|
63 | 74 |
|
64 | 75 | Import of React and Grommet components:
|
65 | 76 |
|
66 |
| - |
67 | 77 | ```javascript
|
68 | 78 | import React from "react";
|
69 | 79 | import { Grommet, Anchor, Box, List, Heading, Paragraph, Text } from "grommet";
|
70 | 80 | ```
|
71 | 81 |
|
72 | 82 | `projectTasks` array with the tasks to be implemented
|
73 | 83 |
|
74 |
| - |
75 | 84 | ```javascript
|
76 | 85 | const projectTasks = [
|
77 | 86 | "Apply a Theme - Add an existing theme to provide some styling",
|
78 | 87 | `Add Theme Toggle Button - Add a button, which when clicked,
|
79 | 88 | will toggle the theme between light and dark modes`,
|
80 | 89 | "Customize Theme - Add custom colors to theme"
|
81 | 90 | ];
|
82 |
| -```` |
| 91 | +``` |
83 | 92 |
|
84 | 93 | The `App` is composed of the following Grommet components:
|
85 | 94 |
|
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. |
90 | 98 |
|
91 | 99 | ```javascript
|
92 | 100 | const App = () => {
|
@@ -115,36 +123,29 @@ export default App;
|
115 | 123 | ```
|
116 | 124 |
|
117 | 125 | # 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. |
119 | 128 |
|
120 | 129 | In App.js, import and apply the Grommet theme:
|
121 | 130 |
|
122 | 131 | Import grommet theme
|
123 | 132 |
|
124 |
| - |
125 |
| -```javascript |
| 133 | +```javascript |
126 | 134 | import { grommet } from "grommet";
|
127 |
| -```` |
| 135 | +``` |
128 | 136 |
|
129 | 137 | Apply it by adding `theme={grommet}` to the Grommet component.
|
130 | 138 |
|
131 |
| -
|
132 | 139 | ```javascript
|
133 | 140 | <Grommet full theme={grommet}>
|
134 | 141 | ```
|
135 | 142 |
|
136 |
| - |
137 |
| - |
138 |
| - |
139 | 143 | 
|
140 | 144 |
|
141 |
| - |
142 |
| - |
143 | 145 | 
|
144 | 146 |
|
145 | 147 | 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.
|
146 | 148 |
|
147 | 149 | 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.
|
148 | 150 |
|
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