An open source design system that's fully customizable and agent ready
TypeScript
fix(Text): skip truncation measurement when the truncation tooltip is off (#7697)
Cindy Zhang870a784be10f
diff --git a/.changeset/text-truncation-tooltip-off-no-measure.md b/.changeset/text-truncation-tooltip-off-no-measure.mdnew file mode 100644index 0000000000..4005fbba60--- /dev/null+++ b/.changeset/text-truncation-tooltip-off-no-measure.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[perf] `Text` and `Heading` with `maxLines` and `hasTruncateTooltip={false}` no longer measure their text or observe it for resizes and content changes. Only the tooltip uses that measurement, so with the tooltip off the text is cut by CSS alone, and a long table of truncated cells no longer pays a layout read per cell. Rendering is unchanged; with the tooltip enabled (the default) nothing changes.++@cixzhangdiff --git a/packages/core/src/Heading/Heading.test.tsx b/packages/core/src/Heading/Heading.test.tsxindex 78b4aa8a68..78c8769c02 100644--- a/packages/core/src/Heading/Heading.test.tsx+++ b/packages/core/src/Heading/Heading.test.tsx@@ -269,3 +269,60 @@ describe('Heading', () => {expect(element).not.toHaveAttribute('data-type', 'editorial');});});++// With hasTruncateTooltip={false} nothing reads the measured overflow, so a+// long table of truncated cells must not pay a layout read per cell.+function countScrollWidthReads(run: () => void): number {+ const proto = window.HTMLElement.prototype;+ const original = Object.getOwnPropertyDescriptor(proto, 'scrollWidth');+ let reads = 0;+ Object.defineProperty(proto, 'scrollWidth', {+ configurable: true,+ get() {+ reads += 1;+ return 0;+ },+ });+ try {+ run();+ } finally {+ if (original) {+ Object.defineProperty(proto, 'scrollWidth', original);+ } else {+ delete (proto as {scrollWidth?: number}).scrollWidth;+ }+ }+ return reads;+}++describe('Heading maxLines without a truncation tooltip', () => {+ it('takes no truncation measurement when hasTruncateTooltip is false', () => {+ const reads = countScrollWidthReads(() => {+ render(+ <>+ {Array.from({length: 20}, (_, index) => (+ <Heading+ level={3}+ maxLines={1}+ hasTruncateTooltip={false}+ key={index}>+ Cell {index} with text far wider than its column+ </Heading>+ ))}+ </>,+ );+ });+ expect(reads).toBe(0);+ });++ it('still measures when the tooltip is enabled', () => {+ const reads = countScrollWidthReads(() => {+ render(+ <Heading level={3} maxLines={1}>+ Text far wider than its column+ </Heading>,+ );+ });+ expect(reads).toBeGreaterThan(0);+ });+});diff --git a/packages/core/src/Heading/Heading.tsx b/packages/core/src/Heading/Heading.tsxindex 8e16c032e6..b3bd051bf8 100644--- a/packages/core/src/Heading/Heading.tsx+++ b/packages/core/src/Heading/Heading.tsx@@ -253,8 +253,12 @@ export function Heading({// the closed built-in StyleX maps with an unknown name.const builtinType = type && isBuiltinHeadingType(type) ? type : undefined;- // Truncation detection- const truncation = useTruncation({maxLines});+ // Truncation detection. Only the tooltip reads the measured overflow, so+ // with the tooltip off the text is clipped by CSS alone and nothing is+ // measured or observed.+ const truncation = useTruncation({+ maxLines: hasTruncateTooltip === false ? 0 : maxLines,+ });// Tooltip for truncated textconst tooltipPlacement: LayerPlacement =diff --git a/packages/core/src/Text/Text.test.tsx b/packages/core/src/Text/Text.test.tsxindex 26e1e901d8..9495be2b4e 100644--- a/packages/core/src/Text/Text.test.tsx+++ b/packages/core/src/Text/Text.test.tsx@@ -346,3 +346,60 @@ describe('truncated text shows one tooltip, not two', () => {}});});++// With hasTruncateTooltip={false} nothing reads the measured overflow, so a+// long table of truncated cells must not pay a layout read per cell.+function countScrollWidthReads(run: () => void): number {+ const proto = window.HTMLElement.prototype;+ const original = Object.getOwnPropertyDescriptor(proto, 'scrollWidth');+ let reads = 0;+ Object.defineProperty(proto, 'scrollWidth', {+ configurable: true,+ get() {+ reads += 1;+ return 0;+ },+ });+ try {+ run();+ } finally {+ if (original) {+ Object.defineProperty(proto, 'scrollWidth', original);+ } else {+ delete (proto as {scrollWidth?: number}).scrollWidth;+ }+ }+ return reads;+}++describe('Text maxLines without a truncation tooltip', () => {+ it('takes no truncation measurement when hasTruncateTooltip is false', () => {+ const reads = countScrollWidthReads(() => {+ render(+ <>+ {Array.from({length: 20}, (_, index) => (+ <Text+ type="body"+ maxLines={1}+ hasTruncateTooltip={false}+ key={index}>+ Cell {index} with text far wider than its column+ </Text>+ ))}+ </>,+ );+ });+ expect(reads).toBe(0);+ });++ it('still measures when the tooltip is enabled', () => {+ const reads = countScrollWidthReads(() => {+ render(+ <Text type="body" maxLines={1}>+ Text far wider than its column+ </Text>,+ );+ });+ expect(reads).toBeGreaterThan(0);+ });+});diff --git a/packages/core/src/Text/Text.tsx b/packages/core/src/Text/Text.tsxindex 9c37c897fd..b11e4c0b43 100644--- a/packages/core/src/Text/Text.tsx+++ b/packages/core/src/Text/Text.tsx@@ -260,8 +260,12 @@ export function Text({// Resolve display - force block when maxLines > 0 or hasCapsizeconst resolvedDisplay = maxLines > 0 || hasCapsize ? 'block' : display;- // Truncation detection- const truncation = useTruncation({maxLines});+ // Truncation detection. Only the tooltip reads the measured overflow, so+ // with the tooltip off the text is clipped by CSS alone and nothing is+ // measured or observed.+ const truncation = useTruncation({+ maxLines: hasTruncateTooltip === false ? 0 : maxLines,+ });// Tooltip for truncated textconst tooltipPlacement: LayerPlacement =