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

gensvgc

v1.0.7

Published

Generating icon components for React and React Native using svg files

Downloads

32

Readme

📚Generation react / react-native icon component

npm version

Generate function with import svg's and (react/react-native) Icon component using folder. Just specify the path to the icons and the path where to generate the component, and the package will do everything for you!

Installation

Npm:

npm install -D gensvgc

Yarn:

yarn add -D gensvgc

Usage

Run this command:

npx gensvgc icons

The only first required argument is the directory where all svg files are located.

The second argument is the directory where the component will be created.

Example flow

Let's take for example a project on vite.

1. Initialize project:

npx create vite my-vue-app

Select react:

./images/select-react.png

Select typescript:

./images/select-typescript.png

2. Next, since we are using vite we need to install vite-plugin-svgr so that svg can be imported as components:

npm i -D vite-plugin-svgr

And add package to vite.config.js

3. Next, we install the gensvgc package:

npm i -D gensvgc

4. For ease of use, let's add a .gensvgcrc.json configuration file to the root of the project with the following parameters:

{
  "watch": false,
  "typescript": true,
  "dynamic": true
}

5. The last step is to add the command to package.json:

./images/add-command.png

Now when you run the command, a ready component with full typescript will appear:

import React from "react";


import { getSvgComponent, SvgComponentType, SvgIconsType } from "./__generated__/dynamic-adapter.ts";

export type IconsType = SvgIconsType;

export interface IconProps extends React.SVGProps<SVGSVGElement> {
  name: IconsType;
}

export const Icon: React.FC<IconProps> = ({ name, ...props }) => {
  const [IconComponent, setIconComponent] = React.useState<SvgComponentType>(null);

  React.useEffect(() => {
    getSvgComponent(name).then(res => setIconComponent(res));
  }, [name]);

  if (IconComponent) {
    return <IconComponent.default {...props} />;
  }

  return <></>;
};

And a special function with import:

export type SvgIconsType = "react" | "vite";
export type SvgComponentType = typeof import("*.svg?react") | null;

export const getSvgComponent = async (
  name: SvgIconsType,
): Promise<SvgComponentType> => {
  let svgComponent = null;

  switch (name) {
    case "react":
      svgComponent = await import("../../../icons/react.svg?react");
      break;
    case "vite":
      svgComponent = await import("../../../icons/vite.svg?react");
      break;
    default:
      break;
  }

  return svgComponent;
};

That's all there is to it :confetti_ball:

CLI Options

For all possible commands, run npx gensvgc --help.

--watch (-w)

  • Type: boolean
  • Default: false
  • Example: npx gensvgc icons --watch

Keep track of adding / changing / deleting files. Nesting is also supported.


--template (-t)

  • Type: string
  • Default: undefined
  • Example: npx gensvgc icons --template=./src/fragments/react.template

The path to the template to be used when generating the component. In the template, you need to specify the snippets that will be automatically replaced with code pieces, depending on your build environment (Next.js/Vite/Cra/React-Native). The .template extension is used, but in general you can use any file extension.

Typescript template example with dynamic svg import:

{nextUseClient}
import React from "react";
{rnTypesImport}

{svgTypesAndComponentImports}

export type IconsType = {svgIconsType};

export interface IconProps extends {extendType} {
  name: IconsType;
}

export const Icon: React.FC<IconProps> = ({ name, ...props }) => {
  const [IconComponent, setIconComponent] = React.useState<SvgComponentType>(null);

  React.useEffect(() => {
    getSvgComponent(name).then(res => setIconComponent(res));
  }, [name]);

  if (IconComponent) {
    return <IconComponent.default {...props} />;
  }

  return <></>;
};

You can also see the defaults ones that are used in the package: templates/component/*


--dynamic (-d)

  • Type: boolean
  • Default: false
  • Example: npx gensvgc icons --dynamic

Generate component with dynamic load svg files. What is meant by dynamic loading is that the svg icons will only be imported as needed.


--typescript

  • Type: boolean
  • Default: false
  • Example: npx gensvgc icons --typescript

Generating import function and component using typescript.


--after (-a)

  • Type: string
  • Default: undefined
  • Example: npx gensvgc icons --after="npm run lint:fix""

After generation, you can call your custom command, for example for linting files.


--nameFileComp (-n)

  • Type: string
  • Default: Icon
  • Example: npx gensvgc icons --nameFileComp="MyIcon"

After generation, you can call your custom command, for example for linting files.


--skipInitial (-s)

  • Type: boolean
  • Default: false
  • Example: npx gensvgc icons --skipInitial

Skips initial generation (if you use the --watch flag).

Config options

You can use any of these popular config formats:

  • .gensvgcrc.json
  • .gensvgcrc.yaml
  • .gensvgcrc.yml
  • gensvgc.config.js
  • .gensvgcrc.js
  • gensvgc.config.mjs
  • .gensvgcrc.mjs
  • gensvgc.config.ts
  • .gensvgcrc.ts
  • .gensvgcrc.cjs
  • gensvgc.config.cjs

License

MIT Licensed. Copyright (c) Victor Razdorov 2024.