Skip to content

feat(core): add module-level registerIcons() for RSC compatibility - #528

Merged
cixzhang merged 2 commits into
mainfrom
navi/feat/register-icons
Mar 8, 2026
Merged

cixzhang merged 2 commits into
mainfrom
navi/feat/register-icons

Conversation

@cixzhang

@cixzhang cixzhang commented Mar 8, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a global icon registry that works in both server and client environments, enabling RSC-rendered components to access custom icons without a React Context boundary.

New API

// app/layout.tsx (RSC — no 'use client' needed)
import { registerIcons } from '@xds/core';
import { brandIcons } from './brand-icons';

registerIcons(brandIcons);
Function Environment Use case
registerIcons(icons) Server + Client Register icons at app init
getIcon(name) Server + Client Get icon without hooks
useXDSIcon(name) Client only Get icon with context support
resetIcons() Testing only Clear global registry

Resolution order

Client (useXDSIcon):

  1. Context registry (XDSTheme) — tree-scoped overrides
  2. Global registry (registerIcons) — app-level
  3. Built-in defaults

Server (getIcon):

  1. Global registry (registerIcons)
  2. Built-in defaults

Architecture

  • globalIconRegistry.tsx — pure module, no 'use client', RSC-safe. Holds the global state, types, and registration functions.
  • IconRegistry.tsx — 'use client', React Context + useXDSIcon hook. Imports from globalIconRegistry for fallback.
  • XDSTheme.tsx — now calls registerIcons() internally so theme icons are available both via context AND globally.

Why not just Context?

React Context requires a client component boundary. Server components can't call useContext. The global registry is module-scoped state that persists in the Node.js module cache — works everywhere.

Test plan

  • 6 new tests for global registry (register, fallback, merge, override, reset)
  • 1364 existing tests pass
  • Build succeeds (CJS, ESM, DTS)

Adds a global icon registry that works in both server and client
environments, enabling server-rendered components to access custom
icons without a React Context boundary.

New API:
- registerIcons(icons) — register icons at module level
- getIcon(name) — get an icon by name (server-safe, no hooks)
- resetIcons() — clear global registry (testing only)

Resolution order for useXDSIcon (client):
1. Context registry (XDSTheme)
2. Global registry (registerIcons)
3. Built-in defaults

Resolution order for getIcon (server):
1. Global registry (registerIcons)
2. Built-in defaults

XDSTheme now calls registerIcons() internally so theme icons are
available both via context AND globally.

Architecture:
- globalIconRegistry.tsx — pure module, no 'use client', RSC-safe
- IconRegistry.tsx — 'use client', context + useXDSIcon hook
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Mar 8, 2026
@github-actions

github-actions Bot commented Mar 8, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

Modified Components

Icon · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A 367B N/A
Lines of Code N/A 321 -
Complexity N/A Medium (7) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@xds/core 9.2KB 14.9KB 2.3KB

Accessibility Audit

Status: 1 accessibility violation(s) found — 1 serious.

XDSIcon - 1 issue(s)
  • 🟠 serious: Ensure the contrast between foreground and background colors meets WCAG 2 AA minimum contrast ratio thresholds
    • Rule: color-contrast · Affects 1/16 stories · Learn more
    • WCAG: 1.4.3 (Level AA)

Component Previews

Includes interactive hover previews as animated GIFs.

XDSIcon

Default

Screenshot:

Default

Interaction Preview: (view video)

Default interaction

View in Storybook · core-xdsicon--default

Primary

Screenshot:

Primary

View in Storybook · core-xdsicon--primary

Secondary

Screenshot:

Secondary

View in Storybook · core-xdsicon--secondary

Accent

Screenshot:

Accent

View in Storybook · core-xdsicon--accent

Positive

Screenshot:

Positive

View in Storybook · core-xdsicon--positive

Negative

Screenshot:

Negative

View in Storybook · core-xdsicon--negative

Warning

Screenshot:

Warning

View in Storybook · core-xdsicon--warning

Extra Small

Screenshot:

Extra Small

View in Storybook · core-xdsicon--extra-small

Small

Screenshot:

Small

View in Storybook · core-xdsicon--small

Medium

Screenshot:

Medium

View in Storybook · core-xdsicon--medium

Large

Screenshot:

Large

View in Storybook · core-xdsicon--large

Solid Icon

Screenshot:

Solid Icon

View in Storybook · core-xdsicon--solid-icon

All Colors

Screenshot:

All Colors

View in Storybook · core-xdsicon--all-colors

All Sizes

Screenshot:

All Sizes

View in Storybook · core-xdsicon--all-sizes

Outline Vs Solid

Screenshot:

Outline Vs Solid

View in Storybook · core-xdsicon--outline-vs-solid

Inherit Color

Screenshot:

Inherit Color

View in Storybook · core-xdsicon--inherit-color


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

- iconRegistry.ts → globalIconRegistry.tsx (actual filename)
@cixzhang

cixzhang commented Mar 8, 2026

Copy link
Copy Markdown
Contributor Author

Self-review complete. Clean implementation — types and functions properly moved to globalIconRegistry.tsx (RSC-safe, no 'use client'). Resolution order (context → global → defaults) is correct. Tests cover register, merge, override, reset. One minor fix pushed: SYNC comment referenced iconRegistry.ts instead of actual filename globalIconRegistry.tsx.

@cixzhang
cixzhang merged commit f6440bb into main Mar 8, 2026
14 checks passed
@cixzhang
cixzhang deleted the navi/feat/register-icons branch March 13, 2026 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant