UNPKG

@pdf-viewer/react

Version:

The PDF Viewer component for React and Next.js

141 lines (140 loc) 5.43 kB
import * as i from "react"; import { u as p, a as R, P as w, c as h, d as _, b as U } from "./index-1cb41342.js"; import { jsx as T } from "react/jsx-runtime"; function z(t, e = globalThis == null ? void 0 : globalThis.document) { const s = p(t); i.useEffect(() => { const n = (r) => { r.key === "Escape" && s(r); }; return e.addEventListener("keydown", n, { capture: !0 }), () => e.removeEventListener("keydown", n, { capture: !0 }); }, [s, e]); } var H = "DismissableLayer", b = "dismissableLayer.update", M = "dismissableLayer.pointerDownOutside", K = "dismissableLayer.focusOutside", O, S = i.createContext({ layers: /* @__PURE__ */ new Set(), layersWithOutsidePointerEventsDisabled: /* @__PURE__ */ new Set(), branches: /* @__PURE__ */ new Set() }), j = i.forwardRef( (t, e) => { const { disableOutsidePointerEvents: s = !1, onEscapeKeyDown: n, onPointerDownOutside: r, onFocusOutside: o, onInteractOutside: l, onDismiss: d, ...v } = t, u = i.useContext(S), [c, B] = i.useState(null), f = (c == null ? void 0 : c.ownerDocument) ?? (globalThis == null ? void 0 : globalThis.document), [, F] = i.useState({}), I = R(e, (a) => B(a)), m = Array.from(u.layers), [W] = [...u.layersWithOutsidePointerEventsDisabled].slice(-1), k = m.indexOf(W), P = c ? m.indexOf(c) : -1, A = u.layersWithOutsidePointerEventsDisabled.size > 0, D = P >= k, N = $((a) => { const E = a.target, C = [...u.branches].some((y) => y.contains(E)); !D || C || (r == null || r(a), l == null || l(a), a.defaultPrevented || d == null || d()); }, f), L = q((a) => { const E = a.target; [...u.branches].some((y) => y.contains(E)) || (o == null || o(a), l == null || l(a), a.defaultPrevented || d == null || d()); }, f); return z((a) => { P === u.layers.size - 1 && (n == null || n(a), !a.defaultPrevented && d && (a.preventDefault(), d())); }, f), i.useEffect(() => { if (c) return s && (u.layersWithOutsidePointerEventsDisabled.size === 0 && (O = f.body.style.pointerEvents, f.body.style.pointerEvents = "none"), u.layersWithOutsidePointerEventsDisabled.add(c)), u.layers.add(c), g(), () => { s && u.layersWithOutsidePointerEventsDisabled.size === 1 && (f.body.style.pointerEvents = O); }; }, [c, f, s, u]), i.useEffect(() => () => { c && (u.layers.delete(c), u.layersWithOutsidePointerEventsDisabled.delete(c), g()); }, [c, u]), i.useEffect(() => { const a = () => F({}); return document.addEventListener(b, a), () => document.removeEventListener(b, a); }, []), /* @__PURE__ */ T( w.div, { ...v, ref: I, style: { pointerEvents: A ? D ? "auto" : "none" : void 0, ...t.style }, onFocusCapture: h(t.onFocusCapture, L.onFocusCapture), onBlurCapture: h(t.onBlurCapture, L.onBlurCapture), onPointerDownCapture: h( t.onPointerDownCapture, N.onPointerDownCapture ) } ); } ); j.displayName = H; var X = "DismissableLayerBranch", Y = i.forwardRef((t, e) => { const s = i.useContext(S), n = i.useRef(null), r = R(e, n); return i.useEffect(() => { const o = n.current; if (o) return s.branches.add(o), () => { s.branches.delete(o); }; }, [s.branches]), /* @__PURE__ */ T(w.div, { ...t, ref: r }); }); Y.displayName = X; function $(t, e = globalThis == null ? void 0 : globalThis.document) { const s = p(t), n = i.useRef(!1), r = i.useRef(() => { }); return i.useEffect(() => { const o = (d) => { if (d.target && !n.current) { let v = function() { x( M, s, u, { discrete: !0 } ); }; const u = { originalEvent: d }; d.pointerType === "touch" ? (e.removeEventListener("click", r.current), r.current = v, e.addEventListener("click", r.current, { once: !0 })) : v(); } else e.removeEventListener("click", r.current); n.current = !1; }, l = window.setTimeout(() => { e.addEventListener("pointerdown", o); }, 0); return () => { window.clearTimeout(l), e.removeEventListener("pointerdown", o), e.removeEventListener("click", r.current); }; }, [e, s]), { // ensures we check React component tree (not just DOM tree) onPointerDownCapture: () => n.current = !0 }; } function q(t, e = globalThis == null ? void 0 : globalThis.document) { const s = p(t), n = i.useRef(!1); return i.useEffect(() => { const r = (o) => { o.target && !n.current && x(K, s, { originalEvent: o }, { discrete: !1 }); }; return e.addEventListener("focusin", r), () => e.removeEventListener("focusin", r); }, [e, s]), { onFocusCapture: () => n.current = !0, onBlurCapture: () => n.current = !1 }; } function g() { const t = new CustomEvent(b); document.dispatchEvent(t); } function x(t, e, s, { discrete: n }) { const r = s.originalEvent.target, o = new CustomEvent(t, { bubbles: !1, cancelable: !0, detail: s }); e && r.addEventListener(t, e, { once: !0 }), n ? _(r, o) : r.dispatchEvent(o); } var G = i["useId".toString()] || (() => { }), J = 0; function Z(t) { const [e, s] = i.useState(G()); return U(() => { t || s((n) => n ?? String(J++)); }, [t]), t || (e ? `radix-${e}` : ""); } export { j as D, Z as u };