diff --git a/.claude/skills/component-stories/SKILL.md b/.claude/skills/component-stories/SKILL.md index 1eb04b380..14ba7db6b 100644 --- a/.claude/skills/component-stories/SKILL.md +++ b/.claude/skills/component-stories/SKILL.md @@ -164,9 +164,11 @@ Stories highlighting specific features. #### 4. Truncation / Responsiveness Stories -Stories that demonstrate how a component truncates or adapts to constrained space. +Stories that demonstrate how a component truncates text or adapts to constrained space. -- Use: `ResponsivenessShowcase` +- Use `ResponsivenessShowcase` for how the component adapts to constrained space or width. +- Use `NumberOfLinesShowcase` for how text is clamped or wraps when a `numberOfLines` prop controls it (the `{Property}Showcase` rule). Show the default, a line count and `0` (no limit). +- Use `TruncationShowcase` only when truncation is fixed and no prop controls it. - Do not use: `TruncateShowcase`, `Truncation`, `LongLabel` ## Comments @@ -187,7 +189,7 @@ Rules verifiable from a diff. | Check | Applies to | Detect | Skip | | ----------------------------------------------------------------------------------------------------------- | ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------- | | Base story named `Default`/`Primary`/`Basic` instead of `Base` | all stories | export name | — | -| Showcase/feature story off-convention | all stories | not `{Property}Showcase` / `With{Feature}` / `ResponsivenessShowcase` | — | +| Showcase/feature story off-convention | all stories | not `{Property}Showcase` / `With{Feature}` / `ResponsivenessShowcase` / `TruncationShowcase` | — | | Missing `layout: 'centered'` + `backgrounds: { default: 'light' }` | all stories | `Base` parameters | — | | `Base` or a feature story (`With{Feature}`) reads its data from a module-level const instead of inlining it | all stories | `data={}` / `args: { data: }` in `Base` or `With{Feature}` | `{Property}Showcase` and variation stories | | `type: 'dynamic'` applied to a story with function props, hooks, state or handlers | all stories | `docs.source.type: 'dynamic'` (on `meta` or the story) where a story has `use*` hooks, component or icon references (`icon={Settings}`), or function props with a meaningful body (`render*`, `get*`, function children, multi-statement handlers, helpers) | stories with plain-data props only | diff --git a/.nx/version-plans/version-plan-1795539502000-ui-react.md b/.nx/version-plans/version-plan-1795539502000-ui-react.md new file mode 100644 index 000000000..19e41b484 --- /dev/null +++ b/.nx/version-plans/version-plan-1795539502000-ui-react.md @@ -0,0 +1,5 @@ +--- +'@ledgerhq/lumen-ui-react': patch +--- + +feat(Card): add numberOfLines to CardContentTitle and CardContentDescription diff --git a/.nx/version-plans/version-plan-1795539502001-ui-rnative.md b/.nx/version-plans/version-plan-1795539502001-ui-rnative.md new file mode 100644 index 000000000..bce91f468 --- /dev/null +++ b/.nx/version-plans/version-plan-1795539502001-ui-rnative.md @@ -0,0 +1,5 @@ +--- +'@ledgerhq/lumen-ui-rnative': patch +--- + +feat(Card): default numberOfLines to 1 on CardContentTitle and CardContentDescription diff --git a/.nx/version-plans/version-plan-1795619083512-ui-react.md b/.nx/version-plans/version-plan-1795619083512-ui-react.md new file mode 100644 index 000000000..f6a88fee9 --- /dev/null +++ b/.nx/version-plans/version-plan-1795619083512-ui-react.md @@ -0,0 +1,5 @@ +--- +'@ledgerhq/lumen-ui-react': patch +--- + +feat(Tile): add numberOfLines to TileTitle and TileDescription diff --git a/.nx/version-plans/version-plan-1795619083513-ui-rnative.md b/.nx/version-plans/version-plan-1795619083513-ui-rnative.md new file mode 100644 index 000000000..a4f01047e --- /dev/null +++ b/.nx/version-plans/version-plan-1795619083513-ui-rnative.md @@ -0,0 +1,5 @@ +--- +'@ledgerhq/lumen-ui-rnative': patch +--- + +feat(Tile): honor numberOfLines on TileTitle and TileDescription diff --git a/.nx/version-plans/version-plan-1795619083515-migrations.md b/.nx/version-plans/version-plan-1795619083515-migrations.md new file mode 100644 index 000000000..82d48d923 --- /dev/null +++ b/.nx/version-plans/version-plan-1795619083515-migrations.md @@ -0,0 +1,5 @@ +--- +'@ledgerhq/lumen-migrations': patch +--- + +feat(migrations): add card-number-of-lines-undefined diff --git a/apps/app-sandbox-rnative/src/app/(blocks)/Cards.tsx b/apps/app-sandbox-rnative/src/app/(blocks)/Cards.tsx index 1c75ca9b7..515ffd144 100644 --- a/apps/app-sandbox-rnative/src/app/(blocks)/Cards.tsx +++ b/apps/app-sandbox-rnative/src/app/(blocks)/Cards.tsx @@ -316,6 +316,61 @@ export default function Cards() { + + + + + + + A very long card title that is cut after one line (default) + + + A very long card description that is cut after one line + (default), the rest stays hidden + + + + + + + + + + + + + A very long card title that should be truncated at 2 lines, but + not at 3 lines + + + A very long card description that keeps going well past the + width of the card and should be truncated at 3 lines but should + not be truncated at 2 lines + + + + + + + + + + + + + A very long card title that does not fit on a single line but it + will never truncate because numberOfLines is set to 0. + + + A very long card description that should keep going well past + the width of the card but it will never truncate because + numberOfLines is set to 0. + + + + + + { diff --git a/apps/app-sandbox-rnative/src/app/(blocks)/Tiles.tsx b/apps/app-sandbox-rnative/src/app/(blocks)/Tiles.tsx index 374975903..152efff5d 100644 --- a/apps/app-sandbox-rnative/src/app/(blocks)/Tiles.tsx +++ b/apps/app-sandbox-rnative/src/app/(blocks)/Tiles.tsx @@ -46,6 +46,42 @@ export default function Tiles() { + + + + + Title cut after one line (default) + + Description cut after one line (default) + + + + + + + + Title cut after two lines, but not after three + + + Description cut after three lines, but not after two lines, so the + rest stays hidden + + + + + + + + Title never cut because numberOfLines is 0 + + + Description never cut because numberOfLines is 0, so the whole + text stays visible + + + + + diff --git a/libs/migrations/src/card-number-of-lines-undefined.md b/libs/migrations/src/card-number-of-lines-undefined.md new file mode 100644 index 000000000..db291addc --- /dev/null +++ b/libs/migrations/src/card-number-of-lines-undefined.md @@ -0,0 +1,52 @@ +--- +changes: + - change: changed + surface: value + ref: '@ledgerhq/lumen-ui-rnative#CardContent*.numberOfLines=undefined' + signal: silent # undefined used to lift the limit; it now means the default of one line + detect: + - { pattern: ']*\bnumberOfLines=\{(?!\s*\d+\s*\})', expect: review } +--- + +## Before → after + +React Native (`@ledgerhq/lumen-ui-rnative`), `undefined` no longer lifts the +limit on `CardContentTitle` and `CardContentDescription`. This covers any value +that can be `undefined`: a conditional, an optional variable, or a prop that a +wrapper component forwards. + +```tsx before +{name}; + +// A wrapper that forwards an optional prop: omitting it showed the full text +const AssetName = ({ numberOfLines }: { numberOfLines?: number }) => {name}; +``` + +```tsx after +{name}; + +// Default the forwarded prop to 0 to keep the full text when it is omitted +const AssetName = ({ numberOfLines = 0 }: { numberOfLines?: number }) => {name}; +``` + +## Decide + +`CardContentTitle` and `CardContentDescription` now default `numberOfLines` to +`1`, and `undefined` gets that default like an omitted prop. Before, an +explicit `undefined` overrode the default and removed the limit. `0` still +means no limit, as in React Native. + +- `numberOfLines={undefined}`, or any expression that can be `undefined`, used + to remove the limit: a conditional, an optional variable, or a prop a wrapper + forwards. It now falls back to one line. Default: use `0` where the full text + was shown, and give a forwarded prop a default of `0` (`numberOfLines = 0`). +- An object spread that carries `numberOfLines: undefined` + (``) has the same effect, and `detect` + can't see it. Check the spreads on these parts. + +## Verify + +Every `numberOfLines={…}` on a Card part that is not a plain number reviewed: +each one still shows the text as before (the full text where it was not +limited, `0` where it must stay unlimited). Open each screen where these texts +wrapped or were clamped: they look as before. diff --git a/libs/ui-react/src/lib/Components/core/Card/Card.mdx b/libs/ui-react/src/lib/Components/core/Card/Card.mdx index dd912b88f..07d660efe 100644 --- a/libs/ui-react/src/lib/Components/core/Card/Card.mdx +++ b/libs/ui-react/src/lib/Components/core/Card/Card.mdx @@ -24,8 +24,8 @@ Card is a flexible container component that uses a composite pattern for display - **CardHeader**: Horizontal layout for the header row. Becomes the click target when `type="expandable"`. - **CardLeading**: Left section containing visual element and content. Reusable in both header and footer. - **CardContent**: Column wrapper for title + description. -- **CardContentTitle**: Primary text label with truncation. -- **CardContentDescription**: Secondary muted text with truncation. +- **CardContentTitle**: Primary text label. Truncated by default, see `numberOfLines`. +- **CardContentDescription**: Secondary muted text. Truncated by default, see `numberOfLines`. - **CardTrailing**: Right section for trailing content (Button, Tag, right-aligned values, etc.). - **CardFooter**: Footer container. `appearance="plain"` (default) uses a muted background; `appearance="no-background"` blends with the card surface. Collapses with a 300ms transition when `expanded === false`. @@ -69,6 +69,16 @@ Cards stack their content horizontally by default. To build a vertical card, wit +## Number of lines + +`CardContentTitle` and `CardContentDescription` take a `numberOfLines` prop: + +- `1` (default): one line, cut with an ellipsis. +- `2` to `9`: up to that many lines, then cut with an ellipsis. +- `0`: no limit, the text wraps fully. + + + ## Compositions Cards support various compositions by combining sub-components differently. diff --git a/libs/ui-react/src/lib/Components/core/Card/Card.stories.tsx b/libs/ui-react/src/lib/Components/core/Card/Card.stories.tsx index 19cce712a..6247c5304 100644 --- a/libs/ui-react/src/lib/Components/core/Card/Card.stories.tsx +++ b/libs/ui-react/src/lib/Components/core/Card/Card.stories.tsx @@ -542,6 +542,82 @@ export const LayoutShowcase: Story = { ), }; +export const NumberOfLinesShowcase: Story = { + render: () => ( +
+ + + + + + + A very long card title that does not fit on a single line + + + A very long card description that keeps going well past the + width of the card + + + + + + + + + + + + + A very long card title that should be truncated at 2 lines, but + not at 3 lines + + + A very long card description that keeps going well past the + width of the card and should be truncated at 3 lines but should + not be truncated at 2 lines + + + + + + + + + + + + + A very long card title that does not fit on a single line but it + will never truncate because numberOfLines is set to 0. + + + A very long card description that should keep going well past + the width of the card but it will never truncate because + numberOfLines is set to 0. + + + + + +
+ ), +}; + export const CompositionsShowcase: Story = { args: { className: 'w-320', diff --git a/libs/ui-react/src/lib/Components/core/Card/Card.test.tsx b/libs/ui-react/src/lib/Components/core/Card/Card.test.tsx index 06a4ba061..108b1a5d1 100644 --- a/libs/ui-react/src/lib/Components/core/Card/Card.test.tsx +++ b/libs/ui-react/src/lib/Components/core/Card/Card.test.tsx @@ -2,6 +2,7 @@ import { render, screen, fireEvent } from '@testing-library/react'; import { describe, it, expect, vi } from 'vitest'; import '@testing-library/jest-dom'; +import type { NumberOfLines } from '../../../../utils/getNumberOfLinesClass/getNumberOfLinesClass'; import { Card, CardHeader, @@ -290,4 +291,51 @@ describe('Card', () => { expect(ref.current).toBeInstanceOf(HTMLDivElement); }); }); + + describe('numberOfLines', () => { + const renderText = (props: { numberOfLines?: NumberOfLines } = {}) => + render( + + + + + Long title + + Long description + + + + + , + ); + + it('truncates title and description to one line by default', () => { + renderText(); + + expect(screen.getByText('Long title')).toHaveClass('truncate'); + expect(screen.getByText('Long description')).toHaveClass('truncate'); + }); + + it('clamps title and description to the given number of lines', () => { + renderText({ numberOfLines: 2 }); + + for (const text of ['Long title', 'Long description']) { + expect(screen.getByText(text)).toHaveClass( + 'wrap-anywhere', + 'line-clamp-2', + ); + expect(screen.getByText(text)).not.toHaveClass('truncate'); + } + }); + + it('removes the limit when numberOfLines is 0', () => { + renderText({ numberOfLines: 0 }); + + for (const text of ['Long title', 'Long description']) { + expect(screen.getByText(text)).toHaveClass('wrap-anywhere'); + expect(screen.getByText(text)).not.toHaveClass('truncate'); + expect(screen.getByText(text).className).not.toContain('line-clamp'); + } + }); + }); }); diff --git a/libs/ui-react/src/lib/Components/core/Card/Card.tsx b/libs/ui-react/src/lib/Components/core/Card/Card.tsx index 8c1a6ed3b..8b5d2be6f 100644 --- a/libs/ui-react/src/lib/Components/core/Card/Card.tsx +++ b/libs/ui-react/src/lib/Components/core/Card/Card.tsx @@ -6,6 +6,7 @@ import { } from '@ledgerhq/lumen-utils-shared'; import { cva } from 'class-variance-authority'; import { useMemo } from 'react'; +import { getNumberOfLinesClass } from '../../../../utils/getNumberOfLinesClass/getNumberOfLinesClass'; import { ChevronDown, ChevronUp } from '../../symbols'; import type { CardContentAlignContextValue, @@ -304,6 +305,7 @@ export const CardContentTitle = ({ ref, children, className, + numberOfLines = 1, ...props }: CardContentTitleProps) => { const { disabled } = useCardContext({ @@ -315,7 +317,8 @@ export const CardContentTitle = ({
{ const { disabled } = useCardContext({ @@ -344,7 +348,8 @@ export const CardContentDescription = ({
+### Number of lines + +`TileTitle` and `TileDescription` take a `numberOfLines` prop: + +- `1` (default): one line, cut with an ellipsis. +- `2` to `9`: up to that many lines, then cut with an ellipsis. +- `0`: no limit, the text wraps fully. + + + ### Centered Content Use the `centered` prop to vertically center tile content. This is useful when a tile has less content than its neighbors, like a "Show more" tile in a grid. diff --git a/libs/ui-react/src/lib/Components/core/Tile/Tile.stories.tsx b/libs/ui-react/src/lib/Components/core/Tile/Tile.stories.tsx index ca24c3ca0..16fa1a99f 100644 --- a/libs/ui-react/src/lib/Components/core/Tile/Tile.stories.tsx +++ b/libs/ui-react/src/lib/Components/core/Tile/Tile.stories.tsx @@ -205,6 +205,51 @@ export const ResponsiveLayout: Story = { ), }; +export const NumberOfLinesShowcase: Story = { + render: () => ( +
+ + + + + A very long tile title that is cut after one line + + + A very long tile description that is cut after one line, the rest + stays hidden + + + + + + + + A very long tile title that is cut after two lines, but not after + three lines + + + A very long tile description that is cut after three lines, but not + after two lines, so the rest of this text stays hidden + + + + + + + + A very long tile title that is never cut because numberOfLines is + set to 0 + + + A very long tile description that is never cut because numberOfLines + is set to 0, so the whole text stays visible + + + +
+ ), +}; + export const AppearanceShowcase: Story = { render: () => (
diff --git a/libs/ui-react/src/lib/Components/core/Tile/Tile.test.tsx b/libs/ui-react/src/lib/Components/core/Tile/Tile.test.tsx index 8c84ce299..a9137362f 100644 --- a/libs/ui-react/src/lib/Components/core/Tile/Tile.test.tsx +++ b/libs/ui-react/src/lib/Components/core/Tile/Tile.test.tsx @@ -2,6 +2,7 @@ import { render, screen, fireEvent } from '@testing-library/react'; import { describe, it, expect, vi } from 'vitest'; import '@testing-library/jest-dom'; +import type { NumberOfLines } from '../../../../utils/getNumberOfLinesClass/getNumberOfLinesClass'; import { Settings, MoreVertical } from '../../symbols'; import { Spot } from '../Spot'; import { Tag } from '../Tag'; @@ -334,4 +335,46 @@ describe('Tile Component', () => { expect(screen.getByText('Second')).toBeInTheDocument(); }); }); + + describe('numberOfLines', () => { + const renderText = (props: { numberOfLines?: NumberOfLines } = {}) => + render( + + + + Long title + Long description + + , + ); + + it('truncates title and description to one line by default', () => { + renderText(); + + expect(screen.getByText('Long title')).toHaveClass('truncate'); + expect(screen.getByText('Long description')).toHaveClass('truncate'); + }); + + it('clamps title and description to the given number of lines', () => { + renderText({ numberOfLines: 3 }); + + for (const text of ['Long title', 'Long description']) { + expect(screen.getByText(text)).toHaveClass( + 'wrap-anywhere', + 'line-clamp-3', + ); + expect(screen.getByText(text)).not.toHaveClass('truncate'); + } + }); + + it('removes the limit when numberOfLines is 0', () => { + renderText({ numberOfLines: 0 }); + + for (const text of ['Long title', 'Long description']) { + expect(screen.getByText(text)).toHaveClass('wrap-anywhere'); + expect(screen.getByText(text)).not.toHaveClass('truncate'); + expect(screen.getByText(text).className).not.toContain('line-clamp'); + } + }); + }); }); diff --git a/libs/ui-react/src/lib/Components/core/Tile/Tile.tsx b/libs/ui-react/src/lib/Components/core/Tile/Tile.tsx index a380c7edf..bdc50da06 100644 --- a/libs/ui-react/src/lib/Components/core/Tile/Tile.tsx +++ b/libs/ui-react/src/lib/Components/core/Tile/Tile.tsx @@ -5,6 +5,7 @@ import { } from '@ledgerhq/lumen-utils-shared'; import { cva } from 'class-variance-authority'; import { useCallback } from 'react'; +import { getNumberOfLinesClass } from '../../../../utils/getNumberOfLinesClass/getNumberOfLinesClass'; import { InteractiveIcon } from '../InteractiveIcon'; import type { TileContentProps, @@ -140,11 +141,12 @@ export const TileContent = ({ /** * The primary text label for a Tile. Automatically inherits the disabled state from the parent Tile. - * Text will truncate with ellipsis if it exceeds the available width. + * Text will truncate with ellipsis if it exceeds the available width, unless `numberOfLines` is raised. */ export const TileTitle = ({ children, className, + numberOfLines = 1, ...props }: TileTitleProps) => { const disabled = useDisabledContext({ @@ -154,7 +156,9 @@ export const TileTitle = ({ return (
{ const disabled = useDisabledContext({ @@ -181,7 +186,9 @@ export const TileDescription = ({ return (
{ + it('truncates on a single line', () => { + expect(getNumberOfLinesClass(1)).toBe('truncate'); + }); + + it('wraps without a limit for 0', () => { + expect(getNumberOfLinesClass(0)).toBe('wrap-anywhere'); + }); + + it.each([2, 3, 4, 5, 6, 7, 8, 9] as const)('clamps to %i lines', (lines) => { + expect(getNumberOfLinesClass(lines)).toBe( + `wrap-anywhere line-clamp-${lines}`, + ); + }); +}); diff --git a/libs/ui-react/src/utils/getNumberOfLinesClass/getNumberOfLinesClass.ts b/libs/ui-react/src/utils/getNumberOfLinesClass/getNumberOfLinesClass.ts new file mode 100644 index 000000000..c8bb57d19 --- /dev/null +++ b/libs/ui-react/src/utils/getNumberOfLinesClass/getNumberOfLinesClass.ts @@ -0,0 +1,24 @@ +export type NumberOfLines = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9; + +// Tailwind only generates classes it can read literally, so no string building. +const LINE_CLAMP_CLASSES = { + 2: 'wrap-anywhere line-clamp-2', + 3: 'wrap-anywhere line-clamp-3', + 4: 'wrap-anywhere line-clamp-4', + 5: 'wrap-anywhere line-clamp-5', + 6: 'wrap-anywhere line-clamp-6', + 7: 'wrap-anywhere line-clamp-7', + 8: 'wrap-anywhere line-clamp-8', + 9: 'wrap-anywhere line-clamp-9', +} as const; + +/** + * Classes that limit a block of text to `numberOfLines`. A single line uses + * `truncate` so unbroken text stays on one line, and `0` removes the limit, + * like React Native. + */ +export const getNumberOfLinesClass = (numberOfLines: NumberOfLines): string => { + if (numberOfLines === 1) return 'truncate'; + if (numberOfLines === 0) return 'wrap-anywhere'; + return LINE_CLAMP_CLASSES[numberOfLines]; +}; diff --git a/libs/ui-rnative/src/lib/Components/core/Card/Card.mdx b/libs/ui-rnative/src/lib/Components/core/Card/Card.mdx index 5fe24a1cb..6f6d62405 100644 --- a/libs/ui-rnative/src/lib/Components/core/Card/Card.mdx +++ b/libs/ui-rnative/src/lib/Components/core/Card/Card.mdx @@ -40,8 +40,8 @@ Card is a flexible container component that uses a composite pattern for display - **CardLeading**: Left section containing visual element and content. Reusable in both header and footer. - **CardContent**: Column wrapper for title + description. Supports right alignment when inside `CardTrailing`. - **CardContentRow**: Horizontal row for placing title/description alongside additional inline content (e.g. Tag). -- **CardContentTitle**: Primary text label with single-line truncation. -- **CardContentDescription**: Secondary muted text with single-line truncation. +- **CardContentTitle**: Primary text label. Truncated to a single line by default, see `numberOfLines`. +- **CardContentDescription**: Secondary muted text. Truncated to a single line by default, see `numberOfLines`. - **CardTrailing**: Right section for trailing content (Button, Tag, right-aligned values, etc.). - **CardFooter**: Footer container. `appearance="plain"` (default) uses a muted background; `appearance="no-background"` blends with the card surface. Collapses with a 300ms animation when `expanded === false`. - **CardFooterActions**: Horizontal row for action buttons. @@ -93,6 +93,16 @@ Cards stack their content horizontally by default. To build a vertical card, wit +## Number of lines + +`CardContentTitle` and `CardContentDescription` take a `numberOfLines` prop: + +- `1` (default): one line, cut with an ellipsis. +- `2` or more: up to that many lines, then cut with an ellipsis. +- `0`: no limit, the text wraps fully. + + + ## Compositions Cards support various compositions by combining sub-components differently. diff --git a/libs/ui-rnative/src/lib/Components/core/Card/Card.stories.tsx b/libs/ui-rnative/src/lib/Components/core/Card/Card.stories.tsx index e76d5ab8f..dd32b3995 100644 --- a/libs/ui-rnative/src/lib/Components/core/Card/Card.stories.tsx +++ b/libs/ui-rnative/src/lib/Components/core/Card/Card.stories.tsx @@ -485,6 +485,79 @@ export const LayoutShowcase: Story = { ), }; +export const NumberOfLinesShowcase: Story = { + parameters: { docs: { source: { type: 'dynamic' } } }, + args: { + type: 'info', + children: null, + }, + render: (args) => ( + + + + + + + + A very long card title that does not fit on a single line + + + A very long card description that keeps going well past the + width of the card + + + + + + + + + + + + + A very long card title that should be truncated at 2 lines, but + not at 3 lines + + + A very long card description that keeps going well past the + width of the card and should be truncated at 3 lines but should + not be truncated at 2 lines + + + + + + + + + + + + + A very long card title that does not fit on a single line but it + will never truncate because numberOfLines is set to 0. + + + A very long card description that should keep going well past + the width of the card but it will never truncate because + numberOfLines is set to 0. + + + + + + + ), +}; + export const CompositionsShowcase: Story = { args: { children: null, diff --git a/libs/ui-rnative/src/lib/Components/core/Card/Card.test.tsx b/libs/ui-rnative/src/lib/Components/core/Card/Card.test.tsx index 490548d0b..7dd369338 100644 --- a/libs/ui-rnative/src/lib/Components/core/Card/Card.test.tsx +++ b/libs/ui-rnative/src/lib/Components/core/Card/Card.test.tsx @@ -223,4 +223,58 @@ describe('Card', () => { expect(screen.getByText('BTC')).toBeTruthy(); }); }); + + describe('numberOfLines', () => { + const renderText = (props: { numberOfLines?: number } = {}) => + render( + + + + + + Long title + + Long description + + + + + + , + ); + + it('truncates title and description to one line by default', () => { + renderText(); + + for (const text of ['Long title', 'Long description']) { + expect(screen.getByText(text).props.numberOfLines).toBe(1); + expect(screen.getByText(text).props.ellipsizeMode).toBe('tail'); + } + }); + + it('clamps title and description to the given number of lines', () => { + renderText({ numberOfLines: 2 }); + + for (const text of ['Long title', 'Long description']) { + expect(screen.getByText(text).props.numberOfLines).toBe(2); + expect(screen.getByText(text).props.ellipsizeMode).toBe('tail'); + } + }); + + it('falls back to one line when numberOfLines is undefined', () => { + renderText({ numberOfLines: undefined }); + + for (const text of ['Long title', 'Long description']) { + expect(screen.getByText(text).props.numberOfLines).toBe(1); + } + }); + + it('removes the limit when numberOfLines is 0', () => { + renderText({ numberOfLines: 0 }); + + for (const text of ['Long title', 'Long description']) { + expect(screen.getByText(text).props.numberOfLines).toBe(0); + } + }); + }); }); diff --git a/libs/ui-rnative/src/lib/Components/core/Card/Card.tsx b/libs/ui-rnative/src/lib/Components/core/Card/Card.tsx index 2194a05fd..780019eea 100644 --- a/libs/ui-rnative/src/lib/Components/core/Card/Card.tsx +++ b/libs/ui-rnative/src/lib/Components/core/Card/Card.tsx @@ -467,6 +467,7 @@ export const CardContentTitle = ({ children, lx = {}, style, + numberOfLines = 1, ...props }: CardContentTitleProps & { ref?: Ref }) => { const { disabled } = useCardContext({ @@ -505,7 +506,7 @@ export const CardContentTitle = ({ ref={ref as Ref>} lx={lx} style={StyleSheet.flatten([styles.asText, style])} - numberOfLines={1} + numberOfLines={numberOfLines} ellipsizeMode='tail' {...props} > @@ -534,6 +535,7 @@ export const CardContentDescription = ({ children, lx = {}, style, + numberOfLines = 1, ...props }: CardContentDescriptionProps & { ref?: Ref }) => { const { disabled } = useCardContext({ @@ -572,7 +574,7 @@ export const CardContentDescription = ({ ref={ref as Ref>} lx={lx} style={StyleSheet.flatten([styles.asText, style])} - numberOfLines={1} + numberOfLines={numberOfLines} ellipsizeMode='tail' {...props} > diff --git a/libs/ui-rnative/src/lib/Components/core/Card/types.ts b/libs/ui-rnative/src/lib/Components/core/Card/types.ts index 10cef6840..fdae0ea5f 100644 --- a/libs/ui-rnative/src/lib/Components/core/Card/types.ts +++ b/libs/ui-rnative/src/lib/Components/core/Card/types.ts @@ -79,6 +79,12 @@ export type CardContentTitleProps = { * The title text or custom inline content. */ children: ReactNode; + /** + * Maximum number of lines before the text is cut with an ellipsis. + * `0` means no limit: the text wraps fully. Only applies to text children. + * @default 1 + */ + numberOfLines?: number; } & Omit; export type CardContentDescriptionProps = { @@ -86,6 +92,12 @@ export type CardContentDescriptionProps = { * The description text or custom inline content. */ children: ReactNode; + /** + * Maximum number of lines before the text is cut with an ellipsis. + * `0` means no limit: the text wraps fully. Only applies to text children. + * @default 1 + */ + numberOfLines?: number; } & Omit; export type CardContentRowProps = { diff --git a/libs/ui-rnative/src/lib/Components/core/Tile/Tile.mdx b/libs/ui-rnative/src/lib/Components/core/Tile/Tile.mdx index 37a92c4b7..02c833117 100644 --- a/libs/ui-rnative/src/lib/Components/core/Tile/Tile.mdx +++ b/libs/ui-rnative/src/lib/Components/core/Tile/Tile.mdx @@ -72,6 +72,16 @@ By default the tile fills the width of its container. The title and description +### Number of lines + +`TileTitle` and `TileDescription` take a `numberOfLines` prop: + +- `1` (default): one line, cut with an ellipsis. +- `2` or more: up to that many lines, then cut with an ellipsis. +- `0`: no limit, the text wraps fully. + + + ### Centered Content Use the `centered` prop to vertically center tile content. This is useful when a tile has less content than its neighbors, like a "Show more" tile in a grid. diff --git a/libs/ui-rnative/src/lib/Components/core/Tile/Tile.stories.tsx b/libs/ui-rnative/src/lib/Components/core/Tile/Tile.stories.tsx index 55600a418..ede055c49 100644 --- a/libs/ui-rnative/src/lib/Components/core/Tile/Tile.stories.tsx +++ b/libs/ui-rnative/src/lib/Components/core/Tile/Tile.stories.tsx @@ -221,6 +221,54 @@ export const ResponsiveLayout: Story = { ), }; +export const NumberOfLinesShowcase: Story = { + args: { + children: null, + }, + render: () => ( + + + + + + A very long tile title that is cut after one line + + + A very long tile description that is cut after one line, the rest + stays hidden + + + + + + + + A very long tile title that is cut after two lines, but not after + three lines + + + A very long tile description that is cut after three lines, but not + after two lines, so the rest of this text stays hidden + + + + + + + + A very long tile title that is never cut because numberOfLines is + set to 0 + + + A very long tile description that is never cut because numberOfLines + is set to 0, so the whole text stays visible + + + + + ), +}; + export const AppearanceShowcase: Story = { args: { children: null, diff --git a/libs/ui-rnative/src/lib/Components/core/Tile/Tile.test.tsx b/libs/ui-rnative/src/lib/Components/core/Tile/Tile.test.tsx index 8c5379bfd..5d1382c8b 100644 --- a/libs/ui-rnative/src/lib/Components/core/Tile/Tile.test.tsx +++ b/libs/ui-rnative/src/lib/Components/core/Tile/Tile.test.tsx @@ -224,4 +224,47 @@ describe('Tile Component', () => { expect(getByText(tagText)).toBeTruthy(); }); }); + + describe('numberOfLines', () => { + const renderText = (props: { numberOfLines?: number } = {}) => + render( + + + + + Long title + Long description + + + , + ); + + it('truncates title and description to one line by default', () => { + const { getByTestId } = renderText(); + + expect(getByTestId('tile-title').props.numberOfLines).toBe(1); + expect(getByTestId('tile-description').props.numberOfLines).toBe(1); + }); + + it('clamps title and description to the given number of lines', () => { + const { getByTestId } = renderText({ numberOfLines: 3 }); + + expect(getByTestId('tile-title').props.numberOfLines).toBe(3); + expect(getByTestId('tile-description').props.numberOfLines).toBe(3); + }); + + it('falls back to one line when numberOfLines is undefined', () => { + const { getByTestId } = renderText({ numberOfLines: undefined }); + + expect(getByTestId('tile-title').props.numberOfLines).toBe(1); + expect(getByTestId('tile-description').props.numberOfLines).toBe(1); + }); + + it('removes the limit when numberOfLines is 0', () => { + const { getByTestId } = renderText({ numberOfLines: 0 }); + + expect(getByTestId('tile-title').props.numberOfLines).toBe(0); + expect(getByTestId('tile-description').props.numberOfLines).toBe(0); + }); + }); }); diff --git a/libs/ui-rnative/src/lib/Components/core/Tile/Tile.tsx b/libs/ui-rnative/src/lib/Components/core/Tile/Tile.tsx index 87d888946..a1f40d3d0 100644 --- a/libs/ui-rnative/src/lib/Components/core/Tile/Tile.tsx +++ b/libs/ui-rnative/src/lib/Components/core/Tile/Tile.tsx @@ -230,10 +230,15 @@ const useTitleStyles = ({ disabled }: { disabled: boolean }) => { /** * The primary text label for a Tile. Automatically inherits the disabled state from the parent Tile. - * Text will truncate with ellipsis if it exceeds the available width. + * Text will truncate with ellipsis if it exceeds the available width, unless `numberOfLines` is changed. * If children is a string, it will be automatically wrapped in a Text component. */ -export const TileTitle = ({ children, lx, style }: TileTitleProps) => { +export const TileTitle = ({ + children, + lx, + style, + numberOfLines = 1, +}: TileTitleProps) => { const disabled = useDisabledContext({ consumerName: 'TileTitle', contextRequired: true, @@ -244,7 +249,7 @@ export const TileTitle = ({ children, lx, style }: TileTitleProps) => { return ( @@ -287,7 +292,7 @@ const useDescriptionStyles = ({ disabled }: { disabled: boolean }) => { /** * The secondary text label for a Tile. Automatically inherits the disabled state from the parent Tile. - * Text will truncate with ellipsis if it exceeds the available width. + * Text will truncate with ellipsis if it exceeds the available width, unless `numberOfLines` is changed. * If children is a string, it will be automatically wrapped in a Text component. * * @example @@ -302,6 +307,7 @@ export const TileDescription = ({ children, lx, style, + numberOfLines = 1, }: TileDescriptionProps) => { const disabled = useDisabledContext({ consumerName: 'TileDescription', @@ -313,7 +319,7 @@ export const TileDescription = ({ return ( diff --git a/libs/ui-rnative/src/lib/Components/core/Tile/types.ts b/libs/ui-rnative/src/lib/Components/core/Tile/types.ts index 176cbf106..10ccd1d7a 100644 --- a/libs/ui-rnative/src/lib/Components/core/Tile/types.ts +++ b/libs/ui-rnative/src/lib/Components/core/Tile/types.ts @@ -57,6 +57,13 @@ export type TileTitleProps = { * The title text to display. */ children: ReactNode; + /** + * Maximum number of lines before the text is cut with an ellipsis. + * `0` or an explicit `undefined` means no limit: the text wraps fully. + * Only applies to text children. + * @default 1 + */ + numberOfLines?: number; } & StyledTextProps; export type TileDescriptionProps = { @@ -64,6 +71,13 @@ export type TileDescriptionProps = { * The description text to display. */ children: ReactNode; + /** + * Maximum number of lines before the text is cut with an ellipsis. + * `0` or an explicit `undefined` means no limit: the text wraps fully. + * Only applies to text children. + * @default 1 + */ + numberOfLines?: number; } & StyledTextProps; export type TileTrailingContentProps = {