├── .travis.yml ├── .gitignore ├── package.json ├── LICENSE ├── index.js └── README.md /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "4" 4 | - "5" 5 | - "6" 6 | install: 7 | - npm install 8 | script: 9 | - npm test 10 | sudo: false 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | 39 | *.svg 40 | dist 41 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "create-react-icon", 3 | "version": "0.2.0", 4 | "description": "convert one or multiple svg files into React component", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "standard" 8 | }, 9 | "bin": { 10 | "create-react-icon": "index.js" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/envato/create-react-icon.git" 15 | }, 16 | "keywords": [ 17 | "svg", 18 | "react", 19 | "icon" 20 | ], 21 | "author": "Envato Foundation", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/envato/create-react-icon/issues" 25 | }, 26 | "homepage": "https://github.com/envato/create-react-icon#readme", 27 | "dependencies": { 28 | "camelcase": "^5.0.0", 29 | "cheerio": "^0.22.0", 30 | "lodash.foreach": "^4.5.0", 31 | "minimist": "^1.2.0", 32 | "mkdirp": "^0.5.1", 33 | "titleize": "^1.0.0" 34 | }, 35 | "devDependencies": { 36 | "standard": "^12.0.1" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Envato 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const fs = require('fs') 4 | const cheerio = require('cheerio') 5 | const camelcase = require('camelcase') 6 | const path = require('path') 7 | const mkdirp = require('mkdirp') 8 | const forEach = require('lodash.foreach') 9 | const titleize = require('titleize') 10 | const minimist = require('minimist') 11 | 12 | const argv = minimist(process.argv.slice(2)) 13 | const src = argv._.slice(0, argv._.length - 1) 14 | const distDir = argv._.pop() 15 | 16 | if (!src || !distDir) { 17 | throw new Error('Please check you argument.') 18 | } 19 | 20 | const cleanAtrributes = ($el, $) => { 21 | const attrs = ['xlink:href', 'clip-path', 'fill-opacity', 'fill'] 22 | 23 | forEach(attrs, (attr) => { 24 | $el.removeAttr(attr) 25 | }) 26 | if ($el.children().length === 0) { 27 | return false 28 | } 29 | 30 | $el.children().each((index, el) => { 31 | cleanAtrributes($(el), $) 32 | }) 33 | } 34 | 35 | const createComponentString = (viewBox, iconSvg, name) => { 36 | return `import React from 'react' 37 | import Icon from 'react-icon-base' 38 | 39 | export default (props) => { 40 | return ( 41 | 42 | {props.title || '${titleize(name)}'} 43 | ${iconSvg} 44 | 45 | ) 46 | } 47 | ` 48 | } 49 | 50 | mkdirp(distDir, (err) => { 51 | if (err) throw new Error('Error creating icons directory.') 52 | 53 | src.forEach((iconPath) => { 54 | const id = path.basename(iconPath, '.svg') 55 | const svg = fs.readFileSync(iconPath, 'utf-8') 56 | let $ = cheerio.load(svg, { 57 | xmlMode: true 58 | }) 59 | const $svg = $('svg') 60 | cleanAtrributes($svg, $) 61 | const iconSvg = $svg.html() 62 | const viewBox = $svg.attr('viewBox') 63 | const componentString = createComponentString(viewBox, iconSvg, id) 64 | 65 | fs.writeFileSync(path.join(distDir, camelcase(id) + '.js'), componentString, 'utf-8') 66 | }) 67 | }) 68 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # create-react-icon 2 | [![NPM version][npm-image]][npm-url] 3 | [![build status][travis-image]][travis-url] 4 | [![Downloads][downloads-image]][downloads-url] 5 | [![js-standard-style][standard-image]][standard-url] 6 | 7 | Convert one or multiple svg files into inline-svg React component, inspired by [react-icons](https://github.com/gorangajic/react-icons) project. 8 | 9 | If you are wondering what the benefit is to use inline svg in React component, please check this post [Creating an SVG Icon System with React](https://css-tricks.com/creating-svg-icon-system-react/). 10 | 11 | ## Installation 12 | 13 | ```sh 14 | $ npm install create-react-icon -g 15 | ``` 16 | 17 | ## Usage 18 | 19 | ```sh 20 | $ create-react-icon ./alert.svg dist 21 | $ create-react-icon ./*.svg dist 22 | ``` 23 | 24 | ## Result 25 | 26 | Your source svg file: 27 | 28 | ```xml 29 | 30 | 31 | ``` 32 | 33 | Becomes to : 34 | 35 | ```js 36 | import React from 'react' 37 | import Icon from 'react-icon-base' 38 | 39 | export default (props) => { 40 | return ( 41 | 42 | {props.title || 'alert'} 43 | 44 | 45 | ) 46 | } 47 | ``` 48 | 49 | ## How to use icons 50 | 51 | ```js 52 | import AlertIcon from 'alertIcon' 53 | 54 | export default (props) => { 55 | return (
56 | Manage my Collections 57 |
) 58 | } 59 | ``` 60 | 61 | ## Roadmap 62 | 63 | * Make the base component configurable 64 | 65 | ## License 66 | 67 | MIT 68 | 69 | [npm-image]: https://img.shields.io/npm/v/create-react-icon.svg?style=flat-square 70 | [npm-url]: https://npmjs.org/package/create-react-icon 71 | [travis-image]: https://img.shields.io/travis/envato/create-react-icon/master.svg?style=flat-square 72 | [travis-url]: https://travis-ci.org/envato/create-react-icon 73 | [downloads-image]: http://img.shields.io/npm/dm/create-react-icon.svg?style=flat-square 74 | [downloads-url]: https://npmjs.org/package/create-react-icon 75 | [standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square 76 | [standard-url]: https://github.com/feross/standard 77 | --------------------------------------------------------------------------------