Skip to main content
Available in Uniwind 1.6.4+

Overview

Uniwind.getCSSVariable returns the value of one or more CSS variables on demand. Unlike useCSSVariable, it isn’t a hook, doesn’t subscribe to theme changes, and can be called from anywhere: event handlers, async functions, utility modules, or third-party callbacks where hooks aren’t allowed.
This function reads the value once. It will not update when the theme switches. If you need a reactive value inside a component, use useCSSVariable instead.

When to Use This Function

Reach for getCSSVariable when you need to:
  • Read a token from outside a React component (utility files, services, event handlers)
  • Resolve a value lazily inside an onPress, onLayout, or async callback
  • Pass a token into an imperative API that runs once (native module, animation driver, logger)
  • Access theme values from worklets or initialization code where hooks aren’t available
Prefer these alternatives when possible:
  • For styling components: Use the className prop directly
  • For reactive values inside components: Use useCSSVariable
  • For multiple style properties at once: Use useResolveClassNames

Usage

Basic Example

Multiple Variables at Once

Pass an array to read several variables in a single call:

Outside of React

Because getCSSVariable is a plain function, you can call it from any module:

Making Variables Available

For getCSSVariable to resolve a value, the variable must be either: Use the variable at least once anywhere in your app’s className props:
This makes --color-primary available to getCSSVariable:

Option 2: Define in Static Theme

If a variable is never used in any className but you still need it from JavaScript, define it in a static theme block in your global.css:
global.css
Then read it anywhere:
Variables defined in @theme static are always available, even if they’re never used in any className.

Development Warnings

In development mode, Uniwind warns if you try to read a variable that isn’t available:

API Reference

Arguments

string | string[]
required
The name of the CSS variable(s) to read, 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:
string | number | undefined
When passing a single string, returns the resolved value of the CSS variable:
  • Web: Always returns a string (e.g., "16px", "#ff0000")
  • Native: Returns string or number depending on the value type (e.g., 16, "#ff0000")
  • Undefined: If the variable cannot be found
Array<string | number | undefined>
When passing an array of strings, returns an array of resolved values in the same order:

Platform Differences

On web, getCSSVariable uses getComputedStyle() to retrieve values from the DOM. All values are returned as strings:
On React Native, getCSSVariable accesses the internal variable store. Numeric values are returned as numbers:

getCSSVariable vs useCSSVariable

If you only need a value once (e.g., inside an onPress handler), getCSSVariable avoids the extra subscription and re-render cost of useCSSVariable.

useCSSVariable

Reactive CSS variable access inside components

useResolveClassNames

Convert full className strings to style objects

Global CSS

Learn how to define CSS variables in your theme

Theming Basics

Understand how themes work in Uniwind