我正在创建React组件,用于渲染各种SVG:
const Close = ({
fill, width, height, float,
}) => (
<svg width={ `${width}px` } height={ `${height}px` } viewBox="0 0 14.48 14.48" style={ { float: `${float}`, cursor: 'pointer' } }>
<title>x</title>
<g id="Layer_2" data-name="Layer 2">
<g id="Background">
<line style={ { fill: 'none', stroke: `${fill}`, strokeMiterlimit: 10 } } x1="14.13" y1="0.35" x2="0.35" y2="14.13" />
<line style={ { fill: 'none', stroke: `${fill}`, strokeMiterlimit: 10 } } x1="14.13" y1="14.13" x2="0.35" y2="0.35" />
</g>
</g>
</svg>
);
能够向该组件提供各种属性以控制尺寸、颜色等非常方便...
然而,我没有一个好的解决方案来DRY(不重复编写)地处理样式。请注意line
元素具有相同的style
值。我目前将它们写成内联样式,因为如果我添加嵌入式样式表,那么我会在页面上渲染其他SVG时遇到类名冲突(我们的SVG软件一遍又一遍地使用相同的类)。
<style scoped>
已从规范中删除:https://github.com/whatwg/html/issues/552
Edge浏览器还不支持Shadow DOM:https://caniuse.com/#feat=shadowdomv1
是否有其他替代方案来限定样式作用范围?