Skip to content

feat(Icon): a theme icon entry can be a function of the requested size - #8753

Merged
imdreamrunner merged 2 commits into
mainfrom
icons/ast087-function-entries
Oct 28, 2026
Merged

imdreamrunner merged 2 commits into
mainfrom
icons/ast087-function-entries

Conversation

@imdreamrunner

@imdreamrunner imdreamrunner commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

User need

A theme author who brings an icon library maps each semantic icon name to that library's artwork. Many libraries draw one glyph at several stroke weights or optical sizes, and the right choice depends on the size Astryx renders the icon at: a 16px info beside a small control needs a heavier stroke than a 24px one to stay legible.

Today a theme icon entry is one React node for every size. The author can only pick one artwork for all four Icon sizes, so small icons look thin or large icons look heavy. Composition cannot fix it: the size is resolved inside Icon (explicit size → owning component's default → md), and an entry has no way to see it.

This PR lets an entry be a pure function of the request, so the theme picks artwork for the size Icon actually renders.

Problem and solution fit

  • Function entries (primary). defineTheme({icons}) and registerIcons() accept, per key, a React node or a pure synchronous function of {size, variant}. Icon resolves its size by component:Icon ORD1 and passes it to the selected entry, so the theme maps each size to its own artwork or props.

  • One source per resolution (supporting). The theme entry wins over a process-wide registration, which wins over the built-in default. A nullish static entry still falls through. A selected function's result is final: a nullish result renders no glyph and never borrows another source's artwork, so a theme can deliberately draw nothing.

  • Node-valued public reads (supporting). getIcon, getExtendedIcon, getIconRegistry, useIcon, IconRegistry, and DefinedTheme['icons'] keep returning React nodes. They read a function entry at {size: 'md'}, the standalone Icon default, so existing readers never receive a function.

  • Consumers keep the final-result rule (supporting). Lab's CommentIcon used getExtendedIcon(key) != null to decide whether a theme overrides a comment:* glyph, so a theme function returning null fell back to the bundled glyph. It now checks against a sentinel fallback, so the theme's null draws nothing (FR6).

  • CLI goldens (supporting). The new icons docs prose changes the topic skeleton and the astryx search icon ranking captured in packages/cli/test/__golden__/no-icon-set/*. They are regenerated.

variant is always undefined in this PR. The variant list and the variant prop follow in the next two stacked PRs.

Current specification

  • spec:AST-087 (current): FR1 (an entry is a node or a function of the request), FR6 (source precedence; a selected function's result is final), FR7 (entries are pure and synchronous), FR9 (entries receive the size resolved by ORD1), FR18 (icons part: public reads return nodes; a function entry is read at {size: 'md'}). Decisions DEC-2, DEC-6, DEC-7.
  • architecture:icon-resolution-and-component-slots → Shared icon resolution, INV11.
  • architecture:theme-authoring-contract → System model (an explicit entry replaces the inherited entry whole; DefinedTheme['icons'] is node-valued), INV3, INV13.
  • component:Icon → FR6 (registry mode passes {size, variant}; here variant is undefined), ORD1.

Not in this PR: iconVariants and IconVariantMap (next PR), <Icon variant> (the PR after), componentIcons slots, FR8 enumeration, FR10 size dimensions, and theme build output.

Public delta and caller burden

DefineThemeInput['icons'] and registerIcons() accept function entries.

Before:

defineTheme({
  name: 'brand',
  icons: {info: <InfoIcon />},
});

After (node entries still work; a function is an additional option):

defineTheme({
  name: 'brand',
  icons: {
    close: <XMarkIcon />,
    info: ({size}) => <InfoIcon weight={size === 'sm' ? 'bold' : 'regular'} />,
  },
});

registerIcons({
  info: ({size}) => <InfoIcon weight={size === 'sm' ? 'bold' : 'regular'} />,
});

New exported types from @astryxdesign/core/Icon (and the package root):

type IconRequest = Readonly<{size: 'xsm' | 'sm' | 'md' | 'lg'; variant?: undefined}>;
type IconEntry = ReactNode | ((request: IconRequest) => ReactNode);

They let a theme type an icon registry kept in its own module (const icons: Partial<Record<IconName, IconEntry>> = …).

Unchanged types: IconRegistry (Record<IconName, ReactNode>), DefinedTheme['icons'] (node-valued), and the return types of getIcon, getExtendedIcon, getIconRegistry, and useIcon.

Behavior:

  • <Icon icon="info" size="sm" /> calls a function entry with {size: 'sm'}; without size, it passes the owning component's default, else md.
  • getIcon('info', theme), getIconRegistry(theme), getExtendedIcon(...), useIcon('info'), and theme.icons.info call it with {size: 'md'}.
  • A function that returns null renders no glyph and no wrapper. getExtendedIcon(key, fallback) returns that null, not fallback.
  • An extends child inherits a base function entry and keeps calling it per request; a child entry for the same key replaces it whole.

Caller decisions: none for existing callers. A theme author who adopts a function entry decides how size maps to artwork, and keeps the function pure and synchronous (no hooks, DOM measurement, browser or mutable state). The docs say so.

Compatibility: additive. Node entries, namespaced keys, nullish fall-through for static entries, direct icon components, and every public read behave as before. A theme with no function entries keeps the same icons object identity.

Theming and accessibility: no change. The icon target, its data-size/data-color reflection, and label semantics are untouched.

Design and alternatives

  • defineTheme keeps the authored entries (functions included) for Astryx resolution in an internal field, and DefinedTheme['icons'] is the node view with each function read at md. When no entry is a function, the internal field is absent and icons is the same object as before. Resolution reads the internal entries first, then icons, so a theme object that carries functions in icons still resolves.
  • Icon calls an internal resolveIcon(name, theme, {size}); the public one-argument reads share the same source selection with {size: 'md'}, so getIcon, the snapshot, and rendered output cannot drift.
  • Rejected by the spec: per-icon capability declarations, adaptive variant trees, falling through on a nullish function result (DEC-2, DEC-6), and public reads that return functions (DEC-7).

Evidence

  • Registry and resolver tests (globalIconRegistry.test.tsx, new function entries suite): each of the four sizes reaches theme and process-wide functions; namespaced keys; registered-theme-name lookups; theme > process-wide > default; nullish static entries fall through; nullish function results are final (getIcon, snapshot, getExtendedIcon with a fallback); extends inheritance and whole-entry replacement; every public read is node-valued and reads at md; useIcon inside Theme; type assertions (IconRegistry and DefinedTheme['icons'] node-valued; contextual size: IconSize, variant: undefined; non-node results and unknown request fields rejected).
  • Icon render tests (Icon.test.tsx, new function registry entries suite): each explicit size with data-size; the standalone md fallback; the owning component's contextual default, with an explicit size taking precedence; process-wide functions; nested themes; a null result renders no glyph; a supplied icon component never calls the entry.
  • defineTheme.test.ts: the public icons view reads functions at md exactly once; function entries merge by key through extends; the existing identity test still passes.
  • packages/lab/src/Comments/icons.test.tsx: the bundled glyph with no entry, a theme node entry, a theme function entry at the glyph's size, and a function returning null that draws nothing. The last test fails with the previous check.
  • packages/cli/test/experimental-isolation.test.mjs: 34/34 with regenerated goldens.
  • Unchanged paths: every existing Icon, registry, theme, IconButton, lab Comments, and richtext test passes.

Scope

  • One capability is added.
  • Unrelated fixes, cleanup, and policy changes were removed or split.
  • Consumer docs, focused tests, and migration evidence ship with the feature.
  • Public text and artifacts contain no internal Meta context.

Testing

  • pnpm vitest run packages/core/src/Icon packages/core/src/theme/defineTheme.test.ts: 237 passed.
  • pnpm vitest run packages/core/src: everything passes except two files that fail identically on clean main in the same environment (postinstall.test.mjs "still nudges when no folder up to the repository root ran init", and the Markdown.fr23.perf.test.ts timing tests, which also fail run alone with --no-file-parallelism).
  • pnpm vitest run packages/lab/src/Comments packages/richtext/src: 665 passed.
  • pnpm vitest run packages/cli/api/docs scripts/check-theme-template.test.mjs packages/cli/api/component plus the theme build icon suites (build.icon-lineage, build.icon-preservation, build.icons-specifier): all pass. Three CLI tests time out only under parallel load and pass when run alone.
  • pnpm -F @astryxdesign/core typecheck (including typecheck:docs): clean.
  • pnpm -F @astryxdesign/cli typecheck: two TS2307 errors for @astryxdesign/theme-neutral/built in page templates, also present on main without a local package build. No other errors.
  • pnpm -F @astryxdesign/core lint and eslint on changed files: 0 errors.
  • prettier --check on changed TS/TSX/MD files: clean.
  • pnpm check:repo (knowledge, sync, changesets, i18n, …): passes.
  • node scripts/release/changeset-coverage.mjs pr --base origin/main --explain: passes.

A theme icon entry in defineTheme({icons}) or registerIcons() may be a
React node or a pure synchronous function of {size, variant}. Icon passes
the size it resolves (explicit size, owning component's default, md), so
a theme can map each Icon size to its icon library's artwork or props.

Sources resolve before the request: theme entry, then process-wide
registration, then built-in default; a nullish static entry falls through,
and a selected function's result is final. getIcon, getExtendedIcon,
getIconRegistry, useIcon, IconRegistry and DefinedTheme['icons'] stay
node-valued and read a function entry at {size: 'md'}. variant is always
undefined until a theme lists variants.

Implements spec:AST-087 FR1, FR6, FR7, FR9 and the icons part of FR18.
@vercel

vercel Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Oct 28, 2026 12:56am UTC

Request Review

@astracat-bot

astracat-bot Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Oct 28, 2026, 1:21 AM UTC

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Size-aware function entries work in core and match spec:AST-087, but two fixes are needed before this can be approved.

  • CommentIcon in packages/lab/src/Comments/icons.tsx checks getExtendedIcon(key, undefined, themeName) != null, so a theme comment:* function that returns null still shows the bundled glyph; please base that check on whether a theme or registered entry exists and add a test for it.
  • The new astryx docs icons prose changes four snapshots in packages/cli/test/experimental-isolation.test.mjs (the test-node (1/4) failure); please confirm the new search ranking and update the snapshot.

[Automated review]

defaultIcons[name as IconName] ??
fallback
return readIconEntry(
selectIconEntry(name, source, fallback),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a function's null now final, getExtendedIcon(key, undefined, theme) != null no longer tells whether a source answers the key. Lab CommentIcon (packages/lab/src/Comments/icons.tsx:123) uses exactly that check, so a theme's 'comment:reply': () => null renders the bundled glyph instead of nothing (spec:AST-087 FR6, DEC-6). Basing that check on whether a theme or registered entry exists, plus a Comments test with a function returning null, would close it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 55cf5ab. CommentIcon now passes a sentinel fallback to getExtendedIcon and treats any other result, null included, as the theme's answer, so a comment:* function entry that returns null draws nothing. The new packages/lab/src/Comments/icons.test.tsx covers a node entry, a function entry at the glyph's size, and a function that returns null; the last test fails with the old check.

},
{
type: 'prose',
text: "An entry can also be a function of the request, so the theme maps Icon's size to its icon library's own props or artwork. `Icon` calls it with `{size, variant}`, where `size` is the size it renders at (`xsm`, `sm`, `md`, or `lg`: an explicit `size`, else the owning component's default, else `md`). Reads that return one node — `getIcon()`, `useIcon()`, `getIconRegistry()`, and the theme's `icons` field — call it with `{size: 'md'}`. The same entry shape works in `registerIcons()`.",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prose changes the icons docs skeleton and the astryx search results that packages/cli/test/experimental-isolation.test.mjs snapshots; four of its tests fail in CI test-node (1/4). Please confirm the new ranking and regenerate that snapshot.

@imdreamrunner imdreamrunner Oct 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed and regenerated in 55cf5ab. The icons topic gains a size-aware prose and code block under Theme Overrides, so its skeleton hash changes. astryx search icon now also returns docs icons theme-overrides, which pushes the last result (ChatMessageMetadataFooter) out of the capped list; for icon set, the same doc moves above ToolbarTableFilter. Nothing else in the results changes. experimental-isolation.test.mjs passes 34/34 locally.

@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

Previews ready: Storybook and Sandbox were built with this PR's docsite. CI evidence will be added when available.

View Storybook for this PR · View Sandbox for this PR

…fresh CLI goldens

CommentIcon decided whether a theme overrides a comment:* key with
getExtendedIcon(key) != null, so a theme function entry that returns null
fell back to the bundled glyph. It now passes a sentinel fallback and
treats any other result, null included, as the theme's answer
(spec:AST-087 FR6).

The new icons docs prose changes the icons topic skeleton and the
astryx search ranking for 'icon'; the experimental-isolation goldens are
regenerated.

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incremental review of 1748a49..55cf5ab; prior findings carried: user_impact, api, evidence_by_spec_claim

Both requested fixes are in and nothing further is needed for this change: CommentIcon now draws nothing when a theme's comment:* function returns null, with a test that fails against the old check, and the regenerated no-icon-set goldens match the new astryx docs icons output. The size-aware function entries match spec:AST-087; the built-theme part of its FR18 stays with the theme build follow-up the description defers.

[Automated review]

@imdreamrunner
imdreamrunner merged commit 05ead02 into main Oct 28, 2026
78 of 79 checks passed
vjeux added a commit to vjeux/astryx that referenced this pull request Oct 28, 2026
The only conflict is the CLI's icon-set search golden, which counts matching
docs: main's meta#8753 and meta#8759 add two icon docs (367 → 369), and this pull
request's on-screen keyboard section adds one. Regenerated at the merged tree
with the golden test's --update, as its header says to: the count is 370 and
the twenty results shown are main's.
vjeux added a commit to vjeux/astryx that referenced this pull request Oct 28, 2026
Main's Icon changes (meta#8753, meta#8759) moved the CLI's "icon set" search golden to 369 matches; this branch's "radio set" docs add one, so the regenerated golden records 370 (experimental-isolation.test.mjs --update on the merged tree; only the count changes).
@github-actions
github-actions Bot deleted the icons/ast087-function-entries branch October 28, 2026 06:17
vjeux added a commit to vjeux/astryx that referenced this pull request Oct 28, 2026
Main's icon changes (meta#8753, meta#8756, meta#8758, meta#8759) moved the `icon set` search
golden. Regenerated with `vitest run
packages/cli/test/experimental-isolation.test.mjs --update`: only the two
count lines differ from main.

This branch was successfully deployed

1 active deployment
Preview — 55cf5abf Deployed Oct 28, 2026 by vercel[bot]
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