[RFC] Explicit opt-in to exclude defaultIcons when an app supplies all icons (#12018)
Follow-up to #12018 (item 2) after closing the deferred-loader PR. Once a maintainer signs off on the API direction below, I will implement the fix and open the PR (and I'll also re-measure item 1 of #12018 once the in-progress hit-area rewrite lands).
Problem Statement
Design systems and applications built on @astryxdesign/core that supply every semantic icon themselves (via defineTheme({ icons }) or registerIcons()) still bundle Icon/defaultIcons.tsx (~30 inline <svg> fallbacks) into every page's first load whenever Icon, Button, or Toast is imported, because Icon/globalIconRegistry.tsx statically imports defaultIcons.
Per #12018 and the maintainer review on the deferred-loader follow-up PR, loading defaultIcons asynchronously breaks synchronous getIcon() / getIconRegistry() calls and causes un-themed pages to render icons after first paint. Apps that already provide all icons need an explicit opt-in to exclude the built-in defaultIcons set entirely without changing default behavior for un-themed apps.
Evidence of Demand
- Reported directly in #12018 (item 2): "An app that calls
registerIcons()for every name the components resolve never draws them. Could the defaults sit behind a separate entry, or be imported only when nothing has been registered for a name?" - Any design system built on
@astryxdesign/corewith its own icon set (or an Astryx theme providingicons) hits this on everyButtonpage sinceButtonimportsIcon. - Measured on
main(@astryxdesign/core0.7.18, Vite 8.3.5 / Rolldown, minified, React external, gzip level 9): stubbingIcon/defaultIcons.jsto{}saves1.76 KB(1,803 B) onIcon,1.76 KB(1,807 B) onButton, and1.75 KB(1,795 B) onToast.
Why Existing Components Don't Cover This
- Calling
registerIcons()ordefineTheme({ icons })overridesdefaultIconsat runtime, butglobalIconRegistry.tsxstill statically importsdefaultIcons.tsx(and usesObject.keys(defaultIcons)/Object.hasOwn(defaultIcons, value)ingetIconRegistry()andisIconName()), so bundlers cannot tree-shakedefaultIconsout. - A runtime-only toggle cannot drop the static import from the bundle graph.
- Deferring
defaultIconsviaimport()breaks synchronousgetIcon()/getIconRegistry()(including RSC/SSR) and violates the rule against changing rendered content after first paint on un-themed pages.
Rough Approaches Considered
First, decouple built-in key metadata in Icon/globalIconRegistry.tsx from the fallback JSX by using a small BUILTIN_ICON_NAME_MAP: Record<IconName, true> for getIconRegistry() and isIconName(), so defaultIcons is only needed for fallback artwork in selectIconEntry().
Then expose an explicit opt-in through one of these shapes:
Option A — Export condition and/or @astryxdesign/build flag (zero change to component imports):
- Route the
defaultIconsimport through a package export condition (e.g.,"astryx:no-default-icons"inresolve.conditions) and/or add an option likedefaultIcons: falsein@astryxdesign/build(astryxStylex()invite.tsandwithAstryx()innext.js) that resolvesdefaultIconsto{ defaultIcons: {} }.
// vite.config.ts (with @astryxdesign/build)
astryxStylex({ defaultIcons: false })
// or plain Vite without @astryxdesign/build
resolve: { conditions: ['astryx:no-default-icons'] }
- Pros: Works transparently across
@astryxdesign/core,@astryxdesign/core/Button,@astryxdesign/core/Icon, etc.; completely non-breaking for default consumers.
Option B — Explicit fallback registration entry (for 0.8.0 or a no-defaults entry):
- Keep
globalIconRegistryfree ofdefaultIconsunless a consumer imports an entry like@astryxdesign/core/default-icons(registerDefaultIcons(defaultIcons)), or provide a separate no-defaults entry. - Pros: Pure ESM with no bundler condition/plugin configuration.
- Cons: Changing the default is breaking for un-themed apps; a separate entry does not apply when components import
Icon/globalIconRegistrytransitively from@astryxdesign/core/Button.
Accessibility Considerations
No change to ARIA contracts when all icons are registered (the intended contract for the opt-in). If an app opts in and forgets to register a semantic name, Icon renders nothing (null) on both SSR and client (no layout/content shift after paint); in development builds we can warn once when an unregistered name resolves to undefined under the opt-in.
Performance Considerations
- Eliminates
1.76 KBgzip level 9 (~1.8 KB) from the first-load chunk ofIcon,Button,Toast, and every entry that reachesIcon(verified with Vite 8.3.5 / Rolldown, minified, React external). - Synchronous resolution order (
theme.icons→registerIcons()) is unchanged and adds zero runtime overhead, zero timers, and zero post-paint re-renders.
Pre-submission Checklist
- [x] I have read the Contributing guide
- [x] I have read the API Conventions
- [x] I have checked that existing Astryx components cannot compose to solve this
- [x] This is a general-purpose UI pattern (not specific to one product)
🤖 (Posted by Cindy's agent.) Thanks for writing this up, @Geervan, and for the measurements behind it (~1.76 KB on Icon, Button and Toast).
Where it stands: today a theme's icons and registerIcons() can override the built-in artwork but not drop it, so the saving you describe is real. Adding an explicit opt-out would be new public API, though, and the project adds new API once at least two real consumers need it. So the recommendation going to the maintainer is "not planned for now". It reopens when a second consumer appears, such as a shipped theme or icon set that supplies every icon name. The maintainer makes the final call; this issue stays open until then.
One request in the meantime: please don't land the BUILTIN_ICON_NAME_MAP groundwork on its own. It doesn't change anything users see by itself, and it's half of an API that isn't decided yet.