An open source design system that's fully customizable and agent ready
TypeScript
fix(core): Icon sizes use rem so they scale with root font-size (#4120)
Cindy Zhangaa8d47537980
diff --git a/.changeset/icon-size-rem.md b/.changeset/icon-size-rem.mdnew file mode 100644index 0000000000..debfc15d6b--- /dev/null+++ b/.changeset/icon-size-rem.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] Icon: size variants (`xsm`/`sm`/`md`/`lg`) now use `rem` instead of hardcoded `px`, so icons scale in step with text when the document/root `font-size` changes — matching the rest of the design system's rem-based type scale. Fixes #4092.+@cixzhangdiff --git a/packages/core/src/Icon/Icon.test.tsx b/packages/core/src/Icon/Icon.test.tsxindex 6e04a332b1..d51eb2f881 100644--- a/packages/core/src/Icon/Icon.test.tsx+++ b/packages/core/src/Icon/Icon.test.tsx@@ -96,6 +96,52 @@ describe('Icon', () => {expect(screen.getByTestId('icon')).toBeInTheDocument();});+ it('sizes component-mode icons in rem so they scale with root font-size', () => {+ const sizes = {+ xsm: '0.75rem',+ sm: '1rem',+ md: '1.25rem',+ lg: '1.5rem',+ } as const;+ for (const [size, expected] of Object.entries(sizes)) {+ const {unmount} = render(+ <Icon+ icon={TestIcon}+ size={size as keyof typeof sizes}+ data-testid="icon"+ />,+ );+ const style = getComputedStyle(screen.getByTestId('icon'));+ expect(style.width).toBe(expected);+ expect(style.height).toBe(expected);+ unmount();+ }+ });++ it('sizes registry (string-mode) icons in rem, including fontSize', () => {+ const sizes = {+ xsm: '0.75rem',+ sm: '1rem',+ md: '1.25rem',+ lg: '1.5rem',+ } as const;+ for (const [size, expected] of Object.entries(sizes)) {+ const {unmount} = render(+ <Icon+ icon="check"+ size={size as keyof typeof sizes}+ data-testid="icon"+ />,+ );+ const style = getComputedStyle(screen.getByTestId('icon'));+ expect(style.width).toBe(expected);+ expect(style.height).toBe(expected);+ // fontSize is expressed in rem so 1em-based registry icons scale too.+ expect(style.fontSize).toBe(expected);+ unmount();+ }+ });+it('forwards ref correctly', () => {const ref = vi.fn();render(<Icon icon={TestIcon} ref={ref} />);diff --git a/packages/core/src/Icon/Icon.tsx b/packages/core/src/Icon/Icon.tsxindex 89bd9a9e44..bd0d7bb0ba 100644--- a/packages/core/src/Icon/Icon.tsx+++ b/packages/core/src/Icon/Icon.tsx@@ -111,51 +111,64 @@ const colorStyles = stylex.create({/*** Size styles for direct SVG icon components.* Uses width/height only — SVG components handle their own viewBox scaling.+ *+ * Sizes are expressed in `rem` (relative to the root font-size) so icons scale+ * in step with text when the document font-size changes, matching the rest of+ * the design system's rem-based type scale. Values are the px-equivalents at a+ * 16px root: 12px → 0.75rem, 16px → 1rem, 20px → 1.25rem, 24px → 1.5rem.*/const sizeStyles = stylex.create({xsm: {- width: 12,- height: 12,+ width: '0.75rem',+ height: '0.75rem',},sm: {- width: 16,- height: 16,+ width: '1rem',+ height: '1rem',},md: {- width: 20,- height: 20,+ width: '1.25rem',+ height: '1.25rem',},lg: {- width: 24,- height: 24,+ width: '1.5rem',+ height: '1.5rem',},});/*** Size styles for string-based (registry) icons.* Includes fontSize so that 1em-based icons from the registry scale correctly.+ *+ * Expressed in `rem` for the same reason as {@link sizeStyles} — icons track the+ * root font-size instead of being locked to absolute pixels.*/const spanSizeStyles = stylex.create({+ /* eslint-disable @astryx/no-hardcoded-styles -- fontSize here sizes 1em-based+ registry SVGs to the icon box; icons use their own 12/16/20/24 scale, not+ the 14px-anchored textSizeVars type scale. Values are rem so icons track+ the root font-size. */xsm: {- width: 12,- height: 12,- fontSize: 12,+ width: '0.75rem',+ height: '0.75rem',+ fontSize: '0.75rem',},sm: {- width: 16,- height: 16,- fontSize: 16,+ width: '1rem',+ height: '1rem',+ fontSize: '1rem',},md: {- width: 20,- height: 20,- fontSize: 20,+ width: '1.25rem',+ height: '1.25rem',+ fontSize: '1.25rem',},lg: {- width: 24,- height: 24,- fontSize: 24,+ width: '1.5rem',+ height: '1.5rem',+ fontSize: '1.5rem',},+ /* eslint-enable @astryx/no-hardcoded-styles */});// =============================================================================@@ -194,10 +207,10 @@ export interface IconProps extends Omit<color?: IconColor;/*** The size of the icon.- * - 'xsm': 12px- * - 'sm': 16px- * - 'md': 20px- * - 'lg': 24px+ * - 'xsm': 0.75rem (12px at a 16px root)+ * - 'sm': 1rem (16px at a 16px root)+ * - 'md': 1.25rem (20px at a 16px root)+ * - 'lg': 1.5rem (24px at a 16px root)* @default 'md'*/size?: IconSize;