react-reorder-list 0.7.2 → 0.8.1

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 CHANGED
@@ -1,9 +1,9 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2024 Sahil Aggawal, <aggarwalsahil2004@gmail.com>
3
+ Copyright (c) Sahil Aggawal, <aggarwalsahil2004@gmail.com>
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
6
 
7
7
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
8
 
9
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -212,6 +212,6 @@ type PositionChangeParams = {
212
212
  type PositionChangeHandler = (params?: PositionChangeParams) => void;
213
213
  ```
214
214
 
215
- ## Author
215
+ ## License
216
216
 
217
- [Sahil Aggarwal](https://www.github.com/SahilAggarwal2004)
217
+ This project is licensed under the [MIT License](LICENSE).
package/dist/animation.js CHANGED
@@ -1,18 +1,17 @@
1
- import { useState, useLayoutEffect, Children } from "react";
2
- import { usePrevious } from "./hooks.js";
1
+ import { useLayoutEffect, Children } from "react";
2
+ import { useStateWithHistory } from "./hooks.js";
3
3
  import { getKey } from "./utils.js";
4
4
  function calculateBoundingBoxes(children) {
5
5
  const boundingBoxes = {};
6
6
  Children.forEach(children, (child) => {
7
7
  const key = getKey(child);
8
8
  if (key)
9
- boundingBoxes[key] = child.ref.current.getBoundingClientRect();
9
+ boundingBoxes[key] = child.props.ref.current.getBoundingClientRect();
10
10
  });
11
11
  return boundingBoxes;
12
12
  }
13
13
  export default function Animation({ duration, children }) {
14
- const [boundingBox, setBoundingBox] = useState({});
15
- const prevBoundingBox = usePrevious(boundingBox);
14
+ const [boundingBox, prevBoundingBox, setBoundingBox] = useStateWithHistory({});
16
15
  useLayoutEffect(() => {
17
16
  if (duration > 0)
18
17
  setBoundingBox(calculateBoundingBoxes(children));
@@ -22,11 +21,10 @@ export default function Animation({ duration, children }) {
22
21
  useLayoutEffect(() => {
23
22
  if (duration > 0 && prevBoundingBox && Object.keys(prevBoundingBox).length)
24
23
  Children.forEach(children, (child) => {
25
- var _a;
26
- const domNode = (_a = child === null || child === void 0 ? void 0 : child.ref) === null || _a === void 0 ? void 0 : _a.current;
27
24
  const key = getKey(child);
28
25
  if (!key)
29
26
  return;
27
+ const domNode = child.props.ref.current;
30
28
  const { left: prevLeft, top: prevTop } = prevBoundingBox[key] || {};
31
29
  const { left, top } = boundingBox[key];
32
30
  const changeInX = prevLeft - left, changeInY = prevTop - top;
@@ -0,0 +1,4 @@
1
+ export declare const scrollThreshold: {
2
+ x: number;
3
+ y: number;
4
+ };
@@ -0,0 +1 @@
1
+ export const scrollThreshold = { x: 10, y: 100 };
package/dist/hooks.d.ts CHANGED
@@ -4,4 +4,4 @@ export declare function useDraggable(initValue?: boolean): readonly [boolean, {
4
4
  onTouchStart: () => void;
5
5
  onTouchEnd: () => void;
6
6
  }];
7
- export declare function usePrevious<T>(value: T): T | undefined;
7
+ export declare function useStateWithHistory<T>(initValue: T): readonly [T, T | undefined, (value: T) => void];
package/dist/hooks.js CHANGED
@@ -1,4 +1,4 @@
1
- import { useEffect, useRef, useState } from "react";
1
+ import { useState } from "react";
2
2
  export function useDraggable(initValue = false) {
3
3
  const [draggable, setDraggable] = useState(initValue);
4
4
  const enableDragging = () => setDraggable(true);
@@ -6,10 +6,12 @@ export function useDraggable(initValue = false) {
6
6
  const draggableProps = { onMouseEnter: enableDragging, onMouseLeave: disableDragging, onTouchStart: enableDragging, onTouchEnd: disableDragging };
7
7
  return [draggable, draggableProps];
8
8
  }
9
- export function usePrevious(value) {
10
- const prevChildrenRef = useRef();
11
- useEffect(() => {
12
- prevChildrenRef.current = value;
13
- }, [value]);
14
- return prevChildrenRef.current;
9
+ export function useStateWithHistory(initValue) {
10
+ const [state, setState] = useState(initValue);
11
+ const [prevState, setPrevState] = useState();
12
+ function setStateWithHistory(value) {
13
+ setPrevState(state);
14
+ setState(value);
15
+ }
16
+ return [state, prevState, setStateWithHistory];
15
17
  }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import React, { CSSProperties, DetailedHTMLProps, DragEventHandler, HTMLAttributes, ReactNode, TouchEventHandler } from "react";
1
+ import { CSSProperties, DetailedHTMLProps, DragEventHandler, HTMLAttributes, JSX, ReactNode, RefObject, TouchEventHandler } from "react";
2
2
  export type Props = DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>;
3
3
  export type PositionChangeHandler = (params?: {
4
4
  start?: number;
@@ -23,6 +23,7 @@ export type DivTouchEventHandler = TouchEventHandler<HTMLDivElement>;
23
23
  export type ReorderItemProps = {
24
24
  useOnlyIconToDrag: boolean;
25
25
  disable: boolean;
26
+ ref: RefObject<HTMLDivElement | null>;
26
27
  style: CSSProperties;
27
28
  onDragStart?: DivDragEventHandler;
28
29
  onDragEnter: DivDragEventHandler;
@@ -32,5 +33,5 @@ export type ReorderItemProps = {
32
33
  children: ReactNode;
33
34
  };
34
35
  export type { IconProps } from "./icons.js";
35
- export default function ReorderList({ useOnlyIconToDrag, selectedItemOpacity, animationDuration, watchChildrenUpdates, preserveOrder, onPositionChange, disabled, props, children }: ReorderListProps): React.JSX.Element;
36
- export declare function ReorderIcon({ children, style, ...props }: Props): React.JSX.Element;
36
+ export declare function ReorderIcon({ children, style, ...props }: Props): JSX.Element;
37
+ export default function ReorderList({ useOnlyIconToDrag, selectedItemOpacity, animationDuration, watchChildrenUpdates, preserveOrder, onPositionChange, disabled, props, children }: ReorderListProps): JSX.Element;
package/dist/index.js CHANGED
@@ -9,15 +9,34 @@ var __rest = (this && this.__rest) || function (s, e) {
9
9
  }
10
10
  return t;
11
11
  };
12
- import React, { Children, cloneElement, createRef, forwardRef, isValidElement, useEffect, useMemo, useRef, useState } from "react";
12
+ import React, { Children, cloneElement, createRef, isValidElement, useEffect, useMemo, useRef, useState } from "react";
13
13
  import Animation from "./animation.js";
14
- import { PiDotsSixVerticalBold } from "./icons.js";
14
+ import { scrollThreshold } from "./constants.js";
15
15
  import { useDraggable } from "./hooks.js";
16
+ import { PiDotsSixVerticalBold } from "./icons.js";
16
17
  import { swap } from "./utils.js";
17
18
  if (typeof window !== "undefined")
18
19
  import("drag-drop-touch");
19
- const scrollThreshold = { x: 10, y: 100 };
20
- const ReorderItemRef = forwardRef(ReorderItem);
20
+ export function ReorderIcon(_a) {
21
+ var { children = React.createElement(PiDotsSixVerticalBold, null), style } = _a, props = __rest(_a, ["children", "style"]);
22
+ return (React.createElement("span", Object.assign({ style: Object.assign({ cursor: "grab" }, style) }, props), children));
23
+ }
24
+ function ReorderItem(_a) {
25
+ var { useOnlyIconToDrag, disable, ref, style, children, onTouchEnd: propOnTouchEnd } = _a, events = __rest(_a, ["useOnlyIconToDrag", "disable", "ref", "style", "children", "onTouchEnd"]);
26
+ const [draggable, _b] = useDraggable(), { onTouchEnd: draggableOnTouchEnd } = _b, draggableProps = __rest(_b, ["onTouchEnd"]);
27
+ if (!draggable)
28
+ events.onDragStart = undefined;
29
+ const recursiveClone = (children) => Children.map(children, (child) => {
30
+ if (!isValidElement(child))
31
+ return child;
32
+ return cloneElement(child, child.type === ReorderIcon ? draggableProps : {}, recursiveClone(child.props.children));
33
+ });
34
+ const recursiveChildren = useMemo(() => (useOnlyIconToDrag ? recursiveClone(children) : children), [useOnlyIconToDrag, children]);
35
+ return (React.createElement("div", Object.assign({ ref: ref, draggable: !disable && draggable, style: style }, (!disable && Object.assign(Object.assign(Object.assign({}, events), (!useOnlyIconToDrag && draggableProps)), { onTouchEnd: (event) => {
36
+ draggableOnTouchEnd();
37
+ propOnTouchEnd(event);
38
+ } }))), recursiveChildren));
39
+ }
21
40
  export default function ReorderList({ useOnlyIconToDrag = false, selectedItemOpacity = 0.5, animationDuration = 400, watchChildrenUpdates = false, preserveOrder = false, onPositionChange, disabled = false, props, children }) {
22
41
  const ref = useRef(null);
23
42
  const [start, setStart] = useState(-1);
@@ -72,11 +91,12 @@ export default function ReorderList({ useOnlyIconToDrag = false, selectedItemOpa
72
91
  return (React.createElement("div", Object.assign({ ref: ref }, props), disabled ? (children) : (React.createElement(Animation, { duration: +(start !== -1 && !scroll) && animationDuration }, items.map((child, i) => {
73
92
  if (!isValidElement(child))
74
93
  return child;
75
- return (React.createElement(ReorderItemRef, { key: child.key, ref: refs[i], useOnlyIconToDrag: useOnlyIconToDrag, disable: disableArr[i], style: { opacity: selected === i ? selectedItemOpacity : 1, touchAction: "pan-y" }, onDragStart: (event) => {
94
+ return (React.createElement(ReorderItem, { key: child.key, ref: refs[i], useOnlyIconToDrag: useOnlyIconToDrag, disable: disableArr[i], style: { opacity: selected === i ? selectedItemOpacity : 1, touchAction: "pan-y" }, onDragStart: (event) => {
95
+ var _a, _b;
76
96
  event.stopPropagation();
77
97
  setStart(i);
78
98
  setSelected(i);
79
- setTemp({ items, rect: ref.current.children[i].getBoundingClientRect() });
99
+ setTemp({ items, rect: ((_b = (_a = ref.current.childNodes[i]).getBoundingClientRect) === null || _b === void 0 ? void 0 : _b.call(_a)) || {} });
80
100
  }, onDragEnter: (event) => {
81
101
  event.stopPropagation();
82
102
  if (start === -1 || selected === i || isAnimating)
@@ -118,23 +138,3 @@ export default function ReorderList({ useOnlyIconToDrag = false, selectedItemOpa
118
138
  }, onTouchEnd: () => setScroll(undefined) }, child));
119
139
  })))));
120
140
  }
121
- function ReorderItem(_a, ref) {
122
- var { useOnlyIconToDrag, disable, style, children, onTouchEnd: propOnTouchEnd } = _a, events = __rest(_a, ["useOnlyIconToDrag", "disable", "style", "children", "onTouchEnd"]);
123
- const [draggable, _b] = useDraggable(), { onTouchEnd: draggableOnTouchEnd } = _b, draggableProps = __rest(_b, ["onTouchEnd"]);
124
- if (!draggable)
125
- events.onDragStart = undefined;
126
- const recursiveClone = (children) => Children.map(children, (child) => {
127
- if (!isValidElement(child))
128
- return child;
129
- return cloneElement(child, child.type === ReorderIcon ? draggableProps : {}, recursiveClone(child.props.children));
130
- });
131
- const recursiveChildren = useMemo(() => (useOnlyIconToDrag ? recursiveClone(children) : children), [useOnlyIconToDrag, children]);
132
- return (React.createElement("div", Object.assign({ ref: ref, draggable: !disable && draggable, style: style }, (!disable && Object.assign(Object.assign(Object.assign({}, events), (!useOnlyIconToDrag && draggableProps)), { onTouchEnd: (event) => {
133
- draggableOnTouchEnd();
134
- propOnTouchEnd(event);
135
- } }))), recursiveChildren));
136
- }
137
- export function ReorderIcon(_a) {
138
- var { children = React.createElement(PiDotsSixVerticalBold, null), style } = _a, props = __rest(_a, ["children", "style"]);
139
- return (React.createElement("span", Object.assign({ style: Object.assign({ cursor: "grab" }, style) }, props), children));
140
- }
package/package.json CHANGED
@@ -1,13 +1,10 @@
1
1
  {
2
2
  "name": "react-reorder-list",
3
- "version": "0.7.2",
3
+ "version": "0.8.1",
4
4
  "description": "A simple react component that facilitates the reordering of JSX/HTML elements through drag-and-drop functionality, allowing for easy position changes.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
8
- "scripts": {
9
- "build": "bun i && tsc"
10
- },
11
8
  "repository": {
12
9
  "type": "git",
13
10
  "url": "git+https://github.com/SahilAggarwal2004/react-reorder-list.git"
@@ -29,12 +26,15 @@
29
26
  },
30
27
  "homepage": "https://github.com/SahilAggarwal2004/react-reorder-list#readme",
31
28
  "peerDependencies": {
32
- "react": ">=17.0.0"
29
+ "react": "^19.0.0"
33
30
  },
34
31
  "devDependencies": {
35
- "@types/react": "^18.2.79"
32
+ "@types/react": "^19.0.2"
36
33
  },
37
34
  "dependencies": {
38
35
  "drag-drop-touch": "^1.3.1"
36
+ },
37
+ "scripts": {
38
+ "build": "pnpm i && tsc"
39
39
  }
40
40
  }