Skip to content

size

A middleware that provides available size data and allows resizing the floating element to fit within boundaries.

Signature

ts
function size(options?: SizeOptions): Middleware

Options

ts
interface SizeOptions {
  apply?: (state: SizeState) => void
  padding?: Padding
  boundary?: Boundary
  rootBoundary?: RootBoundary
  elementContext?: ElementContext
  altBoundary?: boolean
}

interface SizeState extends MiddlewareState {
  availableWidth: number
  availableHeight: number
  elements: {
    floating: HTMLElement
    reference: Element | VirtualElement
  }
}
OptionTypeDefaultDescription
apply(state: SizeState) => voidundefinedCallback to apply size constraints
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, size } from 'v-float'

const context = useFloating(anchorEl, floatingEl, {
  middleware: [
    size({
      apply({ availableWidth, availableHeight, elements }) {
        Object.assign(elements.floating.style, {
          maxWidth: `${availableWidth}px`,
          maxHeight: `${availableHeight}px`
        })
      }
    })
  ]
})
</script>

Match Reference Width

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

const context = useFloating(anchorEl, floatingEl, {
  middleware: [
    size({
      apply({ rects, elements }) {
        Object.assign(elements.floating.style, {
          width: `${rects.reference.width}px`
        })
      }
    })
  ]
})
</script>

See Also

  • shift - Shifts the floating element to keep it in view
  • flip - Flips the floating element to an alternative placement

Released under the MIT License.