An open source design system that's fully customizable and agent ready
TypeScript
Give NumberInput steppers a dedicated icon slot (#5466)
rubyycheung1918344777a5
diff --git a/.changeset/icon-namespaced-keys-from-the-icon-prop.md b/.changeset/icon-namespaced-keys-from-the-icon-prop.mdnew file mode 100644index 0000000000..fb76322534--- /dev/null+++ b/.changeset/icon-namespaced-keys-from-the-icon-prop.md@@ -0,0 +1,8 @@+---+'@astryxdesign/core': patch+'@astryxdesign/cli': patch+---++[feat] `<Icon icon>`, `useIcon`, and `defineTheme({icons})` accept namespaced extension keys (`'numberInput:stepperDown'`, `'richtext:bold'`) alongside the built-in semantic names. A glyph owned by one component or library can now be themed by key while keeping `size`, `color` and `xstyle`, without widening the shared `IconName` union — and so without adding a required key to every downstream `IconRegistry`. Misspelled built-in names are still rejected, and `getIconRegistry()` still returns built-in names only.++@cixzhangdiff --git a/.changeset/number-input-stepper-icon-slot.md b/.changeset/number-input-stepper-icon-slot.mdnew file mode 100644index 0000000000..d84bc58c0e--- /dev/null+++ b/.changeset/number-input-stepper-icon-slot.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[feat] NumberInput steppers now resolve through the component-owned `numberInput:stepperDown` icon key, leaving `chevronDown` for shared disclosure and navigation affordances. Core provides a compact centered chevron fallback, and themes can override the stepper independently without adding a required `IconRegistry` key.++@rubyycheungdiff --git a/packages/cli/assets/docs/icons.doc.mjs b/packages/cli/assets/docs/icons.doc.mjsindex c3558c6fbe..1ea7586a65 100644--- a/packages/cli/assets/docs/icons.doc.mjs+++ b/packages/cli/assets/docs/icons.doc.mjs@@ -102,13 +102,43 @@ export const brandTheme = defineTheme({},],},+ {+ title: 'Component and Library Icons',+ category: 'foundations',+ content: [+ {+ type: 'prose',+ text: 'A glyph that belongs to one component or library gets a namespaced key (`numberInput:stepperDown`, `richtext:bold`) instead of a new semantic name. It resolves through the same registry and a theme overrides it the same way, but the shared IconName list stays reserved for glyphs the whole system uses — so adding one does not make every downstream icon registry grow a key.',+ },+ {+ type: 'code',+ lang: 'tsx',+ label: 'Owning and theming a namespaced icon',+ code: `// The component renders it like any other name, keeping size and color.+<Icon icon="numberInput:stepperDown" size="xsm" />++// A theme maps it independently of the shared chevron.+export const brandTheme = defineTheme({+ name: 'brand',+ icons: {+ chevronDown: <ChevronDownIcon />,+ 'numberInput:stepperDown': <CaretDownFilledIcon />,+ },+});`,+ },+ {+ type: 'prose',+ text: 'Ship the fallback in `defaultIcons` under the same key so the glyph still renders with no theme, or pass one to `getExtendedIcon(key, fallback)` when the icon lives outside core.',+ },+ ],+ },{title: 'Adding New Icons',category: 'foundations',content: [{type: 'prose',- text: 'To add a new semantic icon name to the design system:',+ text: 'To add a new semantic icon name to the design system — only for a glyph the whole system shares; a component-owned one takes a namespaced key instead:',},{type: 'list',diff --git a/packages/core/src/Icon/Icon.test.tsx b/packages/core/src/Icon/Icon.test.tsxindex 0e79619eec..10df5cb27d 100644--- a/packages/core/src/Icon/Icon.test.tsx+++ b/packages/core/src/Icon/Icon.test.tsx@@ -17,7 +17,7 @@ import {Theme} from '../theme/Theme';import {defineTheme} from '../theme/defineTheme';import {resetThemes} from '../theme/themeRegistry';import {Icon} from './Icon';-import {resetIcons} from './globalIconRegistry';+import {registerIcons, resetIcons} from './globalIconRegistry';describe('Icon', () => {it('renders the icon component', () => {@@ -192,6 +192,60 @@ describe('Icon', () => {expect(screen.getByTestId('inner')).toHaveTextContent('inner-check');});+ describe('namespaced extension keys', () => {+ it('renders a theme-registered namespaced key with size and color applied', () => {+ resetIcons();+ resetThemes();+ const theme = defineTheme({+ name: 'ns-theme',+ icons: {'numberInput:stepperDown': 'themed-stepper'},+ });++ render(+ <Theme theme={theme}>+ <Icon icon="numberInput:stepperDown" size="xsm" data-testid="icon" />+ </Theme>,+ );++ const icon = screen.getByTestId('icon');+ expect(icon).toHaveTextContent('themed-stepper');+ expect(icon).toHaveAttribute('aria-hidden', 'true');+ expect(icon.className).not.toBe('');+ });++ it('resolves a namespaced key registered globally', () => {+ resetIcons();+ resetThemes();+ registerIcons({'numberInput:stepperDown': 'global-stepper'});++ render(<Icon icon="numberInput:stepperDown" data-testid="icon" />);++ expect(screen.getByTestId('icon')).toHaveTextContent('global-stepper');+ });++ it('still rejects a misspelled built-in name', () => {+ render(+ // @ts-expect-error — the prop takes built-in names and namespaced+ // keys, not any string, so a typo is still a compile error.+ <Icon icon="chevrondown" data-testid="icon" />,+ );++ expect(screen.queryByTestId('icon')).not.toBeInTheDocument();+ });++ it('renders nothing when a namespaced key resolves to nothing', () => {+ resetIcons();+ resetThemes();++ const {container} = render(+ <Icon icon="numberInput:missing" data-testid="icon" />,+ );++ expect(screen.queryByTestId('icon')).not.toBeInTheDocument();+ expect(container).toBeEmptyDOMElement();+ });+ });+it('lets a string-mode icon be made meaningful by overriding aria-hidden', () => {render(<Icondiff --git a/packages/core/src/Icon/Icon.tsx b/packages/core/src/Icon/Icon.tsxindex 9390ed7108..818f36aa72 100644--- a/packages/core/src/Icon/Icon.tsx+++ b/packages/core/src/Icon/Icon.tsx@@ -28,7 +28,7 @@ import type {StyleXStyles} from '@stylexjs/stylex';import {colorVars} from '../theme/tokens.stylex';import {useThemeName} from '../theme/useTheme';import {getIcon} from './globalIconRegistry';-import type {IconName} from './globalIconRegistry';+import type {IconName, NamespacedIconName} from './globalIconRegistry';import {mergeProps} from '../utils';import {themeProps} from '../utils/themeProps';@@ -199,9 +199,11 @@ export interface IconProps extends Omit</*** Icon to render. Can be:* - A semantic name string (e.g. 'close', 'chevronDown') — resolved from theme or built-in fallback+ * - A namespaced extension key (e.g. 'richtext:bold') for a glyph owned by+ * one component or library — resolved the same way, themeable by key* - An SVG icon component (e.g. from @heroicons/react) — rendered directly*/- icon: IconType | IconName;+ icon: IconType | IconName | NamespacedIconName;/*** The color variant of the icon.* @default 'inherit'@@ -371,7 +373,7 @@ function IconFromRegistry({xstyle,spanProps,}: {- name: IconName;+ name: IconName | NamespacedIconName;color: IconColor;size: IconSize;a11yProps: {role: 'img'; 'aria-label': string} | {'aria-hidden': 'true'};diff --git a/packages/core/src/Icon/defaultIcons.tsx b/packages/core/src/Icon/defaultIcons.tsxindex eab3729ccb..0fa1050275 100644--- a/packages/core/src/Icon/defaultIcons.tsx+++ b/packages/core/src/Icon/defaultIcons.tsx@@ -23,7 +23,8 @@* - /packages/core/src/Icon/Icon.doc.mjs (fallback icon documentation)*/-import type {IconRegistry} from './globalIconRegistry';+import type {ReactNode} from 'react';+import type {IconRegistry, NamespacedIconName} from './globalIconRegistry';const svgProps = {xmlns: 'http://www.w3.org/2000/svg',@@ -53,7 +54,8 @@ const solidSvgProps = {'aria-hidden': true as const,};-export const defaultIcons: IconRegistry = {+export const defaultIcons: IconRegistry &+ Partial<Record<NamespacedIconName, ReactNode>> = {/** ✕ — two diagonal lines */close: (<svg {...svgProps}>@@ -68,6 +70,13 @@ export const defaultIcons: IconRegistry = {</svg>),+ /** ⌄ — compact NumberInput stepper chevron */+ 'numberInput:stepperDown': (+ <svg {...svgProps}>+ <path d="M7.5 9.75l4.5 4.5 4.5-4.5" />+ </svg>+ ),+/** ‹ — left chevron */chevronLeft: (<svg {...svgProps}>diff --git a/packages/core/src/Icon/globalIconRegistry.test.tsx b/packages/core/src/Icon/globalIconRegistry.test.tsxindex 84f5d7bc21..1deb79db62 100644--- a/packages/core/src/Icon/globalIconRegistry.test.tsx+++ b/packages/core/src/Icon/globalIconRegistry.test.tsx@@ -27,12 +27,22 @@ describe('iconRegistry (global, RSC-compatible)', () => {it('returns a default icon registry snapshot', () => {const registry = getIconRegistry();+ const builtInDefaults = Object.fromEntries(+ Object.entries(defaultIcons).filter(([name]) => !name.includes(':')),+ );- expect(Object.keys(registry)).toEqual(Object.keys(defaultIcons));- expect(registry).toEqual(defaultIcons);+ expect(registry).toEqual(builtInDefaults);expect(registry).not.toBe(defaultIcons);});+ it('provides a distinct default for the NumberInput stepper icon', () => {+ const icon = getIcon('numberInput:stepperDown');++ expect(icon).toBe(defaultIcons['numberInput:stepperDown']);+ expect(icon).toBeDefined();+ expect(icon).not.toBe(defaultIcons.chevronDown);+ });+it('returns default icons when nothing is registered', () => {const icon = getIcon('close');expect(icon).toBeDefined();@@ -149,7 +159,10 @@ describe('iconRegistry (global, RSC-compatible)', () => {registerIcons({'richtext:bold': 'my-bold'});// getIconRegistry() is the built-in IconName snapshot; extension keys// are resolved via getIcon/getExtendedIcon, not surfaced here.- expect(Object.keys(getIconRegistry())).toEqual(Object.keys(defaultIcons));+ expect(Object.keys(getIconRegistry())).not.toContain('richtext:bold');+ expect(Object.keys(getIconRegistry())).not.toContain(+ 'numberInput:stepperDown',+ );});it('extension keys are cleared by resetIcons', () => {diff --git a/packages/core/src/Icon/globalIconRegistry.tsx b/packages/core/src/Icon/globalIconRegistry.tsxindex edff668457..0c7ba77c18 100644--- a/packages/core/src/Icon/globalIconRegistry.tsx+++ b/packages/core/src/Icon/globalIconRegistry.tsx@@ -57,6 +57,20 @@ export type IconName =| 'stop'| 'microphone';+/**+ * A namespaced extension key, `'<namespace>:<name>'`.+ *+ * This is the tier for a glyph that belongs to one component or library rather+ * than to the system: `'richtext:bold'`, `'numberInput:stepperDown'`. A theme+ * overrides it by key through `registerIcons` or `defineTheme({icons})`,+ * exactly as it overrides a built-in name, and the core {@link IconName} union+ * stays reserved for glyphs the whole system shares.+ *+ * Accepted anywhere a built-in name is, including `<Icon icon>` — which is+ * what lets a namespaced glyph keep `size`, `color` and `xstyle`.+ */+export type NamespacedIconName = `${string}:${string}`;+/*** A semantic icon name — either one of the built-in {@link IconName}s or an* arbitrary string key contributed by a library/app.@@ -81,9 +95,19 @@ export type IconRegistrySource = DefinedTheme | string | null | undefined;let globalRegistry: Record<string, ReactNode> = {};+/**+ * A key is namespaced when it carries a `<namespace>:` prefix — the runtime+ * form of {@link NamespacedIconName}, which is how the typed+ * {@link getIconRegistry} snapshot tells a built-in name from a component- or+ * library-owned one.+ */+function isNamespacedKey(name: string): boolean {+ return name.includes(':');+}+function getThemeIconOverrides(source: IconRegistrySource,-): Partial<IconRegistry> | null {+): Partial<Record<IconName | NamespacedIconName, ReactNode>> | null {if (source == null) {return null;}@@ -140,23 +164,30 @@ export function registerIcons(export function getIconRegistry(source?: IconRegistrySource,): Readonly<IconRegistry> {- const registry = {...defaultIcons};+ const registry: Record<string, ReactNode> = {};- // Only surface built-in IconName keys here — extension keys registered by- // libraries are resolved via getIcon/getExtendedIcon and intentionally kept- // out of the typed IconRegistry snapshot.+ // Only surface built-in IconName keys here — namespaced keys, whether+ // contributed by a library or shipped as a component's own default, are+ // resolved via getIcon/getExtendedIcon and intentionally kept out of the+ // typed IconRegistry snapshot.for (const name of Object.keys(defaultIcons) as IconName[]) {+ if (isNamespacedKey(name)) {+ continue;+ }registry[name] = globalRegistry[name] ?? defaultIcons[name];}const themeIcons = getThemeIconOverrides(source);if (themeIcons != null) {for (const name of Object.keys(themeIcons) as IconName[]) {+ if (isNamespacedKey(name)) {+ continue;+ }registry[name] = themeIcons[name] ?? registry[name];}}- return registry;+ return registry as IconRegistry;}/**diff --git a/packages/core/src/Icon/index.ts b/packages/core/src/Icon/index.tsindex 4dbff80266..cb63bdaafd 100644--- a/packages/core/src/Icon/index.ts+++ b/packages/core/src/Icon/index.ts@@ -26,6 +26,7 @@ export {export type {IconName,ExtendedIconName,+ NamespacedIconName,IconRegistry,IconRegistrySource,} from './globalIconRegistry';diff --git a/packages/core/src/Icon/useIcon.ts b/packages/core/src/Icon/useIcon.tsindex e3302dadb7..8993ebd566 100644--- a/packages/core/src/Icon/useIcon.ts+++ b/packages/core/src/Icon/useIcon.ts@@ -11,13 +11,20 @@import type {ReactNode} from 'react';import {useThemeName} from '../theme/useTheme';-import {getIcon, type IconName} from './globalIconRegistry';+import {+ getIcon,+ type IconName,+ type NamespacedIconName,+} from './globalIconRegistry';/*** Resolve a semantic icon name from the nearest Theme, falling back through* root theme registration, global icon overrides, and built-in defaults.+ *+ * Accepts a namespaced extension key (`'richtext:bold'`) as well as a built-in+ * name; both resolve through the same registry.*/-export function useIcon(name: IconName): ReactNode {+export function useIcon(name: IconName | NamespacedIconName): ReactNode {const themeName = useThemeName();return getIcon(name, themeName);}diff --git a/packages/core/src/NumberInput/NumberInput.test.tsx b/packages/core/src/NumberInput/NumberInput.test.tsxindex a7e539d84a..1d4de5d1ec 100644--- a/packages/core/src/NumberInput/NumberInput.test.tsx+++ b/packages/core/src/NumberInput/NumberInput.test.tsx@@ -15,6 +15,7 @@ import {render, screen, fireEvent, waitFor} from '@testing-library/react';import userEvent from '@testing-library/user-event';import {TestIcon} from '../__tests__/TestIcon';import {InternationalizationProvider} from '../i18n';+import {registerIcons, resetIcons} from '../Icon';import {InputGroup} from '../InputGroup';import {NumberInput} from './NumberInput';import {defineTheme} from '../theme/defineTheme';@@ -26,6 +27,7 @@ import {__resetLiveRegionsForTest} from '../hooks/useAnnounce';// file never match a leftover region.afterEach(() => {__resetLiveRegionsForTest();+ resetIcons();});// Mock showPopover/hidePopover since jsdom does not implement them. Used by the@@ -2074,6 +2076,33 @@ describe('NumberInput stepping', () => {).toHaveAttribute('tabindex', '-1');});+ it('uses the NumberInput extension icon for both stepper buttons', () => {+ const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});+ registerIcons({+ chevronDown: <svg data-testid="generic-chevron-down" />,+ 'numberInput:stepperDown': <svg data-testid="number-stepper-down" />,+ });++ render(+ <NumberInput+ label="Quantity"+ value={5}+ onChange={() => {}}+ hasNumberSteppers+ />,+ );++ const stepperIcons = screen.getAllByTestId('number-stepper-down');+ expect(stepperIcons).toHaveLength(2);+ for (const icon of stepperIcons) {+ expect(icon.parentElement).toHaveAttribute('data-size', 'xsm');+ }+ expect(+ screen.queryByTestId('generic-chevron-down'),+ ).not.toBeInTheDocument();+ warnSpy.mockRestore();+ });+it('steps the value and returns focus to the input', () => {const onChange = vi.fn();render(diff --git a/packages/core/src/NumberInput/NumberInput.tsx b/packages/core/src/NumberInput/NumberInput.tsxindex a5196ca593..7d9a3f8261 100644--- a/packages/core/src/NumberInput/NumberInput.tsx+++ b/packages/core/src/NumberInput/NumberInput.tsx@@ -972,7 +972,7 @@ export function NumberInput({styles.numberStepperButtonDisabled,)}><Icon- icon="chevronDown"+ icon="numberInput:stepperDown"size="xsm"color="inherit"xstyle={styles.incrementIcon}@@ -994,7 +994,7 @@ export function NumberInput({(isDisabled || isReadOnly || !canDecrement) &&styles.numberStepperButtonDisabled,)}>- <Icon icon="chevronDown" size="xsm" color="inherit" />+ <Icon icon="numberInput:stepperDown" size="xsm" color="inherit" /></button></div>)}diff --git a/packages/core/src/theme/defineTheme.ts b/packages/core/src/theme/defineTheme.tsindex a2e32bd3a9..3bf1617f3b 100644--- a/packages/core/src/theme/defineTheme.ts+++ b/packages/core/src/theme/defineTheme.ts@@ -36,7 +36,16 @@* - /packages/cli/assets/docs/theme.doc.mjs (`astryx docs theme`)*/-import type {IconRegistry} from '../Icon/globalIconRegistry';+import type {ReactNode} from 'react';+import type {IconName, NamespacedIconName} from '../Icon/globalIconRegistry';++/**+ * Icon overrides a theme may declare: any built-in semantic name, plus the+ * namespaced keys components and libraries own (`'richtext:bold'`).+ */+type ThemeIconOverrides = Partial<+ Record<IconName | NamespacedIconName, ReactNode>+>;import type {IndicatorRegistry} from '../Indicator/types';import type {TypographyConfig, FontWeight} from './types';import {@@ -319,7 +328,7 @@ export interface DefineThemeInput {*/components?: ComponentStyleMap;/** Icon registry — maps semantic icon names to React nodes */- icons?: Partial<IconRegistry>;+ icons?: ThemeIconOverrides;/*** Indicator overrides — replaces the components that draw stateful control* visuals with the theme's own, by name.@@ -381,7 +390,7 @@ export interface DefinedTheme {/** Component style overrides */components?: ComponentStyleMap;/** Icon registry */- icons?: Partial<IconRegistry>;+ icons?: ThemeIconOverrides;/** Indicator overrides for stateful control visuals, keyed by name */indicators?: IndicatorRegistry;/** Whether this theme has been pre-compiled by theme build CLI */