From 3422f0aaf74b57812daf064eaa6c0b162c1942e6 Mon Sep 17 00:00:00 2001 From: Ivan Quirino Date: Tue, 20 Feb 2018 21:30:59 -0300 Subject: [PATCH] feat: Add SVG sprite support (#319) --- README.md | 37 +++++++++++++++++++ .../build-sprite-string.test.js.snap | 14 +++++++ bin/__tests__/build-sprite-string.test.js | 12 ++++++ bin/build-sprite-string.js | 30 +++++++++++++++ bin/build-sprite.js | 14 +++++++ bin/build.sh | 1 + 6 files changed, 108 insertions(+) create mode 100644 bin/__tests__/__snapshots__/build-sprite-string.test.js.snap create mode 100644 bin/__tests__/build-sprite-string.test.js create mode 100644 bin/build-sprite-string.js create mode 100644 bin/build-sprite.js diff --git a/README.md b/README.md index 0d9465e..d4ee428 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ npm install feather-icons * [Usage](#usage) * [Client-side](#client-side) * [Node](#node) + * [SVG Sprite](#svg-sprite) * [API Reference](#api-reference) * [`feather.icons`](#feathericons) * [`feather.icons[name].toSvg()`](#feathericonsnametosvgattrs) @@ -166,6 +167,42 @@ feather.icons.x.toSvg({ class: 'foo bar', 'stroke-width': 1, color: 'red' }) See the [API Reference](#api-reference) for more information about the available properties and methods of the `feather` object. +### SVG Sprite +A SVG Sprite is also provided, which can be used as following: +```html + + + +``` +Where `iconName` is the name of the icon you want to display. + +Same result but using a CSS class: +```css +.feather { + width: 24px; + height: 24px; + stroke: currentColor; + stroke-width: 2; + stroke-linecap: round; + stroke-linejoin: round; + fill: none; +} +``` +```html + + + +``` +Prefer using CSS classes to keep things organized. + ## API Reference ### `feather.icons` diff --git a/bin/__tests__/__snapshots__/build-sprite-string.test.js.snap b/bin/__tests__/__snapshots__/build-sprite-string.test.js.snap new file mode 100644 index 0000000..8371f2e --- /dev/null +++ b/bin/__tests__/__snapshots__/build-sprite-string.test.js.snap @@ -0,0 +1,14 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`builds sprite correctly 1`] = ` +" + + + + + + + + +" +`; diff --git a/bin/__tests__/build-sprite-string.test.js b/bin/__tests__/build-sprite-string.test.js new file mode 100644 index 0000000..ac08fa6 --- /dev/null +++ b/bin/__tests__/build-sprite-string.test.js @@ -0,0 +1,12 @@ +/* eslint-env jest */ +import buildSpriteString from '../build-sprite-string'; + +const icons = { + icon1: + '', + icon2: '', +}; + +test('builds sprite correctly', () => { + expect(buildSpriteString(icons)).toMatchSnapshot(); +}); diff --git a/bin/build-sprite-string.js b/bin/build-sprite-string.js new file mode 100644 index 0000000..fcea913 --- /dev/null +++ b/bin/build-sprite-string.js @@ -0,0 +1,30 @@ +import defaultAttrs from '../src/default-attrs.json'; + +const svgStartTag = `\n\n`; +const svgEndTag = '\n'; + +/** + * Renders the inner sprites as SVG Symbols + * @param {object} icons the icons object + * @returns {string} the rendered string with SVG symbols + */ +function buildSpriteString(icons) { + const symbols = Object.keys(icons) + .map(icon => toSvgSymbol(icon, icons[icon])) + .join(''); + + return svgStartTag + symbols + svgEndTag; +} + +/** + * Renders a SVG symbol tag + * @param {string} name The name of the icon + * @param {string} contents The contents of the icon + * @returns {string} the rendered SVG symbol + */ +function toSvgSymbol(name, contents) { + return ` +${contents}\n\n`; +} + +export default buildSpriteString; diff --git a/bin/build-sprite.js b/bin/build-sprite.js new file mode 100644 index 0000000..b1a3b8e --- /dev/null +++ b/bin/build-sprite.js @@ -0,0 +1,14 @@ +import fs from 'fs'; +import path from 'path'; +import icons from '../dist/icons.json'; +import buildSpriteString from './build-sprite-string'; + +const sprite = buildSpriteString(icons); + +const OUT_FILE = path.resolve(__dirname, '../dist/feather-sprite.svg'); + +console.log(`Building ${OUT_FILE}`); // eslint-disable-line no-console + +fs.writeFile(OUT_FILE, sprite, err => { + if (err) throw err; +}); diff --git a/bin/build.sh b/bin/build.sh index a151ba0..32610cf 100755 --- a/bin/build.sh +++ b/bin/build.sh @@ -5,6 +5,7 @@ ./node_modules/.bin/rimraf dist mkdir dist ./node_modules/.bin/babel-node bin/build-icons-json.js +./node_modules/.bin/babel-node bin/build-sprite.js ./node_modules/.bin/rimraf dist/icons mkdir dist/icons