Appearance
size
size provides the available width and height around the floating element so you can resize it to fit the current boundary.
Type
tsfunction size(options?: SizeOptions): Middleware; interface SizeOptions { apply?: (state: SizeState) => void; padding?: Padding; boundary?: Boundary; rootBoundary?: RootBoundary; elementContext?: ElementContext; altBoundary?: boolean; } interface SizeState { availableWidth: number; availableHeight: number; rects: MiddlewareState["rects"]; elements: { floating: HTMLElement; reference: Element | VirtualElement; }; }Details
sizedoes not resize anything on its own. Use theapplycallback to write styles such asmaxWidth,maxHeight, or a matched reference width.This middleware is useful for menus, popovers, and other surfaces that need to stay inside the viewport without overflowing.
Example
vue
<script setup lang="ts">
import { ref } from "vue";
import { size, useFloatingContext, usePosition } from "v-float";
const anchorEl = ref<HTMLElement | null>(null);
const floatingEl = ref<HTMLElement | null>(null);
const open = ref(true);
const context = useFloatingContext({ refs: { anchorEl, floatingEl }, state: { open } });
const { styles } = usePosition(context, {
middleware: {
custom: [
size({
apply({ availableWidth, availableHeight, elements }) {
Object.assign(elements.floating.style, {
maxWidth: `${availableWidth}px`,
maxHeight: `${availableHeight}px`,
});
},
}),
],
},
});
</script>
<template>
<button ref="anchorEl">Anchor</button>
<div v-if="context.state.open.value" ref="floatingEl" :style="styles">Floating content</div>
</template>