prc b6c02717e6 Upload complete no-internet building environment. há 1 ano atrás
..
build b6c02717e6 Upload complete no-internet building environment. há 1 ano atrás
README.md b6c02717e6 Upload complete no-internet building environment. há 1 ano atrás
demo.html b6c02717e6 Upload complete no-internet building environment. há 1 ano atrás
icon.jsx b6c02717e6 Upload complete no-internet building environment. há 1 ano atrás
main.jsx b6c02717e6 Upload complete no-internet building environment. há 1 ano atrás
svg-to-icon-component-runtime-generator.js b6c02717e6 Upload complete no-internet building environment. há 1 ano atrás
webpack.config.js b6c02717e6 Upload complete no-internet building environment. há 1 ano atrás

README.md

Custom runtime generating example

Runtime code generated by loader could be overridden by providing custom generator via runtimeGenerator option. For example you can return React component preconfigured with imported symbol data instead of default symbol instance.

Demo

Config

Input

This import:

import TwitterIcon from '../assets/twitter.svg';

Will be generated to:

import React, {PureComponent} from 'react';
import SpriteSymbol from 'svg-sprite-loader/runtime/symbol';
import sprite from 'svg-sprite-loader/runtime/browser-sprite';
import Icon from './icon.jsx';

const symbol = new SpriteSymbol({ /* symbol data */ });
sprite.add(symbol);

export default class TwitterIcon extends Icon {}

TwitterIcon.defaultProps.glyph = symbol.id;

So when you import SVG, actually React component returns with configured glyph:

import TwitterIcon from '../assets/twitter.svg';

render(
  <div>
    <TwitterIcon width="100" />
    <TwitterIcon fill="red" style={{width: 300}} />
    <TwitterIcon fill="blue" style={{width: 600}} />
  </div>,
  document.querySelector('.app')
);

Output