DocSpace-buildtools/packages/asc-web-components/src/components/icons/svg/create-styled-icon.js

120 lines
2.8 KiB
JavaScript
Raw Normal View History

import React from "react";
import PropTypes from "prop-types";
import styled from "styled-components";
import ReactDOMServer from "react-dom/server";
import { Parser } from "html-to-react";
2019-06-05 08:54:09 +00:00
const iconSizes = {
small: 12,
medium: 16,
big: 24,
};
const getSizeStyle = (size) => {
2019-06-05 08:54:09 +00:00
switch (size) {
case "scale":
2019-06-05 08:54:09 +00:00
return `
&:not(:root) {
width: 100%;
height: 100%;
2019-06-05 08:54:09 +00:00
}
`;
case "small":
case "medium":
case "big":
2019-06-05 08:54:09 +00:00
return `
width: ${iconSizes[size]}px;
2019-07-10 09:48:36 +00:00
min-width: ${iconSizes[size]}px;
2019-06-05 08:54:09 +00:00
height: ${iconSizes[size]}px;
2019-07-10 09:48:36 +00:00
min-height: ${iconSizes[size]}px;
2019-06-05 08:54:09 +00:00
`;
default:
return `
width: ${iconSizes.big}px;
2019-07-10 09:48:36 +00:00
min-width: ${iconSizes.big}px;
2019-06-05 08:54:09 +00:00
height: ${iconSizes.big}px;
2019-07-10 09:48:36 +00:00
min-height: ${iconSizes.big}px;
2019-06-05 08:54:09 +00:00
`;
}
};
export default function createStyledIcon(
Component,
displayName,
fillPath = "*",
strokePath = "*"
) {
class Icon extends React.Component {
render_xml(id, xml_string) {
var doc = new DOMParser().parseFromString(xml_string, "application/xml");
var el = document.getElementById(id);
el.appendChild(el.ownerDocument.importNode(doc.documentElement, true));
}
render() {
// eslint-disable-next-line react/prop-types, no-unused-vars
const {
isfill,
isStroke,
color,
stroke,
fillPath,
strokePath,
...props
} = this.props;
let svg = ReactDOMServer.renderToString(
<Component {...props}></Component>
);
const matchResult = svg.match(/\s*mask id="(\w*)"\s/);
const newId =
Math.random().toString(36).substring(2, 5) +
Math.random().toString(36).substring(2, 5);
if (matchResult != null) {
if (matchResult.length > 1) {
svg = svg.replace(new RegExp(matchResult[1], "g"), newId);
const htmlToReactParser = new Parser();
const reactComponent = htmlToReactParser.parse(svg);
return reactComponent;
}
}
return <Component {...props}></Component>;
}
}
const StyledIcon = styled(Icon)(
(props) => `
${props.fillPath} {
${props.isfill ? "fill:" + props.color : ""};
2019-06-05 08:54:09 +00:00
}
${props.strokePath} {
${props.isStroke ? "stroke:" + props.stroke : ""};
}
2019-12-05 12:13:04 +00:00
overflow: hidden;
vertical-align: middle;
2019-06-05 08:54:09 +00:00
${getSizeStyle(props.size)}
`
);
StyledIcon.displayName = displayName;
StyledIcon.propTypes = {
2019-06-05 08:54:09 +00:00
color: PropTypes.string,
stroke: PropTypes.string,
size: PropTypes.oneOf(["small", "medium", "big", "scale"]),
isfill: PropTypes.bool,
isStroke: PropTypes.bool,
fillPath: PropTypes.string,
strokePath: PropTypes.string,
2019-06-05 08:54:09 +00:00
};
StyledIcon.defaultProps = {
fillPath: fillPath,
strokePath: strokePath,
};
return StyledIcon;
}