Created
November 15, 2018 16:21
-
-
Save Muzietto/0eb9b2959dba14c9661488b85c656daf to your computer and use it in GitHub Desktop.
Scalable import of FontAwesome icons in generic icon component, with Storybook story
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 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 |
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 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