Skip to content

Commit fdffb9c

Browse files
committed
Add dropdowns to documentation
1 parent c69f9f1 commit fdffb9c

3 files changed

Lines changed: 104 additions & 48 deletions

File tree

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<p align="center">
44
<img alt="npm version" src="https://img.shields.io/npm/v/@envato/react-breakpoints?style=for-the-badge" />
55
<img alt="react version" src="https://img.shields.io/npm/dependency-version/@envato/react-breakpoints/dev/react?style=for-the-badge">
6-
<img alt="npm bundle size" src="https://img.shields.io/bundlephobia/minzip/@envato/react-breakpoints?style=for-the-badge">
76
<img alt="license" src="https://img.shields.io/npm/l/@envato/react-breakpoints?style=for-the-badge" />
87
<a href="CODE-OF-CONDUCT.md"><img alt="contributor covenant v2.0 adopted" src="https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg?style=for-the-badge" /></a>
98
</p>
@@ -53,7 +52,7 @@ const App = () => (
5352
)
5453
```
5554

56-
⚠️ **Caution**: You may need to pass some props to `<Provider>` to increase browser support. Please refer to the [API Docs](/docs/api.md#provider).
55+
⚠️ **Caution** You may need to pass some props to `<Provider>` to increase browser support. Please refer to the [API Docs](/docs/api.md#provider).
5756

5857
## Observe an element
5958

docs/api.md

Lines changed: 94 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ Your app must include a `<Provider>`. It creates a React context with a `ResizeO
2929
</Provider>
3030
```
3131

32+
<details>
33+
<summary><big><code>ponyfill</code></big> — <small>optional <code>ResizeObserver</code> constructor</small></summary>
34+
3235
⚠️ **Caution**`Provider` instantiates a `window.ResizeObserver` by default. [`window.ResizeObserver` currently has weak browser support](https://caniuse.com/#feat=mdn-api_resizeobserver_resizeobserver). You may pass a `ResizeObserver` constructor to `Provider` to use instead of `window.ResizeObserver`. I recommend [ponyfilling](https://ponyfill.com) using [`@juggle/resize-observer`](https://github.com/juggle/resize-observer). You can also [monkey patch](https://en.wikipedia.org/wiki/Monkey_patch) `window.ResizeObserver` and use `Provider` without the `ponyfill` prop.
36+
</details>
3337

3438
## Usage
3539

@@ -50,7 +54,7 @@ const App = () => (
5054

5155
You can observe size changes of an element's `box` by rendering it through `<Observe>`'s `render` prop. Your render function receives a `observedElementProps` argument that you spread onto the DOM element you wish to observe. It also receives `widthMatch` and `heightMatch` arguments which match the values you assigned via `<Observe>`'s `breakpoints` prop.
5256

53-
⚠️ **Important** — There is an important distinction between the `box` you're observing and `box`es triggering breakpoints. See [Observing boxes vs. Matching boxes](boxes.md) for more information.
57+
⚠️ **Important** — There is an important distinction between the `box` you observe and the `box` you consume for triggering breakpoints. See [Observing vs. Consuming Boxes](boxes.md) for more information.
5458

5559
## Reference guide
5660

@@ -104,6 +108,43 @@ You can observe size changes of an element's `box` by rendering it through `<Obs
104108
/>
105109
```
106110

111+
<details>
112+
<summary><big><code>box</code></big> — <small>optional <code>String</code></small></summary>
113+
114+
Depending on your implementation of `ResizeObserver`, the [internal `ResizeObserverEntry`](#useresizeobserverentry) can contain size information about multiple "boxes" of the observed element.
115+
116+
This library supports observing the following `box` options (but your browser may not!):
117+
118+
* [`'border-box'`](https://caniuse.com/#feat=mdn-api_resizeobserverentry_borderboxsize)
119+
* [`'content-box'`](https://caniuse.com/#feat=mdn-api_resizeobserverentry_contentboxsize)
120+
* [`'device-pixel-content-box'`](https://github.com/w3c/csswg-drafts/issues/3554)
121+
122+
If `box` is left `undefined` or set to any value other than those listed above, `<Observe>` will default to using information from `ResizeObserverEntry.contentRect`.
123+
124+
⚠️ **Important** — There is an important distinction between the `box` you observe and the `box` you consume for triggering breakpoints. See [Observing vs. Consuming Boxes](boxes.md) for more information.
125+
</details>
126+
127+
<details>
128+
<summary><big><code>breakpoints</code></big> — <small>optional <code>Object</code></small></summary>
129+
130+
This prop accepts an object with a shape identical to the `options` object of [`useBreakpoints()`](#usebreakpoints).
131+
</details>
132+
133+
<details>
134+
<summary><big><code>render</code></big> — <small><code>Function</code></small></summary>
135+
136+
A render prop that takes a function with three arguments.
137+
138+
* `observedElementProps` — <small><code>Object</code></small>
139+
Using the `...` spread operator, you apply this object to the DOM element you want to observe.
140+
* `widthMatch` — <small><code>any</code></small>
141+
If you passed an object with a `widths` property to the `breakpoints` prop, this argument contains the currently matching width breakpoint value.
142+
* `heightMatch` — <small><code>any</code></small>
143+
If you passed an object with a `heights` property to the `breakpoints` prop, this argument contains the currently matching height breakpoint value.
144+
145+
Your function should at least return some JSX with `{...observedElementProps}` applied to a DOM element.
146+
</details>
147+
107148
## Usage
108149

109150
```javascript
@@ -161,12 +202,19 @@ The hook takes an `options` object as its first argument, which must include a `
161202

162203
Optionally, you can include a `box` property, which — depending on your implementation of `ResizeObserver` — can target different observable "boxes" of an element. By default, the legacy `contentRect` property will be used by `useBreakpoints()`.
163204

164-
The hook takes a optional `ResizeObserverEntry` as its second argument. **If you pass one, `useBreakpoints()` will not fetch it from the [context](#context), so use caution!**
205+
The hook takes an optional `ResizeObserverEntry` as its second argument. **If you pass one, `useBreakpoints()` will not fetch it from the [context](#context), so use caution!**
165206

166207
## Reference guide
167208

168209
```javascript
169-
const [widthValue, heightValue] = useBreakpoints({
210+
/* returns an array of matched breakpoint values */
211+
const [
212+
/* first element is the matched value from `widths` */
213+
widthValue,
214+
215+
/* second element is the matched value from `heights` */
216+
heightValue
217+
] = useBreakpoints({
170218
/* (optional) target a box size of the observed element to match widths and heights on */
171219
box: 'border-box',
172220

@@ -192,14 +240,13 @@ const [widthValue, heightValue] = useBreakpoints({
192240
},
193241

194242
/* (optional) a ResizeObserverEntry to use instead of the one provided on context */
195-
myResizeObserverEntry);
243+
injectResizeObserverEntry);
196244
```
197245

198-
### `box`
199-
200-
> `String` <sup>optional</sup>
246+
<details>
247+
<summary><big><code>options.box</code></big> — <small>optional <code>String</code></small></summary>
201248

202-
Depending on your implementation of `ResizeObserver`, the [internal `ResizeObserverEntry`](#useResizeObserverEntry) can contain size information about multiple "boxes" of the observed element.
249+
Depending on your implementation of `ResizeObserver`, the [internal `ResizeObserverEntry`](#useresizeobserverentry) can contain size information about multiple "boxes" of the observed element.
203250

204251
This library supports the following `box` options (but your browser may not!):
205252

@@ -209,11 +256,11 @@ This library supports the following `box` options (but your browser may not!):
209256

210257
If `box` is left `undefined` or set to any value other than those listed above, `useBreakpoints()` will default to using information from `ResizeObserverEntry.contentRect`.
211258

212-
⚠️ **Important** — There is an important distinction between the `box` you're observing and `box`es triggering breakpoints. See [Observing boxes vs. Matching boxes](boxes.md) for more information.
259+
⚠️ **Important** — There is an important distinction between the `box` you observe and the `box` you consume for triggering breakpoints. See [Observing vs. Consuming Boxes](boxes.md) for more information.
260+
</details>
213261

214-
### `widths`
215-
216-
> `Object` <sup>optional</sup>
262+
<details>
263+
<summary><big><code>options.widths</code></big> — <small>optional <code>Object</code></small></summary>
217264

218265
`widths` must be an object with numbers as its keys. The numbers represent the minimum width the `box` must have for that key's value to be returned. The value of the highest matching width will be returned.
219266

@@ -266,10 +313,10 @@ const [Component] = useBreakpoints({
266313
}
267314
});
268315
```
316+
</details>
269317

270-
### `heights`
271-
272-
> `Object` <sup>optional</sup>
318+
<details>
319+
<summary><big><code>options.heights</code></big> — <small>optional <code>Object</code></small></summary>
273320

274321
`heights` must be an object with numbers as its keys. The numbers represent the minimum height the `box` must have for that key's value to be returned. The value of the highest matching height will be returned.
275322

@@ -286,15 +333,22 @@ heights: {
286333

287334
⚠️ **Caution**: If you do not provide `0` as a key for `heights`, you risk receiving `undefined` as a return value. This is intended behaviour, but makes it difficult to distinguish between receiving `undefined` because of a [Server-Side Rendering](server-side-rendering.md) scenario, or because the observed height is less than the next matching height.
288335

289-
Values can be of _any_ type, you are not restricted to return `string` values. See example in [`widths`](#widths) section above.
290-
291-
### `fragment`
336+
Values can be of _any_ type, you are not restricted to return `string` values. See example in `widths` section above.
337+
</details>
292338

293-
> `Number` <sup>optional</sup>
339+
<details>
340+
<summary><big><code>options.fragment</code></big> — <small>optional <code>Number</code></small></summary>
294341

295342
The box sizes are exposed as sequences in order to support elements that have multiple fragments, which occur in multi-column scenarios. You can specify which fragment's size information to use for matching `widths` and `heights` by setting this prop. Defaults to the first fragment.
296343

297-
See [W3C Editor's Draft](https://drafts.csswg.org/resize-observer-1/#resize-observer-entry-interface) for more information about fragments.
344+
See the [W3C Editor's Draft](https://drafts.csswg.org/resize-observer-1/#resize-observer-entry-interface) for more information about fragments.
345+
</details>
346+
347+
<details>
348+
<summary><big><code>injectResizeObserverEntry</code></big> — <small>optional <code>ResizeObserverEntry</code></small></summary>
349+
350+
Allows you to force `useBreakpoints()` to use the `ResizeObserverEntry` you pass here in its calculations rather than retrieving the entry that's on the closest [`<Context>`](#context).
351+
</details>
298352

299353
## Usage
300354

@@ -362,24 +416,10 @@ const MyChildComponent = () => {
362416

363417
⚠️ **Advanced usage** — This hook is used internally in [`<Observe>`](#observe) to:
364418
* start and stop observing an element by passing a `ref` to a DOM element;
365-
* bind a standardised callback to all observations, and set the `ObservedEntry` on a [`<Context>`](#context).
419+
* bind a standardised callback to all observations, and set the `observedEntry` on a [`<Context>`](#context).
366420

367421
This hook takes an optional `options` object argument, which currently only supports a `box` option.
368422

369-
### `box`
370-
371-
> `String` <sup>optional</sup>
372-
373-
Depending on your implementation of `ResizeObserver`, you may observe one of multiple "boxes" of an element to trigger an update of `observedEntry`. By default this option is not set, and the size information of the observed element comes from the legacy `contentRect` property.
374-
375-
This library supports the following `box` options (but your browser may not!):
376-
377-
* [`'border-box'`](https://caniuse.com/#feat=mdn-api_resizeobserverentry_borderboxsize)
378-
* [`'content-box'`](https://caniuse.com/#feat=mdn-api_resizeobserverentry_contentboxsize)
379-
* [`'device-pixel-content-box'`](https://github.com/w3c/csswg-drafts/issues/3554)
380-
381-
⚠️ **Important** — There is an important distinction between the `box` you're observing and `box`es triggering breakpoints. See [Observing boxes vs. Matching boxes](boxes.md) for more information.
382-
383423
## Reference guide
384424

385425
```javascript
@@ -398,6 +438,20 @@ const [
398438
);
399439
```
400440

441+
<details>
442+
<summary><big><code>options.box</code></big> — <small>optional <code>String</code></small></summary>
443+
444+
Depending on your implementation of `ResizeObserver`, you may observe one of multiple "boxes" of an element to trigger an update of `observedEntry`. By default this option is not set, and the size information of the observed element comes from the legacy `contentRect` property.
445+
446+
This library supports the following `box` options (but your browser may not!):
447+
448+
* [`'border-box'`](https://caniuse.com/#feat=mdn-api_resizeobserverentry_borderboxsize)
449+
* [`'content-box'`](https://caniuse.com/#feat=mdn-api_resizeobserverentry_contentboxsize)
450+
* [`'device-pixel-content-box'`](https://github.com/w3c/csswg-drafts/issues/3554)
451+
452+
⚠️ **Important** — There is an important distinction between the `box` you observe and the `box` you consume for triggering breakpoints. See [Observing vs. Consuming Boxes](boxes.md) for more information.
453+
</details>
454+
401455
## Usage
402456

403457
```javascript
@@ -487,12 +541,12 @@ const MyResponsiveComponent = () => {
487541

488542
let className;
489543

490-
if (width >= 0) {
491-
className = 'small';
544+
if (width >= 1025 ) {
545+
className = 'large';
492546
} else if (width >= 769) {
493547
className = 'medium';
494-
} else if (width >= 1025 ) {
495-
className = 'large';
548+
} else if (width >= 0) {
549+
className = 'small';
496550
}
497551

498552
return (
@@ -503,4 +557,4 @@ const MyResponsiveComponent = () => {
503557
}
504558
```
505559

506-
⚠️ **Hint** — You probably don't need this hook, because [`useBreakpoints()`](#usebreakpoints) abstracts the above implementation away for your convenience. You'll likely only need this hook if you need a property from `ResizeObserverEntry` which is not `contentRect` or [one of `box`'s options](#box).
560+
⚠️ **Hint** — You probably don't need this hook, because [`useBreakpoints()`](#usebreakpoints) abstracts the above implementation away for your convenience. You'll likely only need this hook if you need a property from `ResizeObserverEntry` which is not `contentRect` or one of `box`'s options.

docs/boxes.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
# Observing boxes vs. Matching boxes
1+
# Observing vs. Consuming boxes
22

3-
There is an important distinction between the `box` you're observing and `box`es triggering breakpoints.
3+
There is an important distinction between the `box` you observe and the `box` you consume for triggering breakpoints.
44

55
> You can observe a box on an element, and then respond to changes in **another** box of that element.
66
7-
There might be cases where observing the same box you're matching breakpoints against is not desirable. **This package supports observing and matching on different boxes.**
7+
There might be cases where observing the same box you're matching breakpoints against is not desirable. **This package supports observing and consuming different boxes on the same element.**
8+
9+
<details>
10+
<summary><big>What's happening under the hood?</big></summary>
811

9-
## What's happening under the hood?
1012

1113
Consider this example code and chain of events:
1214

@@ -53,6 +55,7 @@ const ChildComponent = () => {
5355
1. Because `border-box` is not observed, `<Observe>`'s context does not get updated, and therefore `<ChildComponent>` does not update.
5456
1. Finally, after a while longer, the element's `device-pixel-content-box` size changes.
5557
1. Even though `<ChildComponent>` uses this box's size, `<Observe>` is not observing changes on this box, and does not update its context to inform `<ChildComponent>`.
58+
</details>
5659

5760
# ⚠️ Important
5861

@@ -62,7 +65,7 @@ Consider the [CSS box model](https://en.wikipedia.org/wiki/CSS_box_model#/media/
6265

6366
![CSS Box Model](css-box-model.png)
6467

65-
When padding or border increase in thickness, the content's size will remain unaffected. If you are observing changes in `content-box`'s size, those padding and border changes will not trigger any updates.
68+
When padding or border increase in thickness, the content's size will remain unaffected. If you are observing changes in `content-box`'s size, those padding and border changes **will not trigger any updates**.
6669

6770
Similarly, let's say you have the following element:
6871

@@ -72,4 +75,4 @@ Similarly, let's say you have the following element:
7275
</div>
7376
```
7477

75-
Then you change that element's `padding` to `0`. If you are observing changes in this element's `border-box` size, this padding change will not trigger any updates because the `border-box` did not change size.
78+
Then you change that element's `padding` to `0`. If you are observing changes in this element's `border-box` size, this padding change **will not trigger any updates** because the `border-box` did not change size.

0 commit comments

Comments
 (0)