Better throttle instead of debounce
This commit is contained in:
parent
d429ef9161
commit
be964f933c
|
@ -1,27 +1,21 @@
|
|||
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 }) => {
|
||||
if (ref.current) {
|
||||
const { scrollHeight } = ref.current;
|
||||
let truncated = scrollHeight > height;
|
||||
if (truncated) {
|
||||
const { height: _height, maxHeight } = getComputedStyle(ref.current);
|
||||
const computedHeight = parseInt(maxHeight || _height, 10);
|
||||
truncated = scrollHeight > computedHeight;
|
||||
}
|
||||
ref.current.classList.toggle(className, truncated);
|
||||
const onResize = useThrottledCallback(({ height }) => {
|
||||
if (ref.current) {
|
||||
const { scrollHeight } = ref.current;
|
||||
let truncated = scrollHeight > height;
|
||||
if (truncated) {
|
||||
const { height: _height, maxHeight } = getComputedStyle(ref.current);
|
||||
const computedHeight = parseInt(maxHeight || _height, 10);
|
||||
truncated = scrollHeight > computedHeight;
|
||||
}
|
||||
},
|
||||
300,
|
||||
{
|
||||
maxWait: 2000,
|
||||
},
|
||||
);
|
||||
ref.current.classList.toggle(className, truncated);
|
||||
}
|
||||
}, 300);
|
||||
useResizeObserver({
|
||||
ref,
|
||||
box: 'border-box',
|
||||
|
|
Loading…
Reference in a new issue