@adyen/adyen-platform-experience-web
Version:

96 lines (95 loc) • 3.7 kB
JavaScript
import { useRef as a, useMemo as g, useCallback as c } from "../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import { InteractionKeyCode as r } from "../../components/types.js";
import k, { focusIsWithin as i, isFocusable as p, getDeepActiveElement as D } from "../../primitives/dom/tabbableRoot/tabbable.js";
import H from "../useReflex.js";
import { isUndefined as I } from "../../utils/value/is.js";
const w = (x, u) => {
const l = a(!1), f = a(null), m = a(!1), P = a(null), E = a(1), A = a(!1), n = g(k, []), d = c((e) => {
const t = f.current;
if (t instanceof HTMLElement && i(e, t)) {
t.focus();
return;
}
e instanceof HTMLElement && e.focus();
}, []), T = g(() => {
let e = null, t;
return (b) => {
I(t) || cancelAnimationFrame(t);
const o = b.composedPath();
let s = o[0], C = 0;
for (; s && s !== b.currentTarget; ) {
if (s instanceof Element && p(s)) {
e = s, t = requestAnimationFrame(() => {
t = requestAnimationFrame(() => {
f.current !== e && e instanceof HTMLElement && (f.current = e)?.focus(), e = null, t = void 0;
});
});
return;
}
s = o[++C];
}
};
}, []), F = c(
(e) => {
n.current = f.current = e.composedPath()[0] || e.target;
},
[n]
), L = c(
(e) => {
const t = n.root;
if (!(t instanceof Element)) return;
const b = e.composedPath()[0] || e.target;
if (!i(t, b) && i(t, e.relatedTarget) && m.current && P.current === r.TAB) {
const o = n.tabbables;
if (o.length) {
const s = E.current === -1 ? o.length - 1 : 0;
o[s]?.focus();
} else
d(t);
}
},
[d, n]
), R = c(
(e) => {
n.tabbables.includes(e.relatedTarget) || i(e.currentTarget, e.relatedTarget) || m.current || (l.current = !0, requestAnimationFrame(() => {
l.current && u(l.current = !1);
}));
},
[u, n]
), h = g(() => {
let e;
return (t) => {
switch (P.current = t.code, t.code) {
case r.ARROW_DOWN:
case r.ARROW_LEFT:
case r.ARROW_RIGHT:
case r.ARROW_UP:
case r.END:
case r.ESCAPE:
case r.HOME:
case r.PAGE_DOWN:
case r.PAGE_UP:
case r.TAB:
cancelAnimationFrame(e), e = requestAnimationFrame(() => {
e = requestAnimationFrame(() => {
m.current = !1, e = void 0;
});
}), m.current = !0;
break;
}
t.code === r.TAB ? (t.preventDefault(), E.current = t.shiftKey ? -1 : 1, n.tabbables.length ? n.current = E.current : n.root instanceof Element && d(n.root)) : t.code === r.ESCAPE && u(!0);
};
}, [d, u, n]);
return H(
c(
(e, t) => {
t instanceof Element && (t.removeEventListener("keydown", h, !0), t.removeEventListener("focusin", F, !0), t.removeEventListener("focusout", R, !1), t.removeEventListener("click", T, !0), document.removeEventListener("focusin", L, !0), A.current && t instanceof HTMLElement && (t.removeAttribute("tabindex"), A.current = !1)), e instanceof Element ? (e.addEventListener("keydown", h, !0), e.addEventListener("focusin", F, !0), e.addEventListener("focusout", R, !1), e.addEventListener("click", T, !0), document.addEventListener("focusin", L, !0), l.current = !1, n.root = e, e instanceof HTMLElement && !e.hasAttribute("tabindex") && (e.setAttribute("tabindex", "-1"), A.current = !0), i(e, D()) || e instanceof HTMLElement && e.focus()) : n.root = null;
},
[T, L, F, R, h, n]
),
x
);
};
export {
w as default
};