Skip to content

Commit 16b3d16

Browse files
authored
Fix visual regression false positives due to UUID truncation race (#3407)
Canvas measureText uses fallback font metrics until the web font loads, and the font swap doesn't necessarily resize the container, so the truncation stayed computed against the fallback font. This made truncated UUIDs nondeterministic, which showed up as false positives in the visual regression suite.
1 parent a64d548 commit 16b3d16

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

‎app/ui/lib/Truncate.tsx‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ export const Truncate = ({
4141
const [truncated, setTruncated] = useState(false)
4242

4343
// Middle truncation has to be computed up front in order to render at all,
44-
// and recomputed whenever the container resizes
44+
// and recomputed whenever the container resizes or a web font finishes
45+
// loading. Canvas measures with fallback font metrics until the real font
46+
// is ready, and the swap doesn't necessarily resize the container, so the
47+
// observer alone would leave a stale truncation based on the fallback font.
4548
useLayoutEffect(() => {
4649
const el = ref.current
4750
if (position !== 'middle' || !el) return
@@ -55,7 +58,11 @@ export const Truncate = ({
5558
update()
5659
const observer = new ResizeObserver(update)
5760
observer.observe(el)
58-
return () => observer.disconnect()
61+
document.fonts.addEventListener('loadingdone', update)
62+
return () => {
63+
observer.disconnect()
64+
document.fonts.removeEventListener('loadingdone', update)
65+
}
5966
}, [text, position])
6067

6168
// For end truncation, CSS does the actual truncating and the only decision

0 commit comments

Comments
 (0)