@vx-oss/heroui-v2-use-draggable 2.1.19-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,32 @@
1
+ SPDX-License-Identifier: MIT
2
+
3
+ The MIT License (MIT)
4
+
5
+ Parameters
6
+
7
+ Creator / Maintainer : Vezham Technologies Private Limited
8
+ Original Author : NextUI Inc
9
+ Licensor : Vezham Technologies Private Limited
10
+
11
+ Copyright © 2025, Vezham Technologies Private Limited. All rights reserved.
12
+ Copyright (c) 2020, NextUI Inc.
13
+
14
+ ---
15
+
16
+ Permission is hereby granted, free of charge, to any person obtaining a copy
17
+ of this software and associated documentation files (the "Software"), to deal
18
+ in the Software without restriction, including without limitation the rights
19
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20
+ copies of the Software, and to permit persons to whom the Software is
21
+ furnished to do so, subject to the following conditions:
22
+
23
+ The above copyright notice and this permission notice shall be included in all
24
+ copies or substantial portions of the Software.
25
+
26
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # @vx-oss/heroui-v2-use-draggable
2
+
3
+ A Quick description of the component
4
+
5
+ > This is an internal utility, not intended for public usage.
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ yarn add @vx-oss/heroui-v2-use-draggable
11
+ # or
12
+ npm i @vx-oss/heroui-v2-use-draggable
13
+ ```
14
+
15
+ ## Contribution
16
+
17
+ Yes please! See the
18
+ [contributing guidelines](https://github.com/heroui-inc/heroui/blob/master/CONTRIBUTING.md)
19
+ for details.
20
+
21
+ ## License
22
+
23
+ This project is licensed under the terms of the
24
+ [MIT license](https://github.com/heroui-inc/heroui/blob/master/LICENSE).
@@ -0,0 +1,26 @@
1
+ import { MoveResult } from '@react-aria/interactions';
2
+
3
+ interface UseDraggableProps {
4
+ /**
5
+ * Ref to the moving target DOM node.
6
+ */
7
+ targetRef?: React.RefObject<HTMLElement>;
8
+ /**
9
+ * Whether to disable the target is draggable.
10
+ * @default false
11
+ */
12
+ isDisabled?: boolean;
13
+ /**
14
+ * Whether the target can overflow the viewport.
15
+ * @default false
16
+ */
17
+ canOverflow?: boolean;
18
+ }
19
+ /**
20
+ * A hook to make a target draggable.
21
+ * @param props UseDraggableProps
22
+ * @returns MoveResult for the drag DOM node.
23
+ */
24
+ declare function useDraggable(props: UseDraggableProps): MoveResult;
25
+
26
+ export { type UseDraggableProps, useDraggable };
@@ -0,0 +1,26 @@
1
+ import { MoveResult } from '@react-aria/interactions';
2
+
3
+ interface UseDraggableProps {
4
+ /**
5
+ * Ref to the moving target DOM node.
6
+ */
7
+ targetRef?: React.RefObject<HTMLElement>;
8
+ /**
9
+ * Whether to disable the target is draggable.
10
+ * @default false
11
+ */
12
+ isDisabled?: boolean;
13
+ /**
14
+ * Whether the target can overflow the viewport.
15
+ * @default false
16
+ */
17
+ canOverflow?: boolean;
18
+ }
19
+ /**
20
+ * A hook to make a target draggable.
21
+ * @param props UseDraggableProps
22
+ * @returns MoveResult for the drag DOM node.
23
+ */
24
+ declare function useDraggable(props: UseDraggableProps): MoveResult;
25
+
26
+ export { type UseDraggableProps, useDraggable };
package/dist/index.js ADDED
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ useDraggable: () => useDraggable
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+ var import_react = require("react");
27
+ var import_interactions = require("@react-aria/interactions");
28
+ function useDraggable(props) {
29
+ const { targetRef, isDisabled = false, canOverflow = false } = props;
30
+ const boundary = (0, import_react.useRef)({ minLeft: 0, minTop: 0, maxLeft: 0, maxTop: 0 });
31
+ const isDragging = (0, import_react.useRef)(false);
32
+ let transform = { offsetX: 0, offsetY: 0 };
33
+ const onMoveStart = (0, import_react.useCallback)(() => {
34
+ var _a, _b, _c, _d, _e;
35
+ isDragging.current = true;
36
+ const { offsetX, offsetY } = transform;
37
+ const targetRect = (_a = targetRef == null ? void 0 : targetRef.current) == null ? void 0 : _a.getBoundingClientRect();
38
+ const targetLeft = (_b = targetRect == null ? void 0 : targetRect.left) != null ? _b : 0;
39
+ const targetTop = (_c = targetRect == null ? void 0 : targetRect.top) != null ? _c : 0;
40
+ const targetWidth = (_d = targetRect == null ? void 0 : targetRect.width) != null ? _d : 0;
41
+ const targetHeight = (_e = targetRect == null ? void 0 : targetRect.height) != null ? _e : 0;
42
+ const clientWidth = document.documentElement.clientWidth;
43
+ const clientHeight = document.documentElement.clientHeight;
44
+ const minLeft = -targetLeft + offsetX;
45
+ const minTop = -targetTop + offsetY;
46
+ const maxLeft = clientWidth - targetLeft - targetWidth + offsetX;
47
+ const maxTop = clientHeight - targetTop - targetHeight + offsetY;
48
+ boundary.current = {
49
+ minLeft,
50
+ minTop,
51
+ maxLeft,
52
+ maxTop
53
+ };
54
+ }, [transform, targetRef == null ? void 0 : targetRef.current]);
55
+ const onMove = (0, import_react.useCallback)(
56
+ (e) => {
57
+ if (isDisabled) {
58
+ return;
59
+ }
60
+ const { offsetX, offsetY } = transform;
61
+ const { minLeft, minTop, maxLeft, maxTop } = boundary.current;
62
+ let moveX = offsetX + e.deltaX;
63
+ let moveY = offsetY + e.deltaY;
64
+ if (!canOverflow) {
65
+ moveX = Math.min(Math.max(moveX, minLeft), maxLeft);
66
+ moveY = Math.min(Math.max(moveY, minTop), maxTop);
67
+ }
68
+ transform = {
69
+ offsetX: moveX,
70
+ offsetY: moveY
71
+ };
72
+ if (targetRef == null ? void 0 : targetRef.current) {
73
+ targetRef.current.style.transform = `translate(${moveX}px, ${moveY}px)`;
74
+ }
75
+ },
76
+ [isDisabled, transform, boundary.current, canOverflow, targetRef == null ? void 0 : targetRef.current]
77
+ );
78
+ const onMoveEnd = (0, import_react.useCallback)(() => {
79
+ isDragging.current = false;
80
+ }, []);
81
+ const { moveProps } = (0, import_interactions.useMove)({
82
+ onMoveStart,
83
+ onMove,
84
+ onMoveEnd
85
+ });
86
+ const preventDefault = (0, import_react.useCallback)((e) => {
87
+ if (isDragging.current) {
88
+ e.preventDefault();
89
+ }
90
+ }, []);
91
+ (0, import_react.useEffect)(() => {
92
+ if (!isDisabled) {
93
+ document.body.addEventListener("touchmove", preventDefault, { passive: false });
94
+ }
95
+ return () => {
96
+ document.body.removeEventListener("touchmove", preventDefault);
97
+ };
98
+ }, [isDisabled, preventDefault]);
99
+ return {
100
+ moveProps: {
101
+ ...moveProps,
102
+ style: { cursor: !isDisabled ? "move" : void 0 }
103
+ }
104
+ };
105
+ }
106
+ // Annotate the CommonJS export names for ESM import in node:
107
+ 0 && (module.exports = {
108
+ useDraggable
109
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,84 @@
1
+ // src/index.ts
2
+ import { useEffect, useRef, useCallback } from "react";
3
+ import { useMove } from "@react-aria/interactions";
4
+ function useDraggable(props) {
5
+ const { targetRef, isDisabled = false, canOverflow = false } = props;
6
+ const boundary = useRef({ minLeft: 0, minTop: 0, maxLeft: 0, maxTop: 0 });
7
+ const isDragging = useRef(false);
8
+ let transform = { offsetX: 0, offsetY: 0 };
9
+ const onMoveStart = useCallback(() => {
10
+ var _a, _b, _c, _d, _e;
11
+ isDragging.current = true;
12
+ const { offsetX, offsetY } = transform;
13
+ const targetRect = (_a = targetRef == null ? void 0 : targetRef.current) == null ? void 0 : _a.getBoundingClientRect();
14
+ const targetLeft = (_b = targetRect == null ? void 0 : targetRect.left) != null ? _b : 0;
15
+ const targetTop = (_c = targetRect == null ? void 0 : targetRect.top) != null ? _c : 0;
16
+ const targetWidth = (_d = targetRect == null ? void 0 : targetRect.width) != null ? _d : 0;
17
+ const targetHeight = (_e = targetRect == null ? void 0 : targetRect.height) != null ? _e : 0;
18
+ const clientWidth = document.documentElement.clientWidth;
19
+ const clientHeight = document.documentElement.clientHeight;
20
+ const minLeft = -targetLeft + offsetX;
21
+ const minTop = -targetTop + offsetY;
22
+ const maxLeft = clientWidth - targetLeft - targetWidth + offsetX;
23
+ const maxTop = clientHeight - targetTop - targetHeight + offsetY;
24
+ boundary.current = {
25
+ minLeft,
26
+ minTop,
27
+ maxLeft,
28
+ maxTop
29
+ };
30
+ }, [transform, targetRef == null ? void 0 : targetRef.current]);
31
+ const onMove = useCallback(
32
+ (e) => {
33
+ if (isDisabled) {
34
+ return;
35
+ }
36
+ const { offsetX, offsetY } = transform;
37
+ const { minLeft, minTop, maxLeft, maxTop } = boundary.current;
38
+ let moveX = offsetX + e.deltaX;
39
+ let moveY = offsetY + e.deltaY;
40
+ if (!canOverflow) {
41
+ moveX = Math.min(Math.max(moveX, minLeft), maxLeft);
42
+ moveY = Math.min(Math.max(moveY, minTop), maxTop);
43
+ }
44
+ transform = {
45
+ offsetX: moveX,
46
+ offsetY: moveY
47
+ };
48
+ if (targetRef == null ? void 0 : targetRef.current) {
49
+ targetRef.current.style.transform = `translate(${moveX}px, ${moveY}px)`;
50
+ }
51
+ },
52
+ [isDisabled, transform, boundary.current, canOverflow, targetRef == null ? void 0 : targetRef.current]
53
+ );
54
+ const onMoveEnd = useCallback(() => {
55
+ isDragging.current = false;
56
+ }, []);
57
+ const { moveProps } = useMove({
58
+ onMoveStart,
59
+ onMove,
60
+ onMoveEnd
61
+ });
62
+ const preventDefault = useCallback((e) => {
63
+ if (isDragging.current) {
64
+ e.preventDefault();
65
+ }
66
+ }, []);
67
+ useEffect(() => {
68
+ if (!isDisabled) {
69
+ document.body.addEventListener("touchmove", preventDefault, { passive: false });
70
+ }
71
+ return () => {
72
+ document.body.removeEventListener("touchmove", preventDefault);
73
+ };
74
+ }, [isDisabled, preventDefault]);
75
+ return {
76
+ moveProps: {
77
+ ...moveProps,
78
+ style: { cursor: !isDisabled ? "move" : void 0 }
79
+ }
80
+ };
81
+ }
82
+ export {
83
+ useDraggable
84
+ };
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@vx-oss/heroui-v2-use-draggable",
3
+ "version": "2.1.19-alpha.0",
4
+ "description": "This hook can provide drag and drop interaction",
5
+ "keywords": [
6
+ "use-draggable"
7
+ ],
8
+ "author": "Vx OSS Devs <oss-developers@vezham.com>",
9
+ "homepage": "https://vezham.com",
10
+ "license": "MIT",
11
+ "main": "dist/index.js",
12
+ "sideEffects": false,
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/vezham/heroui-v2.git",
22
+ "directory": "packages/hooks/use-draggable"
23
+ },
24
+ "bugs": {
25
+ "url": "https://github.com/vezham/heroui-v2/issues"
26
+ },
27
+ "peerDependencies": {
28
+ "react": ">=18 || >=19.0.0-rc.0"
29
+ },
30
+ "clean-package": "../../../clean-package.config.json",
31
+ "tsup": {
32
+ "clean": true,
33
+ "target": "es2019",
34
+ "format": [
35
+ "cjs",
36
+ "esm"
37
+ ]
38
+ },
39
+ "dependencies": {
40
+ "@react-aria/interactions": "3.25.6"
41
+ },
42
+ "module": "dist/index.mjs",
43
+ "types": "dist/index.d.ts",
44
+ "exports": {
45
+ ".": {
46
+ "types": "./dist/index.d.ts",
47
+ "import": "./dist/index.mjs",
48
+ "require": "./dist/index.js"
49
+ },
50
+ "./package.json": "./package.json"
51
+ },
52
+ "scripts": {
53
+ "build": "tsup src --dts",
54
+ "build:fast": "tsup src",
55
+ "dev": "pnpm build:fast --watch",
56
+ "clean": "rimraf dist .turbo",
57
+ "typecheck": "tsc --noEmit"
58
+ }
59
+ }