Skip to content

Instantly share code, notes, and snippets.

@loganpowell
Last active November 10, 2022 19:48
Show Gist options
  • Save loganpowell/b044c9b3d7187f77c6fb401e4029cdcc to your computer and use it in GitHub Desktop.
Save loganpowell/b044c9b3d7187f77c6fb401e4029cdcc to your computer and use it in GitHub Desktop.
Styled-component with react-container-query
// 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;
}
`;