Last active
November 10, 2022 19:48
-
-
Save loganpowell/b044c9b3d7187f77c6fb401e4029cdcc to your computer and use it in GitHub Desktop.
Styled-component with react-container-query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from: https://github.com/styled-components/styled-components/issues/416#issuecomment-276230181 | |
import React from 'react'; | |
import classnames from 'classnames'; | |
import styled from 'styled-components'; | |
import { ContainerQuery } from 'react-container-query'; | |
const query = { | |
'box1': { | |
minWidth: 250, | |
maxWidth: 599, | |
}, | |
'box2': { | |
minWidth: 600, | |
}, | |
}; | |
const Box = styled.div` | |
color: white; | |
background: green; | |
&.box1 { | |
background: gold; | |
} | |
&.box2 { | |
background: red; | |
} | |
`; | |
export default BoxContainer = (props) => { | |
return ( | |
<ContainerQuery query={query}> | |
{(params) => ( | |
<Box {...props} className={classnames(params)} /> | |
)} | |
</ContainerQuery> | |
); | |
} | |
But yes, as @AntonAderum mentioned, it would be nice to be able to do this instead: | |
const Box = styled.div` | |
color: white; | |
background: green; | |
&:container-query(min-width: 250px and max-width: 599px) { | |
background: gold; | |
} | |
&:container-query(min-width: 600px) { | |
background: red; | |
} | |
`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
references: