@pdf-viewer/react
Version:
A react-pdf-viewer component for React and Next.js. Suitable for react-pdf document.
53 lines (52 loc) • 1.75 kB
JavaScript
import { useState as T, useRef as p, useCallback as c, useEffect as X } from "react";
const b = ({
isPressed: v,
scrollSpeed: i = 2
}) => {
const [L, f] = T("unset"), t = p(null), u = p({
startY: 0,
startX: 0,
startScrollTop: 0,
startScrollLeft: 0
}), o = c((r) => {
t.current && (u.current = {
startY: r.pageY,
startX: r.pageX,
startScrollTop: t.current.scrollTop,
startScrollLeft: t.current.scrollLeft
});
}, []), e = c(() => {
f("smooth");
}, []), s = c(
(r) => {
if (!v || !t.current)
return;
r.preventDefault(), f("unset");
const { startX: n, startY: a, startScrollTop: l, startScrollLeft: d } = u.current, h = (r.pageX - n) * i, w = (r.pageY - a) * i;
t.current.scrollTop = l - w, t.current.scrollLeft = d - h;
},
[v, i, t]
), S = c(
(r) => {
var n, a, l;
t.current && ((n = t.current) == null || n.removeEventListener("mousedown", o), (a = t.current) == null || a.removeEventListener("mouseup", e), (l = t.current) == null || l.removeEventListener("mousemove", s)), t.current = r, r && (r == null || r.addEventListener("mousedown", o, { capture: !0 }), r == null || r.addEventListener("mouseup", e), r == null || r.addEventListener("mousemove", s));
},
[o, e, s]
), E = c(() => {
u.current = {
...u.current,
startScrollTop: 0,
startScrollLeft: 0
};
}, []);
return X(() => () => {
t.current && (t.current.removeEventListener("mousedown", o), t.current.removeEventListener("mouseup", e), t.current.removeEventListener("mousemove", s));
}, [o, e, s]), {
initializeGrabScroll: S,
scrollBehavior: L,
resetGrabState: E
};
};
export {
b as useGrabScroll
};