Skip to content

autoPlacement

A middleware that automatically chooses the best placement from available options to keep the floating element in view.

Signature

ts
function autoPlacement(options?: AutoPlacementOptions): Middleware

Options

ts
interface AutoPlacementOptions {
  crossAxis?: boolean
  alignment?: 'start' | 'end' | null
  autoAlignment?: boolean
  allowedPlacements?: Array<Placement>
  boundary?: Boundary
  rootBoundary?: RootBoundary
  elementContext?: ElementContext
  altBoundary?: boolean
  padding?: Padding
}
OptionTypeDefaultDescription
crossAxisbooleanfalseConsider placements on the cross axis
alignment'start' | 'end' | nullundefinedLock alignment to specific value
autoAlignmentbooleanfalseAutomatically infer alignment from preferred side
allowedPlacementsArray<Placement>All placementsRestrict which placements can be chosen
boundaryBoundary'clippingAncestors'Clipping boundary
rootBoundaryRootBoundary'viewport'Root boundary (viewport or document)
elementContextElementContext'floating'Element context for overflow detection
altBoundarybooleanfalseUse alternate element for boundary
paddingPadding0Padding from the boundary edges

Examples

Basic Usage

vue
<script setup>
import { useFloating, autoPlacement } from 'v-float'

const context = useFloating(anchorEl, floatingEl, {
  middleware: [autoPlacement()]
})
</script>

Allowed Placements

vue
<script setup>
import { useFloating, autoPlacement } from 'v-float'

const context = useFloating(anchorEl, floatingEl, {
  middleware: [
    autoPlacement({
      allowedPlacements: ['top', 'bottom']
    })
  ]
})
</script>

With Alignment

vue
<script setup>
import { useFloating, autoPlacement } from 'v-float'

const context = useFloating(anchorEl, floatingEl, {
  middleware: [
    autoPlacement({
      alignment: 'start',
      crossAxis: true
    })
  ]
})
</script>

See Also

  • flip - Alternative middleware for placement fallbacks with a preferred initial placement
  • shift - Shifts element to keep it in view

Released under the MIT License.