react-selectable-box
Version:
A React component used hooks that allows you to select elements in the drag area using the mouse
118 lines (116 loc) • 3.85 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) {
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 smoothScroll = (e, _container) => {
const container = _container === document.body ? document.documentElement : _container;
const { clientX, clientY } = (0, import_utils.getClientXY)(e);
if (clientY - EDGE_OFFSET <= 0 || clientY <= container.getBoundingClientRect().top) {
if (!topRaf.current) {
const callback = () => {
if (container.scrollTop > 0) {
topRaf.current = requestAnimationFrame(() => {
container.scrollTop -= scrollSpeed;
callback();
});
}
};
callback();
}
} else {
cancelRaf(topRaf);
}
if (clientY + EDGE_OFFSET >= window.innerHeight || clientY >= container.getBoundingClientRect().bottom) {
if (!bottomRaf.current) {
const callback = () => {
if (container.scrollTop < container.scrollHeight - container.clientHeight) {
bottomRaf.current = requestAnimationFrame(() => {
container.scrollTop += scrollSpeed;
callback();
});
}
};
callback();
}
} else {
cancelRaf(bottomRaf);
}
if (clientX - EDGE_OFFSET <= 0 || clientX <= container.getBoundingClientRect().left) {
if (!leftRaf.current) {
const callback = () => {
if (container.scrollLeft > 0) {
leftRaf.current = requestAnimationFrame(() => {
container.scrollLeft -= scrollSpeed;
callback();
});
}
};
callback();
}
} else {
cancelRaf(leftRaf);
}
if (clientX + EDGE_OFFSET >= window.innerWidth || clientX >= container.getBoundingClientRect().right) {
if (!rightRaf.current) {
const callback = () => {
if (container.scrollLeft < container.scrollWidth - container.clientWidth) {
rightRaf.current = requestAnimationFrame(() => {
container.scrollLeft += scrollSpeed;
callback();
});
}
};
callback();
}
} else {
cancelRaf(rightRaf);
}
};
return {
cancelScroll,
smoothScroll
};
}