rss

react-svg-sprite

A basic react component that will create an svg sprite based on an svg array.

Showing:

Popularity

Downloads/wk

74

GitHub Stars

2

Maintenance

Last Commit

5yrs ago

Contributors

1

Package

Dependencies

1

Size (min+gzip)

8.9KB

License

ISC

Type Definitions

Tree-Shakeable

No?

Categories

Readme

npm version

React SVG Sprite Component

A basic react component that will create an svg sprite based on an svg array.

installation

npm install react-svg-sprite --save

How to Use

This component only requires an array of your given symbols with name, and svg string.

Minimum Example

import React from 'react'
import SvgSprite from 'react-svg-sprite'

export default class MyComponent extends React.Component {
    render () {
        return (
            <SvgSprite
                symbols=[{
                    name: 'cloud',
                    svg: '<svg></svg>'
                }, {
                    name: 'rain',
                    svg: '<svg></svg>'
                }]
            />
        );
    }
}

To then use your svg icons anywhere on your page you'll want to use the following markup.

<svg>
    <use xlink:href="#cloud"></use>
</svg>

You can use an npm package like react-svg-use to help speed up development, and decrease maintainability.

Example Output

Given your component looks like this...

import React from 'react'
import SvgSprite from 'react-svg-sprite'

export default class MyComponent extends React.Component {
    render () {
        return (
            <SvgSprite
                className="weather-icons"
                symbols=[{
                    className: 'weather-icon',
                    name: 'cloud',
                    svg: '<svg viewBox="0 0 32 32">...</svg>'
                }, {
                    className: 'weather-icon',
                    name: 'rain',
                    svg: '<svg viewBox="0 0 32 32">...</svg>'
                }]
            />
        );
    }
}

Your output would look something like this...

<svg class="weather-icons" style="display:none;">
    <symbol id="cloud" viewBox="0 0 32 32" class="weather-icon">
        <path>...</path>
    </symbol>
    <symbol id="rain" viewBox="0 0 32 32" class="weather-icon">
        <path>...</path>
    </symbol>
</svg>

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100