1
我正在將styled components用於新項目,並且想要使用庫的主題化功能。我正在努力弄清楚是否有向一個對象添加多個樣式屬性的好方法或最佳實踐方法(即創建一種繼承)。現在想要的是:將多個樣式屬性添加到具有樣式組件的單個主題屬性
// 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;
...
`;
現在,對我來說,只有在我看來,您需要將主題屬性傳遞給組件上的每個樣式。有沒有一種模式可以幫助減少我上面例子中看到的一些重複代碼?
對不起,我應該已經說得更清楚了。樣式組件是一個在React組件中使用CSS的庫。我編輯了問題以鏈接到庫源代碼。 – bgmaster