Available in Uniwind 1.6.4+Documentation Index
Fetch the complete documentation index at: https://docs.uniwind.dev/llms.txt
Use this file to discover all available pages before exploring further.
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.
When to Use This Function
Reach forgetCSSVariable 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
Usage
Basic Example
Multiple Variables at Once
Pass an array to read several variables in a single call:Outside of React
BecausegetCSSVariable is a plain function, you can call it from any module:
Making Variables Available
ForgetCSSVariable to resolve a value, 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 getCSSVariable:
Option 2: Define in Static Theme
If a variable is never used in anyclassName but you still need it from JavaScript, define it in a static theme block in your global.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 warns if you try to read a variable that isn’t available:API Reference
Arguments
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: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,
getCSSVariable uses getComputedStyle() to retrieve values from the DOM. All values are returned as strings:Native Platform
Native Platform
On React Native,
getCSSVariable accesses the internal variable store. Numeric values are returned as numbers:getCSSVariable vs useCSSVariable
getCSSVariable | useCSSVariable | |
|---|---|---|
| Where you can call it | Anywhere | Inside React components only |
| Reactive to theme changes | No | Yes |
| Triggers re-renders | No | Yes, when the theme changes |
| Best for | One-off reads, callbacks, utilities | Values rendered in JSX |
Related
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