Tokenami includes common selectors like hover, focus, and active out of the box. However, you can define your own custom selectors to handle more complex styling scenarios like parent state, nested elements, and media query combinations.
Add custom selectors in your tokenami.config.ts file using the selectors key. Use the ampersand (&) to mark where the current element’s selector should be injected:
Selectors can be nested using arrays to combine media queries or other conditional logic:
export default createConfig({ selectors: { // Only apply hover on devices that support it hover: ['@media (hover: hover) and (pointer: fine)', '&:hover'], },});
Style descendant elements using underscores for spaces:
function Article() { return ( <article style={css({ '--{&_p}_color': 'var(--color_text)', '--{&_a}_color': 'var(--color_link)', '--{&_a:hover}_text-decoration': 'underline', })}> <p>This is a paragraph with a <a href="#">link</a>.</p> </article> );}
Arbitrary selectors using {&...} syntax can only style the current element and its descendants. They cannot target parents or siblings.
You can combine custom selectors with breakpoints:
<button style={css({ '--color': 'var(--color_black)', '--hover_color': 'var(--color_primary)', '--md_hover_color': 'var(--color_secondary)', // Different color on medium screens})} />