Overview
TheupdateCSSVariables method allows you to dynamically modify CSS variable values at runtime for a specific theme. This is useful for creating user-customizable themes, implementing dynamic color schemes, or adapting styles based on runtime conditions.
Variable changes are persisted per theme, so switching between themes will preserve the custom values youβve set for each one.
When to Use This Method
UseupdateCSSVariables when you need to:
- Build user-customizable themes (e.g., custom accent colors, spacing preferences)
- Implement dynamic brand theming (e.g., white-label apps)
- Adjust theme colors based on runtime data (e.g., user preferences, A/B testing)
- Create theme editors or design tools within your app
- Adapt colors based on external factors (e.g., time of day, location)
Usage
Basic Example
Multiple Variables at Once
User-Customizable Theme
How It Works
updateCSSVariables modifies CSS variables for a specific theme and persists those changes. The variables you update can be:
- Scoped theme variables: Variables defined inside
@variantblocks (e.g.,@variant light { --color-primary: ... }) - Shared variables: Variables defined in
@themethat are available across all themes
Variable Persistence
API Reference
Method Signature
Parameters
The name of the theme to update. This should match a theme name defined in your
global.css file (e.g., 'light', 'dark', or custom theme names).An object mapping CSS variable names (with
-- prefix) to their new values.- Keys: Must be valid CSS variable names starting with
--(validated in development mode) - Values: Can be strings (colors, units) or numbers (numeric values like spacing)
Return Value
This method returnsvoid. It updates the CSS variables immediately and triggers a re-render if the updated theme is currently active.
Platform Differences
Web Platform
Web Platform
On web,
updateCSSVariables applies changes directly to the DOM using document.documentElement.style.setProperty():- Numeric values are automatically converted to pixel units (e.g.,
16becomes"16px") - String values are applied as-is
- Changes take effect immediately
- Updates trigger listener notifications if the modified theme is active
Native Platform
Native Platform
On React Native,
updateCSSVariables updates the internal variable store with normalized values:- Color values are parsed and normalized to hex format using Culori
- Numeric values are stored directly as numbers
- Variables are added as getters to both
UniwindStore.varsand theme-specific variable objects - Updates trigger listener notifications if the modified theme is active
Important Notes
Updates only trigger component re-renders if the modified theme is currently active. Updating an inactive theme will store the changes but wonβt cause immediate visual updates.
Making Variables Available
ForupdateCSSVariables to work with a CSS variable, the variable must be defined in your theme. There are two ways to ensure variables are available:
Option 1: Define in Theme Variants
Define variables inside@variant blocks in your global.css:
global.css
Option 2: Define in Shared Theme
Define variables in@theme to make them available across all themes:
global.css
Performance Considerations
updateCSSVariables is optimized to only trigger re-renders when necessary. Updates to inactive themes donβt cause re-renders.- Changes are applied synchronously and take effect immediately
- Only components using the updated variables will re-render (if the theme is active)
- Updating variables frequently (e.g., on every slider drag) is fine, but consider debouncing for very rapid updates
- Variables are stored per theme, so memory usage scales with the number of themes and customized variables