admin管理员组文章数量:1416642
I'm using styled ponents in my app. I wanna make a global style sheet to hold all my color variables. The API Docs remend something like this
import { createGlobalStyle } from 'styled-ponents'
const GlobalStyle = createGlobalStyle`
body {
color: ${props => (props.whiteColor ? 'white' : 'black')};
}
`
but i have a list of colors like this
export const COLORS = {
navy: "#2F374B",
green: "#1EB88D",
white: "#FFFFFF",
grey: "#222A3E",
primary5: "#F4FBF9",
danger: "#F13F4A",
};
and i dont think the body field can accept 'colors' or list of colors as a key. Any advice for setting this up?
Thanks in advance
I'm using styled ponents in my app. I wanna make a global style sheet to hold all my color variables. The API Docs remend something like this
import { createGlobalStyle } from 'styled-ponents'
const GlobalStyle = createGlobalStyle`
body {
color: ${props => (props.whiteColor ? 'white' : 'black')};
}
`
but i have a list of colors like this
export const COLORS = {
navy: "#2F374B",
green: "#1EB88D",
white: "#FFFFFF",
grey: "#222A3E",
primary5: "#F4FBF9",
danger: "#F13F4A",
};
and i dont think the body field can accept 'colors' or list of colors as a key. Any advice for setting this up?
Thanks in advance
Share Improve this question asked Feb 2, 2022 at 21:23 user64209user64209 1072 silver badges8 bronze badges1 Answer
Reset to default 6Check out https://styled-ponents./docs/advanced#theming
You can use a theme provider and pass reusable properties there.
import { ThemeProvider } from 'styled-ponents'
import { COLORS } from './wherever'
const theme = {
colors: COLORS
}
function App() {
return <ThemeProvider theme={theme}><MainAppCode /></ThemeProvider>
}
When styling a ponent you can access the theme object as a prop:
const Button = styled.button`
/* Color the border and text with theme */
color: ${props => props.theme.colors.green};
`;
Anywhere you pass a function in the styled ponent template strings, the argument you get will be props, which contains your theme object.
本文标签: javascriptStyled Components Global Colors Style SheetStack Overflow
版权声明:本文标题:javascript - Styled Components: Global Colors Style Sheet - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745250578a2649800.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论