Skip to content

Instantly share code, notes, and snippets.

@timhettler
Last active September 27, 2019 19:54
Show Gist options
  • Save timhettler/5607c54e6765f319ebaa1f9cb33cd5ba to your computer and use it in GitHub Desktop.
Save timhettler/5607c54e6765f319ebaa1f9cb33cd5ba to your computer and use it in GitHub Desktop.
A component that gives SVGs a consistent API
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