useFloating
The main Hook of the library that acts as a controller for all other Hooks and components.
Usage
The useFloating
Svelte hook acts as a controller for all other Floating
UI Svelte features. It handles positioning your floating elements (tooltips, popovers, etc.) relative
to an anchored element. Automatically calculates the best placement and updates it as needed, providing
access to properties for position and style.
Note
Destructured variables are not supported as this would break reactivity.
Options
open | Represents the open/close state of the floating element. |
onOpenChange | Event handler that can be invoked whenever the open state changes. |
placement | Where to place the floating element relative to its reference element. |
strategy | The type of CSS position property to use. |
middleware | Supports all Floating UI middleware |
transform | Whether to use transform instead of top and left styles to position the floating element. |
elements | The reference and floating elements. |
whileElementsMounted | Callback to handle mounting/unmounting of the elements. |
nodeId | A unique node ID for the floating element when using a `FloatingTree`. |
Returns
x | The x-coord of the floating element. |
y | The y-coord of the floating element. |
placement | The stateful placement, which can be different from the initial placement passed as options. |
strategy | The stateful strategy, which can be different from the initial strategy passed as options. |
middlewareData | Additional data from middleware. |
isPositioned | The boolean that let you know if the floating element has been positioned. |
floatingStyles | CSS styles to apply to the floating element to position it. |
elements | The reference and floating elements. |
update | The function to update floating position manually. |
context | Context object containing internal logic to alter the behavior of the floating element. |
Compare
Compare to Floating UI React.