2023-05-18 12:42:19 -05:00
|
|
|
import { detectOverflow, Modifier, ModifierArguments } from '@popperjs/core';
|
|
|
|
|
|
|
|
const MODAL_MARGIN = 20;
|
|
|
|
const FLIP_THRESHOLD = 200;
|
|
|
|
|
|
|
|
export const maxSize: Modifier<'maxSize', {}> = {
|
|
|
|
name: 'maxSize',
|
|
|
|
enabled: true,
|
|
|
|
phase: 'main',
|
|
|
|
requires: ['offset', 'preventOverflow', 'flip'],
|
|
|
|
fn({ state, name, options }: ModifierArguments<{}>) {
|
|
|
|
const overflow = detectOverflow(state, options);
|
|
|
|
const { x, y } = state.modifiersData.preventOverflow || { x: 0, y: 0 };
|
2023-07-13 10:10:38 -05:00
|
|
|
const { width: contentW, height: contentH } = state.rects.popper;
|
|
|
|
const { width: triggerW } = state.rects.reference;
|
2023-05-18 12:42:19 -05:00
|
|
|
const [basePlacement] = state.placement.split('-');
|
|
|
|
|
|
|
|
const widthProp = basePlacement === 'left' ? 'left' : 'right';
|
|
|
|
const heightProp = basePlacement === 'top' ? 'top' : 'bottom';
|
|
|
|
|
|
|
|
state.modifiersData[name] = {
|
2023-07-13 10:10:38 -05:00
|
|
|
maxWidth: contentW - overflow[widthProp] - x,
|
|
|
|
maxHeight: contentH - overflow[heightProp] - y,
|
|
|
|
minWidth: triggerW,
|
2023-05-18 12:42:19 -05:00
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const applyMaxSize: Modifier<'applyMaxSize', {}> = {
|
|
|
|
name: 'applyMaxSize',
|
|
|
|
enabled: true,
|
|
|
|
phase: 'beforeWrite',
|
|
|
|
requires: ['maxSize'],
|
|
|
|
fn({ state }: ModifierArguments<{}>) {
|
2023-07-13 10:10:38 -05:00
|
|
|
const { maxHeight, maxWidth, minWidth } = state.modifiersData.maxSize;
|
2023-06-23 03:26:18 -05:00
|
|
|
|
2023-07-13 10:10:38 -05:00
|
|
|
state.styles.popper.maxHeight ??= `${maxHeight - MODAL_MARGIN}px`;
|
|
|
|
state.styles.popper.minHeight ??= `${FLIP_THRESHOLD}px`;
|
|
|
|
state.styles.popper.maxWidth ??= maxWidth;
|
|
|
|
state.styles.popper.minWidth ??= minWidth;
|
2023-05-18 12:42:19 -05:00
|
|
|
},
|
|
|
|
};
|