npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@onlyredcats/svg-to-react

v1.0.2

Published

CLI tool for generating React icons from SVG files

Downloads

4

Readme

@onlyredcats/svg-to-react

This is a CLI tool for generating React icons from SVG files. It generates a React component for each SVG file and exports them as a single module with a common interface and types for the icons.

Installation

To install the package run the following command:

npm install @onlyredcats/svg-to-react

Usage

Use the following command to generate React icons from SVG files:

svg-to-react --input="./src/assets/" --output="./src/components/Icon/"

In this command:

  • --input specifies the path to the directory where the SVG files are located.
  • --output specifies the path to the output directory where the generated React icon files will be saved.
  • --clean flag is optional and specifies whether to clean width, height and fill attributes from the SVG files in input directory. By default, it is set to false.

Example

Let's assume you have the following directory structure:

my-project/
├── src/
│   ├── assets/
│   │   ├── icon1.svg
│   │   └── icon2.svg
│   └── components/
│       └── common/
│           └── Icon/
└── package.json

To generate React icons from SVG files in the src/assets directory and save them in src/components/common/Icon, you can run the mentioned command

svg-to-react --input="./src/assets/" --output="./src/components/Icon/" --clean

After running CLI command, the src/components/common/Icon directory will contain generated React icon files based on the SVG files.

my-project/
├── src/
│   ├── assets/
│   │   ├── icon1.svg
│   │   └── icon2.svg
│   ├── components/
│   │   ├── common/
│   │   │   ├── Icon/
│   │   │   │   ├── Icon.tsx
│   │   │   │   ├── icons.ts
│   │   │   │   ├── index.tsx
│   │   │   │   └── types.ts
└── package.json

Generated files

Icon.tsx - contains the React component for the icon.

import {CSSProperties, DOMAttributes, FC, FunctionComponent, memo, SVGProps} from 'react';
import * as iconComponents from './icons';
import { IconType } from './types';

export interface IconProps {
  className?: string;
  name: IconType;
  size?: number;
  width?: number;
  height?: number;
  fill?: string;
  style?: CSSProperties;
  onClick?: DOMAttributes<SVGSVGElement>['onClick'];
}

export const iconTestId = 'icon';

const getIconName = (name: IconType) => `Icon${name}`;

export const Icon: FC<IconProps> = memo(({ className, name, fill = 'currentColor', size, width, height, style, onClick, ...rest }) => {
  const IconComponent =
    (iconComponents[getIconName(name) as keyof typeof iconComponents] as FunctionComponent<SVGProps<SVGSVGElement>>) ||
    null;

  return (
    IconComponent && (
      <IconComponent
        onClick={onClick}
        data-testid={iconTestId}
        fill={fill}
        data-name={name}
        className={className}
        style={{ ...style, width: size ?? width, height: size ?? height }}
        {...rest}
      />
    )
  );
});

icons.ts - contains the imports of the icons.

export { ReactComponent as IconIcon1 } from '../../svg/icon1.svg';
export { ReactComponent as IconIcon2 } from '../../svg/icon2.svg';

index.tsx - contains the exports of the icons.

export * from './Icon';
export * from './types';

types.ts - contains the types of the icons.

export const iconNames = [
  'icon1',
  'icon2',
] as const;

export type IconType = typeof iconNames[number];

Contributing

Contributions are welcome. Please open an issue or submit a pull request.

Author

Oleksandr Ratushnyi

https://sashkoratushnyi.com

License

This package is distributed under the MIT license.