Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kalyantm/297b88d6ee5d9837044ca2c5e3f8a357 to your computer and use it in GitHub Desktop.
Save kalyantm/297b88d6ee5d9837044ca2c5e3f8a357 to your computer and use it in GitHub Desktop.
<Icon fileType="svg" icon="check" alt="check icon" />
class Icon extends React.PureComponent<Props, {}> {
render() {
const { fileType, size, gtmClass, alt, icon } = this.props;
const isSvg = fileType === 'svg';
const file = require(`../../resources/icons/${icon}.${
isSvg ? 'svg' : 'png'
}`);
console.log('file is', file);
const svgElement = isSvg ? (
<div className={cStyles('icon', size, gtmClass)}>
{
<svg viewBox="0 0 60 60">
<use xlinkHref={`#${file.url}`} />
</svg>
}
</div>
) : (
<img
className={cStyles('icon', size, gtmClass)}
alt={alt || ''}
src={file}
/>
);
return svgElement || null;
}
}
export default Icon;
const SpriteLoaderPlugin = require('svg-sprite-loader/plugin');
const plugins = [,
.....,
new SpriteLoaderPlugin(),
];
....
{
test: /\.svg$/,
loader: 'svg-sprite-loader',
options: {
extract: true,
},
},
....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment