You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+34-39Lines changed: 34 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,25 +16,32 @@
16
16
17
17
`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.
18
18
19
-
##📦 What's in the box?
19
+
# 📦 What's in the box?
20
20
21
21
> No polling. No event listening. No sentinel elements. **Just a [`ResizeObserver`](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver)!**
22
22
23
23
This package provides you with:
24
24
25
-
- a [`<Provider>`](/docs/api.md#provider) to instantiate the ResizeObserver;
26
-
- an [`<Observe>`](/docs/api.md#observe) component to observe changes in a DOM element;
27
-
- a [`useBreakpoints()`](/docs/api.md#usebreakpoints) hook to change a component's behaviour based on the observed size information in the nearest parent `<Observe>`.
25
+
- a [`<Provider>`](/docs/api.md#provider) to instantiate the `ResizeObserver`;
26
+
- an [`<Observe>`](/docs/api.md#observe) component to observe changes in a DOM element and respond to them.
28
27
29
28
For power users this package also provides:
30
29
31
-
- a [`<Context>`](/docs/api.md#context) on which you can assign a `ResizeObserverEntry` value to update any nested components that are using `useBreakpoints()`;
30
+
- a [`useBreakpoints()`](/docs/api.md#usebreakpoints) hook to change a component's behaviour based on the observed size information in the nearest parent `<Observe>`;
32
31
- a [`useResizeObserver()`](/docs/api.md#useresizeobserver) hook to connect a DOM element in your component to the instantiated `ResizeObserver` on `<Provider>`;
33
32
- a [`useResizeObserverEntry()`](/docs/api.md#useresizeobserverentry) hook to retrieve the `ResizeObserverEntry` put on the nearest `<Context>`. This is what `useBreakpoints()` uses under the hood.
34
33
35
-
# 🚧 Developer status
34
+
# 🐉 Be careful using this package when…
36
35
37
-
Several projects within Envato are currently using this package, giving me confidence that the API is clear and the code adds value. The package is still in an early stage, but exposure to "the wild" will help reveal more edge-cases and hopefully make the package more robust overall.
36
+
-…all you want is the low-level API stuff. See [@envato/react-resize-observer-hook](https://github.com/envato/react-resize-observer-hook).
37
+
-…you want _real_ CSS Element Queries. At the end of the day, this is still a JS solution.
38
+
-…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!**
39
+
40
+
# 🏅 This package is _really good_ at…
41
+
42
+
-…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.
43
+
-…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.
44
+
-…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.**
38
45
39
46
# ⚡️ Quick start
40
47
@@ -44,64 +51,52 @@ Follow these **minimum required steps** to get started with `react-breakpoints`.
⚠️ **Caution** — You may need to pass some props to `<Provider>` to increase browser support. Please refer to the [API Docs](/docs/api.md#provider).
56
63
57
-
## Observe an element
58
-
59
-
Everything you render through `<Observe>` becomes aware of the size of the element that is given `{...observedElementProps}`. This is called an "Observe Scope".
However, `<Observe>` supports additional props allowing you to observe **and** consume observations — no `useBreakpoints()` required!
98
-
99
88
See the [API Docs](/docs/api.md) for reference guides and usage examples.
100
89
101
90
# Observing vs. Consuming `ResizeObserverSize`
102
91
103
92
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.
104
93
94
+
# Re-rendering
95
+
96
+
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**.
97
+
98
+
In some cases, you may want to optimise this. If you only want to re-render your components when the returned breakpoint values actually change, use `React.useMemo` or `React.memo`.
99
+
105
100
# Server-Side Rendering
106
101
107
102
See [Server-Side Rendering](/docs/server-side-rendering.md) for more information.
0 commit comments