Last active
August 18, 2016 15:07
-
-
Save joshdcomp/d5513fa92810c95de31cd21621950512 to your computer and use it in GitHub Desktop.
Generic icon React component for use with SVG glyphs
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
import React, { Component } from 'react'; | |
// _icon is my namespacing choice for glyphs--it could easily be set | |
// as an env variable | |
export default class Icon extends Component { | |
fullName(href='') { | |
return `#${href}_icon-${this.props.glyph}`; | |
} | |
render() { | |
const glyph = this.props.glyph; | |
let wrapperClasses = [ | |
'_icon', | |
'_icon-' + glyph, | |
this.props.className | |
]; | |
return ( | |
<svg className={wrapperClasses.join(' ')}> | |
<use xlinkHref={this.fullName()} /> | |
</svg> | |
) | |
} | |
} | |
Icon.defaultProps = { | |
glyph: '', | |
className: '', | |
}; |
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
var React = require('react'); | |
var Icon = React.createClass({ | |
displayName: 'Icon', | |
getInitialState: function() { | |
return { | |
}; | |
}, | |
getDefaultProps: function() { | |
return { | |
glyph: 'star', | |
className: '', | |
}; | |
}, | |
fullName: function (href) { | |
return ['#', href, '_icon-' + this.props.glyph].join(''); | |
}, | |
render: function() { | |
var glyph = this.props.glyph; | |
var wrapperClasses = [ | |
'_icon', | |
'_icon-' + glyph, | |
this.props.className | |
]; | |
return ( | |
<svg className={wrapperClasses.join(' ')}> | |
<use xlinkHref={this.fullName()} /> | |
</svg> | |
) | |
} | |
}); | |
module.exports = Icon; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment