🚀 DAY 5 OF LAUNCH WEEK: Introducing Socket Firewall Enterprise.Learn more →
Socket
Book a DemoInstallSign in
Socket

gulp-svg-symbol-sprite

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gulp-svg-symbol-sprite

Sprites svgs into a single file

latest
Source
npmnpm
Version
2.0.1
Version published
Maintainers
1
Created
Source

gulp-svg-symbol-sprite

Generate sprites symbols from svgs by cleaning them up with svgo and concatenating them. Outputs JSON (for react inlines) and svg.

Usage:

gulp.task('svg', function () {
    return gulp.src(['fixture/*.svg'])
        .pipe(sprite({
            svg: 'my-sprites.svg',
            json: 'my-sprites.json'
        ), {}) // optional svgo options
        .pipe(gulp.dest('dest'));
});

Sample React Compontent

import React from 'react';
import icons from './icon-sprites.json';
import { find, camelCase } from 'lodash';
import classnames from 'classnames';

export default class SVGIcon extends React.Component {
    renderChildren(child, ix) {
        return React.createElement(
            child.name,
            { ...this.transformAttributes(child.attrs), key: ix },
            child.children ? child.children.map(::this.renderChildren) : []
        );
    }

    transformAttributes(attr) {
        const transformed = {};
        Object.keys(attr).forEach(key => {
            transformed[camelCase(key)] = attr[key];
        });

        return transformed;
    }

    render() {
        const { name } = this.props;
        const icon = find(icons, i => i.attrs.id === name);
        if (!icon) {
            console.warn(`Icon: '${name}' not found`);
            return false;
        }
        const attributes = this.transformAttributes(icon.attrs || {});
        return (
            <i className={classnames(`svg svg-${this.props.name}`, this.props.className)}>
                <svg {...attributes} {...this.props}>
                    {icon.children.map(::this.renderChildren)}
                </svg>
            </i>
        );
    }
}

Keywords

svg

FAQs

Package last updated on 20 Mar 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts