reka-ui
Version:
Vue port for Radix UI Primitives.
74 lines (70 loc) • 2.32 kB
JavaScript
;
const shared_getActiveElement = require('../shared/getActiveElement.cjs');
const ITEM_SELECT = "menu.itemSelect";
const SELECTION_KEYS = ["Enter", " "];
const FIRST_KEYS = ["ArrowDown", "PageUp", "Home"];
const LAST_KEYS = ["ArrowUp", "PageDown", "End"];
const FIRST_LAST_KEYS = [...FIRST_KEYS, ...LAST_KEYS];
const SUB_OPEN_KEYS = {
ltr: [...SELECTION_KEYS, "ArrowRight"],
rtl: [...SELECTION_KEYS, "ArrowLeft"]
};
const SUB_CLOSE_KEYS = {
ltr: ["ArrowLeft"],
rtl: ["ArrowRight"]
};
function getOpenState(open) {
return open ? "open" : "closed";
}
function isIndeterminate(checked) {
return checked === "indeterminate";
}
function getCheckedState(checked) {
return isIndeterminate(checked) ? "indeterminate" : checked ? "checked" : "unchecked";
}
function focusFirst(candidates) {
const PREVIOUSLY_FOCUSED_ELEMENT = shared_getActiveElement.getActiveElement();
for (const candidate of candidates) {
if (candidate === PREVIOUSLY_FOCUSED_ELEMENT)
return;
candidate.focus();
if (shared_getActiveElement.getActiveElement() !== PREVIOUSLY_FOCUSED_ELEMENT)
return;
}
}
function isPointInPolygon(point, polygon) {
const { x, y } = point;
let inside = false;
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
const xi = polygon[i].x;
const yi = polygon[i].y;
const xj = polygon[j].x;
const yj = polygon[j].y;
const intersect = yi > y !== yj > y && x < (xj - xi) * (y - yi) / (yj - yi) + xi;
if (intersect)
inside = !inside;
}
return inside;
}
function isPointerInGraceArea(event, area) {
if (!area)
return false;
const cursorPos = { x: event.clientX, y: event.clientY };
return isPointInPolygon(cursorPos, area);
}
function isMouseEvent(event) {
return event.pointerType === "mouse";
}
exports.FIRST_LAST_KEYS = FIRST_LAST_KEYS;
exports.ITEM_SELECT = ITEM_SELECT;
exports.LAST_KEYS = LAST_KEYS;
exports.SELECTION_KEYS = SELECTION_KEYS;
exports.SUB_CLOSE_KEYS = SUB_CLOSE_KEYS;
exports.SUB_OPEN_KEYS = SUB_OPEN_KEYS;
exports.focusFirst = focusFirst;
exports.getCheckedState = getCheckedState;
exports.getOpenState = getOpenState;
exports.isIndeterminate = isIndeterminate;
exports.isMouseEvent = isMouseEvent;
exports.isPointerInGraceArea = isPointerInGraceArea;
//# sourceMappingURL=utils.cjs.map