React
A React toolkit to position floating elements and optionally create interactions for them.
If you only need positioning without interactions, use the following for a smaller bundle size:
Choose the package you installed for better docs:
The goal of this package is to provide primitives — features common to all types of floating elements — to create components for tooltips, popovers, dropdown menus, hover cards, modal dialogs, select menus, comboboxes, and more.
Instead of providing any pre-built components, it gives you the tools to create them more easily. This is ideal if you want lots of control, but does require more work to get started. The full guides in React Examples contain some copy-pasteable examples to get you started more quickly.
Usage
Floating UI’s usage can be broken down into two disparate parts:
- Positioning (available for both
@floating-ui/react
and@floating-ui/react-dom
). - Interactions (available for only
@floating-ui/react
).
Positioning
useFloating()
is the main Hook of each package.
At its most basic, the Hook returns a refs
object and
a floatingStyles
object:
This will position the floating Tooltip
element at the bottom
center of the Button
element by default.
refs.setReference
sets the reference (or anchor) element that is being referred to for positioning.refs.setFloating
sets 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
prop.
The refs are callback refs (functions) to make them reactive — this ensures changes to the reference or floating elements, such as with conditional rendering, are handled correctly by updating the position.
Anchoring
To ensure the floating element remains anchored to the reference
element, such as when scrolling or resizing, pass
autoUpdate
to the
whileElementsMounted
option:
API docs
Visit the full useFloating
API docs for
detailed information about customizing the positioning of
floating elements.
Testing
When testing your components, ensure you flush microtasks
immediately after the floating element renders. This will avoid
the act
warning.
You may use this a lot, so you can create a custom function:
Narrow reference type
Due to virtual elements, you may need to narrow the type when performing DOM operations on the ref:
Interactions
To add interactions, such as the ability to only show a floating element while hovering over its reference element, the Hook must first accept the following two options:
open
— a boolean that represents whether the floating element is currently rendered.onOpenChange
— an event callback invoked when the open boolean state should change.
Note that floating components do not always require “anchor
positioning”, so floatingStyles
can be ignored.
Hooks
Interaction Hooks allow the open state to change, among other
functionality. Each interaction Hook accepts the
context
object which gets returned from
useFloating()
as their first argument:
The useHover()
and useFocus()
Hooks set up Effects
and return event handler props to change the open state, the
latter of which get merged by useInteractions()
for
rendering.
This API enables each of the Hooks to be fully tree-shakeable and opt-in. The navigation bar on the left explains them in detail.
Prop getters
The prop getters are used to add event handlers returned from the
interaction Hooks, among other functionality, to the reference
and floating elements. When called, they return an object of
props like onFocus
.
Open event callback
The onOpenChange
event callback is invoked with an
optionally-defined event
object and reason
string as the second and third parameter:
Note that onOpenChange
is not called if you
manually changed the open state via the setIsOpen()
setter. You can derive the event yourself in this case anyway.
Changing the positioning reference while retaining events
refs.setReference
element is both the events and position reference by default.refs.setPositionReference
allows you to separate the position to another element (either real or virtual).
Multiple floating elements on a single reference element
Refs can be merged with the
useMergeRefs
hook, and props
can be merged by calling one of the getters inside of the other:
Disabled elements
Disabled elements don’t fire events, so tooltips attached to
disabled buttons don’t show. Avoid using the
disabled
prop, and make the button visually disabled
instead. This ensures you won’t need any wrapper tags and makes
the tooltip accessible to all users.