@3mo/select-field
Version:
A select field web component
28 lines (27 loc) • 972 B
JavaScript
import { Popover } from '@3mo/popover';
/**
* A floating-ui middleware that closes the popover when it is out of the viewport.
*/
export function closeWhenOutOfViewport(options) {
return {
name: 'closeWhenOutOfViewport',
options,
async fn(state) {
const { detectOverflow } = await import('@floating-ui/dom');
const overflow = await detectOverflow(state);
const { height, width } = state.rects.floating;
const popover = state.elements.floating;
if (popover instanceof Popover === false) {
return {};
}
const padding = options?.padding ?? 0;
if (overflow.bottom > height - padding ||
overflow.top > height - padding ||
overflow.left > width - padding ||
overflow.right > width - padding) {
popover.open = false;
}
return {};
}
};
}