Repository navigation
feat(Icon): a theme icon entry can be a function of the requested size - #8753
Conversation
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.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
There was a problem hiding this comment.
Size-aware function entries work in core and match spec:AST-087, but two fixes are needed before this can be approved.
CommentIconinpackages/lab/src/Comments/icons.tsxchecksgetExtendedIcon(key, undefined, themeName) != null, so a themecomment:*function that returnsnullstill 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 iconsprose changes four snapshots inpackages/cli/test/experimental-isolation.test.mjs(thetest-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), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()`.", |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
PR Analysis Report
|
…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.
There was a problem hiding this comment.
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]
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.
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
infobeside 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(explicitsize→ 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})andregisterIcons()accept, per key, a React node or a pure synchronous function of{size, variant}.Iconresolves its size bycomponent:IconORD1 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, andDefinedTheme['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
CommentIconusedgetExtendedIcon(key) != nullto decide whether a theme overrides acomment:*glyph, so a theme function returningnullfell back to the bundled glyph. It now checks against a sentinel fallback, so the theme'snulldraws nothing (FR6).CLI goldens (supporting). The new
iconsdocs prose changes the topic skeleton and theastryx search iconranking captured inpackages/cli/test/__golden__/no-icon-set/*. They are regenerated.variantis alwaysundefinedin this PR. The variant list and thevariantprop 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}; herevariantisundefined), ORD1.Not in this PR:
iconVariantsandIconVariantMap(next PR),<Icon variant>(the PR after),componentIconsslots, FR8 enumeration, FR10 size dimensions, and theme build output.Public delta and caller burden
DefineThemeInput['icons']andregisterIcons()accept function entries.Before:
After (node entries still work; a function is an additional option):
New exported types from
@astryxdesign/core/Icon(and the package root):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 ofgetIcon,getExtendedIcon,getIconRegistry, anduseIcon.Behavior:
<Icon icon="info" size="sm" />calls a function entry with{size: 'sm'}; withoutsize, it passes the owning component's default, elsemd.getIcon('info', theme),getIconRegistry(theme),getExtendedIcon(...),useIcon('info'), andtheme.icons.infocall it with{size: 'md'}.nullrenders no glyph and no wrapper.getExtendedIcon(key, fallback)returns thatnull, notfallback.extendschild 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
iconsobject identity.Theming and accessibility: no change. The
icontarget, itsdata-size/data-colorreflection, and label semantics are untouched.Design and alternatives
defineThemekeeps the authored entries (functions included) for Astryx resolution in an internal field, andDefinedTheme['icons']is the node view with each function read atmd. When no entry is a function, the internal field is absent andiconsis the same object as before. Resolution reads the internal entries first, thenicons, so a theme object that carries functions iniconsstill resolves.Iconcalls an internalresolveIcon(name, theme, {size}); the public one-argument reads share the same source selection with{size: 'md'}, sogetIcon, the snapshot, and rendered output cannot drift.Evidence
globalIconRegistry.test.tsx, newfunction entriessuite): 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,getExtendedIconwith a fallback);extendsinheritance and whole-entry replacement; every public read is node-valued and reads atmd;useIconinsideTheme; type assertions (IconRegistryandDefinedTheme['icons']node-valued; contextualsize: IconSize,variant: undefined; non-node results and unknown request fields rejected).Icon.test.tsx, newfunction registry entriessuite): each explicit size withdata-size; the standalonemdfallback; the owning component's contextual default, with an explicit size taking precedence; process-wide functions; nested themes; anullresult renders no glyph; a supplied icon component never calls the entry.defineTheme.test.ts: the publiciconsview reads functions atmdexactly once; function entries merge by key throughextends; 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 returningnullthat draws nothing. The last test fails with the previous check.packages/cli/test/experimental-isolation.test.mjs: 34/34 with regenerated goldens.Scope
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 cleanmainin the same environment (postinstall.test.mjs"still nudges when no folder up to the repository root ran init", and theMarkdown.fr23.perf.test.tstiming 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/componentplus 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(includingtypecheck:docs): clean.pnpm -F @astryxdesign/cli typecheck: twoTS2307errors for@astryxdesign/theme-neutral/builtin page templates, also present onmainwithout a local package build. No other errors.pnpm -F @astryxdesign/core lintandeslinton changed files: 0 errors.prettier --checkon 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.