javascript - Styled Components: Global Colors Style Sheet - Stack Overflow

I'm using styled ponents in my app. I wanna make a global style sheet to hold all my color variabl

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 badges
Add a ment  | 

1 Answer 1

Reset to default 6

Check 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745250579a4618655.html

相关推荐

  • javascript - Styled Components: Global Colors Style Sheet - Stack Overflow

    I'm using styled ponents in my app. I wanna make a global style sheet to hold all my color variabl

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信