diff --git a/Gruntfile.js b/Gruntfile.js index f3b510b66..1619f8043 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -83,18 +83,23 @@ module.exports = function(grunt) { grunt.registerTask('svg', ['clean', 'svgmin', 'svg_sprite']); // default task, build /dist/ - grunt.registerTask('default', [ 'svg', 'css', 'svg_json']); + grunt.registerTask('default', [ 'svg', 'css', 'json:svg']); - grunt.registerTask('svg_json', 'create a json object with all minimized svg', function() { - var result = {} + grunt.registerTask('json:svg', 'add svg string to data.json build', function() { var files = fs.readdirSync("./build/svg/") + var data = JSON.parse(fs.readFileSync("./lib/data.json")) files.forEach(function(file) { var svg = fs.readFileSync(path.resolve("./build/svg", file)) var key = path.basename(file, ".svg") - result[key] = svg.toString() + if (data[key]) { + var raw = svg.toString() + data[key].path = //g.exec(raw)[0] + data[key].height = /height="(\d+)"/g.exec(raw)[1] + data[key].width = /width="(\d+)"/g.exec(raw)[1] + } }) - fs.writeFileSync("build/svg.json", JSON.stringify(result)); + fs.writeFileSync("build/data.json", JSON.stringify(data)); }) }; diff --git a/README.md b/README.md index 961859f72..3a47d8996 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ ## Install +**NOTE:** The compiled files are located in `/build/`. This directory is located in the published npm package. Which means you can access it when you `npm install octicons`. You can also build this directory by following the [building octicons directions](#building-octicons). The files in the `/lib/` directory are the raw source files and are not compiled or optimized. #### NPM @@ -18,12 +19,122 @@ $ npm install --save octicons ## Usage -The source files included are written in [Sass][sass] (`scss`) You can simply point your sass `include-path` at your `node_modules` directory and import it like this. +For all the usages, we recommend using the CSS located in `./build/octicons.css`. This is some simple CSS to normalize the icons and inherit colors. + +### Node + +After installing `npm install octicons` you can access the icons like this. + +```js +var octicons = require("octicons") +octicons.alert +// { keywords: [ 'warning', 'triangle', 'exclamation', 'point' ], +// path: '', +// height: '16', +// width: '16', +// symbol: 'alert', +// options: +// { version: '1.1', +// width: '16', +// height: '16', +// viewBox: '0 0 16 16', +// class: 'octicon octicon-alert', +// 'aria-hidden': 'true' }, +// toSVG: [Function] } +``` + +There will be a key for every icon, with `keywords` and `svg`. + +#### `octicons.alert.symbol` -```scss -@import "octicons/index.scss"; +Returns the string of the symbol name + +```js +octicons.x.symbol +// "x" ``` +#### `octicons.person.path` + +Path returns the string representation of the path of the icon. + +```js +octicons.x.path +// +``` + +#### `octicons.issue.options` + +This is a json object of all the `options` that will be added to the output tag. + +```js +octicons.x.options +// { version: '1.1', width: '12', height: '16', viewBox: '0 0 12 16', class: 'octicon octicon-x', 'aria-hidden': 'true' } +``` + +#### `octicons.alert.width` + +Width is the icon's true width. Based on the svg view box width. _Note, this doesn't change if you scale it up with size options, it only is the natural width of the icon_ + +#### `octicons.alert.height` + +Height is the icon's true height. Based on the svg view box height. _Note, this doesn't change if you scale it up with size options, it only is the natural height of the icon_ + +#### `keywords` + +Returns an array of keywords for the icon. The data [comes from the octicons repository](https://github.com/primer/octicons/blob/master/lib/data.json). Consider contributing more aliases for the icons. + +```js +octicons.x.keywords +// ["remove", "close", "delete"] +``` + +#### `octicons.alert.toSVG()` + +Returns a string of the svg tag + +```js +octicons.x.toSVG() +// +``` + +The `.toSVG()` method accepts an optional `options` object. This is used to add CSS classnames, a11y options, and sizing. + +##### class + +Add more CSS classes to the `` tag. + +```js +octicons.x.toSVG({ "class": "close" }) +// +``` + +##### aria-label + +Add accessibility `aria-label` to the icon. + +```js +octicons.x.toSVG({ "aria-label": "Close the window" }) +// +``` + +##### width & height + +Size the SVG icon larger using `width` & `height` independently or together. + +```js +octicons.x.toSVG({ "width": "" }) +// +``` + +### Ruby + +If your environment is Ruby on Rails, we have a [octicons_helper](https://github.com/primer/octicons_helper) gem available that renders SVG in your page. The octicons_helper uses the [octicons_gem](https://github.com/primer/octicons_gem) to do the computing and reading of the SVG files. + +### Jekyll + +For jekyll, there's a [jekyll-octicons](https://github.com/primer/jekyll-octicons) plugin available. This works exactly like the octicons_helper. + ## Changing, adding, or deleting icons 1. Open the [Sketch document][sketch-document] in `/lib/`. Each icon exists as an artboard within our master Sketch document. If you’re adding an icon, duplicate one of the artboards and add your shapes to it. Be sure to give your artboard a name that makes sense. diff --git a/index.js b/index.js index 34cf19a1a..0f6e677bc 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,62 @@ -module.exports = { - keywords: require('./lib/keywords'), - svg: require('./build/svg.json') -} +var data = require('./build/data.json') + +Object.keys(data).forEach(function(key) { + + // Returns a string representation of html attributes + var htmlAttributes = function(icon, options) { + var attributes = [] + var attrObj = Object.assign({}, data[key].options, options) + + // If the user passed in options + if (options) { + + // If any of the width or height is passed in + if(options["width"] || options["height"]) { + attrObj["width"] = options["width"] ? options["width"] : (parseInt(options["height"]) * data[key].options["width"] / data[key].options["height"]) + attrObj["height"] = options["height"] ? options["height"] : (parseInt(options["width"]) * data[key].options["height"] / data[key].options["width"]) + } + + // If the user passed in class + if (options["class"]) { + attrObj["class"] = "octicon octicon-" + key + " " + options["class"] + attrObj["class"].trim() + } + + // If the user passed in aria-label + if (options["aria-label"]) { + attrObj["aria-label"] = options["aria-label"] + attrObj["role"] = "img" + + // Un-hide the icon + delete attrObj["aria-hidden"] + } + } + + Object.keys(attrObj).forEach(function(option) { + attributes.push(option + "=\"" + attrObj[option] + "\"") + }) + + return attributes.join(" ").trim() + } + + // Set the symbol for easy access + data[key].symbol = key + + // Set all the default options + data[key].options = { + "version": "1.1", + "width": data[key].width, + "height": data[key].height, + "viewBox": "0 0 " + data[key].width + " " + data[key].height, + "class": "octicon octicon-" + key, + "aria-hidden": "true" + } + + // Function to return an SVG object + data[key].toSVG = function(options) { + return "" + data[key].path + "" + } +}) + +// Import data into exports +module.exports = data diff --git a/lib/keywords.json b/lib/data.json similarity index 100% rename from lib/keywords.json rename to lib/data.json index 50964d0aa..99745e076 100644 --- a/lib/keywords.json +++ b/lib/data.json @@ -331,6 +331,12 @@ "arrow" ] }, + "ellipses": { + "keywords": [ + "dot", + "more" + ] + }, "ellipsis": { "keywords": [ "read", @@ -346,6 +352,11 @@ "see" ] }, + "file": { + "keywords": [ + "file" + ] + }, "file-binary": { "keywords": [ "image", @@ -489,6 +500,13 @@ "world" ] }, + "grabber": { + "keywords": [ + "mover", + "drap", + "drop" + ] + }, "graph": { "keywords": [ "trend", @@ -830,6 +848,12 @@ "broadcast" ] }, + "reply": { + "keywords": [ + "reply all", + "back" + ] + }, "repo": { "keywords": [ "book", @@ -1118,29 +1142,5 @@ "star", "save" ] - }, - "ellipses": { - "keywords": [ - "dot", - "more" - ] - }, - "file": { - "keywords": [ - "file" - ] - }, - "grabber": { - "keywords": [ - "mover", - "drap", - "drop" - ] - }, - "reply": { - "keywords": [ - "reply all", - "back" - ] } } diff --git a/package.json b/package.json index a0a028832..a9e6ddfa6 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,6 @@ "main": "index.js", "files": [ "index.js", - "lib", "build" ], "repository": "https://github.com/primer/octicons.git", diff --git a/test/index.js b/test/index.js index 9c98ff07d..1c592d620 100644 --- a/test/index.js +++ b/test/index.js @@ -4,25 +4,28 @@ import fs from 'fs'; const octiconsLib = fs.readdirSync("../lib/svg/"); -test('octicon keywords are loaded', t => { +test('Octicons are loaded', t => { t.truthy(octicons, "Didn't find any octicons."); - t.not(octicons.keywords.length, 0, "Didn't find any keywords.") + t.not(Object.keys(octicons).length, 0, "Didn't find any octicons.") }); -octiconsLib.forEach( point => { - point = point.replace('.svg', ''); - - ['keywords'].forEach( filename => { - test(filename + '.json contains `' + point + '`', t => { - t.truthy(octicons[filename][point], 'Can\'t find the `' + point + '` octicon in ' + filename + '.json'); - }); - }); +test('Octicons have keywords', t => { + t.truthy(octicons, "Didn't find any octicons."); + Object.keys(octicons).forEach( point => { + t.truthy(octicons[point].keywords, 'The octicon "' + point + '" doesn\'t have any keywords') + t.not(octicons[point].keywords.length, 0, 'The octicon "' + point + '" doesn\'t have any keywords') + }) }); -['keywords'].forEach( filename => { - Object.keys(octicons[filename]).forEach( point => { - test(filename + '.json has the valid octicon `' + point + '`', t => { - t.truthy(octiconsLib.indexOf(point+'.svg') >= 0, filename + '.json contains the deleted octicon `' + point + '`, please remove it.' ); - }); - }); -}); +test('Every octicon is in ./lib/data.json', t => { + octiconsLib.forEach( point => { + point = point.replace('.svg', '') + t.truthy(octicons[point], './lib/data.json doesn\'t include the octicon "' + point + '"') + }) +}) + +test('No deprecated octicons are in ./lib/data.json', t => { + Object.keys(octicons).forEach( point => { + t.truthy(octiconsLib.indexOf(point+'.svg') >= 0, './lib/data.json contains the deleted octicon `' + point + '`, please remove it.' ); + }) +}) diff --git a/test/svg.js b/test/svg.js new file mode 100644 index 000000000..592881858 --- /dev/null +++ b/test/svg.js @@ -0,0 +1,51 @@ +import test from 'ava'; +import octicons from '../'; +import fs from 'fs'; + +const octiconsLib = fs.readdirSync("../lib/svg/"); + +test('Octicons have svg', t => { + t.truthy(octicons, "Didn't find any octicons."); + Object.keys(octicons).forEach( point => { + t.truthy(octicons[point].toSVG(), 'The octicon "' + point + '" doesn\'t have svg') + }) +}); + +test('Octicons have default html attributes', t => { + t.truthy(octicons, "Didn't find any octicons."); + Object.keys(octicons).forEach( point => { + var svg = octicons[point].toSVG() + t.regex(svg, /version="1\.1"/, 'The octicon "' + point + '" doesn\'t have the version attribute') + t.regex(svg, /aria\-hidden="true"/, 'The octicon "' + point + '" doesn\'t have the aria-hidden attribute') + t.regex(svg, new RegExp("width=\"" + octicons[point].width + "\""), 'The octicon "' + point + '" doesn\'t have the width attribute') + t.regex(svg, new RegExp("height=\"" + octicons[point].height + "\""), 'The octicon "' + point + '" doesn\'t have the height attribute') + t.regex(svg, new RegExp("viewBox=\"0 0 " + octicons[point].width + " " + octicons[point].height + "\""), 'The octicon "' + point + '" doesn\'t have the viewBox attribute') + t.regex(svg, new RegExp("class=\"octicon octicon-" + octicons[point].symbol + "\""), 'The octicon "' + point + '" doesn\'t have the class attribute') + }) +}); + +test('Passing in classnames will be included in output', t => { + t.truthy(octicons, "Didn't find any octicons."); + Object.keys(octicons).forEach( point => { + var svg = octicons[point].toSVG({ class: "new-class another-class" }) + t.regex(svg, new RegExp("class=\"octicon octicon-" + octicons[point].symbol + " new-class another-class\""), 'The octicon "' + point + '" doesn\'t have the class attribute') + }) +}); + +test('Passing in aria-label will update the a11y options', t => { + t.truthy(octicons, "Didn't find any octicons."); + Object.keys(octicons).forEach( point => { + var svg = octicons[point].toSVG({ "aria-label": "This is an icon" }) + t.regex(svg, new RegExp("aria\-label=\"This is an icon\""), 'The octicon "' + point + '" doesn\'t have the aria-label attribute') + }) +}); + +test('Passing in width will size properly', t => { + var svg = octicons["x"].toSVG({ "height": 60 }) + t.regex(svg, new RegExp("width=\"45\""), 'The octicon "x" doesn\'t have the width attribute scaled properly') +}); + +test('Passing in height will size properly', t => { + var svg = octicons["x"].toSVG({ "width": 45 }) + t.regex(svg, new RegExp("height=\"60\""), 'The octicon "x" doesn\'t have the height attribute scaled properly') +});