Skip to content

The open alternative to Salesforce, designed for AI.

Fix overflow text with tooltip (#16490)

Baptiste Devessierc13ef0c4a4f3
diff --git a/packages/twenty-front/src/modules/settings/admin-panel/config-variables/components/SettingsAdminConfigCopyableText.tsx b/packages/twenty-front/src/modules/settings/admin-panel/config-variables/components/SettingsAdminConfigCopyableText.tsx
deleted file mode 100644
index 50b2832f8d1..00000000000
--- a/packages/twenty-front/src/modules/settings/admin-panel/config-variables/components/SettingsAdminConfigCopyableText.tsx
+++ /dev/null
@@ -1,56 +0,0 @@
-import styled from '@emotion/styled';
-import { OverflowingTextWithTooltip } from 'twenty-ui/display';
-import { useDebouncedCallback } from 'use-debounce';
-import { useCopyToClipboard } from '~/hooks/useCopyToClipboard';
-
-type SettingsAdminConfigCopyableTextProps = {
- text: string;
- displayText?: React.ReactNode;
- multiline?: boolean;
- maxRows?: number;
-};
-
-const StyledEllipsisLabel = styled.div`
- white-space: nowrap;
- text-overflow: ellipsis;
- overflow: hidden;
-`;
-
-const StyledExpandedEllipsisLabel = styled.div`
- white-space: normal;
- word-break: break-all;
-`;
-
-const StyledCopyContainer = styled.span`
- cursor: pointer;
-`;
-export const SettingsAdminConfigCopyableText = ({
- text,
- displayText,
- multiline = false,
- maxRows,
-}: SettingsAdminConfigCopyableTextProps) => {
- const { copyToClipboard } = useCopyToClipboard();
-
- const copyToClipboardDebounced = useDebouncedCallback((value: string) => {
- copyToClipboard(value);
- }, 200);
-
- return (
- <StyledCopyContainer onClick={() => copyToClipboardDebounced(text)}>
- {maxRows ? (
- <OverflowingTextWithTooltip
- text={displayText?.toString() || text}
- displayedMaxRows={maxRows}
- isTooltipMultiline={multiline}
- />
- ) : multiline ? (
- <StyledExpandedEllipsisLabel>
- {displayText || text}
- </StyledExpandedEllipsisLabel>
- ) : (
- <StyledEllipsisLabel>{displayText || text}</StyledEllipsisLabel>
- )}
- </StyledCopyContainer>
- );
-};
diff --git a/packages/twenty-front/src/modules/ui/navigation/navigation-drawer/components/NavigationDrawerItem.tsx b/packages/twenty-front/src/modules/ui/navigation/navigation-drawer/components/NavigationDrawerItem.tsx
index 5be114c798d..5b38620d860 100644
--- a/packages/twenty-front/src/modules/ui/navigation/navigation-drawer/components/NavigationDrawerItem.tsx
+++ b/packages/twenty-front/src/modules/ui/navigation/navigation-drawer/components/NavigationDrawerItem.tsx
@@ -144,11 +144,6 @@ const StyledLabelParent = styled.div`
overflow: hidden;
text-overflow: clip;
`;
-const StyledEllipsisContainer = styled.div`
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
-`;
const StyledItemLabel = styled.span`
font-weight: ${({ theme }) => theme.font.weight.medium};
@@ -345,17 +340,22 @@ export const NavigationDrawerItem = ({
)}
<StyledLabelParent>
- <StyledEllipsisContainer>
- <StyledItemLabel>
- <OverflowingTextWithTooltip text={label} />
- </StyledItemLabel>
- {secondaryLabel && (
- <StyledItemSecondaryLabel>
- {' · '}
- {secondaryLabel}
- </StyledItemSecondaryLabel>
- )}
- </StyledEllipsisContainer>
+ <OverflowingTextWithTooltip
+ text={
+ <>
+ <StyledItemLabel>{label}</StyledItemLabel>
+ {secondaryLabel && (
+ <StyledItemSecondaryLabel>
+ {' · '}
+ {secondaryLabel}
+ </StyledItemSecondaryLabel>
+ )}
+ </>
+ }
+ tooltipContent={
+ secondaryLabel ? `${label} · ${secondaryLabel}` : label
+ }
+ />
</StyledLabelParent>
{showStyledSpacer && <StyledSpacer />}
diff --git a/packages/twenty-ui/src/display/tooltip/OverflowingTextWithTooltip.tsx b/packages/twenty-ui/src/display/tooltip/OverflowingTextWithTooltip.tsx
index fd17b9730c4..520ed7b7763 100644
--- a/packages/twenty-ui/src/display/tooltip/OverflowingTextWithTooltip.tsx
+++ b/packages/twenty-ui/src/display/tooltip/OverflowingTextWithTooltip.tsx
@@ -1,7 +1,8 @@
import { styled } from '@linaria/react';
-import { useRef, useState } from 'react';
+import { type ReactNode, useRef, useState } from 'react';
import { createPortal } from 'react-dom';
+import { isNonEmptyString } from '@sniptt/guards';
import { THEME_COMMON } from '@ui/theme';
import { isDefined } from 'twenty-shared/utils';
import { AppTooltip, TooltipDelay } from './AppTooltip';
@@ -9,11 +10,12 @@ import { AppTooltip, TooltipDelay } from './AppTooltip';
const spacing4 = THEME_COMMON.spacing(4);
const StyledOverflowingMultilineText = styled.div<{
- cursorPointer: boolean;
+ isContentOverflowing: boolean;
size: 'large' | 'small';
displayedMaxRows: number;
}>`
- cursor: ${({ cursorPointer }) => (cursorPointer ? 'pointer' : 'inherit')};
+ cursor: ${({ isContentOverflowing }) =>
+ isContentOverflowing ? 'pointer' : 'inherit'};
font-family: inherit;
font-size: inherit;
@@ -30,20 +32,14 @@ const StyledOverflowingMultilineText = styled.div<{
display: -webkit-box;
-webkit-box-orient: vertical;
white-space: pre-wrap;
-
- & :hover {
- text-overflow: ${({ cursorPointer }) =>
- cursorPointer ? 'clip' : 'ellipsis'};
- white-space: ${({ cursorPointer }) =>
- cursorPointer ? 'normal' : 'nowrap'};
- }
`;
const StyledOverflowingText = styled.div<{
- cursorPointer: boolean;
+ isContentOverflowing: boolean;
size: 'large' | 'small';
}>`
- cursor: ${({ cursorPointer }) => (cursorPointer ? 'pointer' : 'inherit')};
+ cursor: ${({ isContentOverflowing }) =>
+ isContentOverflowing ? 'pointer' : 'inherit'};
font-family: inherit;
font-size: inherit;
@@ -56,33 +52,35 @@ const StyledOverflowingText = styled.div<{
height: ${({ size }) => (size === 'large' ? spacing4 : 'auto')};
white-space: nowrap;
-
- & :hover {
- text-overflow: ${({ cursorPointer }) =>
- cursorPointer ? 'clip' : 'ellipsis'};
- white-space: ${({ cursorPointer }) =>
- cursorPointer ? 'normal' : 'nowrap'};
- }
`;
-const Styledpre = styled.pre`
+const StyledPre = styled.pre`
font-family: inherit;
white-space: pre-wrap;
`;
+type OverflowingTextWithTooltipProps = {
+ size?: 'large' | 'small';
+ isTooltipMultiline?: boolean;
+ displayedMaxRows?: number;
+} & (
+ | {
+ text: string | null | undefined;
+ tooltipContent?: never;
+ }
+ | {
+ text: Exclude<ReactNode, string | null | undefined>;
+ tooltipContent: string;
+ }
+);
+
export const OverflowingTextWithTooltip = ({
size = 'small',
text,
isTooltipMultiline,
displayedMaxRows,
- hideTooltip,
-}: {
- size?: 'large' | 'small';
- text: string | null | undefined;
- isTooltipMultiline?: boolean;
- displayedMaxRows?: number;
- hideTooltip?: boolean;
-}) => {
+ tooltipContent,
+}: OverflowingTextWithTooltipProps) => {
const textElementId = `title-id-${+new Date()}`;
const textRef = useRef<HTMLDivElement>(null);
@@ -91,11 +89,10 @@ export const OverflowingTextWithTooltip = ({
const [shouldRenderTooltip, setShouldRenderTooltip] = useState(false);
const handleMouseEnter = () => {
- const isOverflowing =
- (text?.length ?? 0) > 0 && textRef.current
- ? textRef.current?.scrollHeight > textRef.current?.clientHeight ||
- textRef.current.scrollWidth > textRef.current.clientWidth
- : false;
+ const isOverflowing = textRef.current
+ ? textRef.current?.scrollHeight > textRef.current?.clientHeight ||
+ textRef.current.scrollWidth > textRef.current.clientWidth
+ : false;
setIsTitleOverflowing(isOverflowing);
setShouldRenderTooltip(true);
@@ -110,12 +107,19 @@ export const OverflowingTextWithTooltip = ({
event.stopPropagation();
event.preventDefault();
};
+
+ const tooltipText = isNonEmptyString(tooltipContent)
+ ? tooltipContent
+ : isNonEmptyString(text)
+ ? text
+ : null;
+
return (
<>
{isDefined(displayedMaxRows) ? (
<StyledOverflowingMultilineText
data-testid="tooltip"
- cursorPointer={isTitleOverflowing}
+ isContentOverflowing={isTitleOverflowing}
size={size}
displayedMaxRows={displayedMaxRows}
ref={textRef}
@@ -128,7 +132,7 @@ export const OverflowingTextWithTooltip = ({
) : (
<StyledOverflowingText
data-testid="tooltip"
- cursorPointer={isTitleOverflowing}
+ isContentOverflowing={isTitleOverflowing}
size={size}
ref={textRef}
id={textElementId}
@@ -138,14 +142,15 @@ export const OverflowingTextWithTooltip = ({
{text}
</StyledOverflowingText>
)}
+
{shouldRenderTooltip &&
isTitleOverflowing &&
+ isDefined(tooltipText) &&
createPortal(
<div onClick={handleTooltipClick}>
<AppTooltip
anchorSelect={`#${textElementId}`}
offset={5}
- hidden={!isTitleOverflowing || hideTooltip}
noArrow
place="bottom"
positionStrategy="absolute"
@@ -153,9 +158,9 @@ export const OverflowingTextWithTooltip = ({
isOpen={true}
>
{isTooltipMultiline ? (
- <Styledpre>{text}</Styledpre>
+ <StyledPre>{tooltipText}</StyledPre>
) : (
- `${text || ''}`
+ tooltipText
)}
</AppTooltip>
</div>,
diff --git a/packages/twenty-ui/src/display/tooltip/__stories__/OverflowTextWithTooltip.stories.tsx b/packages/twenty-ui/src/display/tooltip/__stories__/OverflowTextWithTooltip.stories.tsx
index 133284c1415..3f47efb6373 100644
--- a/packages/twenty-ui/src/display/tooltip/__stories__/OverflowTextWithTooltip.stories.tsx
+++ b/packages/twenty-ui/src/display/tooltip/__stories__/OverflowTextWithTooltip.stories.tsx
@@ -5,8 +5,8 @@ import { ComponentDecorator } from '@ui/testing';
import { OverflowingTextWithTooltip } from '../OverflowingTextWithTooltip';
-const placeholderText =
- 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi tellus diam, rhoncus nec consequat quis, dapibus quis massa. Praesent tincidunt augue at ex bibendum, non finibus augue faucibus. In at gravida orci. Nulla facilisi. Proin ut augue ut nisi pellentesque tristique. Proin sodales libero id turpis tincidunt posuere.';
+const longText =
+ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi tellus diam, rhoncus nec consequat quis, dapibus quis massa. Praesent tincidunt augue at ex bibendum, non finibus augue faucibus. In at gravida orci. Nulla facilisi. Proin ut augue ut nisi pellentesque tristique. Proin sodales libero id turpis tincidunt posuere. Sed euismod, nunc at aliquam ultricies, nunc nisl aliquet nunc, quis aliquam nisl nunc quis nunc. Donec euismod, nunc quis aliquam ultricies, nunc nisl aliquet nunc.';
const meta: Meta<typeof OverflowingTextWithTooltip> = {
title: 'UI/Display/Tooltip/OverflowingTextWithTooltip',
@@ -16,9 +16,117 @@ const meta: Meta<typeof OverflowingTextWithTooltip> = {
export default meta;
type Story = StoryObj<typeof OverflowingTextWithTooltip>;
-export const Default: Story = {
+export const SingleLineOverflowing: Story = {
args: {
- text: placeholderText,
+ text: longText,
+ },
+ decorators: [ComponentDecorator],
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+ const tooltip = await canvas.findByTestId('tooltip');
+ await userEvent.hover(tooltip);
+ },
+};
+
+export const SingleLineNotOverflowing: Story = {
+ args: {
+ text: 'Short',
+ },
+ decorators: [ComponentDecorator],
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+ const tooltip = await canvas.findByTestId('tooltip');
+ await userEvent.hover(tooltip);
+ },
+};
+
+export const MultilineOverflowing: Story = {
+ args: {
+ text: longText,
+ displayedMaxRows: 2,
+ },
+ decorators: [ComponentDecorator],
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+ const tooltip = await canvas.findByTestId('tooltip');
+ await userEvent.hover(tooltip);
+ },
+};
+
+export const MultilineNotOverflowing: Story = {
+ args: {
+ text: 'Short',
+ displayedMaxRows: 2,
+ },
+ decorators: [ComponentDecorator],
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+ const tooltip = await canvas.findByTestId('tooltip');
+ await userEvent.hover(tooltip);
+ },
+};
+
+export const SingleLineWithReactNodeOverflowing: Story = {
+ args: {
+ text: (
+ <>
+ {longText} · <strong>Secondary Label</strong>
+ </>
+ ),
+ tooltipContent: `${longText} · Secondary Label`,
+ },
+ decorators: [ComponentDecorator],
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+ const tooltip = await canvas.findByTestId('tooltip');
+ await userEvent.hover(tooltip);
+ },
+};
+
+export const SingleLineWithReactNodeNotOverflowing: Story = {
+ args: {
+ text: (
+ <>
+ A · <strong>B</strong>
+ </>
+ ),
+ tooltipContent: 'A · B',
+ },
+ decorators: [ComponentDecorator],
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+ const tooltip = await canvas.findByTestId('tooltip');
+ await userEvent.hover(tooltip);
+ },
+};
+
+export const MultilineWithReactNodeOverflowing: Story = {
+ args: {
+ text: (
+ <>
+ Hello! <i>{longText}</i> · <strong>Important Note</strong>
+ </>
+ ),
+ tooltipContent: `Hello! ${longText} · Important Note`,
+ displayedMaxRows: 2,
+ },
+ decorators: [ComponentDecorator],
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+ const tooltip = await canvas.findByTestId('tooltip');
+ await userEvent.hover(tooltip);
+ },
+};
+
+export const MultilineWithReactNodeNotOverflowing: Story = {
+ args: {
+ text: (
+ <>
+ A · <strong>B</strong>
+ </>
+ ),
+ tooltipContent: 'A · B',
+ displayedMaxRows: 2,
},
decorators: [ComponentDecorator],
play: async ({ canvasElement }) => {