Current section

Files

Jump to
corex priv static chunk-EAMC7PNF.mjs
Raw

priv/static/chunk-EAMC7PNF.mjs

import {
getDocument,
getEventTarget,
getWindow,
isMac,
isVirtualClick
} from "./chunk-GFGFZBBD.mjs";
// ../node_modules/.pnpm/@zag-js+focus-visible@1.33.1/node_modules/@zag-js/focus-visible/dist/index.mjs
function isValidKey(e) {
return !(e.metaKey || !isMac() && e.altKey || e.ctrlKey || e.key === "Control" || e.key === "Shift" || e.key === "Meta");
}
var nonTextInputTypes = /* @__PURE__ */ new Set(["checkbox", "radio", "range", "color", "file", "image", "button", "submit", "reset"]);
function isKeyboardFocusEvent(isTextInput, modality, e) {
const target = e ? getEventTarget(e) : null;
const win = getWindow(target);
isTextInput = isTextInput || target instanceof win.HTMLInputElement && !nonTextInputTypes.has(target?.type) || target instanceof win.HTMLTextAreaElement || target instanceof win.HTMLElement && target.isContentEditable;
return !(isTextInput && modality === "keyboard" && e instanceof win.KeyboardEvent && !Reflect.has(FOCUS_VISIBLE_INPUT_KEYS, e.key));
}
var currentModality = null;
var changeHandlers = /* @__PURE__ */ new Set();
var listenerMap = /* @__PURE__ */ new Map();
var hasEventBeforeFocus = false;
var hasBlurredWindowRecently = false;
var FOCUS_VISIBLE_INPUT_KEYS = {
Tab: true,
Escape: true
};
function triggerChangeHandlers(modality, e) {
for (let handler of changeHandlers) {
handler(modality, e);
}
}
function handleKeyboardEvent(e) {
hasEventBeforeFocus = true;
if (isValidKey(e)) {
currentModality = "keyboard";
triggerChangeHandlers("keyboard", e);
}
}
function handlePointerEvent(e) {
currentModality = "pointer";
if (e.type === "mousedown" || e.type === "pointerdown") {
hasEventBeforeFocus = true;
triggerChangeHandlers("pointer", e);
}
}
function handleClickEvent(e) {
if (isVirtualClick(e)) {
hasEventBeforeFocus = true;
currentModality = "virtual";
}
}
function handleFocusEvent(e) {
const target = getEventTarget(e);
if (target === getWindow(target) || target === getDocument(target)) {
return;
}
if (!hasEventBeforeFocus && !hasBlurredWindowRecently) {
currentModality = "virtual";
triggerChangeHandlers("virtual", e);
}
hasEventBeforeFocus = false;
hasBlurredWindowRecently = false;
}
function handleWindowBlur() {
hasEventBeforeFocus = false;
hasBlurredWindowRecently = true;
}
function setupGlobalFocusEvents(root) {
if (typeof window === "undefined" || listenerMap.get(getWindow(root))) {
return;
}
const win = getWindow(root);
const doc = getDocument(root);
let focus = win.HTMLElement.prototype.focus;
function patchedFocus() {
currentModality = "virtual";
triggerChangeHandlers("virtual", null);
hasEventBeforeFocus = true;
focus.apply(this, arguments);
}
try {
Object.defineProperty(win.HTMLElement.prototype, "focus", {
configurable: true,
value: patchedFocus
});
} catch {
}
doc.addEventListener("keydown", handleKeyboardEvent, true);
doc.addEventListener("keyup", handleKeyboardEvent, true);
doc.addEventListener("click", handleClickEvent, true);
win.addEventListener("focus", handleFocusEvent, true);
win.addEventListener("blur", handleWindowBlur, false);
if (typeof win.PointerEvent !== "undefined") {
doc.addEventListener("pointerdown", handlePointerEvent, true);
doc.addEventListener("pointermove", handlePointerEvent, true);
doc.addEventListener("pointerup", handlePointerEvent, true);
} else {
doc.addEventListener("mousedown", handlePointerEvent, true);
doc.addEventListener("mousemove", handlePointerEvent, true);
doc.addEventListener("mouseup", handlePointerEvent, true);
}
win.addEventListener(
"beforeunload",
() => {
tearDownWindowFocusTracking(root);
},
{ once: true }
);
listenerMap.set(win, { focus });
}
var tearDownWindowFocusTracking = (root, loadListener) => {
const win = getWindow(root);
const doc = getDocument(root);
const listenerData = listenerMap.get(win);
if (!listenerData) {
return;
}
try {
Object.defineProperty(win.HTMLElement.prototype, "focus", {
configurable: true,
value: listenerData.focus
});
} catch {
}
doc.removeEventListener("keydown", handleKeyboardEvent, true);
doc.removeEventListener("keyup", handleKeyboardEvent, true);
doc.removeEventListener("click", handleClickEvent, true);
win.removeEventListener("focus", handleFocusEvent, true);
win.removeEventListener("blur", handleWindowBlur, false);
if (typeof win.PointerEvent !== "undefined") {
doc.removeEventListener("pointerdown", handlePointerEvent, true);
doc.removeEventListener("pointermove", handlePointerEvent, true);
doc.removeEventListener("pointerup", handlePointerEvent, true);
} else {
doc.removeEventListener("mousedown", handlePointerEvent, true);
doc.removeEventListener("mousemove", handlePointerEvent, true);
doc.removeEventListener("mouseup", handlePointerEvent, true);
}
listenerMap.delete(win);
};
function isFocusVisible() {
return currentModality === "keyboard";
}
function trackFocusVisible(props = {}) {
const { isTextInput, autoFocus, onChange, root } = props;
setupGlobalFocusEvents(root);
onChange?.({ isFocusVisible: autoFocus || isFocusVisible(), modality: currentModality });
const handler = (modality, e) => {
if (!isKeyboardFocusEvent(!!isTextInput, modality, e)) return;
onChange?.({ isFocusVisible: isFocusVisible(), modality });
};
changeHandlers.add(handler);
return () => {
changeHandlers.delete(handler);
};
}
export {
isFocusVisible,
trackFocusVisible
};