data- attributes
If the value that controls dynamic styles is a boolean or a known small union of values, use data- attributes.
First, define data- attributes on the component. In the example below:
data-disabledrepresents the booleandisabledattribute.disabled || undefinedis required to not adddata-disabled="false"attribute whendisabledisfalseand allow styling with the&[data-disabled]selector.data-orientationrepresents theorientationprop which can be eitherhorizontalorvertical. In styles, you can reference it with the&[data-orientation="horizontal"]selector.
Then add styles in the .module.css file:
Inline styles
If the value that controls dynamic styles is not represented by a known union of values (for example, the value can be any valid CSS color), then you can use inline styles or style props:
If you need to customize a deeply nested element, use the styles prop instead:
Note that it is not possible to use pseudo-classes (for example, :hover, :first-of-type)
and media queries inside the styles prop. For this purpose, use CSS variables
with the classNames prop.
CSS variables
If none of the methods above work for you (for example, you want to customize :hover
styles based on component prop), use CSS variables
with the classNames prop.
First, define CSS variables in the style or styles prop:
Then reference them in the .module.css file: