Floating UI
This guide switches VPick's dropdown positioning to Floating UI. Use it when the rest of your app already positions its popovers, menus and tooltips with Floating UI, and you want dropdowns to follow the same rules.
Demo
Open both. The second lines up with the trigger's end edge.
Approach
- You pass the Floating UI library as a prop. There is no function to write.
- Floating UI does the positioning:
computePositionwithoffset,flip,shiftandhideworks out where the dropdown goes, andautoUpdatekeeps it there. - VPick renders the dropdown into the container it belongs to, such as a scroll container or a modal, which Floating UI does not do.
- Floating UI is not bundled into VPick. VPick uses the copy you pass.
Anatomy
vue
<script setup>
import * as FloatingUI from "@floating-ui/dom"
</script>
<template>
<VPick :options="options" :floating-ui="FloatingUI" />
</template>Setup
Install
bash
npm install @floating-ui/domVPick works with @floating-ui/dom 1.4 or newer.
Pass the library
Import the whole library once and pass it to each VPick that should use it:
js
import * as FloatingUI from "@floating-ui/dom"vue
<VPick :options="options" :floating-ui="FloatingUI" />What Floating UI does
- Placement.
alignbecomes the placement, lining up with the trigger's start or end edge and following the trigger's writing direction.--vpick-listbox-offsetbecomes theoffsetmiddleware. - Staying on screen.
flipopens the dropdown above when there is more room there, andshiftkeeps it 8px inside the visible edges. When a dropdown is too wide for the room beside its trigger,fliplines it up with the trigger's other edge instead. - Staying in place.
autoUpdaterepositions the dropdown on scroll and resize, when the trigger or the dropdown changes size, and when the trigger moves on its own, such as when a message appears above it. - Hiding. With
hideWhenDetachedon,hidehides the dropdown while its trigger is cut off by any container around it, including one withoverflow: hidden.
What VPick still does
strategyandteleportTodecide where the dropdown is rendered, so inside a scrolling modal it stays in the modal, above the modal's backdrop.- Keyboard support, search, selection, scroll lock and theming are unchanged.
When not to use it
VPick's built-in positioning needs no setup and handles dropdowns inside scroll containers and modals. Reach for Floating UI when it already positions the rest of your app, or when a dropdown needs to follow content that moves while it is open.