Skip to content

Commit 3ac5e90

Browse files
authored
fix(emcn): stop wiping virtualized code viewer row measurements on content changes (#5649)
1 parent 66d1e61 commit 3ac5e90

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

  • packages/emcn/src/components/code

packages/emcn/src/components/code/code.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,12 +1015,21 @@ const VirtualizedViewerInner = memo(function VirtualizedViewerInner({
10151015
})
10161016

10171017
/**
1018-
* Re-measure rows when wrap mode or content changes so dynamic (wrapped) row
1019-
* heights stay accurate. Fixed-height (`nowrap`) rows do not need re-measuring.
1018+
* Drop cached row measurements when leaving wrap mode: the measureElement
1019+
* refs detach with their wrapped heights still cached, and falling back to
1020+
* the fixed estimate is exactly correct for nowrap rows. Entering wrap needs
1021+
* no reset — refs re-attach and re-measure as rows render.
1022+
*
1023+
* Deliberately NOT keyed on content (`visibleLines`): `measure()` wipes the
1024+
* cache without re-measuring mounted rows (ResizeObserver only fires on size
1025+
* changes; React never re-invokes an unchanged ref), so a content change that
1026+
* keeps row heights — Prism resolving its lazy load, search `<mark>`
1027+
* highlighting — would permanently collapse wrapped rows onto the rows below.
1028+
* Genuine height changes are caught by the per-row ResizeObserver.
10201029
*/
10211030
useEffect(() => {
1022-
virtualizer.measure()
1023-
}, [wrapText, visibleLines, virtualizer])
1031+
if (!wrapText) virtualizer.measure()
1032+
}, [wrapText, virtualizer])
10241033

10251034
useEffect(() => {
10261035
if (!searchQuery?.trim() || matchCount === 0 || !scrollRef.current) return

0 commit comments

Comments
 (0)