this repo has no description
at hotfix/infinite-loop-intersection-observer 25 lines 805 B view raw
1import { useRef } from 'preact/hooks'; 2import { useThrottledCallback } from 'use-debounce'; 3import useResizeObserver from 'use-resize-observer'; 4 5export default function useTruncated({ className = 'truncated' } = {}) { 6 const ref = useRef(); 7 const onResize = useThrottledCallback(({ height }) => { 8 if (ref.current) { 9 const { scrollHeight } = ref.current; 10 let truncated = scrollHeight > height; 11 if (truncated) { 12 const { height: _height, maxHeight } = getComputedStyle(ref.current); 13 const computedHeight = parseInt(maxHeight || _height, 10); 14 truncated = scrollHeight > computedHeight; 15 } 16 ref.current.classList.toggle(className, truncated); 17 } 18 }, 300); 19 useResizeObserver({ 20 ref, 21 box: 'border-box', 22 onResize, 23 }); 24 return ref; 25}