Skip to main content
Available in Uniwind 1.1.0+

Overview

The updateCSSVariables 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

Use updateCSSVariables 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)
For static theme customization, prefer defining variables directly in your global.css file using @theme and @variant directives.

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 @variant blocks (e.g., @variant light { --color-primary: ... })
  • Shared variables: Variables defined in @theme that are available across all themes
When you switch themes, your customized values are preserved and applied automatically.

Variable Persistence

API Reference

Method Signature

Parameters

string
required
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).
Record<string, string | number>
required
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 returns void. It updates the CSS variables immediately and triggers a re-render if the updated theme is currently active.

Platform Differences

On web, updateCSSVariables applies changes directly to the DOM using document.documentElement.style.setProperty():
  • Numeric values are automatically converted to pixel units (e.g., 16 becomes "16px")
  • String values are applied as-is
  • Changes take effect immediately
  • Updates trigger listener notifications if the modified theme is active
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.vars and theme-specific variable objects
  • Updates trigger listener notifications if the modified theme is active

Important Notes

CSS variable names must include the -- prefix. In development mode, Uniwind will validate this and warn you if you forget the prefix.
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

For updateCSSVariables 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
Now you can update these variables at runtime:

Option 2: Define in Shared Theme

Define variables in @theme to make them available across all themes:
global.css
These can be updated for any theme:

Performance Considerations

updateCSSVariables is optimized to only trigger re-renders when necessary. Updates to inactive themes don’t cause re-renders.
Keep in mind:
  • 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

useCSSVariable

Read CSS variable values in JavaScript

Custom Themes

Learn how to create custom themes

Global CSS

Define CSS variables in your theme configuration

Theming Basics

Understand how themes work in Uniwind