@reactuses/core 5.0.2 → 5.0.4

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
  };
@@ -585,15 +577,10 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
585
577
  onError(err);
586
578
  }
587
579
  const type = guessSerializerType(defaultValue);
588
- const serializer = React.useMemo(()=>{
589
- var _options_serializer;
590
- return (_options_serializer = options.serializer) != null ? _options_serializer : StorageSerializers[type];
591
- }, [
592
- options.serializer,
593
- type
594
- ]);
580
+ var _options_serializer;
581
+ const serializer = (_options_serializer = options.serializer) != null ? _options_serializer : StorageSerializers[type];
595
582
  const [state, setState] = React.useState(getInitialState$2(key, defaultValue, storage, serializer, onError));
596
- React.useEffect(()=>{
583
+ useDeepCompareEffect(()=>{
597
584
  const data = effectStorageValue ? isFunction(effectStorageValue) ? effectStorageValue() : effectStorageValue : defaultValue;
598
585
  const getStoredValue = ()=>{
599
586
  try {
@@ -609,7 +596,6 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
609
596
  }
610
597
  };
611
598
  setState(getStoredValue());
612
- // eslint-disable-next-line react-hooks/exhaustive-deps
613
599
  }, [
614
600
  key,
615
601
  serializer,
@@ -790,7 +776,7 @@ const useDoubleClick = ({ target, latency = 300, onSingleClick = ()=>{}, onDoubl
790
776
  };
791
777
 
792
778
  const useDraggable = (target, options = {})=>{
793
- const draggingElement = options.draggingElement;
779
+ const { draggingElement, containerElement } = options;
794
780
  var _options_handle;
795
781
  const draggingHandle = (_options_handle = options.handle) != null ? _options_handle : target;
796
782
  var _options_initialValue;
@@ -798,6 +784,15 @@ const useDraggable = (target, options = {})=>{
798
784
  x: 0,
799
785
  y: 0
800
786
  });
787
+ useDeepCompareEffect(()=>{
788
+ var _options_initialValue;
789
+ setPositon((_options_initialValue = options.initialValue) != null ? _options_initialValue : {
790
+ x: 0,
791
+ y: 0
792
+ });
793
+ }, [
794
+ options.initialValue
795
+ ]);
801
796
  const [pressedDelta, setPressedDelta] = React.useState();
802
797
  const filterEvent = (e)=>{
803
798
  if (options.pointerTypes) {
@@ -814,6 +809,7 @@ const useDraggable = (target, options = {})=>{
814
809
  }
815
810
  };
816
811
  const start = (e)=>{
812
+ var _container_getBoundingClientRect;
817
813
  const element = target.current;
818
814
  if (!filterEvent(e) || !element) {
819
815
  return;
@@ -821,10 +817,12 @@ const useDraggable = (target, options = {})=>{
821
817
  if (options.exact && e.target !== element) {
822
818
  return;
823
819
  }
824
- const rect = element.getBoundingClientRect();
820
+ const container = containerElement == null ? void 0 : containerElement.current;
821
+ const containerRect = container == null ? void 0 : (_container_getBoundingClientRect = container.getBoundingClientRect) == null ? void 0 : _container_getBoundingClientRect.call(container);
822
+ const targetRect = element.getBoundingClientRect();
825
823
  const pos = {
826
- x: e.pageX - rect.left,
827
- y: e.pageY - rect.top
824
+ x: e.clientX - (container ? targetRect.left - containerRect.left + container.scrollLeft : targetRect.left),
825
+ y: e.clientY - (container ? targetRect.top - containerRect.top + container.scrollTop : targetRect.top)
828
826
  };
829
827
  if ((options.onStart == null ? void 0 : options.onStart.call(options, pos, e)) === false) {
830
828
  return;
@@ -833,15 +831,25 @@ const useDraggable = (target, options = {})=>{
833
831
  handleEvent(e);
834
832
  };
835
833
  const move = (e)=>{
836
- if (!filterEvent(e)) {
834
+ const element = target.current;
835
+ if (!filterEvent(e) || !element) {
837
836
  return;
838
837
  }
839
838
  if (!pressedDelta) {
840
839
  return;
841
840
  }
841
+ const container = containerElement == null ? void 0 : containerElement.current;
842
+ const targetRect = element.getBoundingClientRect();
843
+ let { x, y } = position;
844
+ x = e.clientX - pressedDelta.x;
845
+ y = e.clientY - pressedDelta.y;
846
+ if (container) {
847
+ x = Math.min(Math.max(0, x), container.scrollWidth - targetRect.width);
848
+ y = Math.min(Math.max(0, y), container.scrollHeight - targetRect.height);
849
+ }
842
850
  setPositon({
843
- x: e.pageX - pressedDelta.x,
844
- y: e.pageY - pressedDelta.y
851
+ x,
852
+ y
845
853
  });
846
854
  options.onMove == null ? void 0 : options.onMove.call(options, position, e);
847
855
  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
  };
@@ -578,15 +570,10 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
578
570
  onError(err);
579
571
  }
580
572
  const type = guessSerializerType(defaultValue);
581
- const serializer = useMemo(()=>{
582
- var _options_serializer;
583
- return (_options_serializer = options.serializer) != null ? _options_serializer : StorageSerializers[type];
584
- }, [
585
- options.serializer,
586
- type
587
- ]);
573
+ var _options_serializer;
574
+ const serializer = (_options_serializer = options.serializer) != null ? _options_serializer : StorageSerializers[type];
588
575
  const [state, setState] = useState(getInitialState$2(key, defaultValue, storage, serializer, onError));
589
- useEffect(()=>{
576
+ useDeepCompareEffect(()=>{
590
577
  const data = effectStorageValue ? isFunction(effectStorageValue) ? effectStorageValue() : effectStorageValue : defaultValue;
591
578
  const getStoredValue = ()=>{
592
579
  try {
@@ -602,7 +589,6 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
602
589
  }
603
590
  };
604
591
  setState(getStoredValue());
605
- // eslint-disable-next-line react-hooks/exhaustive-deps
606
592
  }, [
607
593
  key,
608
594
  serializer,
@@ -783,7 +769,7 @@ const useDoubleClick = ({ target, latency = 300, onSingleClick = ()=>{}, onDoubl
783
769
  };
784
770
 
785
771
  const useDraggable = (target, options = {})=>{
786
- const draggingElement = options.draggingElement;
772
+ const { draggingElement, containerElement } = options;
787
773
  var _options_handle;
788
774
  const draggingHandle = (_options_handle = options.handle) != null ? _options_handle : target;
789
775
  var _options_initialValue;
@@ -791,6 +777,15 @@ const useDraggable = (target, options = {})=>{
791
777
  x: 0,
792
778
  y: 0
793
779
  });
780
+ useDeepCompareEffect(()=>{
781
+ var _options_initialValue;
782
+ setPositon((_options_initialValue = options.initialValue) != null ? _options_initialValue : {
783
+ x: 0,
784
+ y: 0
785
+ });
786
+ }, [
787
+ options.initialValue
788
+ ]);
794
789
  const [pressedDelta, setPressedDelta] = useState();
795
790
  const filterEvent = (e)=>{
796
791
  if (options.pointerTypes) {
@@ -807,6 +802,7 @@ const useDraggable = (target, options = {})=>{
807
802
  }
808
803
  };
809
804
  const start = (e)=>{
805
+ var _container_getBoundingClientRect;
810
806
  const element = target.current;
811
807
  if (!filterEvent(e) || !element) {
812
808
  return;
@@ -814,10 +810,12 @@ const useDraggable = (target, options = {})=>{
814
810
  if (options.exact && e.target !== element) {
815
811
  return;
816
812
  }
817
- const rect = element.getBoundingClientRect();
813
+ const container = containerElement == null ? void 0 : containerElement.current;
814
+ const containerRect = container == null ? void 0 : (_container_getBoundingClientRect = container.getBoundingClientRect) == null ? void 0 : _container_getBoundingClientRect.call(container);
815
+ const targetRect = element.getBoundingClientRect();
818
816
  const pos = {
819
- x: e.pageX - rect.left,
820
- y: e.pageY - rect.top
817
+ x: e.clientX - (container ? targetRect.left - containerRect.left + container.scrollLeft : targetRect.left),
818
+ y: e.clientY - (container ? targetRect.top - containerRect.top + container.scrollTop : targetRect.top)
821
819
  };
822
820
  if ((options.onStart == null ? void 0 : options.onStart.call(options, pos, e)) === false) {
823
821
  return;
@@ -826,15 +824,25 @@ const useDraggable = (target, options = {})=>{
826
824
  handleEvent(e);
827
825
  };
828
826
  const move = (e)=>{
829
- if (!filterEvent(e)) {
827
+ const element = target.current;
828
+ if (!filterEvent(e) || !element) {
830
829
  return;
831
830
  }
832
831
  if (!pressedDelta) {
833
832
  return;
834
833
  }
834
+ const container = containerElement == null ? void 0 : containerElement.current;
835
+ const targetRect = element.getBoundingClientRect();
836
+ let { x, y } = position;
837
+ x = e.clientX - pressedDelta.x;
838
+ y = e.clientY - pressedDelta.y;
839
+ if (container) {
840
+ x = Math.min(Math.max(0, x), container.scrollWidth - targetRect.width);
841
+ y = Math.min(Math.max(0, y), container.scrollHeight - targetRect.height);
842
+ }
835
843
  setPositon({
836
- x: e.pageX - pressedDelta.x,
837
- y: e.pageY - pressedDelta.y
844
+ x,
845
+ y
838
846
  });
839
847
  options.onMove == null ? void 0 : options.onMove.call(options, position, e);
840
848
  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.4",
4
4
  "license": "Unlicense",
5
5
  "homepage": "https://www.reactuse.com/",
6
6
  "repository": {