Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fifty-bats-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@launchpad-ui/components": minor
---

Updated the Text component to include an optional tooltip on overflow
50 changes: 50 additions & 0 deletions packages/components/__tests__/Text.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { describe, expect, it } from 'vitest';

import { render, screen } from '../../../test/utils';
import { Text } from '../src';

describe('Text', () => {
it('renders', () => {
render(<Text>Hello world</Text>);
expect(screen.getByText('Hello world')).toBeVisible();
});

it('renders with different sizes', () => {
const { rerender } = render(<Text size="small">Small text</Text>);
expect(screen.getByText('Small text')).toBeVisible();

rerender(<Text size="medium">Medium text</Text>);
expect(screen.getByText('Medium text')).toBeVisible();

rerender(<Text size="large">Large text</Text>);
expect(screen.getByText('Large text')).toBeVisible();
});

it('renders with bold prop', () => {
render(<Text bold>Bold text</Text>);
expect(screen.getByText('Bold text')).toBeVisible();
});

it('renders with maxLines', () => {
render(<Text maxLines={2}>Long text that should be truncated</Text>);
expect(screen.getByText('Long text that should be truncated')).toBeVisible();
});

it('renders with overflow tooltip when enabled', () => {
render(
<Text maxLines={1} showTooltipOnOverflow>
Text with overflow tooltip
</Text>,
);
expect(screen.getByText('Text with overflow tooltip')).toBeVisible();
});

it('renders with custom tooltip content', () => {
render(
<Text maxLines={1} showTooltipOnOverflow tooltipContent="Custom tooltip">
Truncated text
</Text>,
);
expect(screen.getByText('Truncated text')).toBeVisible();
});
});
100 changes: 94 additions & 6 deletions packages/components/src/Text.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import type { Ref } from 'react';
import type { ReactNode, Ref } from 'react';
import type { TextProps as AriaTextProps, ContextValue } from 'react-aria-components';
import type { TooltipProps } from './Tooltip';

import { cva, cx } from 'class-variance-authority';
import { createContext } from 'react';
import { createContext, useCallback, useEffect, useRef, useState } from 'react';
import { useFocus, useFocusVisible, useHover } from 'react-aria';
import { Text as AriaText } from 'react-aria-components';

import { Focusable } from './Focusable';
import styles from './styles/Text.module.css';
import { Tooltip, TooltipTrigger } from './Tooltip';
import { useLPContextProps } from './utils';

