css utility is the core function for authoring styles in Tokenami. It helps you write inline styles while avoiding specificity conflicts and provides a clean way to handle overrides.
Basic Usage
Thecss function accepts Tokenami properties and returns an object suitable for the style attribute:
Overrides
Pass your base styles as the first parameter, then add any number of overrides as additional parameters. The last override wins:Conditional Overrides
Add conditional styles as extra parameters. The last override wins:Return Type
Thecss function returns a TokenamiCSS object. This type is intentionally opaque to work with framework type constraints, but it’s compatible with the style prop in React and other frameworks.
Frameworks typically limit the style type to CSS.PropertiesHyphen or CSS.Properties, which doesn’t include custom properties. Tokenami’s return type works around this limitation.
Creating a Custom CSS Utility
When using custom property aliases, you must create a configured instance of thecss utility to ensure conflicts are resolved correctly across component boundaries.
1. Create a CSS utility file
Create a file in your project (e.g.css.ts) to configure the utility:
2. Use your custom utility
Import from your custom file instead of@tokenami/css:
Configuration Options
The Tokenami configuration object containing property aliases. Required when using custom aliases.
When using arbitrary values, Tokenami escapes special characters by default. Some frameworks automatically escape, which would result in double-escaping. Set this to
false to disable Tokenami’s escaping:Type Safety
Thecss utility provides autocomplete and type checking for all Tokenami properties based on your theme configuration. TypeScript will catch:
- Invalid property names
- Theme token mismatches
- Incorrect value types
TokenamiStyle utility type:
Why No Specificity Issues?
Tokenami applies styles using CSS custom properties on thestyle attribute. Since custom properties inherit and can be overridden at any level, you get predictable cascade behavior without specificity conflicts.
The css utility ensures that:
- Inline styles always have the highest specificity
- Overrides are applied in order, with later overrides winning
- Shorthand properties correctly override longhand properties
- Calculated values are properly toggled
tailwind-merge to handle conflicts.