The Icon component
generated/icon.tsx exports Icon, which renders any icon by its registry name, loading it only when it is first shown. generated/registry.ts is the table behind it.
import { Icon } from "./generated";
<Icon name="bold.logout" size={20} className="text-primary" />./generated exports the registry (iconRegistry, IconName) and Icon with its IconProps, nothing else.
The registry
Section titled “The registry”One line per icon, a function that imports it on demand:
export const iconRegistry = { "icons.camera": () => import("./camera").then((m) => m.Camera), "bold.home": () => import("./bold/home").then((m) => m.Home), "bold.logout": () => import("./bold/logout").then((m) => m.Logout), "bulk.eye": () => import("./bulk/eye").then((m) => m.Eye),} as const;
export type IconName = keyof typeof iconRegistry;IconName is the union of the keys, so name="bold.logut" is a type error. How the keys are made is in Naming.
What Icon does
Section titled “What Icon does”- Loads lazily. The first time a name is shown,
React.lazyimports that icon’s module, so a bundler puts each icon in its own chunk. One lazy wrapper is made per name and kept, because a newReact.lazyon every render would remount the icon each time. - Shows a placeholder while it loads: an empty
<svg>of the samesize, with the sameclassNameandaria-hidden="true", insideReact.Suspense. - Passes your props on. It takes every prop of the icon (Components) plus
name, and forwardsref. - Fills in defaults:
size(optiondefaultSize) andcolor="currentColor". Your props replace them. The stroke width is the one the SVG has, as when you import the component. - Warns about an unknown name and renders nothing. Outside production it logs
[Icon] Unknown icon name: "x".throughconsole.warn. TypeScript already rejects such a name, so this only happens with a value that is not typed. - Starts with
"use client", so it can be used from a client component in a server-components framework.
Importing a component directly
Section titled “Importing a component directly”Each variant folder has an index.ts that exports its components and their props types:
/* eslint-disable *//* This file is auto-generated by @mrzr/react-svg-icons. Do not edit manually. */
export { Home } from "./home";export type { HomeProps } from "./home";export { Logout } from "./logout";export type { LogoutProps } from "./logout";The top-level index.ts does not export the variants. A static export * from "./bold" there would make every icon of the variant reachable from the barrel, and the bundler could no longer split them. Import from the variant: import { Logout } from "./generated/bold".
What the bundler must do
Section titled “What the bundler must do”- Resolve imports with no file extension (
"moduleResolution": "bundler"for TypeScript). - Replace
process.env.NODE_ENV. Vite and webpack do. Where nothing does, define it, or the unknown-name warning will throw aReferenceErrorin the browser.