mirror of
https://github.com/outline/outline.git
synced 2026-11-21 21:44:30 +03:00
39 lines
932 B
TypeScript
39 lines
932 B
TypeScript
import * as React from "react";
|
|
import useUnmount from "./useUnmount";
|
|
|
|
/**
|
|
* Hook to handle click intent logic with mouse enter/leave events.
|
|
* Sets a timer on mouse enter to call the intent callback after a delay,
|
|
* and clears the timer on mouse leave or component unmount.
|
|
*/
|
|
export default function useClickIntent(
|
|
onClickIntent?: () => void,
|
|
delay = 100
|
|
) {
|
|
const timer = React.useRef<number>();
|
|
|
|
const handleMouseEnter = React.useCallback(() => {
|
|
if (timer.current) {
|
|
clearTimeout(timer.current);
|
|
}
|
|
|
|
if (onClickIntent) {
|
|
timer.current = window.setTimeout(onClickIntent, delay);
|
|
}
|
|
}, [onClickIntent, delay]);
|
|
|
|
const handleMouseLeave = React.useCallback(() => {
|
|
if (timer.current) {
|
|
clearTimeout(timer.current);
|
|
}
|
|
}, []);
|
|
|
|
useUnmount(() => {
|
|
if (timer.current) {
|
|
clearTimeout(timer.current);
|
|
}
|
|
});
|
|
|
|
return { handleMouseEnter, handleMouseLeave };
|
|
}
|