const textStyles = cva(styles.text, {
Expand Down Expand Up @@ -37,6 +41,14 @@ interface TextProps extends Omit<AriaTextProps, 'className' | 'elementType'> {
elementType?: AriaTextProps['elementType'];
/** Optional CSS class name */
className?: AriaTextProps['className'];
/** Enable tooltip on text overflow */
showTooltipOnOverflow?: boolean;
/** Custom tooltip content. If not provided, uses the text children as tooltip content */
tooltipContent?: ReactNode;
/** Tooltip placement */
tooltipPlacement?: TooltipProps['placement'];
/** Additional CSS class name for the tooltip */
tooltipClassName?: string;
Comment on lines +44 to +51
Copy link
Contributor Author

Choose a reason for hiding this comment

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

one thing I briefly considered here - this is all the functionality we'd need to show a tooltip for non-overflow cases as well. So technically we could be more flexible and say that if you pass tooltipContent but NOT showtooltiponoverflow, it will show a tooltip ALWAYS.

I haven't done that yet though because some part of me feels like that would be preemptively adding functionality we don't know if we want yet, and it almost feels like 'hiding' a tooltip at that point? plus in our app I can't really imagine us wanting a tooltip on plain text most of the time, we prefer little help icons and whatnot. open to thoughts though

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't necessarily know if it makes sense to introduce the tooltip component here.
We want to treat launchpad components as composable atoms, and introducing this logic here would break that.

Is it possible to create a TextTruncatorWithTooltip in launchpad-experimental that includes this logic, and update the props for the Text component to consume the hover/focus props?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah that's what I had on this PR instead haha https://github.com/launchdarkly/gonfalon/pull/55722 mind giving that one a review instead if it's prefereable?

Copy link
Contributor

Choose a reason for hiding this comment

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

@Zuzze I'm leaning towards the experimental component for the following reasons:

  • useTextOverflow is already available in gonfalon
  • Test should remain an atom, we shouldn't introduce external component dependencies
  • The approach in Anthony's original PR can easily extend to other components like Label and Heading (as you called out!)

What do you think?

Copy link
Collaborator

@Zuzze Zuzze Nov 18, 2025

Choose a reason for hiding this comment

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

@nhironaka @ld-ahartmann I'm fine with both, for better discoverability I was wondering might be convenient to have it in LP but also fine if you prefer to keep it separate as long as there is documentation how to use them together.

General side note to keep molecule component API tidy for future: It's usually a good practice to avoid picking the sub component props (tooltipContent, tooltipPlacement,...) separately to main API and instead have a prop object like tooltip?: TooltipProps which would then understand all tooltip props directly ie.

<Text tooltip={{label: "My tooltip", placement: "bottom"}>Text</Text>

This way:

  • All props are inherited automatically
  • If subcomponent prop API changes, no extra changes needed for molecules using it
  • No additional booleans like showTooltipOnOverflow needed because the subcomponent is either defined or not

In @gonfalon/ui there is currently naming convention where extended components have syntax like <main-component>With<specifier> like MultiSelectWithSearch, SelectWithSearch but open to align on this one as well. Another common convention we could use is <specifier><main-component>

}

const TextContext = createContext<ContextValue<TextProps, HTMLElement>>(null);
Expand All @@ -48,6 +60,38 @@ const getDefaultElementType = (size: 'small' | 'medium' | 'large'): string => {
return 'span';
};

/**
* Custom hook to detect text overflow
*/
const useTextOverflow = () => {
const ref = useRef<HTMLElement>(null);
const [hasOverflow, setHasOverflow] = useState(false);

const checkOverflow = useCallback(() => {
if (!ref.current) return;

const element = ref.current;
const isOverflowing =
element.scrollHeight > element.clientHeight || element.scrollWidth > element.clientWidth;

setHasOverflow(isOverflowing);
}, []);

useEffect(() => {
checkOverflow();

// Recheck on window resize
const resizeObserver = new ResizeObserver(checkOverflow);
if (ref.current) {
resizeObserver.observe(ref.current);
}

return () => resizeObserver.disconnect();
}, [checkOverflow]);

return { ref, hasOverflow };
};

/**
* A generic Text component for body text.
*
Expand All @@ -56,21 +100,49 @@ const getDefaultElementType = (size: 'small' | 'medium' | 'large'): string => {
* Built on top of [React Aria `Text` component](https://react-spectrum.adobe.com/react-spectrum/Text.html#text).
*/
const Text = ({
ref,
ref: externalRef,
size = 'medium',
bold = false,
maxLines,
elementType,
className,
style,
showTooltipOnOverflow = false,
tooltipContent,
tooltipPlacement = 'top',
tooltipClassName,
...props
}: TextProps) => {
[props, ref] = useLPContextProps(props, ref, TextContext);
[props, externalRef] = useLPContextProps(props, externalRef, TextContext);

return (
const { ref: overflowRef, hasOverflow } = useTextOverflow();
const { hoverProps, isHovered } = useHover({});
const [isFocused, setFocused] = useState(false);
const { focusProps } = useFocus({
onFocus: () => setFocused(true),
onBlur: () => setFocused(false),
});
const { isFocusVisible } = useFocusVisible();

// Merge refs
const mergedRef = useCallback(
Copy link
Contributor

Choose a reason for hiding this comment

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

for the future: '@react-aria/utils' exports mergeRefs

(element: HTMLElement | null) => {
overflowRef.current = element;

if (typeof externalRef === 'function') {
externalRef(element);
} else if (externalRef && 'current' in externalRef) {
externalRef.current = element;
}
},
[externalRef, overflowRef],
);

const textElement = (
<AriaText
{...props}
ref={ref}
{...(showTooltipOnOverflow ? { ...hoverProps, ...focusProps } : {})}
ref={mergedRef}
elementType={elementType || getDefaultElementType(size)}
className={cx(textStyles({ size, bold }), maxLines && styles.truncate, className)}
style={{
Expand All @@ -83,6 +155,22 @@ const Text = ({
{props.children}
</AriaText>
);

if (!showTooltipOnOverflow) {
return textElement;
}

return (
<TooltipTrigger
isDisabled={!hasOverflow}
isOpen={hasOverflow && (isHovered || (isFocusVisible && isFocused))}
>
<Focusable>{textElement}</Focusable>
<Tooltip placement={tooltipPlacement} className={tooltipClassName}>
{tooltipContent ?? props.children}
</Tooltip>
</TooltipTrigger>
);
};

export { Text, TextContext, textStyles };
Expand Down
101 changes: 101 additions & 0 deletions packages/components/stories/Text.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ For headings, use [Heading](/docs/components-content-heading--docs). For labels,
maxLines: {
control: { type: 'number' },
},
showTooltipOnOverflow: {
control: { type: 'boolean' },
description: 'Show tooltip when text overflows',
},
tooltipContent: {
control: { type: 'text' },
description: 'Custom tooltip content (defaults to text children)',
},
tooltipPlacement: {
control: { type: 'select' },
options: ['top', 'bottom', 'left', 'right', 'start', 'end'],
description: 'Tooltip placement',
},
tooltipClassName: {
control: { type: 'text' },
description: 'Additional CSS class for tooltip',
},
elementType: {
table: {
disable: true,
Expand Down Expand Up @@ -124,3 +141,87 @@ export const Truncation: Story = {
</div>
),
};

/**
* Show a tooltip when text overflows by enabling the `showTooltipOnOverflow` prop.
* The tooltip only appears when the text is actually truncated.
*/
export const OverflowTooltip: Story = {
render: () => (
<div style={{ display: 'flex', flexDirection: 'column', gap: '2rem', width: '250px' }}>
<div>
<p style={{ marginBottom: '0.5rem', fontSize: '14px', color: '#666' }}>
Single line with overflow tooltip:
</p>
<Text maxLines={1} showTooltipOnOverflow>
This is a very long text that will be truncated and show a tooltip on hover or focus
</Text>
</div>

<div>
<p style={{ marginBottom: '0.5rem', fontSize: '14px', color: '#666' }}>
Multi-line (2 lines) with overflow tooltip:
</p>
<Text maxLines={2} showTooltipOnOverflow>
This is a longer text that will be truncated after two lines and show a tooltip when you
hover over it. The tooltip contains the full text content.
</Text>
</div>

<div>
<p style={{ marginBottom: '0.5rem', fontSize: '14px', color: '#666' }}>
No overflow (tooltip won't show):
</p>
<Text maxLines={2} showTooltipOnOverflow>
Short text
</Text>
</div>
</div>
),
};

/**
* Customize the tooltip content and placement.
*/
export const CustomTooltip: Story = {
render: () => (
<div style={{ display: 'flex', flexDirection: 'column', gap: '2rem', width: '250px' }}>
<div>
<p style={{ marginBottom: '0.5rem', fontSize: '14px', color: '#666' }}>
Custom tooltip content:
</p>
<Text
maxLines={1}
showTooltipOnOverflow
tooltipContent="This is custom tooltip text that's different from the truncated content"
>
This is the truncated text that appears in the component
</Text>
</div>

<div>
<p style={{ marginBottom: '0.5rem', fontSize: '14px', color: '#666' }}>Bottom placement:</p>
<Text maxLines={1} showTooltipOnOverflow tooltipPlacement="bottom">
Hover to see tooltip below this text. This text is long enough to be truncated.
</Text>
</div>

<div>
<p style={{ marginBottom: '0.5rem', fontSize: '14px', color: '#666' }}>
Different sizes with tooltips:
</p>
<div style={{ display: 'flex', flexDirection: 'column', gap: '0.5rem' }}>
<Text size="small" maxLines={1} showTooltipOnOverflow>
Small text with overflow tooltip - this text will be truncated
</Text>
<Text size="medium" maxLines={1} showTooltipOnOverflow>
Medium text with overflow tooltip - this text will be truncated
</Text>
<Text size="large" maxLines={1} showTooltipOnOverflow>
Large text with overflow tooltip - this text will be truncated
</Text>
</div>
</div>
</div>
),
};
Loading