react-selectable-box
Version:
A React component used hooks that allows you to select elements in the drag area using the mouse
142 lines (140 loc) • 4.84 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/hooks/useScroll.ts
var useScroll_exports = {};
__export(useScroll_exports, {
default: () => useScroll
});
module.exports = __toCommonJS(useScroll_exports);
var import_react = require("react");
var import_utils = require("../utils");
var DEFAULT_SCROLL_SPEED = 4;
var EDGE_OFFSET = 1;
function useScroll(scrollSpeed = DEFAULT_SCROLL_SPEED, scrollContainer) {
const topRaf = (0, import_react.useRef)(null);
const bottomRaf = (0, import_react.useRef)(null);
const leftRaf = (0, import_react.useRef)(null);
const rightRaf = (0, import_react.useRef)(null);
const cancelRaf = (raf) => {
if (raf.current) {
cancelAnimationFrame(raf.current);
raf.current = null;
}
};
const cancelScroll = () => {
cancelRaf(topRaf);
cancelRaf(bottomRaf);
cancelRaf(leftRaf);
cancelRaf(rightRaf);
};
(0, import_react.useEffect)(() => {
return cancelScroll;
}, []);
const getScrollContainer = (axis) => {
function getContainer() {
var _a, _b;
if (scrollContainer) {
if (typeof scrollContainer === "function") {
return scrollContainer();
}
if (((_a = scrollContainer.inner) == null ? void 0 : _a.axis) === axis) {
return scrollContainer.inner.getContainer();
}
if (((_b = scrollContainer.outer) == null ? void 0 : _b.axis) === axis) {
return scrollContainer.outer.getContainer();
}
}
return document.body;
}
const container = getContainer();
return container === document.body ? document.documentElement : container;
};
const smoothScroll = (e) => {
const { clientX, clientY } = (0, import_utils.getClientXY)(e);
const xScrollContainer = getScrollContainer("x");
const yScrollContainer = getScrollContainer("y");
if (yScrollContainer) {
if (clientY - EDGE_OFFSET <= 0 || clientY <= yScrollContainer.getBoundingClientRect().top) {
if (!topRaf.current) {
const callback = () => {
if (yScrollContainer.scrollTop > 0) {
topRaf.current = requestAnimationFrame(() => {
yScrollContainer.scrollTop -= scrollSpeed;
callback();
});
}
};
callback();
}
} else {
cancelRaf(topRaf);
}
if (clientY + EDGE_OFFSET >= window.innerHeight || clientY >= yScrollContainer.getBoundingClientRect().bottom) {
if (!bottomRaf.current) {
const callback = () => {
if (yScrollContainer.scrollTop < yScrollContainer.scrollHeight - yScrollContainer.clientHeight) {
bottomRaf.current = requestAnimationFrame(() => {
yScrollContainer.scrollTop += scrollSpeed;
callback();
});
}
};
callback();
}
} else {
cancelRaf(bottomRaf);
}
}
if (xScrollContainer) {
if (clientX - EDGE_OFFSET <= 0 || clientX <= xScrollContainer.getBoundingClientRect().left) {
if (!leftRaf.current) {
const callback = () => {
if (xScrollContainer.scrollLeft > 0) {
leftRaf.current = requestAnimationFrame(() => {
xScrollContainer.scrollLeft -= scrollSpeed;
callback();
});
}
};
callback();
}
} else {
cancelRaf(leftRaf);
}
if (clientX + EDGE_OFFSET >= window.innerWidth || clientX >= xScrollContainer.getBoundingClientRect().right) {
if (!rightRaf.current) {
const callback = () => {
if (xScrollContainer.scrollLeft < xScrollContainer.scrollWidth - xScrollContainer.clientWidth) {
rightRaf.current = requestAnimationFrame(() => {
xScrollContainer.scrollLeft += scrollSpeed;
callback();
});
}
};
callback();
}
} else {
cancelRaf(rightRaf);
}
}
};
return {
cancelScroll,
smoothScroll
};
}