UNPKG

@pdf-viewer/react

Version:

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

72 lines (71 loc) 3.26 kB
import { jsx as N } from "react/jsx-runtime"; import { useRef as _, useState as T, useCallback as w, useEffect as f } from "react"; import { THUMBNAIL_MIN_WIDTH as v } from "./utils/constants.js"; import { useLayoutContainer as A } from "./contexts/LayoutContainerContext.js"; const P = { "rp-sidebar-content-wrapper": "_rp-sidebar-content-wrapper_ppg43_1", "rp-sidebar-content": "_rp-sidebar-content_ppg43_1", "rp-thumbnails-wrapper": "_rp-thumbnails-wrapper_ppg43_17", "rp-thumbnails-wrapper-dragging": "_rp-thumbnails-wrapper-dragging_ppg43_24", "rp-thumbnails-wrapper-hidden": "_rp-thumbnails-wrapper-hidden_ppg43_28" }, L = { "rp-thumbnail-dragger": "_rp-thumbnail-dragger_17ndw_2", "rp-thumbnail-dragger-dragging": "_rp-thumbnail-dragger-dragging_17ndw_12" }, R = { "rp-thumbnail-dragger": "_rp-thumbnail-dragger_8o6id_1", "rp-thumbnail-dragger-dragging": "_rp-thumbnail-dragger-dragging_8o6id_14" }, W = (p) => { p.preventDefault(), p.stopPropagation(); }, k = (p) => { const { onWidthChange: e, thumbnailRef: S, leftSidebarRef: x, isRPLayout: y } = p, n = _(null), E = _(0), o = _(null), [a, B] = T(v), { container: m } = A(), I = w((t) => { var c, u, l, s, d, g, M, D; W(t); const r = ((u = (c = n.current) == null ? void 0 : c.getBoundingClientRect()) == null ? void 0 : u.left) ?? 0, i = ((s = (l = S.current) == null ? void 0 : l.getBoundingClientRect()) == null ? void 0 : s.width) ?? 0; o.current = { x: r, y: 0 }, E.current = i, (d = n.current) == null || d.classList.add(L["rp-thumbnail-dragger-dragging"]), (g = n.current) == null || g.classList.add(R["rp-thumbnail-dragger-dragging"]), (D = (M = n.current) == null ? void 0 : M.closest('[data-rp="thumbnailSidebar"]')) == null || D.classList.add( P["rp-thumbnails-wrapper"], P["rp-thumbnails-wrapper-dragging"] ); }, []), h = w((t) => { var r, i; W(t), o.current = null, (r = n.current) == null || r.classList.remove(L["rp-thumbnail-dragger-dragging"]), (i = n.current) == null || i.classList.remove(R["rp-thumbnail-dragger-dragging"]); }, []), b = w( (t) => { if (!o.current) return; const r = t.clientX - o.current.x, i = Math.max( v, Math.min(E.current + r, a) ); e == null || e(i), W(t); }, [e, a] ); return f(() => { if (!m) return; const t = new ResizeObserver(([r]) => { var s, d, g; const i = ((s = n.current) == null ? void 0 : s.offsetWidth) ?? 0, c = ((g = (d = x.current) == null ? void 0 : d.getBoundingClientRect()) == null ? void 0 : g.width) ?? 0, u = r.contentRect.width / 2, l = i + c; B(u - l); }); return t.observe(m), () => { t.disconnect(); }; }, [m, x]), f(() => { var r; (((r = S.current) == null ? void 0 : r.clientWidth) ?? v) > a && (e == null || e(a)); }, [a, e]), f(() => (window.addEventListener("pointermove", b), window.addEventListener("pointerup", h), () => { window.removeEventListener("pointermove", b), window.removeEventListener("pointerup", h); }), [b, h]), /* @__PURE__ */ N( "div", { ref: n, className: y ? R["rp-thumbnail-dragger"] : L["rp-thumbnail-dragger"], onPointerDown: I } ); }; export { k as R, P as c };