Theming
Setup
Managing design tokens
Design systems establish a consistent user experience across platforms. They are built upon tokens, which store information about branding colors, typography and more.
Firstly, define a theme, preferably by overriding the default tokens:
note
Keeping the runtime as small as possible, only a few tokens (breakpoints
, shorthands
and aliases
) are embedded into production JavaScript bundles. Other values can only be accessed exclusively for styling, as shown in usage.
Apply the theme through <ThemeProvider>
, by wrapping the component tree:
tip
The Gatsby plugin for glaze does this out of the box.
Precise autocompletion
Being written in TypeScript, glaze offers extensive type safety. To activate theme-agnostic code suggestions, include the following snippet in your project:
Static style extraction
A single-purpose CSS class needs to be generated for each design token at build time. This can be set up by following the instructions of treat.
tip
Multiple integrations are available for treat, including a Gatsby plugin and a Next.js plugin.
Afterwards, selector-based CSS rules may be created with globalStyle()
in *.treat.{js|ts}
files. They have to be applied as a side effect, e.g. from a top-level layout component:
Usage
How it works
At first, sx
tries mapping themed values to statically generated CSS class names. Unresolved rules are injected at runtime and detached when no components reference them anymore.
Rule handling
- Transform each alias to its corresponding CSS property name or custom shorthand.
- Resolve values from a scale if available.
- Each custom shorthand's associated CSS properties are resolved one by one.
note
Aliases and shorthands can be combined. For instance, px
could be an alias for the paddingX
shorthand.
Example
Heavily influenced by Tailwind CSS, the default theme contains settings like below:
Entries of a style object passed to sx
are matched to CSS rules as shown:
{ px: 4 }
{ paddingX: 4 }
, after transforming aliases{ paddingLeft: 4, paddingRight: 4 }
, after unfolding custom shorthands{ paddingLeft: '1rem', paddingRight: '1rem' }
, after applying matchers