Skip to content

Instantly share code, notes, and snippets.

@Muzietto
Created November 15, 2018 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Muzietto/0eb9b2959dba14c9661488b85c656daf to your computer and use it in GitHub Desktop.
Save Muzietto/0eb9b2959dba14c9661488b85c656daf to your computer and use it in GitHub Desktop.
Scalable import of FontAwesome icons in generic icon component, with Storybook story
import React from 'react';
import PropTypes from 'prop-types';
import './Icon.scss';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
const Icon = ({ icon: iconName, color, backgroundColor, ...rest }) => {
const faIconName = `fa${iconName.replace(/./, c => c.toUpperCase())}`;
const faIcon = require(`@fortawesome/pro-light-svg-icons/${faIconName}.js`)[faIconName];
return (
<span {...rest}>
<FontAwesomeIcon icon={faIcon} size="1x" />
</span>
)
};
Icon.propTypes = {
icon: PropTypes.string.isRequired,
color: PropTypes.string,
backgroundColor: PropTypes.string,
}
Icon.defaultProps = {
color: 'black',
backgroundColor: 'white',
}
export default Icon
import React from 'react';
import { storiesOf } from '@storybook/react';
import Icon from './Icon';
storiesOf('Icon', module)
.add('FA paper plane', () => <Icon icon="paperPlane" />)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment