Components
DynamicIcon
src/lib/components/ui/dynamic-icon/DynamicIcon.svelte
Purpose
Renders any Lucide™ icon by name at runtime. The icon name is not known at
build time (e.g. it comes from module configuration), so a static import is
not possible. DynamicIcon solves this by pre-registering every icon module
under @lucide/svelte/dist/icons/*.svelte via import.meta.glob, then
lazy-loading and caching the requested icon on first use.
Origin
Custom — fully Primebrick-written. Uses import.meta.glob (Vite) and
@lucide/svelte. Not vendored from any registry.
How it works
- A module-level
import.meta.glob('/node_modules/@lucide/svelte/dist/icons/*.svelte')call produces a map of path → lazy import function that Vite can statically analyze. This is required because Vite cannot resolve dynamicimport()with template literals. - On each name change, an
$effectcallsloadIcon(name)which looks up the loader for/node_modules/@lucide/svelte/dist/icons/${name}.svelte, invokes it, and caches the resolved component in aMap. - While the chunk loads, a placeholder
<span>of the icon'ssizekeeps layout stable. - If the name is not found, a warning is logged and nothing renders.
Usage
Default
Code
With a class
Code
Driven by module config
Code
Props
Full prop table: see API reference — dynamicicon.
Props: name: string, size?: number = 16, class?: string.
Next steps
Last modified on