use Floating
The main Hook of the library that acts as a controller for all other Hooks and components.
Usage
Call the Hook inside a component.
Options
The Hook accepts an object of options to configure its behavior.
placement
default: 'bottom'
The placement of the floating element relative to the reference element.
12 strings are available:
The -start
and -end
alignments are
logical
and will adapt to the writing direction (e.g. RTL) as expected.
strategy
default: 'absolute'
This is the type of CSS position property to use. Two strings are available:
These strategies are differentiated as follows:
'absolute'
— the floating element is positioned relative to its nearest positioned ancestor. With most layouts, this usually requires the browser to do the least work when updating the position.'fixed'
— the floating element is positioned relative to its nearest containing block (usually the viewport). This is useful when the reference element is also fixed to reduce jumpiness with positioning while scrolling. It will in many cases also “break” the floating element out of a clipping ancestor.
transform
default: true
Whether to use CSS transforms to position the floating element
instead of layout (top
and left
CSS properties).
CSS transforms are more performant, but can cause conflicts with transform animations. In that case, you can make the positioned floating element a wrapper to avoid the conflict:
middleware
default: []
An array of middleware objects that change the positioning of the floating element.
When you want granular control over how the floating element is
positioned, middleware are used. They read the current
coordinates, optionally alter them, and/or provide data for
rendering. They compose and work together to produce the final
coordinates which are in the floatingStyles
object.
The following are included in the package:
Placement modifiers
These middleware alter the base placement coordinates.
-
offset
modifies the placement to add distance or margin between the reference and floating elements. -
inline
positions the floating element relative to individual client rects rather than the bounding box for better precision.
Visibility optimizers
These middleware alter the coordinates to ensure the floating element stays on screen optimally.
-
shift
prevents the floating element from overflowing a clipping container by shifting it to stay in view. -
flip
prevents the floating element from overflowing a clipping container by flipping it to the opposite placement to stay in view. -
autoPlacement
automatically chooses a placement for you using a “most space” strategy. -
size
resizes the floating element, for example so it will not overflow a clipping container, or to match the width of the reference element.
Data providers
These middleware only provide data and do not alter the coordinates.
-
arrow
provides data to position an inner element of the floating element such that it is centered to its reference element. -
hide
provides data to hide the floating element in applicable situations when it no longer appears attached to its reference element due to different clipping contexts.
Option reactivity
When using React state and middleware, stateful values inside functions aren’t fresh or reactive.
Specifying the dependencies as a second argument of any middleware function will keep it reactive:
This goes for any function option, including size()
’s
apply
function.
elements
default: undefined
An object of elements passed to the Hook, which is useful for
externally passing them, as an alternative to the refs
object setters.
The elements must be held in state (not plain refs) to ensure that they are reactive.
You can also do the inverse of the above, or pass both externally.
whileElementsMounted
default: undefined
A function that is called when the reference and floating elements are mounted, and returns a cleanup function called when they are unmounted.
This allows you to pass autoUpdate
whose
signature matches the option, to ensure the floating element
remains anchored to the reference element:
open
default: false
Whether the floating element is open or not, which allows you to determine if the floating element has been positioned yet.
Return value
The Hook returns the following type:
placement
The final placement of the floating element relative to the
reference element. Unlike the one passed in the options, this one
can be mutated by middleware like flip()
. This is
necessary to determine the actual side of the floating element
for styling.
strategy
The positoning strategy of the floating element.
x
The final x-coordinate of the floating element. This can be used
as an alternative to floatingStyles
to manually position the
floating element with custom CSS.
y
The final y-coordinate of the floating element. This can be used
as an alternative to floatingStyles
to manually position the
floating element with custom CSS.
middlewareData
The data provided by any middleware used.
isPositioned
Whether the floating element has been positioned yet when used
inside an Effect (not during render). Requires the open
option to be passed.
update
A function that updates the floating element’s position manually.
floatingStyles
The styles that should be applied to the floating element.
refs
The refs that should be applied to the reference and floating elements.
reference
A ref for the reference element. You can access this inside an event handler or in an Effect.
floating
A ref for the floating element. You can access this inside an event handler.
For usage in Effects, prefer using elements.floating
,
since it’s not guaranteed the floating element will be available
on the first pass.
setReference
A function that sets the reference element.
setFloating
A function that sets the floating element.
elements
The elements as set by the refs, useful for access during rendering or when needing to reactively check if the element exists.
reference
The reference element. May be virtual.
floating
The floating element. Enables you to reactively check if the
floating element exists inside Effects, notable when using
<FloatingPortal>
, as it won’t be available on the first
pass.