Skip to content

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.


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.

  • Loads lazily. The first time a name is shown, React.lazy imports 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 new React.lazy on every render would remount the icon each time.
  • Shows a placeholder while it loads: an empty <svg> of the same size, with the same className and aria-hidden="true", inside React.Suspense.
  • Passes your props on. It takes every prop of the icon (Components) plus name, and forwards ref.
  • Fills in defaults: size (option defaultSize) and color="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". through console.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.

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".

  • 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 a ReferenceError in the browser.