Skip to content

Commit 78d6269

Browse files
committed
Update with API changes
1 parent e7071ce commit 78d6269

File tree

2 files changed

+146
-138
lines changed

2 files changed

+146
-138
lines changed

README.md

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,32 @@
1616

1717
`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.
1818

19-
## 📦 What's in the box?
19+
# 📦 What's in the box?
2020

2121
> No polling. No event listening. No sentinel elements. **Just a [`ResizeObserver`](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver)!**
2222
2323
This package provides you with:
2424

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.
2827

2928
For power users this package also provides:
3029

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>`;
3231
- a [`useResizeObserver()`](/docs/api.md#useresizeobserver) hook to connect a DOM element in your component to the instantiated `ResizeObserver` on `<Provider>`;
3332
- a [`useResizeObserverEntry()`](/docs/api.md#useresizeobserverentry) hook to retrieve the `ResizeObserverEntry` put on the nearest `<Context>`. This is what `useBreakpoints()` uses under the hood.
3433

35-
# 🚧 Developer status
34+
# 🐉 Be careful using this package when&hellip;
3635

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+
- &hellip;all you want is the low-level API stuff. See [@envato/react-resize-observer-hook](https://github.com/envato/react-resize-observer-hook).
37+
- &hellip;you want _real_ CSS Element Queries. At the end of the day, this is still a JS solution.
38+
- &hellip;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&hellip;
41+
42+
- &hellip;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+
- &hellip;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+
- &hellip;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.**
3845

3946
# ⚡️ Quick start
4047

@@ -44,64 +51,52 @@ Follow these **minimum required steps** to get started with `react-breakpoints`.
4451
npm install @envato/react-breakpoints
4552
```
4653

47-
## Set up the provider
54+
## Wrap your component tree with the provider
4855

49-
```javascript
56+
```jsx
5057
import { Provider as ResizeObserverProvider } from '@envato/react-breakpoints';
5158

5259
const App = () => <ResizeObserverProvider>...</ResizeObserverProvider>;
5360
```
5461

5562
⚠️ **Caution** — You may need to pass some props to `<Provider>` to increase browser support. Please refer to the [API Docs](/docs/api.md#provider).
5663

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".
64+
## Observe an element and use the results
6065

61-
```javascript
66+
```jsx
6267
import { Observe } from '@envato/react-breakpoints';
6368

64-
const MyObservingComponent = () => (
65-
<Observe
66-
render={({ observedElementProps }) => (
67-
<aside {...observedElementProps}>
68-
<MyResponsiveComponent />
69-
</aside>
70-
)}
71-
/>
72-
);
73-
```
74-
75-
## Consume the observation
76-
77-
Components that are rendered within the "Observe Scope" can consume observation results via `useBreakpoints()`:
78-
79-
```javascript
80-
import { useBreakpoints } from '@envato/react-breakpoints';
81-
82-
const MyResponsiveComponent = () => {
83-
const options = {
69+
const exampleBreakpoints = {
8470
widths: {
8571
0: 'mobile',
8672
769: 'tablet',
8773
1025: 'desktop'
8874
}
8975
};
9076

91-
const [label] = useBreakpoints(options);
92-
93-
return <div className={label}>This element is currently within the {label} range.</div>;
94-
};
77+
export const ExampleComponent = () => (
78+
<Observe breakpoints={exampleBreakpoints}>
79+
{({ observedElementProps, widthMatch = 'ssr' }) => (
80+
<div {...observedElementProps}>
81+
<div className={widthMatch}>
82+
</div>
83+
)}
84+
</Observe>
85+
);
9586
```
9687

97-
However, `<Observe>` supports additional props allowing you to observe **and** consume observations — no `useBreakpoints()` required!
98-
9988
See the [API Docs](/docs/api.md) for reference guides and usage examples.
10089

10190
# Observing vs. Consuming `ResizeObserverSize`
10291

10392
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.
10493

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+
105100
# Server-Side Rendering
106101

107102
See [Server-Side Rendering](/docs/server-side-rendering.md) for more information.

0 commit comments

Comments
 (0)