Overview
TheuseCSSVariable hook allows you to retrieve one or more CSS variable values directly in JavaScript. Itโs particularly useful when you need to access theme colors, spacing values, or other design tokens programmatically for calculations, animations, or third-party library configurations.
When to Use This Hook
UseuseCSSVariable when you need to:
- Access theme colors for third-party libraries (e.g., chart libraries, map markers)
- Perform calculations with design tokens (e.g., dynamic positioning based on spacing values)
- Configure native modules that require color/size values
- Pass theme values to JavaScript animation libraries
- Access design tokens for runtime logic
Usage
Basic Example
Multiple Variables at Once
You can also retrieve multiple CSS variables at once by passing an array:With Chart Libraries
Accessing Custom Theme Variables
Making Variables Available
For the hook to resolve a CSS variable, the variable must be either:Option 1: Used in a className (Recommended)
Use the variable at least once anywhere in your appโsclassName props:
--color-primary available to useCSSVariable:
Option 2: Define in Static Theme
If you have CSS variables that are never used in classNames but need to be accessed in JavaScript, define them in a static theme block in yourglobal.css:
global.css
Variables defined in
@theme static are always available, even if theyโre never used in any className.Development Warnings
In development mode, Uniwind will warn you if you try to access a variable that hasnโt been made available:API Reference
Arguments
The name of the CSS variable(s) to retrieve, including the
-- prefix.- Single variable: Pass a string (e.g.,
'--color-primary') - Multiple variables: Pass an array of strings (e.g.,
['--color-primary', '--spacing-4'])
Return Value
The return type depends on the input:When passing a single string, returns the resolved value of the CSS variable:
- Web: Always returns a
string(e.g.,"16px","#ff0000") - Native: Returns
stringornumberdepending on the value type (e.g.,16,"#ff0000") - Undefined: If the variable cannot be found
When passing an array of strings, returns an array of resolved values in the same order:
Platform Differences
Web Platform
Web Platform
On web,
useCSSVariable uses getComputedStyle() to retrieve values from the DOM. All values are returned as strings:Native Platform
Native Platform
On React Native,
useCSSVariable accesses the internal variable store. Numeric values are returned as numbers:Performance Considerations
The
useCSSVariable hook is reactive and will automatically update when the theme changes, ensuring your values stay in sync with the active theme.- The hook subscribes to theme changes, so components will re-render when themes switch
- Use array syntax for multiple variables: When you need multiple CSS variables, pass an array instead of calling the hook multiple times. This creates only one subscription instead of multiple.
- For static values that donโt need theme reactivity, consider storing them outside the component