Last active
November 9, 2016 13:56
-
-
Save christophengelmayer/a46457a8e92e003ff64cfc79e220acb3 to your computer and use it in GitHub Desktop.
SVG Sprites Workflow
This file contains 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
gulp.task("svg", function () { | |
return gulp | |
.src(config.src+"svg/*.svg") | |
.pipe(svgmin()) | |
.pipe(svgstore({ | |
fileName: "sprite.svg", | |
prefix: "icon-" })) | |
.pipe(cheerio({ | |
run: function ($) { | |
$("[fill]").removeAttr("fill"); | |
}, | |
parserOptions: { xmlMode: true } | |
})) | |
.pipe(gulp.dest(config.dest+"img")); | |
}); |
This file contains 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
import SvgSpriteEmbed from'./components/SvgSpriteEmbed'; | |
const svgSpriteEmbed = new SvgSpriteEmbed('site/templates/img/svg.svg'); |
This file contains 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
export default class SvgSpriteEmbed { | |
constructor(url) { | |
var ajax = new XMLHttpRequest(); | |
ajax.open("GET", url, true); | |
ajax.responseType = "document"; | |
ajax.onload = function(e) { | |
var element = ajax.responseXML.documentElement; | |
element.setAttribute("class", "u-hidden-visually"); | |
document.body.insertBefore(element, document.body.childNodes[0]); | |
} | |
ajax.send(); | |
} | |
} |
This file contains 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
.u-visuallyhidden { | |
border: 0; | |
clip: rect(0 0 0 0); | |
height: 1px; | |
margin: -1px; | |
overflow: hidden; | |
padding: 0; | |
position: absolute; | |
width: 1px; | |
white-space: nowrap; | |
} | |
.icon { | |
width: 1em; | |
height: 1em; | |
fill: currentColor; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment