EXEPERTAI LAB

Research alpha

Time Machine
EXEPERTAI LAB
GALLERY / COLLECTION
← Browse Astryx gallery
Utilities·hook·@astryxdesign/core

useTheme

Programmatic access to theme tokens for non-CSS consumers like SVG, canvas, Vega, D3, maps, or chart libraries that need values in JavaScript instead of CSS custom property references.

Open in Playground @astryxdesign/core/theme

Signature

Call shape
useTheme(): UseThemeReturn
Returns
NameType and behavior
namestring
Name of the nearest theme, or default when no provider is present.
mode'light' | 'dark'
Resolved effective color mode. system mode is resolved to light or dark.
token(name: string) => string
Resolve a single design token to its current CSS value for the effective mode.
tokensRecord<string, string>
All tokens resolved for the current mode, including defaults and theme overrides. Stable until the active theme or mode changes; uses the same resolution logic as resolveThemeTokens(theme, {mode}).

Showcases and examples

1 documented example

useTheme — Token Values

Read resolved theme token values with useTheme for non-CSS consumers like SVG charts.

Preview loads on approachPreview loads on approach
Exact source · use-theme-hook-usage
// Copyright (c) Meta Platforms, Inc. and affiliates.

'use client';

import {useTheme} from '@astryxdesign/core/theme';
import {Card} from '@astryxdesign/core/Card';
import {HStack, VStack} from '@astryxdesign/core/Layout';
import {Text} from '@astryxdesign/core/Text';

export default function UseThemeHookUsage() {
  const {name, mode, token} = useTheme();
  const accent = token('--color-accent');
  const muted = token('--color-accent-muted');
  const text = token('--color-text-primary');

  return (
    <Card width={360} padding={4}>
      <VStack gap={3}>
        <Text type="body" weight="bold">
          {name} · {mode}
        </Text>
        <svg width="300" height="120" role="img" aria-label="Themed bar chart">
          <rect x="24" y="20" width="64" height="80" rx="8" fill={accent} />
          <rect x="118" y="48" width="64" height="52" rx="8" fill={muted} />
          <rect
            x="212"
            y="32"
            width="64"
            height="68"
            rx="8"
            fill={accent}
            opacity="0.72"
          />
          <text x="24" y="114" fill={text} fontSize="12">
            Resolved token values
          </text>
        </svg>
        <HStack gap={2}>
          <Text type="code">--color-accent</Text>
          <Text type="code" color="secondary">
            {accent}
          </Text>
        </HStack>
      </VStack>
    </Card>
  );
}

Usage

Programmatic access to theme tokens for non-CSS consumers like SVG, canvas, Vega, D3, maps, or chart libraries that need values in JavaScript instead of CSS custom property references.

  • Use tokens or token(name) when integrating theme colors into SVG attributes, canvas drawing, chart options, or other non-CSS configuration objects inside React components.
  • Use resolveThemeTokens(theme, {mode}) for the same token resolution outside React hooks.
  • Prefer CSS variables or StyleX tokens for ordinary component styling; use this hook only when JavaScript needs token values.
  • Use data visualization tokens such as --color-data-categorical-blue for chart series instead of arbitrary UI colors.
  • Hardcode light/dark colors in data visualizations: resolve them through the current theme instead.
  • Assume the hook reflects every CSS cascade override. It resolves tokens for the current Theme mode; local media-surface overrides and arbitrary external CSS may not be represented.