0.3.17 ā€¢ Published 2 months ago

react-svgs v0.3.17

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

react-svgs šŸš€

Transform a directory of SVG files into one easily usable React component.

npm.io npm.io npm.io npm.io

Installation

npm i -D react-svgs
yarn add -D react-svgs

Usage

This tool can be used in two ways; with command line arguments or with a config object (svg) in your package.json file.

Command Line Arguments

react-svgs -i src/assets/svg -o src/components/vector -t

This will take SVG files from directory src/assets/svg and generate TypeScript files containing the component and SVG data. From the directory where the command is executed; the generated files will be:

  • src/components/vector/index.tsx - component
  • src/components/vector/types.ts - SVG data, TypeScript types (if -t flag is provided)

Parameters

ParameterDescriptionTypeRequired
--input, -iPath to directory containing SVG filesstringāœ”
--out, -oOutput path (directory will be created)stringāœ”
--name, -nGenerated React component namestringāŒ
--typescript, -tOutput TypeScript filesbooleanāŒ
--force, -f šŸ”øOverwrite existing component filebooleanāŒ
--nojsxUse .js / .ts file extensionsbooleanāŒ
--proptypesGenerate PropTypes definition for componentbooleanāŒ

šŸ”ø SVG data file is always overwritten to ensure it is up to date.

Usage in package.json

The scripts below can be run using npm run svg, both examples achieve the same result.

// package.json

{
    "scripts": {
        "svg": "react-svgs -i src/assets/svg -o src/components/vector -n Vector -t"
    }
}
// package.json

{
    "scripts": {
        "svg": "react-svgs"
    },
    "svg": {
        "input": "src/assets/svg",
        "output": "src/components/vector",
        "name": "Vector",
        "typescript": true
    }
}

Component Usage

Props

PropTypeRequired
name šŸ”østringāœ”
classNamestringāŒ
styleCSSPropertiesāŒ

šŸ”ø name prop must be one of the strings exported in types.js or types.ts - if using TypeScript this will be enforced.

Basic Example

// src/components/settings-icon/index.jsx

import React from "react"
import SVG from "../svg"

export const SettingsIcon = () => (
    <SVG
        name="settings"
        className="settings-icon"
        style={{fill: "red"}}
    />
);

Complete Example

This example demonstrates using a "wrapper" component around the generated component.

// package.json

{
    "scripts": {
        "icons": "react-svgs"
    },
    "svg": {
        "input": "src/assets",
        "output": "src/components/icon/svg",
        "typescript": true
    }
}

Run the script using one of the following commands:

npm run icons
yarn icons
react-svgs
// project structure

before                            after
------                            -----

src                               src
ā”œā”€ā”€ assets                        ā”œā”€ā”€ assets
ā”‚   ā”œā”€ā”€ alarm-clock.svg           ā”‚   ā”œā”€ā”€ alarm-clock.svg
ā”‚   ā””ā”€ā”€ settings.svg              ā”‚   ā””ā”€ā”€ settings.svg
ā”œā”€ā”€ components                    ā”œā”€ā”€ components
ā”‚   ā””ā”€ā”€ icon                      ā”‚   ā””ā”€ā”€ icon
ā”‚       ā””ā”€ā”€ index.tsx             ā”‚       ā”œā”€ā”€ index.tsx
ā””ā”€ā”€ package.json                  ā”‚       ā””ā”€ā”€ svg
                                  ā”‚           ā”œā”€ā”€ index.tsx
                                  ā”‚           ā””ā”€ā”€ types.ts
                                  ā””ā”€ā”€ package.json
// src/components/icon/index.tsx

import React from "react";
import SVG, {SVGTypes} from "./svg";

interface IconProps {
    icon: SVGTypes,
    size: "small" | "medium" | "large"
}

export const Icon = ({icon, size}: IconProps) => {
    const sizePx = size === "small" ? "10px" : "20px";

    return (
        <SVG
            name={icon}
            style={{
                width: sizePx,
                height: sizePx
            }}
        />
    );
};

Assumptions

  • SVG file names must contain only letters or hyphens, such as:
    • settings.svg -> settings
    • alarm-clock.svg -> alarmClock
0.3.17

2 months ago

0.3.16

2 months ago

0.3.15

2 years ago

0.3.14

2 years ago

0.3.13

2 years ago

0.3.12

2 years ago

0.3.11

2 years ago

0.3.10

2 years ago

0.3.9

2 years ago

0.3.8

2 years ago

0.3.6

2 years ago

0.3.5

2 years ago

0.3.4

2 years ago

0.3.3

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.28

2 years ago

0.2.27

2 years ago

0.2.26

2 years ago

0.2.25

2 years ago

0.2.24

2 years ago

0.2.23

2 years ago

0.2.22

2 years ago

0.2.21

2 years ago

0.2.20

2 years ago

0.2.19

2 years ago

0.2.18

2 years ago

0.2.17

2 years ago

0.2.16

2 years ago

0.2.15

2 years ago

0.2.14

2 years ago

0.2.13

2 years ago

0.2.12

2 years ago

0.2.11

2 years ago

0.2.10

2 years ago

0.2.9

2 years ago

0.2.8

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.3

2 years ago

0.2.0-b2

2 years ago

0.2.0-b1

2 years ago