react-breakpoints
allows you to respond to changes in a DOM element's size. You can change the evaluated logic and rendered output of components based on observed size changes in DOM elements. For example, you can change a dropdown menu to a horizontal list menu based on its parent container's width without using CSS media queries.
No polling. No event listening. No sentinel elements. Just a
ResizeObserver
!
This package provides you with:
- a
<Provider>
to instantiate theResizeObserver
; - an
<Observe>
component to observe changes in a DOM element and respond to them.
For power users this package also provides:
- a
useBreakpoints()
hook to change a component's behaviour based on the observed size information in the nearest parent<Observe>
; - a
useResizeObserver()
hook to connect a DOM element in your component to the instantiatedResizeObserver
on<Provider>
; - a
useResizeObserverEntry()
hook to retrieve theResizeObserverEntry
put on the nearest<Context>
. This is whatuseBreakpoints()
uses under the hood.
- …all you want is the low-level API stuff. See @envato/react-resize-observer-hook.
- …you want real CSS Element Queries. At the end of the day, this is still a JS solution.
- …you care deeply about Cumulative Layout Shift on public pages. Keep reading though, this package may still be of value to you!
- …following the latest draft spec, giving you access to cutting edge features like
devicePixelContentBoxSize
and per-fragment observation. - …performantly observing many elements with a single
ResizeObserver
instance. None of that "a newResizeObserver
instance per observed element" bloat that some alternative packages implement. - …building highly-responsive private dashboards 📊. One key thing this package (and every other
ResizeObserver
package out there) can contribute negatively to is Cumulative Layout Shifting. At Envato we've had great success using this package on pages that are only visible after signing in, like our Author Dashboard. We've had less success using it in places where search engines can go, on components with responsive styles that changed the layout vertically. One of our company values is "Tell It Like It Is", so we're letting you know to be mindful of when and how you useResizeObserver
for responsive layouts.
Follow these minimum required steps to get started with react-breakpoints
. This is just the tip of the iceberg, though. Check the API Docs for all options.
npm install @envato/react-breakpoints
import { Provider as ResizeObserverProvider } from '@envato/react-breakpoints';
const App = () => <ResizeObserverProvider>...</ResizeObserverProvider>;
<Provider>
to increase browser support. Please refer to the API Docs.
import { Observe } from '@envato/react-breakpoints';
const exampleBreakpoints = {
widths: {
0: 'mobile',
769: 'tablet',
1025: 'desktop'
}
};
export const ExampleComponent = () => (
<Observe breakpoints={exampleBreakpoints}>
{({ observedElementProps, widthMatch = 'ssr' }) => (
<div {...observedElementProps}>
<div className={widthMatch}>
</div>
)}
</Observe>
);
See the API Docs for reference guides and usage examples.
There is an important distinction between the boxSize
you observe and the boxSize
you pass to your breakpoints. See Observing vs. Consuming ResizeObserverSize
for more information.
Using useResizeObserver()
, useResizeObserverEntry()
or useBreakpoints()
in your components causes them to re-render every time a resize is observed.
See Server-Side Rendering for more information.
- Marc Dingena (owner)
For bug fixes, documentation changes, and small features:
- Fork this repository.
- Create your feature branch (git checkout -b my-new-feature).
- Commit your changes (git commit -am 'Add some feature').
- Push to the branch (git push origin my-new-feature).
- Create a new Pull Request.
For larger new features: Do everything as above, but first also make contact with the project maintainers to be sure your change fits with the project direction and you won't be wasting effort going in the wrong direction.