@ch-ui/vite-plugin-icons
A Vite plugin for spriting icons used in your project in a Tailwind-like way.
Install & configure
pnpm add -D @ch-ui/vite-plugin-icons
import IconsPlugin from '@ch-ui/vite-plugin-icons';
plugins: [
IconsPlugin({
symbolPattern:
'ph-icon--([a-z]+[a-z-]*)--(bold|duotone|fill|light|regular|thin)',
assetPath: (name, variant) =>
`./packages/icons/node_modules/@phosphor-icons/core/assets/${variant}/${name}${
variant === 'regular' ? '' : `-${variant}`
}.svg`,
spritePath: resolve(__dirname, '../dist/assets/sprite.svg'),
contentPaths: ['**/*.stories.tsx'],
}),
]
Usage example
The following example is provided for a React component, but this plugin will work for any text content you might happen to use.
import React, { ComponentPropsWithRef, forwardRef } from 'react';
const SPRITE = './assets/sprite.svg';
type IconName = string;
type IconVariant = 'bold' | 'duotone' | 'fill' | 'light' | 'regular' | 'thin';
type IconProps = ComponentPropsWithRef<'svg'> & {
symbol: `ph-icon--${IconName}--${IconVariant}`;
};
const Icon = forwardRef<SVGSVGElement, IconProps>(
({ symbol, ...props }, forwardedRef) => {
return (
<svg viewBox="0 0 256 256" {...props} ref={forwardedRef}>
<use href={`${SPRITE}#${symbol}`} />
</svg>
);
},
);
export const ExampleIcons = () => {
return (
<>
<style>{`
svg{ inline-size: 4rem; block-size: 4rem; display: inline-block; margin:.25rem; color: blue }
`}</style>
<Icon symbol="ph-icon--address-book--regular" />
<Icon symbol="ph-icon--planet--thin" />
<Icon symbol="ph-icon--anchor--bold" />
<Icon symbol="ph-icon--cards-three--light" />
<Icon symbol="ph-icon--map-pin-simple-area--duotone" />
</>
);
};