Last active
September 27, 2019 19:54
-
-
Save timhettler/5607c54e6765f319ebaa1f9cb33cd5ba to your computer and use it in GitHub Desktop.
A component that gives SVGs a consistent API
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' | |
const BaseIcon = ({ viewBox, height, width, type, children, ...rest }) { | |
// We want the default dimensions to be 100%, which covers most | |
// use-cases, but only if BOTH the width and height props are not set | |
const trueWidth = width || height ? width : '100%' | |
const trueHeight = width || height ? height : '100%' | |
return ( | |
<svg | |
viewBox={viewBox} | |
width={trueWidth} | |
height={trueHeight} | |
focusable='false' // IE behavior requires this, | |
style={{ | |
display: 'block', | |
userSelect: 'none' | |
[type]: currentColor | |
}} | |
{...rest} | |
> | |
{children} | |
</svg> | |
) | |
} | |
BaseIcon.defaultProps = { | |
height: null, | |
width: null, | |
type: 'fill' | |
} | |
BaseIcon.propTypes = { | |
viewBox: PropTypes.string.isRequired, | |
height: PropTypes.string, | |
width: PropTypes.string, | |
type: PropTypes.oneOf(['fill', 'stroke']) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment