useIconColor
A hook that synchronizes CSS color of Lynx image icons with native tint-color.
Import
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
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
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.
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.
- After
depschanges, it synchronizes viarunOnMainThreadin a React effect. - It synchronizes once more in
main-thread:binduiappearwhen 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-coloris not reliable enough in the Lynx image path. - Increasing
setTimeoutor 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
keyremounts orrecyclable={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
| Parameter | Type | Description |
|---|---|---|
deps | DependencyList | The list of dependencies that affect the icon's CSS color. When a value changes, the tint color is resynchronized on the main thread. |
options.sourceRef | RefObject<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
| Property | Type | Description |
|---|---|---|
ref | RefObject<MainThread.Element> | The main-thread ref for the icon <image>. |
main-thread:binduiappear | () => void | A 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
depsentry causes stale tint. Include any state that changes the slot className. - If the icon element itself needs its own
reformain-thread:binduiappear, you'll need to decide on a separate merge strategy for that slot. For the default icon slot in Crispy Lynx components,useIconColorowns the main-thread ref.
Differences from the Web Version
| Item | Lynx (useIconColor) | Web |
|---|---|---|
| Icon rendering | <image> + native tint-color | SVG / CSS currentColor |
| Color source of truth | Computed value of the recipe's CSS color | CSS cascade / currentColor |
| Sync method | Read computed color on the main thread and mirror it to the attribute | No separate bridge needed |
| List initial render | One more sync in binduiappear | Not applicable |