Better throttle instead of debounce

pull/251/head
Lim Chee Aun 2023-10-04 10:05:21 +08:00
rodzic d429ef9161
commit be964f933c
1 zmienionych plików z 12 dodań i 18 usunięć

Wyświetl plik

@ -1,11 +1,10 @@
import { useRef } from 'preact/hooks';
import { useDebouncedCallback } from 'use-debounce';
import { useThrottledCallback } from 'use-debounce';
import useResizeObserver from 'use-resize-observer';
export default function useTruncated({ className = 'truncated' } = {}) {
const ref = useRef();
const onResize = useDebouncedCallback(
({ height }) => {
const onResize = useThrottledCallback(({ height }) => {
if (ref.current) {
const { scrollHeight } = ref.current;
let truncated = scrollHeight > height;
@ -16,12 +15,7 @@ export default function useTruncated({ className = 'truncated' } = {}) {
}
ref.current.classList.toggle(className, truncated);
}
},
300,
{
maxWait: 2000,
},
);
}, 300);
useResizeObserver({
ref,
box: 'border-box',