css-in-js
1. styled-components
npm i styled-components
// Static object
const Box = styled.div({
background: 'palevioletred',
height: '50px',
width: '50px'
});
// A new component based on Button, but with some override styles
const TomatoBox = styled(Box)`
background: tomato;
`;
// Adapting based on props
const PropsBox = styled.div(props => ({
background: props.background,
height: '50px',
width: '50px'
}));
render(
<div>
<Box />
<TomatoBox />
<PropsBox background="blue" />
</div>
);
2.@emotion/react
.babelrc
{
"presets": [
[
"@babel/preset-react",
{ "runtime": "automatic", "importSource": "@emotion/react" }
]
],
"plugins": ["@emotion/babel-plugin"]
}
import { css } from '@emotion/react'
const breakpoints = [576, 768, 992, 1200]
const mq = breakpoints.map(bp => `@media (min-width: ${bp}px)`)
render(
<div>
<div
css={{
color: 'green',
[mq[0]]: {
color: 'gray'
},
[mq[1]]: {
color: 'hotpink'
}
}}
>
Some text!
</div>
<p
css={css`
color: green;
${mq[0]} {
color: gray;
}
${mq[1]} {
color: hotpink;
}
`}
>
Some other text!
</p>
</div>
)