Skip to content

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

Closed

Description

@cixzhang

Problem

XDSText imports useXDSTooltip at the module level for truncation tooltips (hasTruncateTooltip prop). This pulls in the entire layer/popover system:

XDSText → useXDSTooltip → useXDSLayer → useXDSPopover

Cost: ~12.2 KB minified (9.6KB shared layer hooks + 2.6KB XDSTooltip component). This is 34% of the component chunk, loaded on every page that uses XDSText — even if no text is truncated.

How we measured

Built the theme builder app in xds_sandbox and analyzed the webpack chunks:

Chunk 585 (28KB total):
  Layer/popover preamble:  ~9,585 bytes (34% of chunk)
  XDSTooltip:              ~2,624 bytes
  XDSText:                 ~3,367 bytes
  XDSButton:               ~2,309 bytes
  XDSSpinner:              ~2,668 bytes
  ...rest (stacks, badge, divider, heading)

Options

Option A: Native title attribute (simplest, not styleable)

<span title={isTruncated ? fullText : undefined}>
  {children}
</span>

Zero bundle cost but the tooltip is browser-styled and can't be themed. For "see the full string" this is arguably fine, but it looks out of place in a polished UI.

Option B: Lazy-load the styled tooltip

const TruncationTooltip = React.lazy(
  () => import('./TruncationTooltip')
);

// Only loads the 12KB layer system when text actually overflows
{isTruncated ? (
  <Suspense fallback={<span title={fullText}>{children}</span>}>
    <TruncationTooltip text={fullText}>{children}</TruncationTooltip>
  </Suspense>
) : children}

Keeps the styled tooltip, defers the cost until needed. The fallback uses native title so there's no gap. But still loads 12KB when any text truncates.

Option C: Move truncation tooltip to composition (cleanest)

Remove the built-in truncation tooltip from XDSText entirely. Consumers opt in via composition:

// No tooltip (default — zero cost)
<XDSText maxLines={1}>{longText}</XDSText>

// With tooltip (consumer explicitly imports XDSTooltip)
<XDSTooltip content={longText}>
  <XDSText maxLines={1}>{longText}</XDSText>
</XDSTooltip>

This is the cleanest for bundle size — XDSText never imports the layer system. The cost is only paid by consumers who actually want styled truncation tooltips. But it changes the API and removes a convenience feature.

Option D: Hybrid — title by default, opt-in styled

// Default: native title (zero bundle cost)
<XDSText maxLines={1}>Long text...</XDSText>

// Opt-in: consumer wraps with XDSTooltip
<XDSTooltip content={fullText}>
  <XDSText maxLines={1} hasTruncateTooltip={false}>
    {longText}
  </XDSText>
</XDSTooltip>

Other lazy-loading opportunities

Component Dependency Size Trigger
XDSButton XDSSpinner ~2.7 KB isLoading prop
XDSCheckboxInput XDSIcon ~3.1 KB Always (check mark)
XDSTextInput XDSField system ~8.0 KB Always (field wrapper)

Total lazifiable: ~26 KB out of ~59 KB (44% of XDS component JS).

Context

Discovered while comparing bundle sizes between an XDS-based theme builder (~59KB component JS) and an identical raw HTML + StyleX version (~34KB). See discussion in #155.

Activity

  1. cixzhang commented on Feb 19, 2026

    @cixzhang
    ContributorAuthor

    Preferred approach: React.lazy with ref-based anchoring

    The concern with wrapping XDSTooltip around children inside Suspense is that when the lazy component resolves, React remounts the children (the text content). This causes a visible flash and loses any DOM state.

    Solution: Modify XDSTooltip to support a ref-based anchor mode where it renders as a sibling rather than wrapping children:

    // In XDSText — children never remount
    const textRef = useRef(null);
    const LazyTooltip = React.lazy(() => import("./LazyTruncationTooltip"));
    
    <>
      <span ref={textRef}>{children}</span>
      {isTruncated && (
        <Suspense fallback={null}>
          <LazyTooltip anchorRef={textRef} content={fullText} />
        </Suspense>
      )}
    </>
    // New XDSTooltip API — optional anchorRef instead of children
    <XDSTooltip content="tooltip text">
      {children}  {/* existing wrapping mode */}
    </XDSTooltip>
    
    <XDSTooltip anchorRef={someRef} content="tooltip text" />
    {/* new sibling mode — no children, attaches to external ref */}

    This way:

    1. Children are always rendered immediately — no remount
    2. fallback={null} just means no tooltip for ~100ms while the chunk loads (imperceptible)
    3. The 12KB layer system only loads when text actually overflows
    4. The existing wrapping API still works for non-lazy use cases

    useXDSTooltip already supports ref-based anchoring internally — it just needs to be exposed as a prop on XDSTooltip alongside the existing children-based mode.

  2. added 2 commits that reference this issue on Feb 19, 2026
    bfb920d
    dab42aa
  3. added a commit that references this issue on Apr 26, 2026
    2451613
  4. added a commit that references this issue on Jun 21, 2026
    dc0e019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions