Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass props to "css" element of styled components?

I can do this with a styled.div

styled.div`
    color: ${props=> props.color || 'black'};
`

How can I do something similar with a styled components css element?

const BlackBGCSS = css`
    color: ${props=> props.color || 'black'};
`

For now my solution is to create a factory function

const BlackBGCSS = (props)=> css`
    color: ${props=> props.color || 'black'};
`
like image 424
Jad S Avatar asked Nov 22 '18 14:11

Jad S


Video Answer


1 Answers

Kind of how you have it:

const myCSS = css`
    background: ${({ myColor }) => myColor || `black`};
`;

const MyComponent = styled('div')`
    ${myCSS};
`;

Then

<MyComponent myColor="red">Hello World</MyComponent>

Hope that helps.

like image 150
Ricardinho Avatar answered Sep 21 '22 06:09

Ricardinho