Skip to content

Commit

Permalink
Fix broken effect in useCSSTextTruncation hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
codyml committed Dec 3, 2022
1 parent 997950e commit b3254c1
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions superset-frontend/src/hooks/useTruncation/useCSSTextTruncation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,18 @@ const useCSSTextTruncation = <T extends HTMLElement>(): [
] => {
const [isTruncated, setIsTruncated] = useState(true);
const ref = useRef<T>(null);
const { offsetWidth, scrollWidth } = ref.current ?? {};
const prevWidths = useRef({ offsetWidth, scrollWidth });
const { offsetWidth: prevOffsetWidth, scrollWidth: prevScrollWidth } =
prevWidths.current;
const [offsetWidth, setOffsetWidth] = useState(0);
const [scrollWidth, setScrollWidth] = useState(0);

// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(() => {
if (
offsetWidth &&
scrollWidth &&
(offsetWidth !== prevOffsetWidth || scrollWidth !== prevScrollWidth)
) {
prevWidths.current = { offsetWidth, scrollWidth };
setIsTruncated(offsetWidth < scrollWidth);
}
}, [offsetWidth, prevOffsetWidth, prevScrollWidth, scrollWidth]);
setOffsetWidth(ref.current?.offsetWidth ?? 0);
setScrollWidth(ref.current?.scrollWidth ?? 0);
});

useEffect(() => {
setIsTruncated(offsetWidth < scrollWidth);
}, [offsetWidth, scrollWidth]);

return [ref, isTruncated];
};
Expand Down

0 comments on commit b3254c1

Please sign in to comment.