Skip to content

perf(text): lazy-load truncation tooltip to reduce bundle size - #158

Merged
cixzhang merged 2 commits into
mainfrom
navi/perf/lazy-truncation-tooltip
Feb 19, 2026
Merged

cixzhang merged 2 commits into
mainfrom
navi/perf/lazy-truncation-tooltip

Conversation

@cixzhang

Copy link
Copy Markdown
Contributor

Summary

XDSText and XDSHeading now lazy-load the tooltip/layer system via React.lazy instead of importing it statically. The ~12KB layer system (useXDSTooltip, useXDSLayer, useXDSPopover) is only loaded when text actually overflows.

Changes

  • Add anchorRef prop to XDSTooltip — enables sibling-mode rendering where the tooltip attaches to an external ref instead of wrapping children. When anchorRef is provided (and no children), the tooltip renders only the popover element.

  • Create LazyTruncationTooltip — a thin wrapper around XDSTooltip that serves as the dynamic import target for React.lazy. This is the code-split boundary.

  • Update XDSText and XDSHeading — replace static useXDSTooltip import with lazy-loaded LazyTruncationTooltip:

    • Text renders immediately (no waiting for tooltip chunk)
    • Tooltip loads asynchronously only when truncation is detected
    • Native title attribute provides fallback while chunk loads
    • Children never remount (tooltip renders as sibling via ref)
    • Wrapped in <Suspense fallback={null}>

How it works

// Before: static import pulls in entire layer system
import { useXDSTooltip } from '../Layer';

// After: layer system only loads when text overflows
const LazyTruncationTooltip = lazy(() => import('./LazyTruncationTooltip'));

// Renders as a sibling, not a wrapper
<Component ref={mergedRef} title={isTruncated ? fullText : undefined}>
  {children}
</Component>
{tooltipEnabled && (
  <Suspense fallback={null}>
    <LazyTruncationTooltip anchorRef={textRef} content={fullText} />
  </Suspense>
)}

Testing

All 749 tests pass ✅

Fixes #157


Crafted with care by Navi

XDSText and XDSHeading now lazy-load the tooltip/layer system via
React.lazy instead of importing it statically. The 12KB layer system
(useXDSTooltip, useXDSLayer, useXDSPopover) is only loaded when text
actually overflows.

- Add anchorRef prop to XDSTooltip for sibling-mode rendering
- Create LazyTruncationTooltip as the dynamic import target
- Use native title attribute as fallback during chunk load
- Children never remount (tooltip renders as sibling via ref)

Fixes #157

Co-authored-by: Navi <[email protected]>
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Feb 19, 2026
@github-actions

github-actions Bot commented Feb 19, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR

Modified Components

Layer
Metric Before After Delta
Bundle Size (ESM) N/A 423B N/A
Lines of Code N/A 1301 -
Complexity N/A Very High (141) -
Text
Metric Before After Delta
Bundle Size (ESM) N/A 361B N/A
Lines of Code N/A 559 -
Complexity N/A Very High (76) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@xds/core 6.0KB 9.9KB 1.6KB

Accessibility Audit

Status: 4 accessibility violation(s) found.

XDSText - 2 issue(s)
  • critical: Form elements must have labels
    • Rule: label
  • serious: Links must have discernible text
    • Rule: link-name
XDSTextArea - 1 issue(s)
  • serious: Links must have discernible text
    • Rule: link-name
XDSTextInput - 1 issue(s)
  • serious: Links must have discernible text
    • Rule: link-name

Component Previews

Includes interactive hover previews as animated GIFs.

XDSText

Body

Screenshot:

Body

Interaction Preview: (view video)

Body interaction

Run yarn storybook and navigate to: typography-xdstext--body

Large

Screenshot:

Large

Interaction Preview: (view video)

Large interaction

Run yarn storybook and navigate to: typography-xdstext--large

Label

Screenshot:

Label

Interaction Preview: (view video)

Label interaction

Run yarn storybook and navigate to: typography-xdstext--label

Supporting

Screenshot:

Supporting

Interaction Preview: (view video)

Supporting interaction

Run yarn storybook and navigate to: typography-xdstext--supporting

Code

Screenshot:

Code

Interaction Preview: (view video)

Code interaction

Run yarn storybook and navigate to: typography-xdstext--code

All Types

Screenshot:

All Types

Interaction Preview: (view video)

All Types interaction

Run yarn storybook and navigate to: typography-xdstext--all-types

Color Variants

Screenshot:

Color Variants

Interaction Preview: (view video)

Color Variants interaction

Run yarn storybook and navigate to: typography-xdstext--color-variants

Weight Variants

Screenshot:

Weight Variants

Interaction Preview: (view video)

Weight Variants interaction

Run yarn storybook and navigate to: typography-xdstext--weight-variants

Display Modes

Screenshot:

Display Modes

Interaction Preview: (view video)

Display Modes interaction

Run yarn storybook and navigate to: typography-xdstext--display-modes

Single Line Truncation

Screenshot:

Single Line Truncation

Interaction Preview: (view video)

Single Line Truncation interaction

Run yarn storybook and navigate to: typography-xdstext--single-line-truncation

Multi Line Truncation

Screenshot:

Multi Line Truncation

Interaction Preview: (view video)

Multi Line Truncation interaction

Run yarn storybook and navigate to: typography-xdstext--multi-line-truncation

Truncation Without Tooltip

Screenshot:

Truncation Without Tooltip

Interaction Preview: (view video)

Truncation Without Tooltip interaction

Run yarn storybook and navigate to: typography-xdstext--truncation-without-tooltip

Truncation Variants

Screenshot:

Truncation Variants

Interaction Preview: (view video)

Truncation Variants interaction

Run yarn storybook and navigate to: typography-xdstext--truncation-variants

Word Break Variants

Screenshot:

Word Break Variants

Interaction Preview: (view video)

Word Break Variants interaction

Run yarn storybook and navigate to: typography-xdstext--word-break-variants

Text Wrap Variants

Screenshot:

Text Wrap Variants

Interaction Preview: (view video)

Text Wrap Variants interaction

Run yarn storybook and navigate to: typography-xdstext--text-wrap-variants

Strikethrough

Screenshot:

Strikethrough

Interaction Preview: (view video)

Strikethrough interaction

Run yarn storybook and navigate to: typography-xdstext--strikethrough

Tabular Numbers

Screenshot:

Tabular Numbers

Interaction Preview: (view video)

Tabular Numbers interaction

Run yarn storybook and navigate to: typography-xdstext--tabular-numbers

Capsize

Screenshot:

Capsize

Interaction Preview: (view video)

Capsize interaction

Run yarn storybook and navigate to: typography-xdstext--capsize

As Elements

Screenshot:

As Elements

Interaction Preview: (view video)

As Elements interaction

Run yarn storybook and navigate to: typography-xdstext--as-elements

Card Example

Screenshot:

Card Example

Interaction Preview: (view video)

Card Example interaction

Run yarn storybook and navigate to: typography-xdstext--card-example

Metrics Example

Screenshot:

Metrics Example

Interaction Preview: (view video)

Metrics Example interaction

Run yarn storybook and navigate to: typography-xdstext--metrics-example

Form Example

Screenshot:

Form Example

Interaction Preview: (view video)

Form Example interaction

Run yarn storybook and navigate to: typography-xdstext--form-example


Generated by PR Enrichment workflow | Storybook | View full report

@@ -0,0 +1,62 @@
/**

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid creating this file and use XDSTooltip directly with React.lazy?

…directly

Address review feedback: instead of a wrapper file, use React.lazy
with .then() to re-export XDSTooltip as a default export inline:

  const LazyXDSTooltip = lazy(() =>
    import('../Layer/XDSTooltip').then(mod => ({ default: mod.XDSTooltip }))
  );

The truncation tooltip content styling (max-width, word-break) is
inlined at the render site in XDSText and XDSHeading.

Co-authored-by: Navi <[email protected]>
@cixzhang

Copy link
Copy Markdown
Contributor Author

Addressed — removed LazyTruncationTooltip.tsx wrapper. Now lazy-loads XDSTooltip directly using the .then() pattern:

const LazyXDSTooltip = lazy(() =>
  import('../Layer/XDSTooltip').then((mod) => ({default: mod.XDSTooltip})),
);

The truncation content styling (max-width: 300px, word-break: break-word) is inlined at the render site in both XDSText and XDSHeading. All 749 tests pass.

@cixzhang
cixzhang merged commit dab42aa into main Feb 19, 2026
5 checks passed
@cixzhang
cixzhang deleted the navi/perf/lazy-truncation-tooltip branch February 19, 2026 18:04
cixzhang added a commit that referenced this pull request Apr 26, 2026
* perf(text): lazy-load truncation tooltip to reduce bundle size

XDSText and XDSHeading now lazy-load the tooltip/layer system via
React.lazy instead of importing it statically. The 12KB layer system
(useXDSTooltip, useXDSLayer, useXDSPopover) is only loaded when text
actually overflows.

- Add anchorRef prop to XDSTooltip for sibling-mode rendering
- Create LazyTruncationTooltip as the dynamic import target
- Use native title attribute as fallback during chunk load
- Children never remount (tooltip renders as sibling via ref)

Fixes #157

Co-authored-by: Navi <[email protected]>

* refactor: remove LazyTruncationTooltip wrapper, lazy-load XDSTooltip directly

Address review feedback: instead of a wrapper file, use React.lazy
with .then() to re-export XDSTooltip as a default export inline:

  const LazyXDSTooltip = lazy(() =>
    import('../Layer/XDSTooltip').then(mod => ({ default: mod.XDSTooltip }))
  );

The truncation tooltip content styling (max-width, word-break) is
inlined at the render site in XDSText and XDSHeading.

Co-authored-by: Navi <[email protected]>

---------

Co-authored-by: Navi <[email protected]>
cixzhang added a commit that referenced this pull request Jun 21, 2026
* perf(text): lazy-load truncation tooltip to reduce bundle size

XDSText and XDSHeading now lazy-load the tooltip/layer system via
React.lazy instead of importing it statically. The 12KB layer system
(useXDSTooltip, useXDSLayer, useXDSPopover) is only loaded when text
actually overflows.

- Add anchorRef prop to XDSTooltip for sibling-mode rendering
- Create LazyTruncationTooltip as the dynamic import target
- Use native title attribute as fallback during chunk load
- Children never remount (tooltip renders as sibling via ref)

Fixes #157


* refactor: remove LazyTruncationTooltip wrapper, lazy-load XDSTooltip directly

Address review feedback: instead of a wrapper file, use React.lazy
with .then() to re-export XDSTooltip as a default export inline:

  const LazyXDSTooltip = lazy(() =>
    import('../Layer/XDSTooltip').then(mod => ({ default: mod.XDSTooltip }))
  );

The truncation tooltip content styling (max-width, word-break) is
inlined at the render site in XDSText and XDSHeading.


---------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: XDSText pulls in 12KB tooltip/layer system for truncation

1 participant