auto Update
Automatically updates the position of the floating element when necessary to ensure it stays anchored.
To ensure the floating element remains anchored to its reference element, such as when scrolling and resizing the screen, its position needs to be continually updated when necessary.
To solve this, autoUpdate()
adds listeners that will
automatically call an update function which invokes
computePosition()
when necessary. Updates typically take
only ~1ms.
Usage
It’s important that this function is only called/set-up when the floating element is open on the screen, and cleaned up when it’s removed. Otherwise, it can cause severe performance degradation, especially with many floating elements being created.
Call autoUpdate()
only when the floating element is
open or mounted:
Then, when the user unhovers or clicks away, unmount the floating element and ensure you call the cleanup function to stop the auto updates:
Options
These are the options you can pass as a fourth argument to
autoUpdate()
.
ancestorScroll
default: true
Whether to update the position when an overflow ancestor is scrolled.
ancestorResize
default: true
Whether to update the position when an overflow ancestor is
resized. This uses the resize
event.
elementResize
default: true
Whether to update the position when either the reference or
floating elements resized. This uses a ResizeObserver
.
layoutShift
default: true
Whether to update the position of the floating element if the reference element moved on the screen as the result of layout shift.
animationFrame
default: false
Whether to update the position of the floating element on every animation frame if required. While optimized for performance, it should be used sparingly in the following cases:
- The reference element is animating on the screen with
transform
s. - Ensure a nested floating element is anchored when it’s outside of ancestor floating elements’ scrolling contexts.