color-picker-svelte
Version:
Color picker for Svelte
57 lines (56 loc) • 1.86 kB
JavaScript
export function clamp(min, max, x) {
if (x < min) {
return min;
}
else if (x > max) {
return max;
}
else {
return x;
}
}
export var Position;
(function (Position) {
Position[Position["Auto"] = 0] = "Auto";
Position[Position["Above"] = 1] = "Above";
Position[Position["Below"] = 2] = "Below";
})(Position || (Position = {}));
/** Determines if `popupElement` should be shown above `positioningContextElement` */
export function shouldShowAbove(popupElement, positioningContextElement) {
const inputBounds = positioningContextElement.getBoundingClientRect();
const spaceAbove = inputBounds.top;
const spaceBelow = window.innerHeight - (inputBounds.top + inputBounds.height);
const enoughSpaceAbove = spaceAbove > popupElement.clientHeight;
const enoughSpaceBelow = spaceBelow > popupElement.clientHeight;
return !enoughSpaceBelow && enoughSpaceAbove;
}
let isMac;
export function checkModifiers(e, options = {}) {
if (isMac === undefined) {
isMac = navigator.userAgent.indexOf('Mac') != -1;
}
const target = {
shift: options.shift || false,
alt: options.alt || false,
ctrl: (!isMac && options.cmdOrCtrl) || false,
meta: (isMac && options.cmdOrCtrl) || false,
};
const pressed = {
shift: !!e.shiftKey,
alt: !!e.altKey,
ctrl: !!e.ctrlKey,
meta: !!e.metaKey,
};
return (pressed.shift === target.shift &&
pressed.alt === target.alt &&
pressed.ctrl === target.ctrl &&
pressed.meta === target.meta);
}
export function checkShortcut(e, key, options = {}) {
if (e.key.toUpperCase() !== key.toUpperCase())
return false;
return checkModifiers(e, options);
}
export function checkMouseShortcut(e, options = {}) {
return checkModifiers(e, options);
}