Skip to content

feat(svg-sprite): Adds build sprite script with tests and docs - #319

Merged
colebemis merged 8 commits into
feathericons:masterfrom
ivanquirino:master
Feb 21, 2018
Merged

feat(svg-sprite): Adds build sprite script with tests and docs#319
colebemis merged 8 commits into
feathericons:masterfrom
ivanquirino:master

Conversation

@ivanquirino

Copy link
Copy Markdown
Contributor

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.

@ivanquirino ivanquirino mentioned this pull request Feb 19, 2018
@colebemis

Copy link
Copy Markdown
Member

This looks great. I'll do a thorough review soon 👍

@colebemis colebemis left a comment

Copy link
Copy Markdown
Member

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.

Comment thread 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

Copy link
Copy Markdown
Member

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.

@ivanquirino ivanquirino Feb 20, 2018

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Member

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.

Comment thread README.md Outdated
}
```
```html
<svg class="feather-default feather feather-[iconName]">

Copy link
Copy Markdown
Member

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.

Comment thread 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

Copy link
Copy Markdown
Member

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

Comment thread 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';

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Member

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.

@ivanquirino

Copy link
Copy Markdown
Contributor Author

Good observations! I'll work on them and send another PR as soon as possible.

Comment thread bin/build-sprite-string.js Outdated

const svgEndTag = ' </defs>\n</svg>';

export default function buildSprite(icons) {

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, sorry!

Comment thread 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';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ivan Quirino added 2 commits February 20, 2018 16:27
fixes the buildSpriteString function name
JSDoc for the buildSpriteString function
Comment thread README.md

See the [API Reference](#api-reference) for more information about the available properties and methods of the `feather` object.

### SVG Sprite

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Contributor Author

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.

Comment thread README.md Outdated
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
fill: none

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

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';

Copy link
Copy Markdown
Member

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
Comment thread 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) {

Copy link
Copy Markdown
Member

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

Comment thread 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, '');

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

Comment thread bin/build-sprite-string.js Outdated
* @returns {string} the rendered SVG symbol
*/
function toSvgSymbol(name, contents) {
return ` <symbol id="${name}" viewBox="${defaultAttrs.viewBox}">

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

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 colebemis left a comment

Copy link
Copy Markdown
Member

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.

@ivanquirino

Copy link
Copy Markdown
Contributor Author

@colebemis 👍 It's all ours now! 😄
Thanks for creating such great icons and a library that makes it easy to manipulate them. I'm thinking on coding a React component that uses icons.json directly. I think is the best way to make a React component for Feather. You may see another PR soon.

@colebemis

colebemis commented Feb 21, 2018

Copy link
Copy Markdown
Member

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 react-feather repo that uses the feather-icons npm package. This will allow us to use the icons directory to build the React components. With this approach, icons be used like this:

import Icon from 'react-feather'

<Icon name="circle" />

or

import IconCircle from 'react-feather/circle'

<IconCircle />

Since there is already a react-feather repo, it probably makes more sense to update that repo to use the approach outlined in the article above. I'd like to transfer that repo into the feathericons organization if @carmelopullara is on board.

@colebemis
colebemis merged commit 3422f0a into feathericons:master Feb 21, 2018
@carmelopullara

Copy link
Copy Markdown
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants