【发布时间】:2019-02-22 14:04:59
【问题描述】:
我正在创建将呈现各种 SVG 的 React 组件:
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
范围样式还有其他替代方法吗?
【问题讨论】:
-
嗨@Mister。你为什么不去 css-in-js。 ?
-
@MisterEpic 我对你在找什么有点困惑。每个 SVG 都非常独特,那么为什么要在它们之间共享样式呢?
-
css-modules (github.com/css-modules/css-modules) 怎么样?它符合您的要求吗?
-
对于范围样式,请考虑使用 shadow DOM。对于边缘,使用 polyfills
-
@MisterEpic OP 去哪儿了?