Multiple Props Options for Styled Components

前端 未结 5 1588
死守一世寂寞
死守一世寂寞 2021-02-06 11:55

I have a navbar component that I have created using Styled Components. I would like to create some props that change the background-color and/or text color.

For instanc

5条回答
  •  情话喂你
    2021-02-06 12:23

    Styled components also accepts a function where in you can read props. Moreover if you choose to pass a theme prop instead, you can also define an object for your themes.

    
    const themes = {
      dark: css `
         background: ${colors.dark};
         color: ${colors.light};
      `,
      light: css`
         background: ${colors.light};
         color: ${colors.dark};
      `
    }
    export const Navbar = styled.nav(({theme})=>`
      width: 100%;
      background: ${colors.light};
      color: ${colors.dark};
      ... // rest of the css
    
      ${theme?themes[theme]:''}
    
      `)
    
    
    

提交回复
热议问题