feat(svg-sprite): Adds build sprite script with tests and docs#319
feat(svg-sprite): Adds build sprite script with tests and docs#319colebemis merged 8 commits intofeathericons:masterfrom
Conversation
|
This looks great. I'll do a thorough review soon 👍 |
colebemis
left a comment
There was a problem hiding this comment.
Awesome job! I just have a few comments.
README.md
Outdated
|
|
||
| All elements that have a `data-feather` attribute will be replaced with SVG markup corresponding to their `data-feather` attribute value. See the [API Reference](#api-reference) for more information about `feather.replace()`. | ||
|
|
||
| #### 5. SVG Sprite |
There was a problem hiding this comment.
I feel like this heading should be on the same level as "Client-side" and "Node." The idea was that each subheading of "Usage" is a different way to use Feather. I'm planning to add sections like "React" and "Sketch" soon.
There was a problem hiding this comment.
I'm already using the sprite with React, but I was thinking that using the icons.json file to render a React Component is even better.
There was a problem hiding this comment.
Yeah, totally. I was just suggesting that the SVG Sprite section should be on the same hierarchical level as Client-side and Node.
README.md
Outdated
| } | ||
| ``` | ||
| ```html | ||
| <svg class="feather-default feather feather-[iconName]"> |
There was a problem hiding this comment.
I think we could just use the .feather class to style the SVGs in this example.
bin/build-sprite-function.js
Outdated
| /** | ||
| * Renders a SVG symbol tag | ||
| * @param {*} name The name of the icon | ||
| * @param {*} contents The contents of the icon |
There was a problem hiding this comment.
Could you replace * with the actual type? In this case I think both of them should be string. Also it looks like this is missing an @returns statement
bin/build-sprite.js
Outdated
| import fs from 'fs'; | ||
| import path from 'path'; | ||
| import icons from '../dist/icons.json'; | ||
| import buildSprite from './build-sprite-function'; |
There was a problem hiding this comment.
I'm not in love with calling this file build-sprite-function. I can't think of a better title right now though. I'll have to think about this one.
There was a problem hiding this comment.
Maybe buildSpriteString and build-sprite-string.js since this function returns a string? I think I like that better than build-sprite-function.js.
|
Good observations! I'll work on them and send another PR as soon as possible. |
fixes requested by @colebemis
bin/build-sprite-string.js
Outdated
|
|
||
| const svgEndTag = ' </defs>\n</svg>'; | ||
|
|
||
| export default function buildSprite(icons) { |
There was a problem hiding this comment.
Since this file is called build-sprite-string.js, I think it makes sense to call this function buildSpriteString.
bin/build-sprite.js
Outdated
| import fs from 'fs'; | ||
| import path from 'path'; | ||
| import icons from '../dist/icons.json'; | ||
| import buildSprite from './build-sprite-string'; |
There was a problem hiding this comment.
fixes the buildSpriteString function name
JSDoc for the buildSpriteString function
|
|
||
| See the [API Reference](#api-reference) for more information about the available properties and methods of the `feather` object. | ||
|
|
||
| ### SVG Sprite |
There was a problem hiding this comment.
Awesome, can you add this to the table of contents?
There was a problem hiding this comment.
yes, sure! I forgot that, I'm not used to contribute to such well made projects.
README.md
Outdated
| stroke-width: 2; | ||
| stroke-linecap: round; | ||
| stroke-linejoin: round; | ||
| fill: none |
There was a problem hiding this comment.
[Minor] Oops, we're missing a semicolon here.
| @@ -0,0 +1,12 @@ | |||
| /* eslint-env jest */ | |||
| import buildSpriteString from '../build-sprite-string'; | |||
There was a problem hiding this comment.
Super minor, but this test should probably be called build-sprite-string.test.js because that's actually the function we're testing here.
* fixes add SVG sprite to the table of contents * fixes incorrect CSS example * renames the test correctly
bin/build-sprite-string.js
Outdated
| * @param {object} icons the icons object | ||
| * @returns {string} the rendered string with SVG symbols | ||
| */ | ||
| export default function buildSpriteString(icons) { |
There was a problem hiding this comment.
I generally like to define the main function (i.e. buildSpriteString) at the top of the file and the helper functions (i.e. toSvgSymbol) after the main function. I find that it makes files a lot more readable. Using that style this page would be structured something like this:
imports...
constants...
function buildSpriteString (...) { ... }
function toSvgSymbol(...) { ...}
export default buildSpriteString
Here's a good example of this: https://github.com/feathericons/feather/blob/master/bin/build-icons-object.js
fix snapshot file name
file code styling
bin/build-sprite-string.js
Outdated
| function buildSpriteString(icons) { | ||
| const symbols = Object.keys(icons) | ||
| .map(icon => toSvgSymbol(icon, icons[icon])) | ||
| .reduce((spriteString, symbolString) => spriteString + symbolString, ''); |
There was a problem hiding this comment.
Could we just use .join('') here instead?
bin/build-sprite-string.js
Outdated
| * @returns {string} the rendered SVG symbol | ||
| */ | ||
| function toSvgSymbol(name, contents) { | ||
| return ` <symbol id="${name}" viewBox="${defaultAttrs.viewBox}"> |
There was a problem hiding this comment.
Is there a reason we need to have add indentation to the strings? It might help with performance if we omit unneeded whitespace.
There was a problem hiding this comment.
It was just for reading the svg. But since this is a build artifact, you are correct. I'm out now but I'm gonna fix this too.
colebemis
left a comment
There was a problem hiding this comment.
Awesome job! Thanks for being so responsive to my comments. Just give me the 👍 and I'll release this.
|
@colebemis 👍 It's all ours now! 😄 |
|
Awesome, if you end up working on a React PR, here are my thoughts: I'd do something like this: Making SVG icon libraries for React apps I'd like to have a separate import Icon from 'react-feather'
<Icon name="circle" />or import IconCircle from 'react-feather/circle'
<IconCircle />Since there is already a |
|
I agree with you, @colebemis. Since react-feather has become pretty popular on npm, I think it makes sense to update the repo and transfer it into the organization. I can start working on it this weekend. |
Adds a build script for svg sprite. SVG attributes must be passed on the inline svg tag or through CSS class, as described in the docs for the sprite feature.