@reactuses/core 5.0.2 → 5.0.3

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/dist/index.cjs CHANGED
@@ -61,15 +61,11 @@ function getTargetElement(target, defaultElement) {
61
61
  return targetElement;
62
62
  }
63
63
 
64
- const isPrimitive$1 = (val)=>val !== Object(val);
65
64
  const useCustomCompareEffect = (effect, deps, depsEqual)=>{
66
65
  if (process.env.NODE_ENV !== "production") {
67
66
  if (!Array.isArray(deps) || !deps.length) {
68
67
  console.warn("`useCustomCompareEffect` should not be used with no dependencies. Use React.useEffect instead.");
69
68
  }
70
- if (deps.every(isPrimitive$1)) {
71
- console.warn("`useCustomCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead.");
72
- }
73
69
  if (typeof depsEqual !== "function") {
74
70
  console.warn("`useCustomCompareEffect` should be used with depsEqual callback for comparing deps list");
75
71
  }
@@ -82,15 +78,11 @@ const useCustomCompareEffect = (effect, deps, depsEqual)=>{
82
78
  React.useEffect(effect, ref.current);
83
79
  };
84
80
 
85
- const isPrimitive = (val)=>val !== Object(val);
86
81
  const useDeepCompareEffect = (effect, deps)=>{
87
82
  if (process.env.NODE_ENV !== "production") {
88
83
  if (!Array.isArray(deps) || !deps.length) {
89
84
  console.warn("`useDeepCompareEffect` should not be used with no dependencies. Use React.useEffect instead.");
90
85
  }
91
- if (deps.every(isPrimitive)) {
92
- console.warn("`useDeepCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead.");
93
- }
94
86
  }
95
87
  useCustomCompareEffect(effect, deps, lodashEs.isEqual);
96
88
  };
@@ -790,7 +782,7 @@ const useDoubleClick = ({ target, latency = 300, onSingleClick = ()=>{}, onDoubl
790
782
  };
791
783
 
792
784
  const useDraggable = (target, options = {})=>{
793
- const draggingElement = options.draggingElement;
785
+ const { draggingElement, containerElement } = options;
794
786
  var _options_handle;
795
787
  const draggingHandle = (_options_handle = options.handle) != null ? _options_handle : target;
796
788
  var _options_initialValue;
@@ -798,6 +790,15 @@ const useDraggable = (target, options = {})=>{
798
790
  x: 0,
799
791
  y: 0
800
792
  });
793
+ useDeepCompareEffect(()=>{
794
+ var _options_initialValue;
795
+ setPositon((_options_initialValue = options.initialValue) != null ? _options_initialValue : {
796
+ x: 0,
797
+ y: 0
798
+ });
799
+ }, [
800
+ options.initialValue
801
+ ]);
801
802
  const [pressedDelta, setPressedDelta] = React.useState();
802
803
  const filterEvent = (e)=>{
803
804
  if (options.pointerTypes) {
@@ -814,6 +815,7 @@ const useDraggable = (target, options = {})=>{
814
815
  }
815
816
  };
816
817
  const start = (e)=>{
818
+ var _container_getBoundingClientRect;
817
819
  const element = target.current;
818
820
  if (!filterEvent(e) || !element) {
819
821
  return;
@@ -821,10 +823,12 @@ const useDraggable = (target, options = {})=>{
821
823
  if (options.exact && e.target !== element) {
822
824
  return;
823
825
  }
824
- const rect = element.getBoundingClientRect();
826
+ const container = containerElement == null ? void 0 : containerElement.current;
827
+ const containerRect = container == null ? void 0 : (_container_getBoundingClientRect = container.getBoundingClientRect) == null ? void 0 : _container_getBoundingClientRect.call(container);
828
+ const targetRect = element.getBoundingClientRect();
825
829
  const pos = {
826
- x: e.pageX - rect.left,
827
- y: e.pageY - rect.top
830
+ x: e.clientX - (container ? targetRect.left - containerRect.left + container.scrollLeft : targetRect.left),
831
+ y: e.clientY - (container ? targetRect.top - containerRect.top + container.scrollTop : targetRect.top)
828
832
  };
829
833
  if ((options.onStart == null ? void 0 : options.onStart.call(options, pos, e)) === false) {
830
834
  return;
@@ -833,15 +837,25 @@ const useDraggable = (target, options = {})=>{
833
837
  handleEvent(e);
834
838
  };
835
839
  const move = (e)=>{
836
- if (!filterEvent(e)) {
840
+ const element = target.current;
841
+ if (!filterEvent(e) || !element) {
837
842
  return;
838
843
  }
839
844
  if (!pressedDelta) {
840
845
  return;
841
846
  }
847
+ const container = containerElement == null ? void 0 : containerElement.current;
848
+ const targetRect = element.getBoundingClientRect();
849
+ let { x, y } = position;
850
+ x = e.clientX - pressedDelta.x;
851
+ y = e.clientY - pressedDelta.y;
852
+ if (container) {
853
+ x = Math.min(Math.max(0, x), container.scrollWidth - targetRect.width);
854
+ y = Math.min(Math.max(0, y), container.scrollHeight - targetRect.height);
855
+ }
842
856
  setPositon({
843
- x: e.pageX - pressedDelta.x,
844
- y: e.pageY - pressedDelta.y
857
+ x,
858
+ y
845
859
  });
846
860
  options.onMove == null ? void 0 : options.onMove.call(options, position, e);
847
861
  handleEvent(e);
package/dist/index.d.cts CHANGED
@@ -491,7 +491,13 @@ interface UseDraggableOptions {
491
491
  * @zh 将“pointermove”和“pointerup”事件附加到的dom元素
492
492
  * @defaultValue window
493
493
  */
494
- draggingElement?: RefObject<HTMLElement | SVGElement | Window | Document>;
494
+ draggingElement?: BasicTarget<HTMLElement | SVGElement>;
495
+ /**
496
+ * @en Element for calculating bounds (If not set, it will use the event's target).
497
+ * @zh 设置拖拽容器边界
498
+ * @defaultValue undefined
499
+ */
500
+ containerElement?: RefObject<HTMLElement | SVGAElement>;
495
501
  /**
496
502
  * @en Handle that triggers the drag event
497
503
  * @zh 触发拖动事件的dom元素
package/dist/index.d.mts CHANGED
@@ -491,7 +491,13 @@ interface UseDraggableOptions {
491
491
  * @zh 将“pointermove”和“pointerup”事件附加到的dom元素
492
492
  * @defaultValue window
493
493
  */
494
- draggingElement?: RefObject<HTMLElement | SVGElement | Window | Document>;
494
+ draggingElement?: BasicTarget<HTMLElement | SVGElement>;
495
+ /**
496
+ * @en Element for calculating bounds (If not set, it will use the event's target).
497
+ * @zh 设置拖拽容器边界
498
+ * @defaultValue undefined
499
+ */
500
+ containerElement?: RefObject<HTMLElement | SVGAElement>;
495
501
  /**
496
502
  * @en Handle that triggers the drag event
497
503
  * @zh 触发拖动事件的dom元素
package/dist/index.d.ts CHANGED
@@ -491,7 +491,13 @@ interface UseDraggableOptions {
491
491
  * @zh 将“pointermove”和“pointerup”事件附加到的dom元素
492
492
  * @defaultValue window
493
493
  */
494
- draggingElement?: RefObject<HTMLElement | SVGElement | Window | Document>;
494
+ draggingElement?: BasicTarget<HTMLElement | SVGElement>;
495
+ /**
496
+ * @en Element for calculating bounds (If not set, it will use the event's target).
497
+ * @zh 设置拖拽容器边界
498
+ * @defaultValue undefined
499
+ */
500
+ containerElement?: RefObject<HTMLElement | SVGAElement>;
495
501
  /**
496
502
  * @en Handle that triggers the drag event
497
503
  * @zh 触发拖动事件的dom元素
package/dist/index.mjs CHANGED
@@ -54,15 +54,11 @@ function getTargetElement(target, defaultElement) {
54
54
  return targetElement;
55
55
  }
56
56
 
57
- const isPrimitive$1 = (val)=>val !== Object(val);
58
57
  const useCustomCompareEffect = (effect, deps, depsEqual)=>{
59
58
  if (process.env.NODE_ENV !== "production") {
60
59
  if (!Array.isArray(deps) || !deps.length) {
61
60
  console.warn("`useCustomCompareEffect` should not be used with no dependencies. Use React.useEffect instead.");
62
61
  }
63
- if (deps.every(isPrimitive$1)) {
64
- console.warn("`useCustomCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead.");
65
- }
66
62
  if (typeof depsEqual !== "function") {
67
63
  console.warn("`useCustomCompareEffect` should be used with depsEqual callback for comparing deps list");
68
64
  }
@@ -75,15 +71,11 @@ const useCustomCompareEffect = (effect, deps, depsEqual)=>{
75
71
  useEffect(effect, ref.current);
76
72
  };
77
73
 
78
- const isPrimitive = (val)=>val !== Object(val);
79
74
  const useDeepCompareEffect = (effect, deps)=>{
80
75
  if (process.env.NODE_ENV !== "production") {
81
76
  if (!Array.isArray(deps) || !deps.length) {
82
77
  console.warn("`useDeepCompareEffect` should not be used with no dependencies. Use React.useEffect instead.");
83
78
  }
84
- if (deps.every(isPrimitive)) {
85
- console.warn("`useDeepCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead.");
86
- }
87
79
  }
88
80
  useCustomCompareEffect(effect, deps, isEqual);
89
81
  };
@@ -783,7 +775,7 @@ const useDoubleClick = ({ target, latency = 300, onSingleClick = ()=>{}, onDoubl
783
775
  };
784
776
 
785
777
  const useDraggable = (target, options = {})=>{
786
- const draggingElement = options.draggingElement;
778
+ const { draggingElement, containerElement } = options;
787
779
  var _options_handle;
788
780
  const draggingHandle = (_options_handle = options.handle) != null ? _options_handle : target;
789
781
  var _options_initialValue;
@@ -791,6 +783,15 @@ const useDraggable = (target, options = {})=>{
791
783
  x: 0,
792
784
  y: 0
793
785
  });
786
+ useDeepCompareEffect(()=>{
787
+ var _options_initialValue;
788
+ setPositon((_options_initialValue = options.initialValue) != null ? _options_initialValue : {
789
+ x: 0,
790
+ y: 0
791
+ });
792
+ }, [
793
+ options.initialValue
794
+ ]);
794
795
  const [pressedDelta, setPressedDelta] = useState();
795
796
  const filterEvent = (e)=>{
796
797
  if (options.pointerTypes) {
@@ -807,6 +808,7 @@ const useDraggable = (target, options = {})=>{
807
808
  }
808
809
  };
809
810
  const start = (e)=>{
811
+ var _container_getBoundingClientRect;
810
812
  const element = target.current;
811
813
  if (!filterEvent(e) || !element) {
812
814
  return;
@@ -814,10 +816,12 @@ const useDraggable = (target, options = {})=>{
814
816
  if (options.exact && e.target !== element) {
815
817
  return;
816
818
  }
817
- const rect = element.getBoundingClientRect();
819
+ const container = containerElement == null ? void 0 : containerElement.current;
820
+ const containerRect = container == null ? void 0 : (_container_getBoundingClientRect = container.getBoundingClientRect) == null ? void 0 : _container_getBoundingClientRect.call(container);
821
+ const targetRect = element.getBoundingClientRect();
818
822
  const pos = {
819
- x: e.pageX - rect.left,
820
- y: e.pageY - rect.top
823
+ x: e.clientX - (container ? targetRect.left - containerRect.left + container.scrollLeft : targetRect.left),
824
+ y: e.clientY - (container ? targetRect.top - containerRect.top + container.scrollTop : targetRect.top)
821
825
  };
822
826
  if ((options.onStart == null ? void 0 : options.onStart.call(options, pos, e)) === false) {
823
827
  return;
@@ -826,15 +830,25 @@ const useDraggable = (target, options = {})=>{
826
830
  handleEvent(e);
827
831
  };
828
832
  const move = (e)=>{
829
- if (!filterEvent(e)) {
833
+ const element = target.current;
834
+ if (!filterEvent(e) || !element) {
830
835
  return;
831
836
  }
832
837
  if (!pressedDelta) {
833
838
  return;
834
839
  }
840
+ const container = containerElement == null ? void 0 : containerElement.current;
841
+ const targetRect = element.getBoundingClientRect();
842
+ let { x, y } = position;
843
+ x = e.clientX - pressedDelta.x;
844
+ y = e.clientY - pressedDelta.y;
845
+ if (container) {
846
+ x = Math.min(Math.max(0, x), container.scrollWidth - targetRect.width);
847
+ y = Math.min(Math.max(0, y), container.scrollHeight - targetRect.height);
848
+ }
835
849
  setPositon({
836
- x: e.pageX - pressedDelta.x,
837
- y: e.pageY - pressedDelta.y
850
+ x,
851
+ y
838
852
  });
839
853
  options.onMove == null ? void 0 : options.onMove.call(options, position, e);
840
854
  handleEvent(e);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reactuses/core",
3
- "version": "5.0.2",
3
+ "version": "5.0.3",
4
4
  "license": "Unlicense",
5
5
  "homepage": "https://www.reactuse.com/",
6
6
  "repository": {