Prodular UI

Token color

Resolves color tokens to sRGB for canvas, WebGL, MapLibre, D3 and chart libraries, and updates them when the theme or mode changes.

npx prodular-ui add token-color

Canvas

Bars drawn on a canvas with chart and border tokens. Switch the theme or mode and they redraw.

Resolving colors…

Anatomy

const colors = useTokenColors(["chart-1", "border"])useEffect(() => {  if (colors) map.setPaintProperty("sites", "circle-color", colors["chart-1"])}, [colors])

Guidance

Use it when

  • Renderers that take color strings or numbers instead of CSS: canvas 2D, WebGL, MapLibre styles, D3 scales.
  • useTokenColors in React, so colors update with the theme.
  • tokenColor(token, { format: "array" }) for WebGL and image data.

Avoid

  • Hard-coding hex values that only match one theme.
  • Painting a pixel and reading it back to convert colors.
  • Calling tokenColor during server rendering; it returns null there.

Rules for AI agents

Published in llms-full.txt, the registry and the MCP server, so coding agents follow them when they build with this component.

  • Resolve colors in an effect or with useTokenColors, and redraw when they change.
  • Pass token names without var(): "chart-1", "severity-high", "--border".
  • Pass element to read a token under a scoped data-theme or .dark.

API

tokenColor

PropTypeDefaultDescription
(token, options)string | [r, g, b, a] | nulloptions: format ("hex" | "rgb" | "rgba" | "array"), element, alpha.

useTokenColors

PropTypeDefaultDescription
(tokens, options)Record<token, string> | nullResolved colors, null until mounted.

onTokensChange

PropTypeDefaultDescription
(callback, element?)() => voidSubscribes to theme, mode and OS color scheme changes. Returns the unsubscribe.

Installation

The Prodular UI CLI adds the source to components/ui with everything it depends on. In a project that already uses shadcn/ui, the shadcn CLI installs it from the Prodular registry too. See Installation for the first setup.

npx prodular-ui add token-color# or, with the registry in components.jsonnpx shadcn@latest add @prodular/token-color