|
| 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. |
0 commit comments