Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

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

Cindy Zhangdc0e0192361f
diff --git a/packages/core/src/Layer/XDSTooltip.tsx b/packages/core/src/Layer/XDSTooltip.tsx
index 412a12cb93..6e4cc72031 100644
--- a/packages/core/src/Layer/XDSTooltip.tsx
+++ b/packages/core/src/Layer/XDSTooltip.tsx
@@ -44,8 +44,18 @@ const styles = stylex.create({
export interface XDSTooltipProps {
/**
* The trigger element(s). Children refs are preserved.
+ * When `anchorRef` is provided, children can be omitted and the tooltip
+ * attaches to the external ref element as a sibling.
*/
- children: ReactNode;
+ children?: ReactNode;
+
+ /**
+ * External ref to use as the tooltip anchor.
+ * When provided (and no children), the tooltip attaches to this element
+ * instead of wrapping children. This enables sibling-mode rendering,
+ * useful for lazy-loaded tooltips that shouldn't remount children.
+ */
+ anchorRef?: React.RefObject<HTMLElement>;
/**
* Content to display in the tooltip.
@@ -153,6 +163,7 @@ function mergeIds(...ids: (string | undefined | null)[]): string | undefined {
*/
export function XDSTooltip({
children,
+ anchorRef,
content,
placement = 'above',
alignment = 'center',
@@ -165,7 +176,7 @@ export function XDSTooltip({
hasHoverIndication = 'auto',
}: XDSTooltipProps): ReactElement {
const wrapperRef = useRef<HTMLElement>(null);
- const textOnly = isTextOnly(children);
+ const textOnly = children != null ? isTextOnly(children) : false;
// Get theme context for hover indication override
const themeContext = useContext(ThemeContext);
@@ -188,8 +199,36 @@ export function XDSTooltip({
onHide,
});
+ // Sibling mode: attach to external anchorRef
+ useLayoutEffect(() => {
+ if (!anchorRef) return;
+
+ const el = anchorRef.current;
+ if (!el) return;
+
+ // Use combined ref for position + interaction
+ tooltip.ref(el);
+
+ // Set aria-describedby, merging with existing
+ const existingDescribedBy = el.getAttribute('aria-describedby');
+ el.setAttribute(
+ 'aria-describedby',
+ mergeIds(existingDescribedBy, tooltip.describedBy) ?? '',
+ );
+
+ return () => {
+ tooltip.ref(null);
+ if (existingDescribedBy) {
+ el.setAttribute('aria-describedby', existingDescribedBy);
+ } else {
+ el.removeAttribute('aria-describedby');
+ }
+ };
+ }, [anchorRef, tooltip.ref, tooltip.describedBy]);
+
// For element children with display:contents, attach ref to first child
useLayoutEffect(() => {
+ if (anchorRef) return; // Skip if using anchorRef mode
if (textOnly) return; // Skip for text-only (ref is on wrapper)
const wrapper = wrapperRef.current;
@@ -216,7 +255,12 @@ export function XDSTooltip({
firstChild.removeAttribute('aria-describedby');
}
};
- }, [textOnly, tooltip.ref, tooltip.describedBy]);
+ }, [anchorRef, textOnly, tooltip.ref, tooltip.describedBy]);
+
+ // Sibling mode: render only the tooltip (no wrapper needed)
+ if (anchorRef && children == null) {
+ return <>{tooltip.renderTooltip(content)}</>;
+ }
// For text-only children: use inline span with ref on wrapper
if (textOnly) {
diff --git a/packages/core/src/Text/XDSHeading.tsx b/packages/core/src/Text/XDSHeading.tsx
index c6b99a2767..27674e46c3 100644
--- a/packages/core/src/Text/XDSHeading.tsx
+++ b/packages/core/src/Text/XDSHeading.tsx
@@ -11,7 +11,15 @@
* - /apps/storybook/stories/Text.stories.tsx (storybook stories)
*/
-import {forwardRef, useCallback, useContext, type ReactNode} from 'react';
+import {
+ forwardRef,
+ lazy,
+ Suspense,
+ useCallback,
+ useContext,
+ useRef,
+ type ReactNode,
+} from 'react';
import * as stylex from '@stylexjs/stylex';
import type {StyleXStyles} from '@stylexjs/stylex';
import {ThemeContext} from '../theme/ThemeContext';
@@ -33,7 +41,11 @@ import {
truncationTooltipStyles,
} from './text.stylex';
import {useTruncation} from './useTruncation';
-import {useXDSTooltip, type LayerPlacement} from '../Layer';
+import type {LayerPlacement} from '../Layer';
+
+const LazyXDSTooltip = lazy(() =>
+ import('../Layer/XDSTooltip').then(mod => ({default: mod.XDSTooltip})),
+);
/**
* Heading level (1-6). Determines both visual styling and semantic HTML element.
@@ -232,12 +244,10 @@ export const XDSHeading = forwardRef<HTMLHeadingElement, XDSHeadingProps>(
const tooltipEnabled =
maxLines > 0 && hasTruncateTooltip !== false && truncation.isTruncated;
- const tooltip = useXDSTooltip({
- placement: tooltipPlacement,
- isEnabled: tooltipEnabled,
- });
+ // Ref for the heading element (used as tooltip anchor)
+ const headingRef = useRef<HTMLHeadingElement>(null);
- // Merge refs: forwardedRef, truncation.ref, tooltip.ref
+ // Merge refs: forwardedRef, truncation.ref, headingRef
const mergedRef = useCallback(
(element: HTMLHeadingElement | null) => {
// Forward ref
@@ -248,10 +258,12 @@ export const XDSHeading = forwardRef<HTMLHeadingElement, XDSHeadingProps>(
}
// Truncation ref
truncation.ref(element);
- // Tooltip ref
- tooltip.ref(element);
+ // Local ref for tooltip anchor
+ (
+ headingRef as React.MutableRefObject<HTMLHeadingElement | null>
+ ).current = element;
},
- [forwardedRef, truncation.ref, tooltip.ref],
+ [forwardedRef, truncation.ref],
);
// Build inline style for -webkit-line-clamp (dynamic value)
@@ -282,17 +294,24 @@ export const XDSHeading = forwardRef<HTMLHeadingElement, XDSHeadingProps>(
xstyle,
)}
style={inlineStyle}
- aria-describedby={tooltipEnabled ? tooltip.describedBy : undefined}
+ title={tooltipEnabled ? truncation.fullText : undefined}
{...ariaProps}
{...props}>
{children}
</Component>
- {tooltipEnabled &&
- tooltip.renderTooltip(
- <span {...stylex.props(truncationTooltipStyles.content)}>
- {truncation.fullText}
- </span>,
- )}
+ {tooltipEnabled && (
+ <Suspense fallback={null}>
+ <LazyXDSTooltip
+ anchorRef={headingRef}
+ content={
+ <span {...stylex.props(truncationTooltipStyles.content)}>
+ {truncation.fullText}
+ </span>
+ }
+ placement={tooltipPlacement}
+ />
+ </Suspense>
+ )}
</>
);
},
diff --git a/packages/core/src/Text/XDSText.tsx b/packages/core/src/Text/XDSText.tsx
index 1ed9c9e891..8c8fad3e53 100644
--- a/packages/core/src/Text/XDSText.tsx
+++ b/packages/core/src/Text/XDSText.tsx
@@ -11,7 +11,15 @@
* - /apps/storybook/stories/Text.stories.tsx (storybook stories)
*/
-import {forwardRef, useCallback, useContext, type ReactNode} from 'react';
+import {
+ forwardRef,
+ lazy,
+ Suspense,
+ useCallback,
+ useContext,
+ useRef,
+ type ReactNode,
+} from 'react';
import * as stylex from '@stylexjs/stylex';
import type {StyleXStyles} from '@stylexjs/stylex';
import {ThemeContext} from '../theme/ThemeContext';
@@ -37,7 +45,11 @@ import {
truncationTooltipStyles,
} from './text.stylex';
import {useTruncation} from './useTruncation';
-import {useXDSTooltip, type LayerPlacement} from '../Layer';
+import type {LayerPlacement} from '../Layer';
+
+const LazyXDSTooltip = lazy(() =>
+ import('../Layer/XDSTooltip').then(mod => ({default: mod.XDSTooltip})),
+);
export type {XDSTextType, XDSTextSize};
@@ -218,12 +230,10 @@ export const XDSText = forwardRef<HTMLElement, XDSTextProps>(function XDSText(
const tooltipEnabled =
maxLines > 0 && hasTruncateTooltip !== false && truncation.isTruncated;
- const tooltip = useXDSTooltip({
- placement: tooltipPlacement,
- isEnabled: tooltipEnabled,
- });
+ // Ref for the text element (used as tooltip anchor)
+ const textRef = useRef<HTMLElement>(null);
- // Merge refs: forwardedRef, truncation.ref, tooltip.ref
+ // Merge refs: forwardedRef, truncation.ref, textRef
const mergedRef = useCallback(
(element: HTMLElement | null) => {
// Forward ref
@@ -234,10 +244,10 @@ export const XDSText = forwardRef<HTMLElement, XDSTextProps>(function XDSText(
}
// Truncation ref
truncation.ref(element);
- // Tooltip ref
- tooltip.ref(element);
+ // Local ref for tooltip anchor
+ (textRef as React.MutableRefObject<HTMLElement | null>).current = element;
},
- [forwardedRef, truncation.ref, tooltip.ref],
+ [forwardedRef, truncation.ref],
);
// Build inline style for -webkit-line-clamp (dynamic value)
@@ -270,16 +280,23 @@ export const XDSText = forwardRef<HTMLElement, XDSTextProps>(function XDSText(
xstyle,
)}
style={inlineStyle}
- aria-describedby={tooltipEnabled ? tooltip.describedBy : undefined}
+ title={tooltipEnabled ? truncation.fullText : undefined}
{...props}>
{children}
</Component>
- {tooltipEnabled &&
- tooltip.renderTooltip(
- <span {...stylex.props(truncationTooltipStyles.content)}>
- {truncation.fullText}
- </span>,
- )}
+ {tooltipEnabled && (
+ <Suspense fallback={null}>
+ <LazyXDSTooltip
+ anchorRef={textRef}
+ content={
+ <span {...stylex.props(truncationTooltipStyles.content)}>
+ {truncation.fullText}
+ </span>
+ }
+ placement={tooltipPlacement}
+ />
+ </Suspense>
+ )}
</>
);
});