@pdf-viewer/react
Version:
The PDF Viewer component for React and Next.js
570 lines (569 loc) • 19.9 kB
JavaScript
import { jsx as d, jsxs as j } from "react/jsx-runtime";
import * as a from "react";
import { forwardRef as We } from "react";
import { P as O, e as ae, a as k, h as Ye, u as Be, b as oe, f as Ge, c as E, g as Xe, S as Ve } from "../../index-1cb41342.js";
import { u as Ue, D as qe } from "../../index-aa2d3884.js";
import { u as ze, a as Ze, o as Ke, s as Je, f as Qe, b as et, c as tt, h as rt, l as ot } from "../../floating-ui.react-dom-15b9b819.js";
import '../../assets/RPTooltip.css';var nt = "Arrow", se = a.forwardRef((e, r) => {
const { children: t, width: o = 10, height: n = 5, ...s } = e;
return /* @__PURE__ */ d(
O.svg,
{
...s,
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, s = ce(pe, t), i = a.useRef(null), h = k(r, i);
return a.useEffect(() => {
s.onAnchorChange((o == null ? void 0 : o.current) || i.current);
}), o ? null : /* @__PURE__ */ d(O.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: s = "center",
alignOffset: i = 0,
arrowPadding: h = 0,
avoidCollisions: l = !0,
collisionBoundary: p = [],
collisionPadding: c = 0,
sticky: f = "partial",
hideWhenDetached: m = !1,
updatePositionStrategy: u = "optimized",
onPlaced: v,
...g
} = e, y = ce(G, t), [w, x] = a.useState(null), T = k(r, (_) => x(_)), [C, b] = a.useState(null), P = Ye(C), N = (P == null ? void 0 : P.width) ?? 0, V = (P == null ? void 0 : P.height) ?? 0, Ee = o + (s !== "center" ? "-" + s : ""), Re = typeof c == "number" ? c : { top: 0, right: 0, bottom: 0, left: 0, ...c }, U = Array.isArray(p) ? p : [p], _e = U.length > 0, S = {
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: q, placement: Ne, isPositioned: D, middlewareData: A } = ze({
// default to `fixed` strategy so users don't have to pick and we also avoid focus scroll issues
strategy: "fixed",
placement: Ee,
whileElementsMounted: (..._) => Ze(..._, {
animationFrame: u === "always"
}),
elements: {
reference: y.anchor
},
middleware: [
Ke({ mainAxis: n + V, alignmentAxis: i }),
l && Je({
mainAxis: !0,
crossAxis: !1,
limiter: f === "partial" ? ot() : void 0,
...S
}),
l && Qe({ ...S }),
et({
...S,
apply: ({ elements: _, rects: re, availableWidth: Me, availableHeight: $e }) => {
const { width: Fe, height: je } = re.reference, I = _.floating.style;
I.setProperty("--radix-popper-available-width", `${Me}px`), I.setProperty("--radix-popper-available-height", `${$e}px`), I.setProperty("--radix-popper-anchor-width", `${Fe}px`), I.setProperty("--radix-popper-anchor-height", `${je}px`);
}
}),
C && tt({ element: C, padding: h }),
pt({ arrowWidth: N, arrowHeight: V }),
m && rt({ strategy: "referenceHidden", ...S })
]
}), [z, Se] = ge(Ne), H = Be(v);
oe(() => {
D && (H == null || H());
}, [D, H]);
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, [ke, Le] = a.useState();
return oe(() => {
w && Le(window.getComputedStyle(w).zIndex);
}, [w]), /* @__PURE__ */ d(
"div",
{
ref: Oe.setFloating,
"data-radix-popper-content-wrapper": "",
style: {
...q,
transform: D ? q.transform : "translate(0, -200%)",
// keep off the page when measuring
minWidth: "max-content",
zIndex: ke,
"--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: z,
onArrowChange: b,
arrowX: De,
arrowY: He,
shouldHideArrow: Ie,
children: /* @__PURE__ */ d(
O.div,
{
"data-side": z,
"data-align": Se,
...g,
ref: T,
style: {
...g.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: D ? 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, s = lt(he, o), i = ct[s.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: s.onArrowChange,
style: {
position: "absolute",
left: s.arrowX,
top: s.arrowY,
[i]: 0,
transformOrigin: {
top: "",
right: "0 0",
bottom: "center 0",
left: "100% 0"
}[s.placedSide],
transform: {
top: "translateY(100%)",
right: "translateY(50%) rotate(90deg) translateX(-50%)",
bottom: "rotate(180deg)",
left: "translateY(50%) rotate(-90deg) translateX(50%)"
}[s.placedSide],
visibility: s.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 y, w, x;
const { placement: t, rects: o, middlewareData: n } = r, i = ((y = n.arrow) == null ? void 0 : y.centerOffset) !== 0, h = i ? 0 : e.arrowWidth, l = i ? 0 : e.arrowHeight, [p, c] = ge(t), f = { start: "0%", center: "50%", end: "100%" }[c], m = (((w = n.arrow) == null ? void 0 : w.x) ?? 0) + h / 2, u = (((x = n.arrow) == null ? void 0 : x.y) ?? 0) + l / 2;
let v = "", g = "";
return p === "bottom" ? (v = i ? f : `${m}px`, g = `${-l}px`) : p === "top" ? (v = i ? f : `${m}px`, g = `${o.floating.height + l}px`) : p === "right" ? (v = `${-l}px`, g = i ? f : `${u}px`) : p === "left" && (v = `${o.floating.width + l}px`, g = i ? f : `${u}px`), { data: { x: v, y: g } };
}
});
function ge(e) {
const [r, t = "center"] = e.split("-");
return [r, t];
}
var ut = de, ft = ue, ht = fe, vt = ve, gt = "VisuallyHidden", me = a.forwardRef(
(e, r) => /* @__PURE__ */ d(
O.span,
{
...e,
ref: r,
style: {
// 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",
...e.style
}
}
)
);
me.displayName = gt;
var mt = me, [L, Wt] = ae("Tooltip", [
le
]), M = le(), ye = "TooltipProvider", yt = 700, W = "tooltip.open", [wt, X] = L(ye), we = (e) => {
const {
__scopeTooltip: r,
delayDuration: t = yt,
skipDelayDuration: o = 300,
disableHoverableContent: n = !1,
children: s
} = e, [i, h] = a.useState(!0), l = a.useRef(!1), p = a.useRef(0);
return a.useEffect(() => {
const c = p.current;
return () => window.clearTimeout(c);
}, []), /* @__PURE__ */ d(
wt,
{
scope: r,
isOpenDelayed: i,
delayDuration: t,
onOpen: a.useCallback(() => {
window.clearTimeout(p.current), h(!1);
}, []),
onClose: a.useCallback(() => {
window.clearTimeout(p.current), p.current = window.setTimeout(
() => h(!0),
o
);
}, [o]),
isPointerInTransitRef: l,
onPointerInTransitChange: a.useCallback((c) => {
l.current = c;
}, []),
disableHoverableContent: n,
children: s
}
);
};
we.displayName = ye;
var $ = "Tooltip", [xt, F] = L($), xe = (e) => {
const {
__scopeTooltip: r,
children: t,
open: o,
defaultOpen: n = !1,
onOpenChange: s,
disableHoverableContent: i,
delayDuration: h
} = e, l = X($, e.__scopeTooltip), p = M(r), [c, f] = a.useState(null), m = Ue(), u = a.useRef(0), v = i ?? l.disableHoverableContent, g = h ?? l.delayDuration, y = a.useRef(!1), [w = !1, x] = Ge({
prop: o,
defaultProp: n,
onChange: (N) => {
N ? (l.onOpen(), document.dispatchEvent(new CustomEvent(W))) : l.onClose(), s == null || s(N);
}
}), T = a.useMemo(() => w ? y.current ? "delayed-open" : "instant-open" : "closed", [w]), C = a.useCallback(() => {
window.clearTimeout(u.current), u.current = 0, y.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(() => {
y.current = !0, x(!0), u.current = 0;
}, g);
}, [g, x]);
return a.useEffect(() => () => {
u.current && (window.clearTimeout(u.current), u.current = 0);
}, []), /* @__PURE__ */ d(ut, { ...p, children: /* @__PURE__ */ d(
xt,
{
scope: r,
contentId: m,
open: w,
stateAttribute: T,
trigger: c,
onTriggerChange: f,
onTriggerEnter: a.useCallback(() => {
l.isOpenDelayed ? P() : C();
}, [l.isOpenDelayed, 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 = $;
var Y = "TooltipTrigger", Ce = a.forwardRef(
(e, r) => {
const { __scopeTooltip: t, ...o } = e, n = F(Y, t), s = X(Y, t), i = M(t), h = a.useRef(null), l = 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, ...i, children: /* @__PURE__ */ d(
O.button,
{
"aria-describedby": n.open ? n.contentId : void 0,
"data-state": n.stateAttribute,
...o,
ref: l,
onPointerMove: E(e.onPointerMove, (m) => {
m.pointerType !== "touch" && !c.current && !s.isPointerInTransitRef.current && (n.onTriggerEnter(), c.current = !0);
}),
onPointerLeave: E(e.onPointerLeave, () => {
n.onTriggerLeave(), c.current = !1;
}),
onPointerDown: E(e.onPointerDown, () => {
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 = Y;
var Ct = "TooltipPortal", [Yt, Pt] = L(Ct, {
forceMount: void 0
}), R = "TooltipContent", Pe = a.forwardRef(
(e, r) => {
const t = Pt(R, e.__scopeTooltip), { forceMount: o = t.forceMount, side: n = "top", ...s } = e, i = F(R, e.__scopeTooltip);
return /* @__PURE__ */ d(Xe, { present: o || i.open, children: i.disableHoverableContent ? /* @__PURE__ */ d(Te, { side: n, ...s, ref: r }) : /* @__PURE__ */ d(Tt, { side: n, ...s, ref: r }) });
}
), Tt = a.forwardRef((e, r) => {
const t = F(R, e.__scopeTooltip), o = X(R, e.__scopeTooltip), n = a.useRef(null), s = k(r, n), [i, h] = a.useState(null), { trigger: l, onClose: p } = t, c = n.current, { onPointerInTransitChange: f } = o, m = a.useCallback(() => {
h(null), f(!1);
}, [f]), u = a.useCallback(
(v, g) => {
const y = v.currentTarget, w = { x: v.clientX, y: v.clientY }, x = Et(w, y.getBoundingClientRect()), T = Rt(w, x), C = _t(g.getBoundingClientRect()), b = Nt([...T, ...C]);
h(b), f(!0);
},
[f]
);
return a.useEffect(() => () => m(), [m]), a.useEffect(() => {
if (l && c) {
const v = (y) => u(y, c), g = (y) => u(y, l);
return l.addEventListener("pointerleave", v), c.addEventListener("pointerleave", g), () => {
l.removeEventListener("pointerleave", v), c.removeEventListener("pointerleave", g);
};
}
}, [l, c, u, m]), a.useEffect(() => {
if (i) {
const v = (g) => {
const y = g.target, w = { x: g.clientX, y: g.clientY }, x = (l == null ? void 0 : l.contains(y)) || (c == null ? void 0 : c.contains(y)), T = !Ot(w, i);
x ? m() : T && (m(), p());
};
return document.addEventListener("pointermove", v), () => document.removeEventListener("pointermove", v);
}
}, [l, c, i, p, m]), /* @__PURE__ */ d(Te, { ...e, ref: s });
}), [bt, At] = L($, { isInside: !1 }), Te = a.forwardRef(
(e, r) => {
const {
__scopeTooltip: t,
children: o,
"aria-label": n,
onEscapeKeyDown: s,
onPointerDownOutside: i,
...h
} = e, l = F(R, t), p = M(t), { onClose: c } = l;
return a.useEffect(() => (document.addEventListener(W, c), () => document.removeEventListener(W, c)), [c]), a.useEffect(() => {
if (l.trigger) {
const f = (m) => {
const u = m.target;
u != null && u.contains(l.trigger) && c();
};
return window.addEventListener("scroll", f, { capture: !0 }), () => window.removeEventListener("scroll", f, { capture: !0 });
}
}, [l.trigger, c]), /* @__PURE__ */ d(
qe,
{
asChild: !0,
disableOutsidePointerEvents: !1,
onEscapeKeyDown: s,
onPointerDownOutside: i,
onFocusOutside: (f) => f.preventDefault(),
onDismiss: c,
children: /* @__PURE__ */ j(
ht,
{
"data-state": l.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(Ve, { children: o }),
/* @__PURE__ */ d(bt, { scope: t, isInside: !0, children: /* @__PURE__ */ d(mt, { id: l.contentId, role: "tooltip", children: n || o }) })
]
}
)
}
);
}
);
Pe.displayName = R;
var be = "TooltipArrow", Ae = a.forwardRef(
(e, r) => {
const { __scopeTooltip: t, ...o } = e, n = M(t);
return At(
be,
t
).isInside ? null : /* @__PURE__ */ d(vt, { ...n, ...o, ref: r });
}
);
Ae.displayName = be;
function Et(e, r) {
const t = Math.abs(r.top - e.y), o = Math.abs(r.bottom - e.y), n = Math.abs(r.right - e.x), s = Math.abs(r.left - e.x);
switch (Math.min(t, o, n, s)) {
case s:
return "left";
case n:
return "right";
case t:
return "top";
case o:
return "bottom";
default:
throw new Error("unreachable");
}
}
function Rt(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 _t(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 Ot(e, r) {
const { x: t, y: o } = e;
let n = !1;
for (let s = 0, i = r.length - 1; s < r.length; i = s++) {
const h = r[s].x, l = r[s].y, p = r[i].x, c = r[i].y;
l > o != c > o && t < (p - h) * (o - l) / (c - l) + h && (n = !n);
}
return n;
}
function Nt(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), St(r);
}
function St(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 s = r[r.length - 1], i = r[r.length - 2];
if ((s.x - i.x) * (n.y - i.y) >= (s.y - i.y) * (n.x - i.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 s = t[t.length - 1], i = t[t.length - 2];
if ((s.x - i.x) * (n.y - i.y) >= (s.y - i.y) * (n.x - i.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 Dt = "_tooltipContent_12gjy_1", Ht = "_tooltipArrow_12gjy_8", It = "_fadeIn_12gjy_1", ne = {
tooltipContent: Dt,
tooltipArrow: Ht,
fadeIn: It
}, kt = We(
({ children: e, content: r, className: t, style: o }, n) => /* @__PURE__ */ d(we, { children: /* @__PURE__ */ j(xe, { delayDuration: 1e3, children: [
/* @__PURE__ */ d(Ce, { asChild: !0, children: /* @__PURE__ */ d("div", { ref: n, className: t, style: o, children: e }) }),
/* @__PURE__ */ j(Pe, { className: ne.tooltipContent, sideOffset: 5, children: [
/* @__PURE__ */ d(Ae, { className: ne.tooltipArrow }),
/* @__PURE__ */ d("span", { children: r })
] })
] }) })
);
kt.displayName = "RPTooltip";
export {
kt as default
};