// // Copyright 2026 DXOS.org // import '../../theme/index.css'; import { type Meta, type StoryObj } from '@storybook/react-vite'; import React, { useState } from 'react'; import { expect, userEvent, waitFor, within } from 'storybook/test'; import { translations } from '#translations'; import { type Size, SIZES } from '../../sizes.ts'; import { GEOMETRY, byTestId, centreX, controlSize, expectTooltip, sizeRow } from '../../testing/geometry.ts'; import { withLayout, withTheme } from '../../testing/index.ts'; import { SIZE_ARG_TYPES, type SizeArgs, withSizes } from '../../testing/stories.tsx'; import * as Avatar from '../Avatar/Avatar.tsx'; import { Block } from '../Block/Block.tsx'; import { Button } from '../Button/Button.tsx'; import { Container } from '../Container/Container.tsx'; import * as DragHandle from '../DragHandle/DragHandle.tsx'; import * as Icon from '../Icon/Icon.tsx'; import * as Menu from '../Menu/Menu.tsx'; import { Switch } from '../Switch/Switch.tsx'; import * as Tag from '../Tag/Tag.tsx'; import * as Typography from '../Typography/Typography.tsx'; import * as Card from './Card.tsx'; /** Inline SVG, so the story never fetches from the network. */ const POSTER = `data:image/svg+xml,${encodeURIComponent( ` `, )}`; /** A malformed data URI fails to decode without any network request. */ const BROKEN = 'data:image/png;base64,AAAA'; type RowsCardProps = { size: Size; /** Places icons and trailing actions in the card's rails; without it they sit inline in each row. */ grid?: Card.RootProps['grid']; /** Keeps the test ids and accessible names of the inline copy distinct. */ prefix?: string; rows: number; onInvite: () => void; }; /** A card of sections, rows, a link, a menu and a drag handle, in either placement of its leading and trailing cells. */ const RowsCard = ({ size, grid, prefix = '', rows, onInvite }: RowsCardProps) => { const name = prefix ? 'Inline ' : ''; return ( {name}Project Owner} data-testid={`${prefix}row-${size}`} > Ada Lovelace } data-testid={`${prefix}long-row-${size}`} > Charles Babbage, Lucasian Professor of Mathematics at Cambridge {name}Invite ({rows}) Updated today. ); }; /** * A poster card, a card with a header action and footer, and one with a broken poster; then a card of sections, rows, * a link, a menu and a drag handle, as a `grid` card and beside it as a default (inline) one; a clickable, selected card with * a nested action; and a borderless card. */ const DefaultStory = ({ size = 'md' }: SizeArgs) => { const [opened, setOpened] = useState(0); const [starred, setStarred] = useState(0); const [rows, setRows] = useState(0); return (
Launch The first public release, with sharing and sync. Roadmap Notes A card with a broken poster and no footer. setRows((count) => count + 1)} /> setRows((count) => count + 1)} /> setOpened((count) => count + 1)} data-testid={`clickable-${size}`}> Opened {opened} setStarred((count) => count + 1)} data-testid={`star-${size}`} /> Starred {starred} Borderless
); }; /** * A rail card's geometry: its inner edges (inside the border and the inline padding), the centres of its block-wide * start and end rails, and its content edge. */ const rails = (card: HTMLElement, size: Size) => { const rect = card.getBoundingClientRect(); const style = getComputedStyle(card); const inner = parseFloat(style.borderLeftWidth) + parseFloat(style.paddingLeft); const { block } = GEOMETRY[size]; return { padding: parseFloat(style.paddingLeft), innerStart: rect.left + inner, innerEnd: rect.right - inner, start: rect.left + inner + block / 2, end: rect.right - inner - block / 2, contentStart: rect.left + inner + block, }; }; const meta = { title: 'ui/react-ui-core/components/Card', render: DefaultStory, decorators: [withSizes(), withLayout({ classNames: 'p-0 w-[52rem]' }), withTheme()], args: { size: 'md' }, argTypes: SIZE_ARG_TYPES, parameters: { layout: 'centered', translations }, } satisfies Meta; export default meta; type Story = StoryObj; export const Default: Story = {}; /** `size` scopes a card's own metrics: its blocks and controls take that size whatever the host's. */ export const Sized: Story = { render: () => ( Small card ), play: async ({ canvasElement }) => { // A header Block is one control at the card's size. await expect(byTestId(canvasElement, 'sized-block').getBoundingClientRect().height).toBeCloseTo( controlSize('sm'), 0, ); }, }; const TILES = [ { icon: 'ph--kanban--regular', hue: 'indigo', title: 'Kanban', text: 'Boards of cards in columns.' }, { icon: 'ph--table--regular', hue: 'green', title: 'Sheet', text: 'Spreadsheets with formulas, ranges and charts, shared live with everyone in the space, and a much longer description that clamps to three lines however wide the card is.', }, { icon: 'ph--compass--regular', hue: 'amber', title: 'Explorer', text: 'Browse the graph.' }, ] as const; const TileGridStory = () => { const [opened, setOpened] = useState(''); return ( {TILES.map(({ icon, hue, title, text }) => ( setOpened(title)} data-testid={`tile-${title}`} /> {title} {text} labs ))} {opened} ); }; /** * A responsive grid of tile cards: a `row` Container whose columns auto-fill, `align='stretch'` so a row's cards share * the tallest's height, and `padBlock` on the template root so the grid starts a gutter below its top edge. */ export const TileGrid: Story = { render: () => , play: async ({ canvasElement }) => { const card = byTestId(canvasElement, 'tile-card-Kanban').getBoundingClientRect(); const tile = byTestId(canvasElement, 'tile-Kanban'); const tileRect = tile.getBoundingClientRect(); // The tile fills the card's start edge and full height, inside its border. await expect(tileRect.left - card.left).toBeCloseTo(1, 0); await expect(tileRect.height).toBeCloseTo(card.height - 2, 0); await expect(getComputedStyle(tile).backgroundColor).not.toBe( getComputedStyle(byTestId(canvasElement, 'tile-card-Kanban')).backgroundColor, ); // Cards of a row stretch to the tallest, and each footer ends at its card's bottom. const sheet = byTestId(canvasElement, 'tile-card-Sheet').getBoundingClientRect(); await expect(sheet.height).toBeCloseTo(card.height, 0); const footer = byTestId(canvasElement, 'tile-footer-Kanban').getBoundingClientRect(); await expect(card.bottom - footer.bottom).toBeLessThan(12); // `padBlock` starts the grid a gutter below the root's edge. const root = byTestId(canvasElement, 'tile-root').getBoundingClientRect(); await expect(card.top - root.top).toBeGreaterThan(0); await userEvent.click(tile); await waitFor(() => expect(byTestId(canvasElement, 'tile-opened')).toHaveTextContent('Kanban')); }, }; /** A Row's `leading` content (here an avatar) takes the icon's Block in the start rail. */ export const LeadingRow: Story = { render: () => ( } trailing={} onClick={() => {}} > Ada Lovelace Standup ), play: async ({ canvasElement }) => { const avatar = byTestId(canvasElement, 'leading-avatar'); const icon = canvasElement.querySelectorAll('[data-part="row"]')[1]?.querySelector('svg'); // Both sit in the start rail's Block, so the row texts start at the same edge. await expect(avatar.closest('[data-part="row"] > *')).not.toBeNull(); await expect(centreX(avatar.getBoundingClientRect())).toBeCloseTo( icon ? centreX(icon.getBoundingClientRect()) : 0, 0, ); }, }; /** A wrapping Title keeps the header's icon and icon-only Button beside its first line. */ export const WrappingTitle: Story = { render: () => ( A title long enough to wrap over several lines