mirror of
https://github.com/cheeaun/phanpy.git
synced 2025-02-25 09:18:51 +01:00
Better throttle instead of debounce
This commit is contained in:
parent
d429ef9161
commit
be964f933c
1 changed files with 12 additions and 18 deletions
|
@ -1,11 +1,10 @@
|
||||||
import { useRef } from 'preact/hooks';
|
import { useRef } from 'preact/hooks';
|
||||||
import { useDebouncedCallback } from 'use-debounce';
|
import { useThrottledCallback } from 'use-debounce';
|
||||||
import useResizeObserver from 'use-resize-observer';
|
import useResizeObserver from 'use-resize-observer';
|
||||||
|
|
||||||
export default function useTruncated({ className = 'truncated' } = {}) {
|
export default function useTruncated({ className = 'truncated' } = {}) {
|
||||||
const ref = useRef();
|
const ref = useRef();
|
||||||
const onResize = useDebouncedCallback(
|
const onResize = useThrottledCallback(({ height }) => {
|
||||||
({ height }) => {
|
|
||||||
if (ref.current) {
|
if (ref.current) {
|
||||||
const { scrollHeight } = ref.current;
|
const { scrollHeight } = ref.current;
|
||||||
let truncated = scrollHeight > height;
|
let truncated = scrollHeight > height;
|
||||||
|
@ -16,12 +15,7 @@ export default function useTruncated({ className = 'truncated' } = {}) {
|
||||||
}
|
}
|
||||||
ref.current.classList.toggle(className, truncated);
|
ref.current.classList.toggle(className, truncated);
|
||||||
}
|
}
|
||||||
},
|
}, 300);
|
||||||
300,
|
|
||||||
{
|
|
||||||
maxWait: 2000,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
useResizeObserver({
|
useResizeObserver({
|
||||||
ref,
|
ref,
|
||||||
box: 'border-box',
|
box: 'border-box',
|
||||||
|
|
Loading…
Reference in a new issue