mirror of
https://github.com/Tria-plc/emaui.git
synced 2026-08-27 16:40:57 +00:00
33 lines
965 B
JavaScript
33 lines
965 B
JavaScript
'use client';
|
|
import { useState, useRef, useCallback, useEffect } from 'react';
|
|
|
|
function useDebouncedValue(value, wait, options = { leading: false }) {
|
|
const [_value, setValue] = useState(value);
|
|
const mountedRef = useRef(false);
|
|
const timeoutRef = useRef(null);
|
|
const cooldownRef = useRef(false);
|
|
const cancel = useCallback(() => window.clearTimeout(timeoutRef.current), []);
|
|
useEffect(() => {
|
|
if (mountedRef.current) {
|
|
if (!cooldownRef.current && options.leading) {
|
|
cooldownRef.current = true;
|
|
setValue(value);
|
|
} else {
|
|
cancel();
|
|
timeoutRef.current = window.setTimeout(() => {
|
|
cooldownRef.current = false;
|
|
setValue(value);
|
|
}, wait);
|
|
}
|
|
}
|
|
}, [value, options.leading, wait]);
|
|
useEffect(() => {
|
|
mountedRef.current = true;
|
|
return cancel;
|
|
}, []);
|
|
return [_value, cancel];
|
|
}
|
|
|
|
export { useDebouncedValue };
|
|
//# sourceMappingURL=use-debounced-value.mjs.map
|