-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(svg-sprite): Adds build sprite script with tests and docs #319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
This looks great. I'll do a thorough review soon 👍 |
colebemis
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this file is called build-sprite-string.js, I think it makes sense to call this function buildSpriteString.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, sorry!
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, can you add this to the table of contents?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Minor] Oops, we're missing a semicolon here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!
| @@ -0,0 +1,12 @@ | |||
| /* eslint-env jest */ | |||
| import buildSpriteString from '../build-sprite-string'; | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we just use .join('') here instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.