UNPKG

@aotearoan/neon

Version:

Neon is a lightweight design library of Vue 3 components with minimal dependencies.

62 lines (61 loc) 2.32 kB
import { NeonDropdownPlacementObject as s } from "../models/NeonDropdownPlacementObject.es.js"; import { NeonPosition as m } from "../enums/NeonPosition.es.js"; import { NeonPlacementUtils as l } from "./NeonPlacementUtils.es.js"; class n { /** * Calculate the placement of an element relative to a trigger element given a desired placement. This method will * determine if there is enough space to place the element in the desired location & if not it will then determine the * next best place to position the element. * * @param triggerElement The trigger element, e.g. the button to trigger opening a select. * @param contentElement The content element, i.e. the element for which to calculate the placement. * @param placement The desired placement relative to the trigger element. * @param placementContainer An optional containing element to use for calculating the placement instead * of the screen dimensions. This can be useful if the contentElement must be contained by e.g. A modal div. * * @returns The calculated placement of the content element. */ static calculatePlacement(o, a, i, t) { const e = s.toNeonDropdownPlacementObject(i), { maxWidth: f, maxHeight: r } = l.calculateBounds(t), c = l.calculateAvailableSpace( o, f, r, t ), h = { top: c.bottom + o.offsetHeight, bottom: c.top + o.offsetHeight, left: c.right + o.offsetWidth, right: c.left + o.offsetWidth }; return n.findPlacement( a, c, h, e, s.flipMinor(e), s.flipMajor(e), s.flip(e) ); } static findPlacement(o, a, i, ...t) { var f; const e = t.findIndex( (r) => n.validPlacement(o, a, i, r) ); return ((f = t[e]) == null ? void 0 : f.placement) || t[0].placement; } static validPlacement(o, a, i, t) { switch (t.majorPlacement) { case m.Top: case m.Bottom: return o.offsetHeight <= a[t.majorPlacement] && !!t.minorPlacement && o.offsetWidth <= i[t.minorPlacement]; case m.Left: case m.Right: return o.offsetWidth <= a[t.majorPlacement] && !!t.minorPlacement && o.offsetHeight <= i[t.minorPlacement]; } } } export { n as NeonDropdownPlacementUtils }; //# sourceMappingURL=NeonDropdownPlacementUtils.es.js.map