The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Add Text typography primitive with truncate and lineClamp (#24278)
Raphaël Bosi343f780c06fe
diff --git a/packages/twenty-ui/src/data-display/Avatar/Avatar.tsx b/packages/twenty-ui/src/data-display/Avatar/Avatar.tsxindex c18809ac0ce..76e1f8eb4f5 100644--- a/packages/twenty-ui/src/data-display/Avatar/Avatar.tsx+++ b/packages/twenty-ui/src/data-display/Avatar/Avatar.tsx@@ -1,15 +1,16 @@-import { isNonEmptyString, isNull, isUndefined } from '@sniptt/guards';+import { isNonEmptyString, isNull } from '@sniptt/guards';import { clsx } from 'clsx';import { useState } from 'react';import { handleClickableElementKeyDown } from '@ui/accessibility/utils/handleClickableElementKeyDown';-import { AvatarImageLoadErrorEffect } from '@ui/data-display/Avatar/components/AvatarImageLoadErrorEffect';+import { AvatarImageLoadErrorEffect } from '@ui/data-display/Avatar/internal/AvatarImageLoadErrorEffect';import { type AvatarSize } from '@ui/data-display/Avatar/types/AvatarSize';import { type AvatarType } from '@ui/data-display/Avatar/types/AvatarType';import { type IconComponent } from '@ui/icon/types/IconComponent';import { useTheme } from '@ui/theme-constants';import { stringToThemeColorP3String } from '@ui/utilities';import { type Nullable } from '@ui/utilities/types/Nullable';+import { isDefined } from '@ui/utilities/utils/isDefined';import styles from './Avatar.module.scss';@@ -110,27 +111,30 @@ export const Avatar = ({: {}),} as React.CSSProperties;+ const avatarClassName = clsx(+ styles.root,+ styles[size],+ pulsing && styles.pulsing,+ className,+ );++ const isClickable = isDefined(onClick);++ const clickableAriaLabel = isNonEmptyString(placeholder)+ ? placeholder+ : 'Avatar';+return (+ // oxlint-disable-next-line jsx-a11y/no-static-element-interactions<div- className={clsx(- styles.root,- styles[size],- pulsing && styles.pulsing,- className,- )}+ className={avatarClassName}data-type={type ?? undefined}- data-clickable={!isUndefined(onClick) ? true : undefined}- role={!isUndefined(onClick) ? 'button' : undefined}- tabIndex={!isUndefined(onClick) ? 0 : undefined}- aria-label={- !isUndefined(onClick)- ? isNonEmptyString(placeholder)- ? placeholder- : 'Avatar'- : undefined- }+ data-clickable={isClickable || undefined}+ role={isClickable ? 'button' : undefined}+ tabIndex={isClickable ? 0 : undefined}+ aria-label={isClickable ? clickableAriaLabel : undefined}onClick={onClick}- onKeyDown={handleClickableElementKeyDown}+ onKeyDown={isClickable ? handleClickableElementKeyDown : undefined}style={avatarStyle}>{isNonEmptyString(avatarImageURI) && (diff --git a/packages/twenty-ui/src/data-display/Avatar/__stories__/Avatar.stories.tsx b/packages/twenty-ui/src/data-display/Avatar/__stories__/Avatar.stories.tsxindex 6eafc1869e0..5aeab7fea1d 100644--- a/packages/twenty-ui/src/data-display/Avatar/__stories__/Avatar.stories.tsx+++ b/packages/twenty-ui/src/data-display/Avatar/__stories__/Avatar.stories.tsx@@ -1,5 +1,5 @@import { type Meta, type StoryObj } from '@storybook/react-vite';-import { expect, within } from 'storybook/test';+import { expect, fn, userEvent, within } from 'storybook/test';import { AVATAR_URL_MOCK, ComponentDecorator } from '@ui/testing';@@ -59,3 +59,55 @@ export const ImageFailingToLoadFallsBackToPlaceholder: Story = {await expect(placeholderChar).toBeVisible();},};++export const NotClickable: Story = {+ play: async ({ canvasElement }) => {+ const canvas = within(canvasElement);++ await expect(canvas.queryByRole('button')).not.toBeInTheDocument();+ },+};++const clickFromEnterKey = fn();++export const ClickableActivatesOnEnter: Story = {+ args: { placeholder: 'Eldritch', onClick: clickFromEnterKey },+ play: async ({ canvasElement }) => {+ const canvas = within(canvasElement);++ const avatar = await canvas.findByRole('button', { name: 'Eldritch' });+ await expect(avatar).toHaveAttribute('tabindex', '0');++ avatar.focus();+ await userEvent.keyboard('{Enter}');++ await expect(clickFromEnterKey).toHaveBeenCalledTimes(1);+ },+};++const clickFromSpaceKey = fn();++export const ClickableActivatesOnSpace: Story = {+ args: { placeholder: 'Eldritch', onClick: clickFromSpaceKey },+ play: async ({ canvasElement }) => {+ const canvas = within(canvasElement);++ const avatar = await canvas.findByRole('button', { name: 'Eldritch' });++ avatar.focus();+ await userEvent.keyboard(' ');++ await expect(clickFromSpaceKey).toHaveBeenCalledTimes(1);+ },+};++export const ClickableWithoutPlaceholderIsStillLabelled: Story = {+ args: { placeholder: '', onClick: fn() },+ play: async ({ canvasElement }) => {+ const canvas = within(canvasElement);++ await expect(+ await canvas.findByRole('button', { name: 'Avatar' }),+ ).toBeVisible();+ },+};diff --git a/packages/twenty-ui/src/data-display/Avatar/components/AvatarImageLoadErrorEffect.tsx b/packages/twenty-ui/src/data-display/Avatar/internal/AvatarImageLoadErrorEffect.tsxsimilarity index 100%rename from packages/twenty-ui/src/data-display/Avatar/components/AvatarImageLoadErrorEffect.tsxrename to packages/twenty-ui/src/data-display/Avatar/internal/AvatarImageLoadErrorEffect.tsxdiff --git a/packages/twenty-ui/src/data-display/EllipsisDisplay/EllipsisDisplay.module.scss b/packages/twenty-ui/src/data-display/EllipsisDisplay/EllipsisDisplay.module.scssindex 389db6bbb76..acbd3b63b66 100644--- a/packages/twenty-ui/src/data-display/EllipsisDisplay/EllipsisDisplay.module.scss+++ b/packages/twenty-ui/src/data-display/EllipsisDisplay/EllipsisDisplay.module.scss@@ -3,8 +3,5 @@display: flex;height: 20px;max-width: 100%;- overflow: hidden;- text-overflow: ellipsis;- white-space: nowrap;width: 100%;}diff --git a/packages/twenty-ui/src/data-display/EllipsisDisplay/EllipsisDisplay.tsx b/packages/twenty-ui/src/data-display/EllipsisDisplay/EllipsisDisplay.tsxindex a432ee80746..a40216511e9 100644--- a/packages/twenty-ui/src/data-display/EllipsisDisplay/EllipsisDisplay.tsx+++ b/packages/twenty-ui/src/data-display/EllipsisDisplay/EllipsisDisplay.tsx@@ -1,5 +1,6 @@import { clsx } from 'clsx';+import { Text } from '@ui/typography/Text/Text';import { isDefined } from '@ui/utilities/utils/isDefined';import styles from './EllipsisDisplay.module.scss';@@ -15,10 +16,11 @@ export const EllipsisDisplay = ({maxWidth,className,}: EllipsisDisplayProps) => (- <div+ <Text+ truncateclassName={clsx(styles.ellipsis, className)}style={isDefined(maxWidth) ? { maxWidth: `${maxWidth}px` } : undefined}>{children}- </div>+ </Text>);diff --git a/packages/twenty-ui/src/surfaces/OverflowingTextWithTooltip/OverflowingTextWithTooltip.module.scss b/packages/twenty-ui/src/surfaces/OverflowingTextWithTooltip/OverflowingTextWithTooltip.module.scssindex 7c065fb3eed..23915dff487 100644--- a/packages/twenty-ui/src/surfaces/OverflowingTextWithTooltip/OverflowingTextWithTooltip.module.scss+++ b/packages/twenty-ui/src/surfaces/OverflowingTextWithTooltip/OverflowingTextWithTooltip.module.scss@@ -3,16 +3,11 @@font-family: inherit;font-size: inherit;font-weight: inherit;+ height: auto;max-width: 100%;- overflow: hidden;+ overflow-wrap: break-word;text-decoration: inherit;- text-overflow: ellipsis;- height: auto;- -webkit-line-clamp: var(--displayed-max-rows, 1);- display: -webkit-box;- -webkit-box-orient: vertical;white-space: pre-wrap;- overflow-wrap: break-word;&[data-content-overflowing] {cursor: pointer;@@ -24,12 +19,9 @@font-family: inherit;font-size: inherit;font-weight: inherit;+ height: auto;max-width: 100%;text-decoration: inherit;- text-overflow: ellipsis;- overflow: hidden;- height: auto;- white-space: nowrap;&[data-content-overflowing] {cursor: pointer;diff --git a/packages/twenty-ui/src/surfaces/OverflowingTextWithTooltip/OverflowingTextWithTooltip.tsx b/packages/twenty-ui/src/surfaces/OverflowingTextWithTooltip/OverflowingTextWithTooltip.tsxindex f9db9d040c8..1e7ba97f545 100644--- a/packages/twenty-ui/src/surfaces/OverflowingTextWithTooltip/OverflowingTextWithTooltip.tsx+++ b/packages/twenty-ui/src/surfaces/OverflowingTextWithTooltip/OverflowingTextWithTooltip.tsx@@ -1,15 +1,10 @@-import {- type CSSProperties,- type ReactNode,- useId,- useRef,- useState,-} from 'react';+import { type ReactNode, useId, useRef, useState } from 'react';import { createPortal } from 'react-dom';import { isNonEmptyString } from '@sniptt/guards';import { clsx } from 'clsx';import { LinkifiedText } from '@ui/typography/LinkifiedText/LinkifiedText';+import { Text } from '@ui/typography/Text/Text';import { isDefined } from '@ui/utilities/utils/isDefined';import { AppTooltip, TooltipDelay } from '@ui/surfaces/AppTooltip/AppTooltip';@@ -77,29 +72,24 @@ export const OverflowingTextWithTooltip = ({return (<>{isDefined(displayedMaxRows) ? (- <div+ <Text+ lineClamp={displayedMaxRows || 1}data-testid="tooltip"data-content-overflowing={isTitleOverflowing ? '' : undefined}className={clsx(styles.overflowingMultilineText,size === 'large' && styles.large,)}- style={- {- '--displayed-max-rows': displayedMaxRows- ? displayedMaxRows.toString()- : '1',- } as CSSProperties- }ref={textRef}id={textElementId}onMouseEnter={handleMouseEnter}onMouseLeave={handleMouseLeave}>{isNonEmptyString(text) ? <LinkifiedText text={text} /> : text}- </div>+ </Text>) : (- <div+ <Text+ truncatedata-testid="tooltip"data-content-overflowing={isTitleOverflowing ? '' : undefined}className={clsx(@@ -112,7 +102,7 @@ export const OverflowingTextWithTooltip = ({onMouseLeave={handleMouseLeave}>{isNonEmptyString(text) ? <LinkifiedText text={text} /> : text}- </div>+ </Text>)}{shouldRenderTooltip &&diff --git a/packages/twenty-ui/src/typography/Text/Text.module.scss b/packages/twenty-ui/src/typography/Text/Text.module.scssnew file mode 100644index 00000000000..a8448503b61--- /dev/null+++ b/packages/twenty-ui/src/typography/Text/Text.module.scss@@ -0,0 +1,12 @@+.truncate {+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;+}++.lineClamp {+ display: -webkit-box;+ overflow: hidden;+ -webkit-box-orient: vertical;+ -webkit-line-clamp: var(--text-line-clamp, 1);+}diff --git a/packages/twenty-ui/src/typography/Text/Text.module.scss.d.ts b/packages/twenty-ui/src/typography/Text/Text.module.scss.d.tsnew file mode 100644index 00000000000..b734d2cebb5--- /dev/null+++ b/packages/twenty-ui/src/typography/Text/Text.module.scss.d.ts@@ -0,0 +1,5 @@+declare const classNames: {+ readonly truncate: 'truncate';+ readonly lineClamp: 'lineClamp';+};+export default classNames;diff --git a/packages/twenty-ui/src/typography/Text/Text.tsx b/packages/twenty-ui/src/typography/Text/Text.tsxnew file mode 100644index 00000000000..3df041e89c0--- /dev/null+++ b/packages/twenty-ui/src/typography/Text/Text.tsx@@ -0,0 +1,45 @@+import { useRender } from '@base-ui/react/use-render';+import { clsx } from 'clsx';+import { type CSSProperties } from 'react';++import { isDefined } from '@ui/utilities/utils/isDefined';++import styles from './Text.module.scss';++type TextTruncationProps =+ | { truncate?: boolean; lineClamp?: never }+ | { truncate?: never; lineClamp?: number };++type TextProps = useRender.ComponentProps<'div'> & TextTruncationProps;++export const Text = ({+ render,+ truncate,+ lineClamp,+ className,+ style,+ ref,+ ...props+}: TextProps) => {+ const shouldClamp =+ isDefined(lineClamp) && Number.isInteger(lineClamp) && lineClamp > 0;++ return useRender({+ render,+ ref,+ props: {+ ...props,+ className: clsx(+ truncate && styles.truncate,+ shouldClamp && styles.lineClamp,+ className,+ ),+ style: shouldClamp+ ? ({+ ...style,+ '--text-line-clamp': lineClamp,+ } as CSSProperties)+ : style,+ },+ });+};diff --git a/packages/twenty-ui/src/typography/Text/__stories__/Text.stories.tsx b/packages/twenty-ui/src/typography/Text/__stories__/Text.stories.tsxnew file mode 100644index 00000000000..750f5a94440--- /dev/null+++ b/packages/twenty-ui/src/typography/Text/__stories__/Text.stories.tsx@@ -0,0 +1,59 @@+import { type Meta, type StoryObj } from '@storybook/react-vite';+import { expect, within } from 'storybook/test';++import { ComponentDecorator } from '@ui/testing';++import { Text } from '@ui/typography/Text/Text';++const meta: Meta<typeof Text> = {+ title: 'UI/Typography/Text',+ component: Text,+ decorators: [ComponentDecorator],+};++export default meta;++type Story = StoryObj<typeof Text>;++const LONG_TEXT =+ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore';++export const Truncate: Story = {+ args: {+ truncate: true,+ children: LONG_TEXT,+ },+ parameters: {+ container: { width: 160 },+ },+ play: async ({ canvasElement }) => {+ const canvas = within(canvasElement);++ const text = await canvas.findByText(LONG_TEXT);+ const { textOverflow, whiteSpace } = getComputedStyle(text);++ await expect(whiteSpace).toBe('nowrap');+ await expect(textOverflow).toBe('ellipsis');+ await expect(text.scrollWidth).toBeGreaterThan(text.clientWidth);+ },+};++export const LineClamp: Story = {+ args: {+ lineClamp: 2,+ children: LONG_TEXT,+ },+ parameters: {+ container: { width: 160 },+ },+ play: async ({ canvasElement }) => {+ const canvas = within(canvasElement);++ const text = await canvas.findByText(LONG_TEXT);+ const { overflow, webkitLineClamp } = getComputedStyle(text);++ await expect(webkitLineClamp).toBe('2');+ await expect(overflow).toBe('hidden');+ await expect(text.scrollHeight).toBeGreaterThan(text.clientHeight);+ },+};diff --git a/packages/twenty-ui/src/typography/index.ts b/packages/twenty-ui/src/typography/index.tsindex 3eff53c400c..aea9b3a90c1 100644--- a/packages/twenty-ui/src/typography/index.ts+++ b/packages/twenty-ui/src/typography/index.ts@@ -19,3 +19,4 @@ export {StyledTextWrapper,StyledText,} from './StyledText/StyledText';+export { Text } from './Text/Text';