astryx icon indicating copy to clipboard operation
astryx copied to clipboard

[RFC] Explicit opt-in to exclude defaultIcons when an app supplies all icons (#12018)

Open Geervan opened this issue 1 week ago • 1 comments

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/core with its own icon set (or an Astryx theme providing icons) hits this on every Button page since Button imports Icon.
  • Measured on main (@astryxdesign/core 0.7.18, Vite 8.3.5 / Rolldown, minified, React external, gzip level 9): stubbing Icon/defaultIcons.js to {} saves 1.76 KB (1,803 B) on Icon, 1.76 KB (1,807 B) on Button, and 1.75 KB (1,795 B) on Toast.

Why Existing Components Don't Cover This

  • Calling registerIcons() or defineTheme({ icons }) overrides defaultIcons at runtime, but globalIconRegistry.tsx still statically imports defaultIcons.tsx (and uses Object.keys(defaultIcons) / Object.hasOwn(defaultIcons, value) in getIconRegistry() and isIconName()), so bundlers cannot tree-shake defaultIcons out.
  • A runtime-only toggle cannot drop the static import from the bundle graph.
  • Deferring defaultIcons via import() breaks synchronous getIcon() / 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 defaultIcons import through a package export condition (e.g., "astryx:no-default-icons" in resolve.conditions) and/or add an option like defaultIcons: false in @astryxdesign/build (astryxStylex() in vite.ts and withAstryx() in next.js) that resolves defaultIcons to { 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 globalIconRegistry free of defaultIcons unless 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/globalIconRegistry transitively 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 KB gzip level 9 (~1.8 KB) from the first-load chunk of Icon, Button, Toast, and every entry that reaches Icon (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)

Geervan avatar Nov 25 '26 18:11 Geervan

🤖 (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.

cixzhang avatar Nov 25 '26 20:11 cixzhang