Skip to content

Commit

Permalink
fix(useEllipsisDetection): add debounce for window resize
Browse files Browse the repository at this point in the history
  • Loading branch information
GaroGabrielyan committed Mar 4, 2024
1 parent ed47d84 commit 457fd1e
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/hooks/useEllipsisDetection.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,26 @@ const useEllipsisDetection = (ref, externalDependencies = []) => {
const [isTruncated, setIsTruncated] = useState(false);

useEffect(() => {
let timeoutId;
const handleResize = () => {
if (!ref.current) return;
const { scrollWidth, clientWidth, scrollHeight, clientHeight } = ref.current;
setIsTruncated(scrollWidth > clientWidth || scrollHeight > clientHeight + EQUAL_HEIGHT_DIFF);
};

window.addEventListener('resize', handleResize);
const debounceResize = () => {
clearTimeout(timeoutId);
timeoutId = setTimeout(handleResize, 100);
};

window.addEventListener('resize', debounceResize);

handleResize();

return () => window.removeEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', debounceResize);
clearTimeout(timeoutId);
};
}, [
ref,
ref?.current?.scrollWidth,
Expand Down

0 comments on commit 457fd1e

Please sign in to comment.