Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/envato/react-breakpoints
Respond to changes in a DOM element's size. With React Breakpoints, element queries are no longer "web design's unicorn" 🦄
https://github.com/envato/react-breakpoints
breakpoints element-queries hooks media-queries performance react react-hooks resize-observer
Last synced: 3 months ago
JSON representation
Respond to changes in a DOM element's size. With React Breakpoints, element queries are no longer "web design's unicorn" 🦄
- Host: GitHub
- URL: https://github.com/envato/react-breakpoints
- Owner: envato
- License: mit
- Created: 2019-11-22T06:19:31.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2022-07-12T05:35:51.000Z (over 2 years ago)
- Last Synced: 2024-09-23T01:54:29.128Z (4 months ago)
- Topics: breakpoints, element-queries, hooks, media-queries, performance, react, react-hooks, resize-observer
- Language: TypeScript
- Homepage:
- Size: 312 KB
- Stars: 71
- Watchers: 5
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE-OF-CONDUCT.md
- Codeowners: CODEOWNERS
Awesome Lists containing this project
README
React Breakpoints
---
`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.
# 📦 What's in the box?
> No polling. No event listening. No sentinel elements. **Just a [`ResizeObserver`](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver)!**
This package provides you with:
- a [``](/docs/api.md#provider) to instantiate the `ResizeObserver`;
- an [``](/docs/api.md#observe) component to observe changes in a DOM element and respond to them.For power users this package also provides:
- a [`useBreakpoints()`](/docs/api.md#usebreakpoints) hook to change a component's behaviour based on the observed size information in the nearest parent ``;
- a [`useResizeObserver()`](/docs/api.md#useresizeobserver) hook to connect a DOM element in your component to the instantiated `ResizeObserver` on ``;
- a [`useResizeObserverEntry()`](/docs/api.md#useresizeobserverentry) hook to retrieve the `ResizeObserverEntry` put on the nearest ``. This is what `useBreakpoints()` uses under the hood.# 🐉 Be careful using this package when…
- …all you want is the low-level API stuff. See [@envato/react-resize-observer-hook](https://github.com/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](https://web.dev/cls/) on public pages. **Keep reading though, this package may still be of value to you!**# 🏅 This package is _really good_ at…
- …following the latest [draft spec](https://drafts.csswg.org/resize-observer/), giving you access to cutting edge features like `devicePixelContentBoxSize` and [per-fragment](https://drafts.csswg.org/css-break-3/) observation.
- …performantly observing many elements with a single `ResizeObserver` instance. None of that "a new `ResizeObserver` instance per observed element" bloat that [some](https://github.com/ZeeCoder/use-resize-observer/blob/314b29c33cfcd2c51b8854b775b0a2a5c325d94a/src/index.ts#L151-L157) 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](https://web.dev/cls/). 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 use `ResizeObserver` for responsive layouts.**# ⚡️ Quick start
Follow these **minimum required steps** to get started with `react-breakpoints`. This is just the tip of the iceberg, though. Check the [API Docs](/docs/api.md) for all options.
```shell
npm install @envato/react-breakpoints
```## Wrap your component tree with the provider
```jsx
import { Provider as ResizeObserverProvider } from '@envato/react-breakpoints';const App = () => ...;
```⚠️ **Caution** — You may need to pass some props to `` to increase browser support. Please refer to the [API Docs](/docs/api.md#provider).
## Observe an element and use the results
```jsx
import { Observe } from '@envato/react-breakpoints';const exampleBreakpoints = {
widths: {
0: 'mobile',
769: 'tablet',
1025: 'desktop'
}
};export const ExampleComponent = () => (
{({ observedElementProps, widthMatch = 'ssr' }) => (
)}
);
```See the [API Docs](/docs/api.md) for reference guides and usage examples.
# Observing vs. Consuming `ResizeObserverSize`
There is an important distinction between the `boxSize` you observe and the `boxSize` you pass to your breakpoints. See [Observing vs. Consuming `ResizeObserverSize`](/docs/boxSizes.md) for more information.
# Re-rendering
Using [`useResizeObserver()`](/docs/api.md#useresizeobserver), [`useResizeObserverEntry()`](/docs/api.md#useresizeobserverentry) or [`useBreakpoints()`](/docs/api.md#usebreakpoints) in your components causes them to re-render **every time a resize is observed**.
# Server-Side Rendering
See [Server-Side Rendering](/docs/server-side-rendering.md) for more information.
# Maintainers
- [Marc Dingena](https://github.com/mdingena) (owner)
# Contributing
For bug fixes, documentation changes, and small features:
1. Fork this repository.
1. Create your feature branch (git checkout -b my-new-feature).
1. Commit your changes (git commit -am 'Add some feature').
1. Push to the branch (git push origin my-new-feature).
1. 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.