Last active
December 3, 2018 08:50
-
-
Save MoOx/1eb30eac43b2114de73a to your computer and use it in GitHub Desktop.
Svg icons with React.js with webpack loader (svg: raw-loader)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* small helper for outside react view */ | |
| module.exports = function(id, modifier) { | |
| return "<svg viewBox=\"0 0 512 512\" class=\"IconSvg IconSvg-" + id + " IconSvg--" + modifier + "\"><use xlink:href=\"#IconSvg-" + id + "\"></use></svg>" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .IconSvg { | |
| display: inline-block; | |
| width: 1em; | |
| height: 1em; | |
| line-height: 1; | |
| vertical-align: middle; | |
| -webkit-font-smoothing: antialiased; | |
| -moz-osx-font-smoothing: grayscale; | |
| } | |
| .IconSvg--half { | |
| width: .5em; | |
| height: .5em; | |
| } | |
| .IconSvg--white { fill: #fff } | |
| .IconSvg--black { fill: #000 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * <IconSvg /> | |
| * | |
| * Usage | |
| * <IconSvg id="gear" /> | |
| * <IconSvg id="gear" modifier="white"/> | |
| * <IconSvg id="gear" modifier="half" color="#f00" /> | |
| * | |
| * @es6 | |
| * @jsx React.DOM | |
| */ | |
| module React from "react" | |
| class _IconSvg{ | |
| render() { | |
| var iconClass = "IconSvg IconSvg-" + this.props.id | |
| var style = {} | |
| if (this.props.modifier) { | |
| iconClass += " IconSvg--" + this.props.modifier | |
| } | |
| if (this.props.color) { | |
| style.fill = this.props.color | |
| } | |
| return <svg viewBox="0 0 512 512" className={iconClass} style={style} dangerouslySetInnerHTML={{__html: "<use xlink:href=\"#IconSvg-" + this.props.id + "\"></use>"}}></svg> | |
| } | |
| } | |
| export const IconSvg= React.createClass(_IconSvg.prototype); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var gulp = require("gulp") | |
| var opts = require("./options") | |
| var util = require("gulp-util") | |
| var plumber = require("gulp-plumber") | |
| var svgSprites = require("gulp-svg-sprites").svg | |
| module.exports = function() { | |
| return gulp.src("src/**/*.svg") | |
| .pipe(svgSprites({ | |
| svgId: "IconSvg-%f", | |
| defs: true, | |
| svg: {defs: "icons.svg"}, | |
| generatePreview: false, | |
| generateCSS: false | |
| })) | |
| .pipe(gulp.dest("./dist/")) | |
| } |
@MoOx do you have a solution for using the same icon multiple times in the same page without having the same markup multiple times, causing more work for the browser?
Ooh found a performance comparison (font files win) and apparently there's a symbol mode in svg.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We use the same convention for modifiers in React. :-)