Vue
This package provides Vue bindings for @floating-ui/dom
— a
library that provides anchor positioning for a floating element
to position it next to a given reference element.
Usage
useFloating()
is the main composable:
This will position the floating Tooltip
element at the bottom
center of the Button
element by default.
reference
is the reference (or anchor) element that is being referred to for positioning.floating
is the floating element that is being positioned relative to the reference element.floatingStyles
is an object of positioning styles to apply to the floating element’sstyle
attribute.
Disabling transform
By default, the floating element is positioned using
transform
in the floatingStyles
object. This is the
most performant way to position elements, but can be disabled:
If you’d like to retain transform styles while allowing transform animations, create a wrapper, where the outermost node is the positioned one, and the inner is the actual styled element.
Custom position styles
The composable returns the coordinates and positioning strategy
directly if floatingStyles
is not suitable for full
customization.
Return value
The composable
returns all the values from computePosition
,
plus some extras to work with Vue. This includes data about the
final placement and middleware data which are useful when
rendering.
Options
The composable accepts all the
options from computePosition
,
which allows you to customize the position. Here’s an example:
The composable also accepts Ref
options:
Middleware can alter the
positioning from the basic placement
, act as visibility
optimizers, or provide data to use.
The docs for the middleware that were passed are available here:
Anchoring
The position is only calculated once on render, or when the
reference
or floating
elements changed —
for example, the floating element get mounted via conditional
rendering.
To ensure the floating element remains anchored to its reference
element in a variety of scenarios without detaching — such as
when scrolling or resizing the page — you can pass the
autoUpdate
utility to the
whileElementsMounted
prop:
To pass options to autoUpdate
:
Ensure you are using conditional rendering (v-if
)
for the floating element, not an opacity/visibility/display
style. If you are using v-show
instead, avoid the
whileElementsMounted
prop.
Manual updating
While autoUpdate
covers most cases where the position of the
floating element must be updated, it does not cover every single
one possible due to performance/platform limitations.
The composable returns an update()
function to update the
position at will:
Custom components
The composable also accepts custom component template refs:
Effects
Positioning is done in an async function, which means the position is ready during a microtask, after layout effects are executed. This means initially, the floating element is situated at the top-left (0, 0) of its offset container — so calling DOM methods that cause side-effects like scrolling will result in unexpected behavior.
The composable returns an isPositioned
boolean ref
that lets you know if the floating element has been positioned:
The open
option accepts a boolean ref that represents the
open/close state of the floating element. This ensures you can
wait each time it opens when the host component does not unmount,
which is necessary in cases where the reference element relocates
on the page.
Arrow
The arrow
module exported from this package allows template
refs in addition to elements. See arrow for
details.
Virtual Element
See Virtual Elements for details. Example: