Skip to content

Instantly share code, notes, and snippets.

@joshdcomp
Last active August 18, 2016 15:07
Show Gist options
  • Save joshdcomp/d5513fa92810c95de31cd21621950512 to your computer and use it in GitHub Desktop.
Save joshdcomp/d5513fa92810c95de31cd21621950512 to your computer and use it in GitHub Desktop.
Generic icon React component for use with SVG glyphs
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: '',
};
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