phanpy/src/utils/useTruncated.js
Lim Chee Aun f9b2ab3b94 Refactor truncated class
Also removed the hack fix, not sure why/how it's even fixed.
Don't even know how to explain the logic.
Will revisit and investigate more if the bug happens.

This `useTruncated` can now be reusable.
2023-09-19 16:27:22 +08:00

17 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;
}