-
Notifications
You must be signed in to change notification settings - Fork 829
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
Creating an official octicons-react component #196
Closed
Closed
Changes from 14 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
542d2f6
First commit on a react-octicons component
jonrohan 2cb57f7
Renaming for npmjs
jonrohan b5a9afd
Updating sheild location
jonrohan 0cccc4f
remove clasee
jonrohan a0d68d5
building babel dist
jonrohan d7728f2
Outputting to build instead
jonrohan 36e3e4b
Setting up linting
jonrohan 581a4f2
github-lint?
jonrohan 60d21a7
1.1.0
jonrohan 9d94633
1.1.0
jonrohan 8f120ac
Be explicit so that it's not mixed up with index.scss
jonrohan 20170b6
Making the sizing stricter
jonrohan 11933d8
Large
jonrohan b41121c
Adding alignment options
jonrohan 0071790
Merge branch 'master' into react_octicons
jonrohan ba46dc4
Removing lint from test
jonrohan c72f20b
Renaming
jonrohan a8a7a07
Updating react package to build components
jonrohan 571e219
Revert package.json
jonrohan 1b30538
Cleaning lint
jonrohan d2e5c24
Don't need this in files
jonrohan f962884
Importing separately for all vs specific octicons
jonrohan f0b872f
Adding named.js to the package files
jonrohan 130110c
Merge branch 'master' into react_octicons
jonrohan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
build/ | ||
*.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": ["plugin:github/es6", "plugin:github/react"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 GitHub Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# GitHub Octicons React Component | ||
|
||
[![npm version](https://img.shields.io/npm/v/%40github%2Focticons-react.svg)](https://www.npmjs.org/package/%40github%2Focticons-react) | ||
[![Build Status](https://travis-ci.org/primer/octicons.svg?branch=master)](https://travis-ci.org/primer/octicons) | ||
|
||
> A react component for installing octicons | ||
|
||
## Install | ||
|
||
``` | ||
$ npm install @github/octicons-react --save | ||
``` | ||
|
||
## Usage | ||
|
||
The entire library will be available when importing `@github/octicons-react`. Specifying the [icon you want to use][octicons], by supplying the `name=""` to the component. | ||
|
||
```js | ||
// Example usage | ||
import Octicon from "@github/octicons-react" | ||
|
||
return ( | ||
<Octicon name="beaker" /> | ||
) | ||
``` | ||
|
||
### Alignment | ||
|
||
By default the octicons have `vertical-align: text-bottom;` applied to them. But there are cases where you'll want to change the alignment. The props available are `top`, `middle`. | ||
|
||
```js | ||
// Example usage | ||
import Octicon from "@github/octicons-react" | ||
|
||
return ( | ||
<h1> | ||
<Octicon name="repo" large middle /> github/github | ||
</h1> | ||
) | ||
``` | ||
|
||
|
||
### ariaLabel | ||
|
||
You have the option to add accessibility information to the icon using `aria-label`. | ||
|
||
```js | ||
// Example usage | ||
import Octicon from "@github/octicons-react" | ||
|
||
return ( | ||
<button> | ||
<Octicon name="plus" ariaLabel="Add new item" /> New | ||
</button> | ||
) | ||
``` | ||
|
||
|
||
### Sizes | ||
|
||
The properties `large`, `medium`, `small` are available for setting the size of the icon. | ||
|
||
| Prop | Rendered Size | | ||
| :- | :- | | ||
| Small | 16px height by `computed` width | | ||
| Medium | 32px height by `computed` width | | ||
| Large | 64px height by `computed` width | | ||
|
||
```js | ||
// Example usage | ||
import Octicon from "@github/octicons-react" | ||
|
||
return ( | ||
<h1> | ||
<a href="https://github.com"> | ||
<Octicon name="logo-github" large ariaLabel="GitHub"/> | ||
</a> | ||
</h1> | ||
) | ||
``` | ||
|
||
## License | ||
|
||
(c) GitHub, Inc. | ||
|
||
When using the GitHub logos, be sure to follow the [GitHub logo guidelines](https://github.com/logos). | ||
|
||
[MIT](./LICENSE) | ||
|
||
[octicons]: https://octicons.github.com/ | ||
[primer]: https://github.com/primer/primer | ||
[docs]: http://primercss.io/ | ||
[npm]: https://www.npmjs.com/ | ||
[install-npm]: https://docs.npmjs.com/getting-started/installing-node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React from "react"; | ||
import octicons from "octicons"; | ||
|
||
export default class Octicon extends React.Component { | ||
prepareAttributes(octicon) { | ||
const { ariaLabel, medium, large } = this.props; | ||
const attr = Object.assign({}, octicon.options); | ||
// Default octicon sizes to small | ||
attr["height"] = 16; | ||
|
||
// Delete class set by octicons library | ||
delete attr["class"]; | ||
|
||
// Read in small, medium, large props | ||
if (medium) { | ||
attr["height"] = 32; | ||
} else if (large) { | ||
attr["height"] = 64; | ||
} | ||
|
||
// Calculate the width based on height | ||
attr["width"] = | ||
attr["height"] * octicon.options["width"] / octicon.options["height"]; | ||
|
||
// If the user passed in aria-label | ||
if (ariaLabel) { | ||
attr["aria-label"] = ariaLabel; | ||
attr["role"] = "img"; | ||
|
||
// Un-hide the icon | ||
delete attr["aria-hidden"]; | ||
} | ||
|
||
return attr; | ||
} | ||
|
||
render() { | ||
const { name } = this.props; | ||
const octicon = octicons[name]; | ||
|
||
const alignment = props => { | ||
const { top, middle } = props; | ||
|
||
if (top) { | ||
return "text-top"; | ||
} else if (middle) { | ||
return "middle"; | ||
} | ||
return "text-bottom"; | ||
}; | ||
|
||
if (octicon) { | ||
const svgStyle = { | ||
display: "inline-block", | ||
fill: "currentColor", | ||
verticalAlign: alignment(this.props), | ||
userSelect: "none" | ||
}; | ||
|
||
return ( | ||
<svg | ||
{...this.prepareAttributes(octicon)} | ||
style={svgStyle} | ||
/* eslint-disable-next-line react/no-danger */ | ||
dangerouslySetInnerHTML={{ __html: octicon.path }} | ||
/> | ||
); | ||
} else { | ||
throw new Error(`No such octicon: "${name}"!`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{ | ||
"name": "@github/octicons-react", | ||
"version": "1.1.0", | ||
"description": "A scalable set of icons handcrafted with <3 by GitHub.", | ||
"homepage": "https://octicons.github.com", | ||
"author": "GitHub Inc.", | ||
"license": "MIT", | ||
"main": "build/index.js", | ||
"repository": "https://github.com/primer/octicons.git", | ||
"files": [ | ||
"index.js", | ||
"build" | ||
], | ||
"scripts": { | ||
"lint": "$(npm bin)/github-lint", | ||
"build": "$(npm bin)/babel index.js -d build", | ||
"prepare": "npm run build" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/primer/octicons/issues" | ||
}, | ||
"keywords": [ | ||
"GitHub", | ||
"icons", | ||
"svg", | ||
"octicons", | ||
"react", | ||
"primer" | ||
], | ||
"babel": { | ||
"presets": [ | ||
"env", | ||
"react" | ||
] | ||
}, | ||
"peerDependencies": { | ||
"react": ">=16.2.0" | ||
}, | ||
"dependencies": { | ||
"octicons": "7.1.0", | ||
"react": "^16.2.0" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-preset-react": "^6.24.1", | ||
"eslint": "^4.18.1", | ||
"eslint-plugin-github": "^0.23.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'd be ideal to avoid
dangerouslySetInnerHTML
for performance reasons. When this component is marked as dirty, react will have to update the DOM usinginnerHTML
rather than changing just the attribute that has changed.https://medium.com/@paularmstrong/twitter-lite-and-high-performance-react-progressive-web-apps-at-scale-d28a00e780a3#3732
Since all the SVGs are static, we should be able to head of time generate the
React.createElement
tree builder calls.