Lynx

useIconColor

A hook that synchronizes CSS color of Lynx image icons with native tint-color.

Available since@seed-design/lynx-react@0.1.0

Import

import { useIconColor } from "@seed-design/lynx-react";

useIconColor is included in the @seed-design/lynx-react package. It is mainly used when Crispy Lynx components inject monochrome icons into slots.

Usage

Basic Usage

import { cloneElement, isValidElement, type ReactElement } from "react";
import { useMainThreadRef } from "@lynx-js/react";
import { useIconColor } from "@seed-design/lynx-react";

function IconSlot({ icon, variant, disabled }: {
  icon: ReactElement;
  variant: string;
  disabled: boolean;
}) {
  const wrapperRef = useMainThreadRef(null);
  const iconColorProps = useIconColor([variant, disabled], { sourceRef: wrapperRef });

  if (!isValidElement(icon)) return null;

  return (
    <view main-thread:ref={wrapperRef} className="my-icon-slot">
      {cloneElement(icon, {
        ...iconColorProps,
        style: { width: "100%", height: "100%" },
      })}
    </view>
  );
}

The object returned by useIconColor is a set of props you can inject directly into the icon element. If you pass sourceRef, it reads the wrapper's computed color and reflects it in the icon <image>'s tint-color. Used as before without sourceRef, it reads the icon element's own computed color.

State Dependencies

const iconColorProps = useIconColor([
  variant,
  disabled,
  loading,
  checked,
  pressed,
  tone,
  size,
]);

Include in deps every value that can affect the icon's CSS color. For example, ActionButton includes variant, disabled, loading, and the Checkbox and RadioGroup indicator include states that affect the indicator color, such as checked, pressed, tone, size.

Why It's Needed

Lynx's monochrome icons render an <image> internally, and the actual icon color is applied via the native tint-color attribute. Crispy recipes, on the other hand, declare CSS color on the slot className.

.seed-action-button__icon {
  color: var(--seed-color-palette-static-white);
}

On the web, this color can be used naturally with SVG or the CSS currentColor pattern, but in Lynx's <image> tint-color path, putting a CSS variable string in directly does not work reliably. That's why useIconColor reads the computed color on the main thread and reflects the resolved color in the tint-color attribute.

How It Works

useIconColor performs the same synchronization at two points in time.

  1. After deps changes, it synchronizes via runOnMainThread in a React effect.
  2. It synchronizes once more in main-thread:binduiappear when the icon's UI node appears.

The second synchronization matters especially inside list / list-item. In screens that render many previews on a list basis, like VariantTable, the timing when the background thread's effect runs can drift out of sync with the timing when the main thread UI node is actually attached and its computed style can be read. binduiappear is a mechanism for running the same sync once more at the point in the lifecycle when the UI node becomes visible, without polling or timer retries.

Design Decisions

useIconColor does not reinterpret color semantics in JS. It doesn't duplicate Crispy semantic token names or palette token values inside @seed-design/lynx-react; instead it lets the CSS variables injected by the recipe and the page/root remain the source of truth for color.

This decision was made for the following reasons.

  • Regardless of what color the user uses — #123144, rgb(...), a per-theme semantic token, etc. — it must be handled based on the CSS computed value.
  • Keeping a semantic color table in the React package would easily drift out of sync with theme, token, and recipe changes.
  • Putting a CSS variable string directly into tint-color is not reliable enough in the Lynx image path.
  • Increasing setTimeout or frame retries can mask the list's initial render timing issue, but it's hard to explain the cause in code and it raises performance risk.
  • Catalog-only key remounts or recyclable={false} workarounds can hide the component package's actual problem.

In short, useIconColor is a "thin bridge that reflects the CSS computed color onto the native image tint." Color calculation is the recipe and CSS's responsibility, and the hook's only responsibility is to pass that result on as a Lynx native attribute.

API

Parameters

ParameterTypeDescription
depsDependencyListThe list of dependencies that affect the icon's CSS color. When a value changes, the tint color is resynchronized on the main thread.
options.sourceRefRefObject<MainThread.Element>Optional. Pass this when you want to use the wrapper element's computed color as the source instead of the icon itself.

Return

PropertyTypeDescription
refRefObject<MainThread.Element>The main-thread ref for the icon <image>.
main-thread:binduiappear() => voidA main-thread handler that reflects the computed color back onto tint-color when the UI node appears.

Usage Notes

  • Use this for icons that get their color from a native <image tint-color>, like monochrome icons.
  • Do not use this for cases where the original image color must be preserved, like multicolor icons.
  • Omitting a deps entry causes stale tint. Include any state that changes the slot className.
  • If the icon element itself needs its own ref or main-thread:binduiappear, you'll need to decide on a separate merge strategy for that slot. For the default icon slot in Crispy Lynx components, useIconColor owns the main-thread ref.

Differences from the Web Version

ItemLynx (useIconColor)Web
Icon rendering<image> + native tint-colorSVG / CSS currentColor
Color source of truthComputed value of the recipe's CSS colorCSS cascade / currentColor
Sync methodRead computed color on the main thread and mirror it to the attributeNo separate bridge needed
List initial renderOne more sync in binduiappearNot applicable

Table of contents