Skip to content

Commit 8f8b3db

Browse files
perf(Text): take a new text's first truncation measurement from the shared observer (#10668)
A newly observed truncating element is first measured on the shared ResizeObserver's first notification, which the browser sends after its own layout, instead of in a microtask after the commit that forced a layout pass. An element another subscriber already observes gets no new first notification, so it keeps the microtask measurement. The tooltip behaves as before. This diff was generated with AI assistance (Navi). Tags: ai-generated
1 parent 7bdeb06 commit 8f8b3db

4 files changed

Lines changed: 61 additions & 23 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[perf] Truncating Text and Heading take a newly mounted element's first measurement from the shared resize observer's first notification, after the browser's own layout, instead of forcing a layout pass after each commit. Pages and windowed tables that mount many truncating texts commit faster; the tooltip behaves as before.
6+
7+
@humbertovirtudes

‎packages/core/src/Text/useTruncation.test.tsx‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,18 @@ describe('useTruncation', () => {
4343
vi.unstubAllGlobals();
4444
});
4545

46-
// Measuring inside React's commit forces a style and layout pass per text
47-
// on a page that mounts many; after the commit, every text reads together.
48-
it('measures after the commit, not inside it', async () => {
46+
// Measuring inside React's commit, or in a microtask after it, forces a
47+
// layout pass on a page that mounts many texts. A newly observed element
48+
// waits for the observer's first notification, sent after layout.
49+
it("takes a new text's first measurement from the observer's first notification", async () => {
4950
const observed: Element[] = [];
51+
let notify: ResizeObserverCallback | null = null;
5052
vi.stubGlobal(
5153
'ResizeObserver',
5254
class {
55+
constructor(callback: ResizeObserverCallback) {
56+
notify = callback;
57+
}
5358
observe(target: Element) {
5459
observed.push(target);
5560
}
@@ -60,12 +65,17 @@ describe('useTruncation', () => {
6065

6166
const {unmount} = render(<Probe text="a status line" />);
6267

63-
// Observed, but not measured during the commit.
68+
// Observed, but measured neither in the commit nor in a microtask.
6469
expect(observed).toHaveLength(1);
65-
expect(screen.getByTestId('full-text')).toHaveTextContent('');
66-
6770
await act(async () => {});
71+
expect(screen.getByTestId('full-text')).toHaveTextContent('');
6872

73+
act(() => {
74+
notify?.(
75+
[{target: observed[0]} as ResizeObserverEntry],
76+
{} as ResizeObserver,
77+
);
78+
});
6979
expect(screen.getByTestId('full-text')).toHaveTextContent(
7080
'a status line',
7181
);
@@ -94,11 +104,24 @@ describe('useTruncation', () => {
94104
configurable: true,
95105
get: () => 100,
96106
});
107+
const {observeResize} = await import('../utils/sharedResizeObserver');
97108
function Unclamping({maxLines}: {maxLines: number}) {
98109
const truncation = useTruncation({maxLines});
110+
const {ref} = truncation;
111+
// Another subscriber watches the span first, so the hook's first
112+
// measurement takes the microtask path, which the guard decides.
113+
const attach = useCallback(
114+
(element: HTMLElement | null) => {
115+
if (element) {
116+
observeResize(element, () => {});
117+
}
118+
ref(element);
119+
},
120+
[ref],
121+
);
99122
return (
100123
<>
101-
<span ref={truncation.ref}>overflowing text</span>
124+
<span ref={attach}>overflowing text</span>
102125
<output data-testid="state">
103126
{String(truncation.isTruncated)}|{truncation.fullText}
104127
</output>

‎packages/core/src/Text/useTruncation.ts‎

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414

1515
import {useCallback, useRef, useState, type RefCallback} from 'react';
16-
import {observeResize} from '../utils/sharedResizeObserver';
16+
import {isResizeObserved, observeResize} from '../utils/sharedResizeObserver';
1717
import {observeContent} from '../utils/sharedMutationObserver';
1818

1919
export interface UseTruncationOptions {
@@ -123,17 +123,17 @@ export function useTruncation(
123123
elementRef.current = element;
124124

125125
if (element && maxLines > 0) {
126-
// Observe via shared singleton (if available). The first measurement
127-
// runs after the commit, in a microtask, instead of in the call
128-
// observeResize makes on registration: that call runs inside React's
129-
// commit, after other components' DOM writes, and forced a style and
130-
// layout pass per text on a page that mounts many. Every text
131-
// registered in one commit then reads layout once, together, and a
132-
// text whose element another subscriber already observes is measured
133-
// too. The truncated state only decides whether to compose the
134-
// tooltip, so nothing paints differently.
126+
// Observe via the shared singleton, if available. The call that
127+
// observeResize makes on registration runs inside React's commit, so
128+
// it measures nothing. The observer's first notification measures a
129+
// new element. The browser sends it after its own layout, so mounting
130+
// text forces no layout pass. An element another subscriber already
131+
// observes gets no new first notification, so a microtask after the
132+
// commit measures it. The truncated state only decides whether to
133+
// compose the tooltip, so nothing paints differently.
135134
if (typeof ResizeObserver !== 'undefined') {
136135
let registered = false;
136+
const wasObserved = isResizeObserved(element);
137137
const unobserve = observeResize(element, () => {
138138
if (registered) {
139139
checkTruncation(element);
@@ -143,11 +143,13 @@ export function useTruncation(
143143
unobserveRef.current = unobserve;
144144
// Skipped when this registration has ended by then (measuring
145145
// turned off, or the element replaced, in the same task).
146-
queueMicrotask(() => {
147-
if (unobserveRef.current === unobserve) {
148-
checkTruncation(element);
149-
}
150-
});
146+
if (wasObserved) {
147+
queueMicrotask(() => {
148+
if (unobserveRef.current === unobserve) {
149+
checkTruncation(element);
150+
}
151+
});
152+
}
151153
} else {
152154
checkTruncation(element);
153155
}

‎packages/core/src/utils/sharedResizeObserver.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
/**
66
* @file sharedResizeObserver.ts
77
* @input ResizeObserver API
8-
* @output Exports observeResize / unobserveResize for shared observation
8+
* @output Exports observeResize / unobserveResize for shared observation, and
9+
* the internal isResizeObserved
910
* @position Utility; consumed by useTruncation, useOverflow, and any component
1011
* that needs resize observation without creating per-instance observers
1112
*
@@ -52,6 +53,11 @@ function getObserver(): ResizeObserver | null {
5253
return observer;
5354
}
5455

56+
/** Whether the shared observer already watches `element`. Internal. */
57+
export function isResizeObserved(element: Element): boolean {
58+
return callbacks.has(element);
59+
}
60+
5561
/**
5662
* Observe an element's size via a shared ResizeObserver singleton.
5763
*

0 commit comments

Comments
 (0)