@aotearoan/neon
Version:
Neon is a lightweight design library of Vue 3 components with minimal dependencies.
59 lines (58 loc) • 2.06 kB
JavaScript
import { NeonPosition as t } from "../enums/NeonPosition.es.js";
import { NeonPlacementUtils as s } from "./NeonPlacementUtils.es.js";
class o {
/**
* 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(a, c, e, r) {
const { maxWidth: i, maxHeight: f } = s.calculateBounds(r), n = s.calculateAvailableSpace(
a,
i,
f,
r
), u = o.calculatePlacementOrder(e);
return o.findPlacement(c, n, u);
}
static calculatePlacementOrder(a) {
switch (a) {
case t.Top:
return [t.Top, t.Bottom, t.Left, t.Right];
case t.Bottom:
return [t.Bottom, t.Top, t.Left, t.Right];
case t.Left:
return [t.Left, t.Right, t.Top, t.Bottom];
case t.Right:
return [t.Right, t.Left, t.Top, t.Bottom];
}
}
static findPlacement(a, c, e) {
const r = e.findIndex(
(i) => o.validPlacement(a, c, i)
);
return e[r] || e[0];
}
static validPlacement(a, c, e) {
switch (e) {
case t.Top:
case t.Bottom:
return a.offsetHeight <= c[e];
case t.Left:
case t.Right:
return a.offsetWidth <= c[e];
}
}
}
export {
o as NeonTooltipPlacementUtils
};
//# sourceMappingURL=NeonTooltipPlacementUtils.es.js.map