How to Create a Custom Theme for UI Using Grommet V2
Grommet V2 is a React component library focused on accessibility and composability. Its theming system lets you define colours, typography, spacing, and component-level styles in a single JavaScript object, which is then injected at the root of your application.
This post walks through creating a custom theme, applying it, and merging it with Grommet's defaults so you keep the base styling for components you have not overridden.
Installation
npm install grommet grommet-icons styled-components
Step 1: Define the Theme Object
A Grommet theme is a plain JavaScript object. The global key covers application-wide tokens; each component can also have its own key for targeted overrides.
// theme.js
export const customTheme = {
global: {
colors: {
brand: '#1A73E8',
'accent-1': '#34A853',
'accent-2': '#FBBC04',
'neutral-1': '#1A1D20',
'custom-1': '#E8F0FE',
'custom-2': '#D2E3FC',
'custom-3': '#174EA6',
background: '#ffffff',
text: '#1A1D20',
},
font: {
family: "'Inter', sans-serif",
size: '16px',
},
focus: {
border: {
color: '#1A73E8',
},
},
},
button: {
border: {
radius: '4px',
},
primary: {
color: '#1A73E8',
},
},
};
Step 2: Apply the Theme
Wrap your application with the <Grommet> component and pass your theme:
import React from 'react';
import { Grommet, Box, Button, Text } from 'grommet';
import { customTheme } from './theme';
function App() {
return (
<Grommet theme={customTheme} full>
<Box background="brand" pad="medium">
<Text color="white" size="large">Themed App</Text>
<Button primary label="Get Started" margin={{ top: 'small' }} />
</Box>
</Grommet>
);
}
Grommet makes your colour tokens available across all child components by name. background="brand" resolves to #1A73E8 without needing to hardcode the hex value.
Step 3: Merge with the Default Theme
If you only want to override specific values while keeping all of Grommet's defaults for everything else, use deepMerge with the generate helper:
import { deepMerge } from 'grommet/utils';
import { generate } from 'grommet/themes';
import { customTheme } from './theme';
const mergedTheme = deepMerge(generate(16), customTheme);
The generate(16) call creates the full default Grommet theme based on a 16px base font size. deepMerge then overlays your custom values on top, so any component you have not explicitly styled still inherits Grommet's sensible defaults.
<Grommet theme={mergedTheme} full>
{/* your app */}
</Grommet>
Using Custom Colours in Components
Custom colour names are available anywhere Grommet accepts a colour prop:
<Box background="custom-1" pad="medium">
<Text color="custom-3">Highlighted text</Text>
</Box>
<Button
label="Action"
style={{ background: 'var(--custom-2)' }}
/>
Conclusion
Grommet's theme system centralises your design tokens and makes them available consistently throughout the component tree. By merging with generate() you get Grommet's full default component styling as a baseline, with your overrides applied on top — a clean separation between the library's defaults and your application's design language.