Grommet Component Styling Skill
When to Use
- Creating or modifying a
StyledComponentName.jsfile - Adding or extending theme tokens in
src/js/themes/base.jsandbase.d.ts - Applying disabled or readOnly visual states to a component
- Choosing sizing and spacing values
- Composing UI using Grommet primitives (
Box,Text,Button, etc.) - Troubleshooting why a theme value is not applying, or why a styled component is leaking props to the DOM
Workflow
- Read the theme — check
src/js/themes/base.jsfor the component's existing namespace (theme.<componentName>) or the nearest related component's namespace. - Create the styled component — use
styled.element.withConfig(styledComponentsConfig). ImportstyledComponentsConfig,genericStyles,normalizeColor, and any state helpers from../../utils. - Wire to the component — call
useThemeValue()in the React component and spread{...passThemeFlag}on every styled component instance so the theme propagates correctly. - Add theme tokens — add the namespace to
base.jsin alphabetical order. Mirror the exact shape inbase.d.ts. - Apply state styles — use
disabledStyle()for disabled states andreadOnlyStyle()for readOnly states. Never write custom CSS for these. - Size with t-shirt values — reference theme tokens (
xsmall,small,medium,large,xlarge) rather than hardcoded pixel values. - Compose with primitives — prefer Grommet atoms (
Box,Text,Button) over raw HTML elements for layout and typography.
Key Rules
const { theme, passThemeFlag } = useThemeValue()— always; neveruseContext(ThemeContext)directly- Spread
{...passThemeFlag}on every styled component instance - Theme namespace:
theme.<componentName>at the top level ofbase.js(nottheme.global.myComponent) - No inline
style={{}}attributes or CSS class names - No hardcoded fallbacks:
theme.foo ?? '#000'silently hides missing tokens from custom theme users - Use
backgroundnotbackgroundColor; usecolorfor text and icon colors - Icons:
grommet-iconspackage only; always allow icon override via theme token
See Also
- REFERENCE.md — complete code examples for styledComponentsConfig, normalizeColor, genericStyles, disabledStyle, readOnlyStyle, and theme token registration