-
Notifications
You must be signed in to change notification settings - Fork 9
feat: implementing an overflow tooltip for the Text component #1819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, { | ||
|
|
@@ -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; | ||
| } | ||
|
|
||
| const TextContext = createContext<ContextValue<TextProps, HTMLElement>>(null); | ||
|
|
@@ -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. | ||
| * | ||
|
|
@@ -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( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for the future: '@react-aria/utils' exports |
||
| (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={{ | ||
|
|
@@ -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 }; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
useTextOverflowis already available in gonfalonWhat do you think?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 liketooltip?: TooltipPropswhich would then understand all tooltip props directly ie.This way:
showTooltipOnOverflowneeded because the subcomponent is either defined or notIn
@gonfalon/uithere is currently naming convention where extended components have syntax like<main-component>With<specifier>likeMultiSelectWithSearch,SelectWithSearchbut open to align on this one as well. Another common convention we could use is<specifier><main-component>