@yogiswara/honcho-editor-ui 3.0.1 → 3.0.2
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.
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export default function useSliderEvents(onDragStart: () => void, onDragEnd: () => void, isBatchMode: boolean): React.RefObject<HTMLDivElement>;
|
|
1
|
+
export default function useSliderEvents(onDragStart: () => void, onDragEnd: () => void, isBatchMode: boolean): import("react").RefObject<HTMLElement>;
|
|
@@ -1,47 +1,38 @@
|
|
|
1
1
|
import { useEffect, useRef } from "react";
|
|
2
2
|
export default function useSliderEvents(onDragStart, onDragEnd, isBatchMode) {
|
|
3
|
-
|
|
3
|
+
// Use a generic HTMLElement to work for both <div> and <span>
|
|
4
|
+
const elementRef = useRef(null);
|
|
4
5
|
useEffect(() => {
|
|
5
|
-
const
|
|
6
|
-
if (!
|
|
6
|
+
const element = elementRef.current;
|
|
7
|
+
if (!element)
|
|
7
8
|
return;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
document.addEventListener("touchend", handleTouchEnd);
|
|
9
|
+
// This handler is attached to the WINDOW to reliably catch the end of the drag.
|
|
10
|
+
const handleDragEnd = () => {
|
|
11
|
+
onDragEnd();
|
|
12
|
+
// Clean up the global listeners immediately after they fire.
|
|
13
|
+
window.removeEventListener('mouseup', handleDragEnd);
|
|
14
|
+
window.removeEventListener('touchend', handleDragEnd);
|
|
15
15
|
};
|
|
16
|
-
|
|
16
|
+
// This handler is attached to the slider THUMB to start the drag.
|
|
17
|
+
const handleDragStart = () => {
|
|
17
18
|
if (!isBatchMode) {
|
|
18
19
|
onDragStart();
|
|
19
20
|
}
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
};
|
|
24
|
-
const handleMouseUp = () => {
|
|
25
|
-
onDragEnd();
|
|
26
|
-
// Remove document listeners when dragging ends
|
|
27
|
-
document.removeEventListener("mouseup", handleMouseUp);
|
|
28
|
-
document.removeEventListener("touchend", handleTouchEnd);
|
|
29
|
-
};
|
|
30
|
-
const handleTouchEnd = () => {
|
|
31
|
-
onDragEnd();
|
|
32
|
-
// Remove document listeners when dragging ends
|
|
33
|
-
document.removeEventListener("mouseup", handleMouseUp);
|
|
34
|
-
document.removeEventListener("touchend", handleTouchEnd);
|
|
21
|
+
// When dragging starts, listen on the entire window for the end.
|
|
22
|
+
window.addEventListener('mouseup', handleDragEnd);
|
|
23
|
+
window.addEventListener('touchend', handleDragEnd);
|
|
35
24
|
};
|
|
36
|
-
|
|
37
|
-
|
|
25
|
+
// Attach "start" listeners to the specific element (the thumb).
|
|
26
|
+
element.addEventListener('mousedown', handleDragStart);
|
|
27
|
+
element.addEventListener('touchstart', handleDragStart, { passive: true });
|
|
28
|
+
// Cleanup function when the component unmounts.
|
|
38
29
|
return () => {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
//
|
|
42
|
-
|
|
43
|
-
|
|
30
|
+
element.removeEventListener('mousedown', handleDragStart);
|
|
31
|
+
element.removeEventListener('touchstart', handleDragStart);
|
|
32
|
+
// Ensure any lingering window listeners are also removed.
|
|
33
|
+
window.removeEventListener('mouseup', handleDragEnd);
|
|
34
|
+
window.removeEventListener('touchend', handleDragEnd);
|
|
44
35
|
};
|
|
45
36
|
}, [onDragStart, onDragEnd, isBatchMode]);
|
|
46
|
-
return
|
|
37
|
+
return elementRef;
|
|
47
38
|
}
|