@floating-ui/dom
Provides positioning tools for anchored floating elements, including tooltips and popovers.
- Downloads / mo
- 539.0M
- Stars
- 32.8K
- Last release
- 1.8.0
- License
- MIT
Strengths
- Keeps floating elements anchored. Use autoUpdate to refresh positioning during scrolling and resizing.
- Controls viewport collision behavior. Add flip to change placement when the tooltip overflows.
Limitations
- DOM interactions need application code. Add the hover and focus handlers that show and hide tooltips.
- Automatic updates need cleanup. Stop autoUpdate when the floating element is removed.
Quickstart
import { computePosition } from "@floating-ui/dom"; const reference = document.getElementById("button"); const floating = document.getElementById("tooltip"); if (!reference || !floating) throw new Error("Missing host elements"); floating.style.position = "absolute"; const { x, y } = await computePosition(reference, floating); Object.assign(floating.style, { left: `${x}px`, top: `${y}px` });
@floating-ui/dom examples
(reference: VirtualElement) => { computePosition(reference, this.element, { placement: this.#floatingUIOptions.placement ?? 'top', middleware: [ flip(), offset(this.#offset), shift(this.#shift), ...this.#middleware, ], ...this.#floatingUIOptions, }) .then(({ x, y }) => { Object.assign(this.element.style, { left: `${x}px`, top: `${y}px`, }) }) .catch(console.error) }