mirror of
https://github.com/cheeaun/phanpy.git
synced 2024-12-25 11:48:14 +03:00
18 lines
456 B
JavaScript
18 lines
456 B
JavaScript
|
import { useRef } from 'preact/hooks';
|
||
|
import useResizeObserver from 'use-resize-observer';
|
||
|
|
||
|
export default function useTruncated({ className = 'truncated' } = {}) {
|
||
|
const ref = useRef();
|
||
|
useResizeObserver({
|
||
|
ref,
|
||
|
box: 'border-box',
|
||
|
onResize: ({ height }) => {
|
||
|
if (ref.current) {
|
||
|
const { scrollHeight } = ref.current;
|
||
|
ref.current.classList.toggle(className, scrollHeight > height);
|
||
|
}
|
||
|
},
|
||
|
});
|
||
|
return ref;
|
||
|
}
|