UNPKG

@pdf-viewer/react

Version:

A react-pdf-viewer component for React and Next.js. Suitable for react-pdf document.

570 lines (569 loc) 20 kB
import { jsx as d, jsxs as Y } from "react/jsx-runtime"; import * as a from "react"; import { P as S, c as ae, u as k, d as Fe, f as oe, e as We, a as E, b as Be, g as Ge } from "../../index-35c7f4a2.js"; import { u as Xe, a as Ve, o as Ue, s as ze, f as qe, b as Ze, c as Ke, h as Je, d as Qe, l as et, e as tt, D as rt } from "../../floating-ui.react-dom-6b2fe0df.js"; import { withRef as ot } from "../../utils/withRef.js"; import "react-dom"; var nt = "Arrow", se = a.forwardRef((e, r) => { const { children: t, width: o = 10, height: n = 5, ...i } = e; return /* @__PURE__ */ d( S.svg, { ...i, ref: r, width: o, height: n, viewBox: "0 0 30 10", preserveAspectRatio: "none", children: e.asChild ? t : /* @__PURE__ */ d("polygon", { points: "0,0 30,0 15,10" }) } ); }); se.displayName = nt; var at = se, B = "Popper", [ie, le] = ae(B), [st, ce] = ie(B), de = (e) => { const { __scopePopper: r, children: t } = e, [o, n] = a.useState(null); return /* @__PURE__ */ d(st, { scope: r, anchor: o, onAnchorChange: n, children: t }); }; de.displayName = B; var pe = "PopperAnchor", ue = a.forwardRef( (e, r) => { const { __scopePopper: t, virtualRef: o, ...n } = e, i = ce(pe, t), l = a.useRef(null), h = k(r, l), s = a.useRef(null); return a.useEffect(() => { const p = s.current; s.current = (o == null ? void 0 : o.current) || l.current, p !== s.current && i.onAnchorChange(s.current); }), o ? null : /* @__PURE__ */ d(S.div, { ...n, ref: h }); } ); ue.displayName = pe; var G = "PopperContent", [it, lt] = ie(G), fe = a.forwardRef( (e, r) => { var Z, K, J, Q, ee, te; const { __scopePopper: t, side: o = "bottom", sideOffset: n = 0, align: i = "center", alignOffset: l = 0, arrowPadding: h = 0, avoidCollisions: s = !0, collisionBoundary: p = [], collisionPadding: c = 0, sticky: f = "partial", hideWhenDetached: g = !1, updatePositionStrategy: u = "optimized", onPlaced: v, ...y } = e, m = ce(G, t), [w, x] = a.useState(null), T = k(r, (_) => x(_)), [C, b] = a.useState(null), P = Fe(C), N = (P == null ? void 0 : P.width) ?? 0, V = (P == null ? void 0 : P.height) ?? 0, Ee = o + (i !== "center" ? "-" + i : ""), Re = typeof c == "number" ? c : { top: 0, right: 0, bottom: 0, left: 0, ...c }, U = Array.isArray(p) ? p : [p], _e = U.length > 0, D = { padding: Re, boundary: U.filter(dt), // with `strategy: 'fixed'`, this is the only way to get it to respect boundaries altBoundary: _e }, { refs: Oe, floatingStyles: z, placement: Se, isPositioned: H, middlewareData: A } = Xe({ // default to `fixed` strategy so users don't have to pick and we also avoid focus scroll issues strategy: "fixed", placement: Ee, whileElementsMounted: (..._) => Ve(..._, { animationFrame: u === "always" }), elements: { reference: m.anchor }, middleware: [ Ue({ mainAxis: n + V, alignmentAxis: l }), s && ze({ mainAxis: !0, crossAxis: !1, limiter: f === "partial" ? et() : void 0, ...D }), s && qe({ ...D }), Ze({ ...D, apply: ({ elements: _, rects: re, availableWidth: Me, availableHeight: $e }) => { const { width: je, height: Ye } = re.reference, L = _.floating.style; L.setProperty("--radix-popper-available-width", `${Me}px`), L.setProperty("--radix-popper-available-height", `${$e}px`), L.setProperty("--radix-popper-anchor-width", `${je}px`), L.setProperty("--radix-popper-anchor-height", `${Ye}px`); } }), C && Ke({ element: C, padding: h }), pt({ arrowWidth: N, arrowHeight: V }), g && Je({ strategy: "referenceHidden", ...D }) ] }), [q, Ne] = ge(Se), I = Qe(v); oe(() => { H && (I == null || I()); }, [H, I]); const De = (Z = A.arrow) == null ? void 0 : Z.x, He = (K = A.arrow) == null ? void 0 : K.y, Ie = ((J = A.arrow) == null ? void 0 : J.centerOffset) !== 0, [Le, ke] = a.useState(); return oe(() => { w && ke(window.getComputedStyle(w).zIndex); }, [w]), /* @__PURE__ */ d( "div", { ref: Oe.setFloating, "data-radix-popper-content-wrapper": "", style: { ...z, transform: H ? z.transform : "translate(0, -200%)", // keep off the page when measuring minWidth: "max-content", zIndex: Le, "--radix-popper-transform-origin": [ (Q = A.transformOrigin) == null ? void 0 : Q.x, (ee = A.transformOrigin) == null ? void 0 : ee.y ].join(" "), // hide the content if using the hide middleware and should be hidden // set visibility to hidden and disable pointer events so the UI behaves // as if the PopperContent isn't there at all ...((te = A.hide) == null ? void 0 : te.referenceHidden) && { visibility: "hidden", pointerEvents: "none" } }, dir: e.dir, children: /* @__PURE__ */ d( it, { scope: t, placedSide: q, onArrowChange: b, arrowX: De, arrowY: He, shouldHideArrow: Ie, children: /* @__PURE__ */ d( S.div, { "data-side": q, "data-align": Ne, ...y, ref: T, style: { ...y.style, // if the PopperContent hasn't been placed yet (not all measurements done) // we prevent animations so that users's animation don't kick in too early referring wrong sides animation: H ? void 0 : "none" } } ) } ) } ); } ); fe.displayName = G; var he = "PopperArrow", ct = { top: "bottom", right: "left", bottom: "top", left: "right" }, ve = a.forwardRef(function(r, t) { const { __scopePopper: o, ...n } = r, i = lt(he, o), l = ct[i.placedSide]; return ( // we have to use an extra wrapper because `ResizeObserver` (used by `useSize`) // doesn't report size as we'd expect on SVG elements. // it reports their bounding box which is effectively the largest path inside the SVG. /* @__PURE__ */ d( "span", { ref: i.onArrowChange, style: { position: "absolute", left: i.arrowX, top: i.arrowY, [l]: 0, transformOrigin: { top: "", right: "0 0", bottom: "center 0", left: "100% 0" }[i.placedSide], transform: { top: "translateY(100%)", right: "translateY(50%) rotate(90deg) translateX(-50%)", bottom: "rotate(180deg)", left: "translateY(50%) rotate(-90deg) translateX(50%)" }[i.placedSide], visibility: i.shouldHideArrow ? "hidden" : void 0 }, children: /* @__PURE__ */ d( at, { ...n, ref: t, style: { ...n.style, // ensures the element can be measured correctly (mostly for if SVG) display: "block" } } ) } ) ); }); ve.displayName = he; function dt(e) { return e !== null; } var pt = (e) => ({ name: "transformOrigin", options: e, fn(r) { var m, w, x; const { placement: t, rects: o, middlewareData: n } = r, l = ((m = n.arrow) == null ? void 0 : m.centerOffset) !== 0, h = l ? 0 : e.arrowWidth, s = l ? 0 : e.arrowHeight, [p, c] = ge(t), f = { start: "0%", center: "50%", end: "100%" }[c], g = (((w = n.arrow) == null ? void 0 : w.x) ?? 0) + h / 2, u = (((x = n.arrow) == null ? void 0 : x.y) ?? 0) + s / 2; let v = "", y = ""; return p === "bottom" ? (v = l ? f : `${g}px`, y = `${-s}px`) : p === "top" ? (v = l ? f : `${g}px`, y = `${o.floating.height + s}px`) : p === "right" ? (v = `${-s}px`, y = l ? f : `${u}px`) : p === "left" && (v = `${o.floating.width + s}px`, y = l ? f : `${u}px`), { data: { x: v, y } }; } }); function ge(e) { const [r, t = "center"] = e.split("-"); return [r, t]; } var ut = de, ft = ue, ht = fe, vt = ve, gt = Object.freeze({ // See: https://github.com/twbs/bootstrap/blob/main/scss/mixins/_visually-hidden.scss position: "absolute", border: 0, width: 1, height: 1, padding: 0, margin: -1, overflow: "hidden", clip: "rect(0, 0, 0, 0)", whiteSpace: "nowrap", wordWrap: "normal" }), yt = "VisuallyHidden", ye = a.forwardRef( (e, r) => /* @__PURE__ */ d( S.span, { ...e, ref: r, style: { ...gt, ...e.style } } ) ); ye.displayName = yt; var mt = ye, [M, Wt] = ae("Tooltip", [ le ]), $ = le(), me = "TooltipProvider", wt = 700, F = "tooltip.open", [xt, X] = M(me), we = (e) => { const { __scopeTooltip: r, delayDuration: t = wt, skipDelayDuration: o = 300, disableHoverableContent: n = !1, children: i } = e, l = a.useRef(!0), h = a.useRef(!1), s = a.useRef(0); return a.useEffect(() => { const p = s.current; return () => window.clearTimeout(p); }, []), /* @__PURE__ */ d( xt, { scope: r, isOpenDelayedRef: l, delayDuration: t, onOpen: a.useCallback(() => { window.clearTimeout(s.current), l.current = !1; }, []), onClose: a.useCallback(() => { window.clearTimeout(s.current), s.current = window.setTimeout( () => l.current = !0, o ); }, [o]), isPointerInTransitRef: h, onPointerInTransitChange: a.useCallback((p) => { h.current = p; }, []), disableHoverableContent: n, children: i } ); }; we.displayName = me; var O = "Tooltip", [Ct, j] = M(O), xe = (e) => { const { __scopeTooltip: r, children: t, open: o, defaultOpen: n, onOpenChange: i, disableHoverableContent: l, delayDuration: h } = e, s = X(O, e.__scopeTooltip), p = $(r), [c, f] = a.useState(null), g = tt(), u = a.useRef(0), v = l ?? s.disableHoverableContent, y = h ?? s.delayDuration, m = a.useRef(!1), [w, x] = We({ prop: o, defaultProp: n ?? !1, onChange: (N) => { N ? (s.onOpen(), document.dispatchEvent(new CustomEvent(F))) : s.onClose(), i == null || i(N); }, caller: O }), T = a.useMemo(() => w ? m.current ? "delayed-open" : "instant-open" : "closed", [w]), C = a.useCallback(() => { window.clearTimeout(u.current), u.current = 0, m.current = !1, x(!0); }, [x]), b = a.useCallback(() => { window.clearTimeout(u.current), u.current = 0, x(!1); }, [x]), P = a.useCallback(() => { window.clearTimeout(u.current), u.current = window.setTimeout(() => { m.current = !0, x(!0), u.current = 0; }, y); }, [y, x]); return a.useEffect(() => () => { u.current && (window.clearTimeout(u.current), u.current = 0); }, []), /* @__PURE__ */ d(ut, { ...p, children: /* @__PURE__ */ d( Ct, { scope: r, contentId: g, open: w, stateAttribute: T, trigger: c, onTriggerChange: f, onTriggerEnter: a.useCallback(() => { s.isOpenDelayedRef.current ? P() : C(); }, [s.isOpenDelayedRef, P, C]), onTriggerLeave: a.useCallback(() => { v ? b() : (window.clearTimeout(u.current), u.current = 0); }, [b, v]), onOpen: C, onClose: b, disableHoverableContent: v, children: t } ) }); }; xe.displayName = O; var W = "TooltipTrigger", Ce = a.forwardRef( (e, r) => { const { __scopeTooltip: t, ...o } = e, n = j(W, t), i = X(W, t), l = $(t), h = a.useRef(null), s = k(r, h, n.onTriggerChange), p = a.useRef(!1), c = a.useRef(!1), f = a.useCallback(() => p.current = !1, []); return a.useEffect(() => () => document.removeEventListener("pointerup", f), [f]), /* @__PURE__ */ d(ft, { asChild: !0, ...l, children: /* @__PURE__ */ d( S.button, { "aria-describedby": n.open ? n.contentId : void 0, "data-state": n.stateAttribute, ...o, ref: s, onPointerMove: E(e.onPointerMove, (g) => { g.pointerType !== "touch" && !c.current && !i.isPointerInTransitRef.current && (n.onTriggerEnter(), c.current = !0); }), onPointerLeave: E(e.onPointerLeave, () => { n.onTriggerLeave(), c.current = !1; }), onPointerDown: E(e.onPointerDown, () => { n.open && n.onClose(), p.current = !0, document.addEventListener("pointerup", f, { once: !0 }); }), onFocus: E(e.onFocus, () => { p.current || n.onOpen(); }), onBlur: E(e.onBlur, n.onClose), onClick: E(e.onClick, n.onClose) } ) }); } ); Ce.displayName = W; var Pt = "TooltipPortal", [Bt, Tt] = M(Pt, { forceMount: void 0 }), R = "TooltipContent", Pe = a.forwardRef( (e, r) => { const t = Tt(R, e.__scopeTooltip), { forceMount: o = t.forceMount, side: n = "top", ...i } = e, l = j(R, e.__scopeTooltip); return /* @__PURE__ */ d(Be, { present: o || l.open, children: l.disableHoverableContent ? /* @__PURE__ */ d(Te, { side: n, ...i, ref: r }) : /* @__PURE__ */ d(bt, { side: n, ...i, ref: r }) }); } ), bt = a.forwardRef((e, r) => { const t = j(R, e.__scopeTooltip), o = X(R, e.__scopeTooltip), n = a.useRef(null), i = k(r, n), [l, h] = a.useState(null), { trigger: s, onClose: p } = t, c = n.current, { onPointerInTransitChange: f } = o, g = a.useCallback(() => { h(null), f(!1); }, [f]), u = a.useCallback( (v, y) => { const m = v.currentTarget, w = { x: v.clientX, y: v.clientY }, x = _t(w, m.getBoundingClientRect()), T = Ot(w, x), C = St(y.getBoundingClientRect()), b = Dt([...T, ...C]); h(b), f(!0); }, [f] ); return a.useEffect(() => () => g(), [g]), a.useEffect(() => { if (s && c) { const v = (m) => u(m, c), y = (m) => u(m, s); return s.addEventListener("pointerleave", v), c.addEventListener("pointerleave", y), () => { s.removeEventListener("pointerleave", v), c.removeEventListener("pointerleave", y); }; } }, [s, c, u, g]), a.useEffect(() => { if (l) { const v = (y) => { const m = y.target, w = { x: y.clientX, y: y.clientY }, x = (s == null ? void 0 : s.contains(m)) || (c == null ? void 0 : c.contains(m)), T = !Nt(w, l); x ? g() : T && (g(), p()); }; return document.addEventListener("pointermove", v), () => document.removeEventListener("pointermove", v); } }, [s, c, l, p, g]), /* @__PURE__ */ d(Te, { ...e, ref: i }); }), [At, Et] = M(O, { isInside: !1 }), Rt = Ge("TooltipContent"), Te = a.forwardRef( (e, r) => { const { __scopeTooltip: t, children: o, "aria-label": n, onEscapeKeyDown: i, onPointerDownOutside: l, ...h } = e, s = j(R, t), p = $(t), { onClose: c } = s; return a.useEffect(() => (document.addEventListener(F, c), () => document.removeEventListener(F, c)), [c]), a.useEffect(() => { if (s.trigger) { const f = (g) => { const u = g.target; u != null && u.contains(s.trigger) && c(); }; return window.addEventListener("scroll", f, { capture: !0 }), () => window.removeEventListener("scroll", f, { capture: !0 }); } }, [s.trigger, c]), /* @__PURE__ */ d( rt, { asChild: !0, disableOutsidePointerEvents: !1, onEscapeKeyDown: i, onPointerDownOutside: l, onFocusOutside: (f) => f.preventDefault(), onDismiss: c, children: /* @__PURE__ */ Y( ht, { "data-state": s.stateAttribute, ...p, ...h, ref: r, style: { ...h.style, "--radix-tooltip-content-transform-origin": "var(--radix-popper-transform-origin)", "--radix-tooltip-content-available-width": "var(--radix-popper-available-width)", "--radix-tooltip-content-available-height": "var(--radix-popper-available-height)", "--radix-tooltip-trigger-width": "var(--radix-popper-anchor-width)", "--radix-tooltip-trigger-height": "var(--radix-popper-anchor-height)" }, children: [ /* @__PURE__ */ d(Rt, { children: o }), /* @__PURE__ */ d(At, { scope: t, isInside: !0, children: /* @__PURE__ */ d(mt, { id: s.contentId, role: "tooltip", children: n || o }) }) ] } ) } ); } ); Pe.displayName = R; var be = "TooltipArrow", Ae = a.forwardRef( (e, r) => { const { __scopeTooltip: t, ...o } = e, n = $(t); return Et( be, t ).isInside ? null : /* @__PURE__ */ d(vt, { ...n, ...o, ref: r }); } ); Ae.displayName = be; function _t(e, r) { const t = Math.abs(r.top - e.y), o = Math.abs(r.bottom - e.y), n = Math.abs(r.right - e.x), i = Math.abs(r.left - e.x); switch (Math.min(t, o, n, i)) { case i: return "left"; case n: return "right"; case t: return "top"; case o: return "bottom"; default: throw new Error("unreachable"); } } function Ot(e, r, t = 5) { const o = []; switch (r) { case "top": o.push( { x: e.x - t, y: e.y + t }, { x: e.x + t, y: e.y + t } ); break; case "bottom": o.push( { x: e.x - t, y: e.y - t }, { x: e.x + t, y: e.y - t } ); break; case "left": o.push( { x: e.x + t, y: e.y - t }, { x: e.x + t, y: e.y + t } ); break; case "right": o.push( { x: e.x - t, y: e.y - t }, { x: e.x - t, y: e.y + t } ); break; } return o; } function St(e) { const { top: r, right: t, bottom: o, left: n } = e; return [ { x: n, y: r }, { x: t, y: r }, { x: t, y: o }, { x: n, y: o } ]; } function Nt(e, r) { const { x: t, y: o } = e; let n = !1; for (let i = 0, l = r.length - 1; i < r.length; l = i++) { const h = r[i], s = r[l], p = h.x, c = h.y, f = s.x, g = s.y; c > o != g > o && t < (f - p) * (o - c) / (g - c) + p && (n = !n); } return n; } function Dt(e) { const r = e.slice(); return r.sort((t, o) => t.x < o.x ? -1 : t.x > o.x ? 1 : t.y < o.y ? -1 : t.y > o.y ? 1 : 0), Ht(r); } function Ht(e) { if (e.length <= 1) return e.slice(); const r = []; for (let o = 0; o < e.length; o++) { const n = e[o]; for (; r.length >= 2; ) { const i = r[r.length - 1], l = r[r.length - 2]; if ((i.x - l.x) * (n.y - l.y) >= (i.y - l.y) * (n.x - l.x)) r.pop(); else break; } r.push(n); } r.pop(); const t = []; for (let o = e.length - 1; o >= 0; o--) { const n = e[o]; for (; t.length >= 2; ) { const i = t[t.length - 1], l = t[t.length - 2]; if ((i.x - l.x) * (n.y - l.y) >= (i.y - l.y) * (n.x - l.x)) t.pop(); else break; } t.push(n); } return t.pop(), r.length === 1 && t.length === 1 && r[0].x === t[0].x && r[0].y === t[0].y ? r : r.concat(t); } const It = "_tooltipContent_12gjy_1", Lt = "_tooltipArrow_12gjy_8", kt = "_fadeIn_12gjy_1", ne = { tooltipContent: It, tooltipArrow: Lt, fadeIn: kt }, Gt = ot( ({ children: e, content: r, className: t, style: o }, n) => /* @__PURE__ */ d(we, { children: /* @__PURE__ */ Y(xe, { delayDuration: 1e3, children: [ /* @__PURE__ */ d(Ce, { asChild: !0, children: /* @__PURE__ */ d("div", { ref: n, className: t, style: o, children: e }) }), /* @__PURE__ */ Y(Pe, { className: ne.tooltipContent, sideOffset: 5, children: [ /* @__PURE__ */ d(Ae, { className: ne.tooltipArrow }), /* @__PURE__ */ d("span", { children: r }) ] }) ] }) }) ); export { Gt as default };