useContainerQuery hook or containerSize property #7615
-
With Container Queries now being implemented in all major browsers, it'd great to have custom components define their own breakpoint. This allows custom components to be self-contained and truly modular. One way would be to introduce a useContainerQueryHook similar to the media query. Box and other native components can make use of a new property containerSize to avoid a breaking change. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
For now you can do this. Give each child you want to style a class (or in my case a data-attr) and then target it using vanilla container queries syntax in the https://codesandbox.io/s/naughty-williams-pk2khp?file=/src/App.js
|
Beta Was this translation helpful? Give feedback.
-
One of the reasons I like react-container-query is that I can use a container-query not just for CSS, but for JS. That's important if I'm working with another (non-React) library like D3 and need to change properties based on, e.g. the inline-size of the chart: const query = {
'less-than-600': {
maxWidth: 599,
},
'at-least-600': {
minWidth: 600,
},
};
const [params, containerRef] = useContainerQuery(query)
const dateFormatter = useMemo(() => {
const style = params['less-than-600'] ? { month: 'narrow' } : { month: 'long' }
return new Intl.DateTimeFormat('default', style)
}, params) As long as Chakra components properly forward their refs, it's possible to do this: const [params, containerRef] = useContainerQuery(query)
<Box ref={containerRef} className={classnames(params)} pl={4} pr={2}>
</Box> |
Beta Was this translation helpful? Give feedback.
I realized you can also write it like this, with the styles located on the element they are styling. A bit better having the styles colocated.