Skip to content

Commit 89f5f04

Browse files
authored
Add docs for IntersectionObserver, IntersectionObserverEntry (#4868)
1 parent 3597409 commit 89f5f04

3 files changed

Lines changed: 184 additions & 0 deletions

File tree

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
id: global-intersectionobserver
3+
title: IntersectionObserver 🧪
4+
---
5+
6+
import CanaryAPIWarning from './\_canary-channel-api-warning.mdx';
7+
8+
<CanaryAPIWarning />
9+
10+
The global [`IntersectionObserver`](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver) interface, as defined in Web specifications. It provides a way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document's viewport.
11+
12+
---
13+
14+
# Reference
15+
16+
## Constructor
17+
18+
### `IntersectionObserver()`
19+
20+
See [documentation in MDN](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver).
21+
22+
Creates a new `IntersectionObserver` object which will execute a specified callback function when it detects that a target element's visibility has crossed one or more `threshold` or `rnRootThreshold` values.
23+
24+
```ts
25+
new IntersectionObserver(callback, options?)
26+
```
27+
28+
#### Parameters
29+
30+
**`callback`**
31+
32+
A function which is called when the percentage of the target element is visible crosses a threshold. The callback receives two parameters:
33+
34+
- `entries`: An array of [`IntersectionObserverEntry`](global-intersectionobserverentry) objects, each representing one threshold which was crossed, either becoming more or less visible than the percentage specified by that threshold.
35+
- `observer`: The `IntersectionObserver` instance which invoked the callback.
36+
37+
**`options`** (optional)
38+
39+
An optional object with the following properties:
40+
41+
| Name | Type | Description |
42+
| -------------------- | -------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
43+
| `root` | [Element](element-nodes) \| null | An element that is an ancestor of the target, whose bounding rectangle will be considered the viewport. Defaults to the root viewport if not specified or if `null`. |
44+
| `rootMargin` | string | A string which specifies a set of offsets to add to the root's bounding box when calculating intersections. Defaults to `"0px 0px 0px 0px"`. |
45+
| `threshold` | number \| number[] | Either a single number or an array of numbers between 0.0 and 1.0, specifying a ratio of intersection area to total bounding box area for the observed target. Defaults to `[0]` if `rnRootThreshold` is not set. |
46+
| `rnRootThreshold` ⚠️ | number \| number[] | **React Native specific.** Either a single number or an array of numbers between 0.0 and 1.0, specifying a ratio of intersection area to the total root area. |
47+
48+
## Instance properties
49+
50+
### `root`
51+
52+
See [documentation in MDN](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/root).
53+
54+
The element or document whose bounds are used as the bounding box when testing for intersection.
55+
56+
### `rootMargin`
57+
58+
See [documentation in MDN](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/rootMargin).
59+
60+
An offset rectangle applied to the root's bounding box when calculating intersections.
61+
62+
### `rnRootThresholds` ⚠️
63+
64+
:::warning Non-standard
65+
This is a React Native specific extension.
66+
:::
67+
68+
A list of root thresholds, sorted in increasing numeric order, where each threshold is a ratio of intersection area to bounding box area of the specified root view, which defaults to the viewport.
69+
70+
Notifications for a target are generated when any of the thresholds specified in `rnRootThresholds` or `thresholds` are crossed for that target.
71+
72+
```ts
73+
get rnRootThresholds(): ReadonlyArray<number> | null;
74+
```
75+
76+
### `thresholds`
77+
78+
See [documentation in MDN](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/thresholds).
79+
80+
A list of thresholds, sorted in increasing numeric order, where each threshold is a ratio of intersection area to bounding box area of an observed target.
81+
82+
Notifications for a target are generated when any of the thresholds specified in `rnRootThresholds` or `thresholds` are crossed for that target.
83+
84+
## Instance methods
85+
86+
### `disconnect()`
87+
88+
See [documentation in MDN](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/disconnect).
89+
90+
Stops the `IntersectionObserver` object from observing any target.
91+
92+
### `observe()`
93+
94+
See [documentation in MDN](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/observe).
95+
96+
Tells the `IntersectionObserver` to begin observing a target element.
97+
98+
### `takeRecords()`
99+
100+
See [documentation in MDN](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/takeRecords).
101+
102+
Returns an array of `IntersectionObserverEntry` objects for all observed targets.
103+
104+
### `unobserve()`
105+
106+
See [documentation in MDN](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/unobserve).
107+
108+
Tells the `IntersectionObserver` to stop observing a particular target element.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
id: global-intersectionobserverentry
3+
title: IntersectionObserverEntry 🧪
4+
---
5+
6+
import CanaryAPIWarning from './\_canary-channel-api-warning.mdx';
7+
8+
<CanaryAPIWarning />
9+
10+
The [`IntersectionObserverEntry`](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry) interface, as defined in Web specifications. It describes the intersection between the target element and its root container at a specific moment of transition.
11+
12+
Instances of `IntersectionObserverEntry` are delivered to an [`IntersectionObserver`](global-intersectionobserver) callback in its `entries` parameter.
13+
14+
---
15+
16+
# Reference
17+
18+
## Instance properties
19+
20+
### `boundingClientRect`
21+
22+
See [documentation in MDN](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry/boundingClientRect).
23+
24+
Returns the bounds rectangle of the target element as a `DOMRectReadOnly`.
25+
26+
### `intersectionRatio`
27+
28+
See [documentation in MDN](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry/intersectionRatio).
29+
30+
Returns the ratio of the `intersectionRect` to the `boundingClientRect`.
31+
32+
### `intersectionRect`
33+
34+
See [documentation in MDN](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry/intersectionRect).
35+
36+
Returns a `DOMRectReadOnly` representing the target's visible area.
37+
38+
### `isIntersecting`
39+
40+
See [documentation in MDN](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry/isIntersecting).
41+
42+
A Boolean value which is `true` if the target element intersects with the intersection observer's root. If this is `true`, then the `IntersectionObserverEntry` describes a transition into a state of intersection; if it's `false`, then you know the transition is from intersecting to not-intersecting.
43+
44+
### `rnRootIntersectionRatio` ⚠️
45+
46+
:::warning Non-standard
47+
This is a React Native specific extension.
48+
:::
49+
50+
Returns the ratio of the `intersectionRect` to the `rootBounds`.
51+
52+
```ts
53+
get rnRootIntersectionRatio(): number;
54+
```
55+
56+
This is analogous to `intersectionRatio`, but computed relative to the root's bounding box instead of the target's bounding box. This corresponds to the `rnRootThreshold` option and allows you to determine what percentage of the root area is covered by the target element.
57+
58+
### `rootBounds`
59+
60+
See [documentation in MDN](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry/rootBounds).
61+
62+
Returns a `DOMRectReadOnly` for the intersection observer's root.
63+
64+
### `target`
65+
66+
See [documentation in MDN](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry/target).
67+
68+
The `Element` whose intersection with the root changed.
69+
70+
### `time`
71+
72+
See [documentation in MDN](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry/time).
73+
74+
A `DOMHighResTimeStamp` indicating the time at which the intersection was recorded, relative to the `IntersectionObserver`'s time origin.

website/sidebars.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ export default {
247247
'global-FormData',
248248
'global-global',
249249
'global-Headers',
250+
'global-intersectionobserver',
251+
'global-intersectionobserverentry',
250252
'global-navigator',
251253
'global-performance',
252254
'global-PerformanceEntry',

0 commit comments

Comments
 (0)