@yogiswara/honcho-editor-ui 2.11.4 → 3.0.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.
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export default function useSliderEvents(onDragStart: () => void, onDragEnd: () => void, isBatchMode: boolean): React.RefObject<HTMLSpanElement>;
|
|
1
|
+
export default function useSliderEvents(onDragStart: () => void, onDragEnd: () => void, isBatchMode: boolean): import("react").RefObject<HTMLSpanElement>;
|
|
@@ -5,28 +5,32 @@ export default function useSliderEvents(onDragStart, onDragEnd, isBatchMode) {
|
|
|
5
5
|
const slider = sliderRef.current;
|
|
6
6
|
if (!slider)
|
|
7
7
|
return;
|
|
8
|
-
//
|
|
9
|
-
const
|
|
8
|
+
// This function will be attached to the WINDOW to listen for the end of a drag.
|
|
9
|
+
const handleDragEnd = () => {
|
|
10
|
+
onDragEnd();
|
|
11
|
+
// IMPORTANT: Clean up the global listeners immediately after the drag ends.
|
|
12
|
+
window.removeEventListener('mouseup', handleDragEnd);
|
|
13
|
+
window.removeEventListener('touchend', handleDragEnd);
|
|
14
|
+
};
|
|
15
|
+
// This function is attached ONLY to the slider thumb to start a drag.
|
|
16
|
+
const handleDragStart = () => {
|
|
10
17
|
if (!isBatchMode) {
|
|
11
18
|
onDragStart();
|
|
12
19
|
}
|
|
20
|
+
// When a drag starts, attach the listeners to the window to catch the end.
|
|
21
|
+
window.addEventListener('mouseup', handleDragEnd);
|
|
22
|
+
window.addEventListener('touchend', handleDragEnd);
|
|
13
23
|
};
|
|
14
|
-
//
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
// Listen for BOTH mouse and touch start events
|
|
19
|
-
slider.addEventListener('mousedown', handleStart);
|
|
20
|
-
slider.addEventListener('touchstart', handleStart, { passive: true });
|
|
21
|
-
// Listen for BOTH mouse and touch end events
|
|
22
|
-
slider.addEventListener('mouseup', handleEnd);
|
|
23
|
-
slider.addEventListener('touchend', handleEnd, { passive: true });
|
|
24
|
-
// Cleanup function to remove all listeners
|
|
24
|
+
// Attach the "start" listeners to the slider thumb.
|
|
25
|
+
slider.addEventListener('mousedown', handleDragStart);
|
|
26
|
+
slider.addEventListener('touchstart', handleDragStart, { passive: true });
|
|
27
|
+
// The main cleanup function for when the component unmounts.
|
|
25
28
|
return () => {
|
|
26
|
-
slider.removeEventListener('mousedown',
|
|
27
|
-
slider.removeEventListener('touchstart',
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
slider.removeEventListener('mousedown', handleDragStart);
|
|
30
|
+
slider.removeEventListener('touchstart', handleDragStart);
|
|
31
|
+
// Also remove any lingering window listeners, just in case.
|
|
32
|
+
window.removeEventListener('mouseup', handleDragEnd);
|
|
33
|
+
window.removeEventListener('touchend', handleDragEnd);
|
|
30
34
|
};
|
|
31
35
|
}, [onDragStart, onDragEnd, isBatchMode]);
|
|
32
36
|
return sliderRef;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type SliderChangeHandler = (event: Event, value: number | number[]) => void;
|
|
2
|
+
/**
|
|
3
|
+
* A hook to reliably track the start and end of a slider drag interaction.
|
|
4
|
+
* @param onDragStart - Callback for when dragging begins.
|
|
5
|
+
* @param onDragEnd - Callback for when dragging ends.
|
|
6
|
+
* @returns { handleChange, handleChangeCommitted } - Handlers to be passed to the MUI Slider.
|
|
7
|
+
*/
|
|
8
|
+
export declare function useSliderInteraction(onDragStart: () => void, onDragEnd: () => void): {
|
|
9
|
+
handleChange: SliderChangeHandler;
|
|
10
|
+
handleChangeCommitted: SliderChangeHandler;
|
|
11
|
+
};
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { useRef } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* A hook to reliably track the start and end of a slider drag interaction.
|
|
4
|
+
* @param onDragStart - Callback for when dragging begins.
|
|
5
|
+
* @param onDragEnd - Callback for when dragging ends.
|
|
6
|
+
* @returns { handleChange, handleChangeCommitted } - Handlers to be passed to the MUI Slider.
|
|
7
|
+
*/
|
|
8
|
+
export function useSliderInteraction(onDragStart, onDragEnd) {
|
|
9
|
+
const isDraggingRef = useRef(false);
|
|
10
|
+
// This handler wraps the original `onChange`.
|
|
11
|
+
const handleChange = (event, value) => {
|
|
12
|
+
if (!isDraggingRef.current) {
|
|
13
|
+
// If not already dragging, this is the start.
|
|
14
|
+
isDraggingRef.current = true;
|
|
15
|
+
onDragStart();
|
|
16
|
+
}
|
|
17
|
+
// You would call your original `onChange` logic here if needed.
|
|
18
|
+
// For example: props.setTempScore("tempScore", value as number);
|
|
19
|
+
};
|
|
20
|
+
// This handler wraps the original `onChangeCommitted`.
|
|
21
|
+
const handleChangeCommitted = (event, value) => {
|
|
22
|
+
if (isDraggingRef.current) {
|
|
23
|
+
// If a drag was in progress, this is the end.
|
|
24
|
+
isDraggingRef.current = false;
|
|
25
|
+
onDragEnd();
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
return { handleChange, handleChangeCommitted };
|
|
29
|
+
}
|