Skip to content

shift

A middleware that shifts the floating element along its axis to keep it in view.

Signature

ts
function shift(options?: ShiftOptions): Middleware

Options

ts
interface ShiftOptions {
  mainAxis?: boolean
  crossAxis?: boolean
  limiter?: {
    fn: (state: MiddlewareState) => Coords
    options?: any
  }
  padding?: Padding
  boundary?: Boundary
  rootBoundary?: RootBoundary
  elementContext?: ElementContext
  altBoundary?: boolean
}
OptionTypeDefaultDescription
mainAxisbooleantrueWhether to shift on the main axis
crossAxisbooleanfalseWhether to shift on the cross axis
limiterobjectundefinedFunction to limit the shifting behavior
paddingPadding0Padding from the boundary edges
boundaryBoundary'clippingAncestors'Clipping boundary
rootBoundaryRootBoundary'viewport'Root boundary (viewport or document)
elementContextElementContext'floating'Element context for overflow detection
altBoundarybooleanfalseUse alternate element for boundary

Examples

Basic Usage

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

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

With Padding

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

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

With Cross Axis

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

const context = useFloating(anchorEl, floatingEl, {
  middleware: [
    shift({ 
      mainAxis: true,
      crossAxis: true 
    })
  ]
})
</script>

See Also

  • flip - Flips the floating element to an alternative placement
  • offset - Offsets the floating element from its anchor
  • size - Controls the size of the floating element

Released under the MIT License.