我正在为一个新项目使用样式组件,并希望使用库的主题功能。我正在努力弄清楚是否存在向一个对象添加多个样式属性的良好或最佳实践方法(即,创建一种继承)。想要的是:
// in the theme, define a group of styles for a given component
// e.g. all fonts have a font-family, weight, size, etc
const theme = {
fonts: {
font-family: ...,
font-weight: ...,
font-size: ...,
}
}
// then in my styled component,
// define the multi-line theme, along with component specific styles
// or even override the theme styles
const Header = styled.span`
${props => props.theme.fonts};
text-decoration: underline;
...
`;
现在,对我来说很明显,您需要将主题属性传递给组件上的每个样式。有没有一种模式可以帮助减少上面示例中出现的一些重复代码?