import React, { Fragment } from 'react' import PropTypes from 'prop-types' import styled, { css } from 'styled-components' const createHelpers = (width, height, css) => { // somehow sizes is ending up in markup, even if it is not a valid svg attribute // until we have a better solution, just render it empty, instead to '[Object object]' const sanitizeSizes = sizes => Object.defineProperty(sizes, 'toString', { value: () => '', enumerable: false }) const getDimensions = (size, sizes) => { if ( size && typeof size.width === 'number' && typeof size.height === 'number' ) { return size } return size && sizes[size] ? sizes[size] : { width, height } } const getCss = (size, sizes, fillColor, fillColorRule, noStyles) => { if (noStyles) { return '' } const dimensions = getDimensions(size, sizes) const fillRule = fillColor && fillColorRule ? `${fillColorRule}{ fill: ${fillColor}; }` : '' return css` width: ${dimensions.width}px; height: ${dimensions.height}px; ${fillRule} ` } const propsToCss = ({ size, sizes, fillColor, fillColorRule, noStyles }) => getCss(size, sizes, fillColor, fillColorRule, noStyles) return { getCss, getDimensions, propsToCss, sanitizeSizes } } const width = '18' const height = '18' const viewBox = '0 0 18 18' const { getDimensions, getCss, propsToCss, sanitizeSizes } = createHelpers( width, height, css ) const sizes = sanitizeSizes({ small: { width: 18, height: 18 }, medium: { width: 24, height: 24 }, large: { width: 36, height: 36 } }) const Image = styled.svg` ${propsToCss} ` const children = ( ) const defaultProps = { children, viewBox, fillColor: null, fillColorRule: '&&& path, &&& use, &&& g', sizes, size: null } Image.propTypes /* remove-proptypes */ = { fillColor: PropTypes.string, fillColorRule: PropTypes.string, viewBox: PropTypes.string.isRequired, children: PropTypes.node.isRequired, size: PropTypes.oneOfType([ PropTypes.string, PropTypes.shape({ height: PropTypes.number.isRequired, width: PropTypes.number.isRequired }) ]), sizes: PropTypes.shape({ height: PropTypes.number, width: PropTypes.number }) } export default Object.assign(Image, { getDimensions, getCss, defaultProps, displayName: 'Warning' })