Phosphor Icons for React

repository·master·Indexed 23 days ago

https://github.com/phosphor-icons/react

A high-performance, tree-shakable React library providing a clean and friendly icon family for interfaces, diagrams, and presentations. Version 2.1.10 supports multiple weights (thin, light, regular, bold, fill, duotone), custom icon creation via IconBase, and dedicated SSR support for React Server Components through the /dist/ssr submodule. Includes IconContext.Provider for global style configuration.

Tokens
3.7K
Snippets
8
Records
40
Agent score
82%

What's inside @phosphor-icons/react

  1. Configure default icon styles with IconContext

    master

    Use IconContext.Provider to apply default styles (color, size, weight, etc.) to all icons within a specific part of your component tree. Icons will use the nearest IconContext provider above them.

    import { IconContext, HorseIcon, HeartIcon, CubeIcon } from "@phosphor-icons/react";
    
    const App = () => {
      return (
        <IconContext.Provider
          value={{
            color: "limegreen",
            size: 32,
            weight: "bold",
            mirrored: false,
          }}
        >
          <div>
            <HorseIcon /> {/* I'm lime-green, 32px, and bold! */}
            <HeartIcon /> {/* Me too! */}
            <CubeIcon /> {/* Me three :) */}
          </div>
        </IconContext.Provider>
      );
    };
  2. Compose and animate icons with SVG children

    master

    Icon components can accept arbitrary SVG elements as children. These children are placed below the normal icon contents within the icon's viewBox (which is 256x256). This allows for adding background layers, filters, or animations.

    const RotatingCube = () => {
      return (
        <CubeIcon color="darkorchid" weight="duotone">
          <animate
            attributeName="opacity"
            values="0;1;0"
            dur="4s"
            repeatCount="indefinite"
          ></animate>
          <animateTransform
            attributeName="transform"
            attributeType="XML"
            type="rotate"
            dur="5s"
            from="0 0 0"
            to="360 0 0"
            repeatCount="indefinite"
          ></animateTransform>
        </CubeIcon>
      );
    };
  3. Use Phosphor Icons in React Server Components and SSR

    master

    In environments that do not support the React Context API (such as Next.js Server Components or SSR), import icons from the /dist/ssr submodule.

    Note: These SSR variants do not use React Context and cannot inherit styles from an ancestor IconContext.

    import { FishIcon } from "@phosphor-icons/react/ssr";
    
    const MyServerComponent = () => {
      return <FishIcon weight="duotone" />;
    };
  4. Create Custom Icons

    master

    To extend Phosphor with custom icons, design them on a 256x256 grid as SVG. Flatten the assets to use only path elements and strip fill or stroke attributes so they can inherit styles from the wrapper.

    Use IconBase (or SSRBase for Server Components) and a Map<IconWeight, ReactElement> to define the icon's appearance for each weight.

    import { forwardRef, ReactElement } from "react";
    import { Icon, IconBase, IconWeight } from "@phosphor-icons/react";
    
    const weights = new Map<IconWeight, ReactElement>([
      ["thin", <path d="..." />],
      ["light", <path d="..." />],
      ["regular", <path d="..." />],
      ["bold", <path d="..." />],
      ["fill", <path d="..." />],
      [
        "duotone",
        <>
          <path d="..." opacity="0.2" />
          <path d="..." />
        </>,
      ],
    ]);
    
    const CustomIcon: Icon = forwardRef((props, ref) => (
      <IconBase ref={ref} {...props} weights={weights} />
    ));
    
    CustomIcon.displayName = "CustomIcon";
    
    export default CustomIcon;
  5. Optimize Import Performance

    master

    To prevent bundlers from eagerly transpiling all 9,000+ icons during development, you can import icons directly from their specific file paths:

    import { BellSimpleIcon } from "@phosphor-icons/react/dist/csr/BellSimple";

    Next.js Optimization

    If using Next.js 13+, you can use optimizePackageImports in your next.config.js to allow direct imports from the main module without performance penalties:

    module.exports = {
      experimental: {
        optimizePackageImports: ["@phosphor-icons/react"],
      },
    }
  6. Import icons from @phosphor-icons/react

    master

    The @phosphor-icons/react package exports a vast collection of icon components. You can import any icon directly from the main entrypoint. Each icon is a React component that can be used in your application.

    To use an icon, import it using its PascalCase name (e.g., PencilSimpleSlash, User, WifiHigh) from the package.

  7. Basic Usage of Phosphor Icons

    master

    Import specific icons from @phosphor-icons/react and use them as React components. The library supports tree-shaking to ensure only the icons you import are included in your bundle.

    import { HorseIcon, HeartIcon, CubeIcon } from "@phosphor-icons/react";
    
    const App = () => {
      return (
        <main>
          <HorseIcon />
          <HeartIcon color="#AE2983" weight="fill" size={32} />
          <CubeIcon color="teal" weight="duotone" />
        </main>
      );
    };
  8. Icon Props Reference

    master

    Icon components accept all standard SVG props (like style, onClick, aria-label, etc.). The primary styling props are:

    • color? (string): Icon stroke/fill color (hex, rgb, hsl, named colors, or currentColor).
    • size? (number | string): Icon height & width (e.g., 32 or `
  9. Import SSR-compatible icons from @phosphor-icons/react/ssr

    master

    When building applications that use React Server Components (RSC) or Server-Side Rendering (SSR), you should import icons from the src/ssr entrypoint to ensure compatibility with server environments. This entrypoint exports the same icon components as the standard package but is optimized for server-side execution.

    Commonly used icons available in this entrypoint include User, Shield, Wifi, ArrowUp, Check, and many others. The specific list of exported icons is exhaustive and mirrors the main library's icon set.

  10. Use SSR-compatible icon components

    master
    The src/ssr/index.ts entrypoint exports a collection of icon components specifically optimized or prepared for Server-Side Rendering (SSR). When building applications that require icons to be rendered on the server (such as with Next.js or other SSR frameworks), you should import your desired icons from this SSR entrypoint to ensure compatibility and prevent hydration mismatches.