Phosphor React Native

repository·main·Indexed 42 days ago

https://github.com/duongdev/phosphor-react-native

A React Native icon library providing 1512 icons in 6 different weights: thin, light, regular, bold, fill, and duotone. Version 3.0.6 is a port of the Phosphor icon family designed for mobile interfaces, requiring react-native-svg for installation.

Tokens
2K
Snippets
10
Records
11
Agent score
5%

What's inside Phosphor React Native

  1. Install phosphor-react-native

    main

    Install phosphor-react-native along with react-native-svg using your preferred package manager. If you are using Expo, use the npx expo install command to ensure compatible versions.

    # Using yarn
    yarn add phosphor-react-native react-native-svg
    
    # Using npm
    npm install --save phosphor-react-native react-native-svg
    
    # Using Expo
    npx expo install phosphor-react-native react-native-svg
  2. Migrate from v2 to v3

    main
    In version 3, every icon is exported with an Icon suffix (e.g., Horse becomes HorseIcon). While old names still work, they are deprecated. Note that Circle, Path, and Infinity were removed as bare names to avoid collisions and must now use their Icon suffixed versions.
  3. Optimize bundle size with direct imports

    main

    Because Metro does not tree-shake by default, importing icons from the main barrel import (import { ... } from 'phosphor-react-native') may include all 1512 icons in your bundle. To avoid this and keep your bundle small, import icons directly from their specific file path.

    import { StarIcon } from 'phosphor-react-native/src/icons/Star';
  4. Configure TypeScript for 'className' errors

    main

    If you encounter TypeScript errors regarding the className property on SvgProps or IconProps, add the following module augmentation to your global.d.ts file to widen the interfaces.

    import 'react-native-svg';
    import 'phosphor-react-native';
    
    declare module 'react-native-svg' {
      interface SvgProps {
        className?: string;
      }
    }
    declare module 'phosphor-react-native' {
      interface IconProps {
        className?: string;
      }
    }
  5. Basic usage of Phosphor icons

    main

    Import specific icons from phosphor-react-native and use them in your React Native components. You can customize the appearance using props like color, weight, and size.

    import React from 'react';
    import { View } from 'react-native';
    import { HorseIcon, HeartIcon, CubeIcon } from 'phosphor-react-native';
    
    const App = () => {
      return (
        <View>
          <HorseIcon />
          <HeartIcon color="#AE2983" weight="fill" size={32} />
          <CubeIcon color="teal" weight="duotone" />
        </View>
      );
    };
  6. Import all icons as a namespace

    main

    You can import all icons at once using the * as Icon syntax. Warning: Without tree-shaking enabled, this will pull every icon into your bundle. Use this pattern only if you have tree-shaking configured (e.g., via Expo).

    import * as Icon from "phosphor-react-native";
    
    <Icon.SmileyIcon />
    <Icon.FolderIcon weight="thin" />
    <Icon.BatteryHalfIcon size="24px" />
    <Icon.AirplaneTakeoffIcon size="24px" mirrored={true} />
  7. Set default icon styles using IconContext

    main

    Use IconContext.Provider to apply a default style to all icons within a specific part of your component tree. Individual icon props will override the context values.

    import React from 'react';
    import { View } from 'react-native';
    import { IconContext, HorseIcon, HeartIcon, CubeIcon } from 'phosphor-react-native';
    
    const App = () => {
      return (
        <IconContext.Provider
          value={{
            color: 'limegreen',
            size: 32,
            weight: 'bold',
          }}
        >
          <View>
            <HorseIcon /> {/* lime-green, 32px, bold */}
            <HeartIcon /> {/* lime-green, 32px, bold */}
            <CubeIcon /> {/* lime-green, 32px, bold */}
          </View>
        </IconContext.Provider>
      );
    };
  8. Reference IconProps

    main

    Icons are styled using the following props. Note that IconProps is a closed type; props outside this list are forwarded to the underlying Svg at runtime but will be rejected by TypeScript unless you use module augmentation.

    // Available IconProps:
    // color?: string (Default: '#000')
    // size?: number | string (Default: 24)
    // weight?: 'thin' | 'light' | 'regular' | 'bold' | 'fill' | 'duotone' (Default: 'regular')
    // mirrored?: boolean (Default: false)
    // style?: StyleProp<ViewStyle | Omit<TextStyle, 'cursor'>>
    // title?: string
    // titleId?: string
    // testID?: string
    // duotoneColor?: string (Defaults to 'color')
    // duotoneOpacity?: number (Default: 0.2)
  9. Define IconProps for Phosphor Icons

    main

    The IconProps interface defines the configuration options available for every icon component in the library. You can use these props to control the visual appearance, size, and accessibility of the icon.

    export interface IconProps {
      color?: string;
      duotoneColor?: string;
      duotoneOpacity?: number;
      mirrored?: boolean;
      size?: string | number;
      style?: StyleProp<ViewStyle | Omit<TextStyle, 'cursor'>>;
      testID?: string;
      title?: string; // SVGRProps
      titleId?: string; // SVGRProps
      weight?: IconWeight;
    }
  10. Configure global icon defaults with IconContext

    main

    The IconContext allows you to provide default props (like color, size, and weight) to all icons within a specific component tree. This is useful for maintaining a consistent icon style throughout your application without passing props to every individual icon.

    import { IconContext } from 'phosphor-react-native';
    
    // The default context values are:
    // color: '#000'
    // size: 24
    // weight: 'regular'
    // mirrored: false
    // duotoneOpacity: 0.2