0.5.3 • Published 10 months ago

@ch-ui/vite-plugin-icons v0.5.3

Weekly downloads
-
License
Ch Public License
Repository
-
Last release
10 months ago

@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({
      // Define the regular expression for the symbols you intend to use for your project; the expression must capture any strings you need to determine the path to the SVG asset.
      symbolPattern:
        'ph-icon--([a-z]+[a-z-]*)--(bold|duotone|fill|light|regular|thin)',
      // The matches in your regular expression are spread onto `assetPath` which must return the path to the SVG file specified by the symbol.
      assetPath: (name, variant) =>
        `./packages/icons/node_modules/@phosphor-icons/core/assets/${variant}/${name}${
          variant === 'regular' ? '' : `-${variant}`
        }.svg`,
      // Tell the plugin where to put the sprite.
      spritePath: resolve(__dirname, '../dist/assets/sprite.svg'),
      // Tell the plugin in which files to look for icon symbols.
      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" />
    </>
  );
};
0.5.3

10 months ago

0.5.2

10 months ago

0.5.1

12 months ago

0.5.0

1 year ago