Skip to content

Commit c8c8922

Browse files
committed
docs: rewrite autoUpdate
1 parent 063ec2e commit c8c8922

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

website/pages/docs/autoUpdate.mdx

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
# autoUpdate
22

3-
This function adds listeners that automatically update the
4-
position of the floating element when required.
3+
This function adds listeners that automatically call an update
4+
function when necessary so that the floating element remains
5+
“anchored” to the reference element in a variety of scenarios
6+
without detaching.
57

6-
Only call this when the floating element is mounted to the DOM
7-
(or visible on the screen).
8+
```js
9+
autoUpdate(referenceEl, floatingEl, update, options);
10+
```
811

9-
```js /autoUpdate/
10-
import {computePosition, autoUpdate} from '@floating-ui/dom';
12+
Only call this function when the floating element is mounted to
13+
the DOM (or visible on the screen).
1114

12-
async function update() {
13-
const {x, y} = await computePosition(referenceEl, floatingEl);
14-
}
15+
```js
16+
import {computePosition, autoUpdate} from '@floating-ui/dom';
1517

16-
const cleanup = autoUpdate(referenceEl, floatingEl, update);
18+
const cleanup = autoUpdate(referenceEl, floatingEl, () => {
19+
computePosition(referenceEl, floatingEl).then(({x, y}) => {
20+
// ...
21+
});
22+
});
1723
```
1824

1925
It returns a cleanup function that should be invoked when the
2026
floating element is no longer mounted on the DOM (or visible on
2127
the screen).
2228

23-
It does not call `update(){:js}` immediately, so for the first
24-
update, you should call the update function yourself.
29+
```js
30+
// Later, when the element is removed from the screen
31+
cleanup();
32+
```
2533

26-
> By default, this requires a `ResizeObserver{:.class}`. To
27-
> support old browsers, either polyfill this constructor, or
28-
> disable the `elementResize{:.objectKey}` option and update
29-
> manually.
34+
> The update function is called immediately by default and
35+
> requires a `ResizeObserver{:.class}`.
3036
3137
## Options
3238

@@ -75,6 +81,12 @@ default: `true{:js}`
7581
Whether to update the position when either the reference or
7682
floating elements resized. This uses a `ResizeObserver{:.class}`.
7783

84+
To support old browsers, polyfill the constructor, or disable
85+
this option and update manually when the elements resize.
86+
87+
> Disabling this means `autoUpdate(){:js}` will no longer call
88+
> your update function immediately.
89+
7890
```js
7991
const cleanup = autoUpdate(referenceEl, floatingEl, update, {
8092
elementResize: false,

0 commit comments

Comments
 (0)