Writing component styles in a way that is easy to maintain over the life of a growing and changing project is a challenging task.
To solve this, we came up with the idea of style configuration or styleConfig
.
This is a consistent theming API that makes component styling easy to understand
and maintain.
Most component style consists of base or default styles and modifier styles that alter its size or visual style based on some properties or state.
Common modifier styles includes:
Most components we build today are either single part components (e.g. Button, Badge) or multipart components (e.g. Tabs, Menu, Modal).
A single part component is a component that returns a single element. For
example, the <Button>
component renders a <button>
HTML element:
A multipart component is a component that has multiple parts, and require these parts to work correctly. This commonly referred to as a composite component.
For example, a Tabs
component consists of TabList
, Tab
, TabPanels
, and
TabPanel
. Styling this component as a whole might require styling each
component part.
The basic API for styling a single part component is:
Let's say we want to create a custom button component following the design spec below.
Here's a contrived implementation of the design:
Make sense? Next, we'll update the theme to include this new component style.
Now that the button's style configuration is hooked into the theme, we can
consume within any component using useStyleConfig
hook.
themeKey
: the key in theme.components
that points to the desired
styleConfig.props
: the options object used to compute the component styles. It typically
consists of the size
, variant
, and colorScheme
The computed styles for the component based on props
passed. If no props
is
passed, the defaultProps
defined in the style config will be used.
And lastly - the fun part - let's use our custom button component anywhere in our app:
This is very similar to styling single part components with a few differences you need to be aware of.
part
key in the style config.part
for baseStyle
, sizes
, and
variants
.Here's what the style config for multipart components looks like:
For example, here's what the style configurations for a custom menu component looks like:
Next, we'll update the theme object to included this new component style.
Now that the style config is hooked into the theme, we can consume within any
component using useMultiStyleConfig
hook.
We can also mount the computed styles on a specialized context provider called
StylesProvider
. These styles will now be available to other sub-components. To
read from the context, use the useStyles
hook.
themeKey
: the key in theme.components
that points to the desired
styleConfig.props
: an option of the options for the computing the final styles. It
typically consists of the size
, variant
, and colorScheme
The computed styles for each component part based on size
, or variant
. If
none of these were passed, the defaultProps
defined in the styleConfig will be
used.
That's it! We can use our newly created multipart component in our application: