// // Copyright 2026 DXOS.org // // @import-as-namespace import React, { type ComponentPropsWithoutRef, type MouseEvent, type ReactNode, forwardRef, useId } from 'react'; import { mx } from '@dxos/ui-theme'; import { type ThemedClassName } from '@dxos/ui-types'; import { clickableProps } from '../../clickable.ts'; import { recipes } from '../../recipes.ts'; import { type Size } from '../../sizes.ts'; import { composable, composableProps } from '../../util/slots.ts'; import { Block } from '../Block/Block.tsx'; import { Button, type ButtonProps } from '../Button/Button.tsx'; import { Container } from '../Container/Container.tsx'; import { Group, type GroupProps } from '../Group/Group.tsx'; import * as Icon from '../Icon/Icon.tsx'; import { Image, type ImageProps } from '../Image/Image.tsx'; import * as Menu from '../Menu/Menu.tsx'; import * as SystemButton from '../SystemButton/SystemButton.tsx'; import * as Typography from '../Typography/Typography.tsx'; /** A click inside a clickable card or row (a trailing action, a menu) must not also activate it. */ const stopPropagation = (event: MouseEvent) => event.stopPropagation(); // // Root // type CardRootProps = ThemedClassName> & { /** The separator border and radius; `false` for a card inside a surface that already frames it. */ border?: boolean; /** Marks the chosen card of a set (`data-selected`, `aria-current`). */ selected?: boolean; /** * Makes the card a `gutter='rail'` Container: Body, Section and Row become subgrids of it, and a Header's or Row's * leading and trailing cells sit in its rails, one gap inside the border, so every text starts at the content edge. * Off by default: the card is a padded column, and rows lay their icon, text and trailing cells out inline. */ grid?: boolean; /** * The rails' width in a `grid` card: one block (`rail`, the default), or a gutter step for a card whose rows should * sit further in. Rows and their icons follow the rails; a Header lays out by blocks, so wider rails suit row headers. */ gutter?: 'rail' | 'md' | 'lg'; /** Sizes the card's rows, blocks and controls (its `data-size` scope), whatever its host's size. */ size?: Size; }; /** * A card one level above its host (`level='+1'`), whose Header, Body and Footer share one content edge. With `grid` it * is a rail Container (the child div wins the `asChild` merge, so the part keeps the card scope); without, a plain * column that creates no grid. With `onClick` the card is a button (Enter and Space activate it); nested actions and * menus stop their clicks reaching it. */ const CardRoot = composable>>( ({ border = true, selected, grid = false, gutter = 'rail', size, onClick, onKeyDown, ...props }, forwardedRef) => { // Composable, so the card can be an `asChild` host's child (a Mosaic tile, a Focus item) and take its props. const { className, ...rest } = composableProps(props, { classNames: [recipes.cardRoot(), onClick && recipes.cardClickable()], }); const card = (
); return grid ? ( {card} ) : ( card ); }, ); CardRoot.displayName = 'Card.Root'; // // Poster // type CardPosterProps = ImageProps; /** A full-bleed Image across the card's gutters; first in the card, it takes the card's top corners. */ const CardPoster = forwardRef(({ classNames, ...props }, forwardedRef) => ( )); CardPoster.displayName = 'Card.Poster'; // // Tile // type CardTileProps = ThemedClassName, 'children'>> & { icon: string; /** Fills the tile with the hue's surface and colours the icon with its foreground. */ hue?: Icon.IconHue; }; /** * A full-height leading tile of the card's hue holding a large icon; first in the card, it lays the card out as a row, * so the parts after it (a Body) form the column beside it. With `onClick` it is a button (Enter and Space activate it). */ const CardTile = forwardRef( ({ classNames, icon, hue, onClick, onKeyDown, ...props }, forwardedRef) => (
{/* No `hue` on the Icon: its own hue rule would paint the hue's text tone over the tile's foreground. */}
), ); CardTile.displayName = 'Card.Tile'; // // Header // type CardHeaderProps = ThemedClassName>; /** * A block row across the card's rails holding the Title: a Block or icon-only Button before the Title fills the start * rail, and the last one after it the end rail, so the Title starts at the content edge. */ const CardHeader = forwardRef(({ classNames, ...props }, forwardedRef) => (
)); CardHeader.displayName = 'Card.Header'; // // Title // type CardTitleProps = ThemedClassName> & Pick; /** An `h3` on Typography, so it clamps (`lines`), truncates and takes a tone like any text. */ const CardTitle = forwardRef( ({ classNames, truncate, lines, tone, ...props }, forwardedRef) => (

), ); CardTitle.displayName = 'Card.Title'; // // Description // type CardDescriptionProps = ThemedClassName>; const CardDescription = forwardRef( ({ classNames, ...props }, forwardedRef) => (

), ); CardDescription.displayName = 'Card.Description'; // // Body // type CardBodyProps = ThemedClassName>; /** In a `grid` card a subgrid of it, so its children sit in the content track; otherwise a plain column. */ const CardBody = forwardRef(({ classNames, ...props }, forwardedRef) => (

)); CardBody.displayName = 'Card.Body'; // // Footer // type CardFooterProps = ThemedClassName> & { /** `between` splits leading actions (a nested Group) from a trailing control. */ justify?: GroupProps['justify']; }; /** A `Group` of actions in the content track, end-justified unless `justify` says otherwise. */ const CardFooter = forwardRef( ({ classNames, justify = 'end', ...props }, forwardedRef) => (
), ); CardFooter.displayName = 'Card.Footer'; // // Section // type CardSectionProps = ThemedClassName> & { /** A caption heading the section; the section is then a `group` named by it. */ title?: ReactNode; /** Set by a slotting parent (`asChild`), e.g. `Collapsible.Content`, whose animation lives in its class. */ className?: string; }; /** * Groups rows under an optional caption; in a `grid` card a subgrid of it, so its rows keep the card's rails, otherwise * a plain column. */ const CardSection = forwardRef( ({ classNames, className, title, children, ...props }, forwardedRef) => { const titleId = useId(); return (
{title && (
{title}
)} {children}
); }, ); CardSection.displayName = 'Card.Section'; // // Row // type CardRowProps = ThemedClassName> & { /** Leading icon, in a Block in the card's start rail, so row text starts at the content edge with or without one. */ icon?: string; /** Leading content in place of the icon (e.g. a person's avatar), in the same Block. */ leading?: ReactNode; /** * Trailing content (a Tag, a count, an action), kept whole while the text truncates; it ends in the card's end rail, * which an icon-only action fills, and a wider one extends back into the content track. */ trailing?: ReactNode; /** * Content centred in the card's end rail (an icon-only action, a unit), mirroring `leading` in the start rail; unlike * `trailing` it never extends back into the content track, so every row's end column lines up. */ end?: ReactNode; /** The chosen row of a set (`aria-current`). */ current?: boolean; /** * How far the row's text runs in a `grid` card: `full` across both rails (content with nothing to align beside it, * such as a snippet), `end` on through the end rail when there is no trailing cell. The content track by default. */ span?: 'full' | 'end'; /** * `start` aligns the leading icon and the trailing cell to the row's first line rather than its middle, for content * of several lines (a title over a snippet); wrapped `Typography` text does this on its own. */ align?: 'start'; }; /** * A block-tall row of icon, text and trailing content, laid out inline; in a `grid` card it is a subgrid, so the icon * lands in the card's start rail, the text in its content track and the trailing content in its end rail. With `onClick` the row is a button (Enter and Space activate * it), as the current `Card.Action` row was. */ const CardRow = forwardRef( ( { classNames, icon, leading, trailing, end, current, span, align, onClick, onKeyDown, children, ...props }, forwardedRef, ) => (
{(leading != null || icon) && {leading ?? (icon && )}}
{children}
{trailing != null && (
{trailing}
)}
{end != null && {end}}
), ); CardRow.displayName = 'Card.Row'; // // Text // type CardTextProps = ThemedClassName> & { truncate?: boolean; /** At most this many lines, the last ending in an ellipsis. */ lines?: number; /** `description` reads as secondary text. */ variant?: 'default' | 'muted'; }; /** Card text on Typography, with the current `Card.Text` variants. */ const CardText = forwardRef( ({ classNames, truncate, lines, variant = 'default', ...props }, forwardedRef) => ( ), ); CardText.displayName = 'Card.Text'; // // Action // type CardActionProps = Omit & ( | { /** A `SystemButton` preset: its icon, and its translated label unless `label` is given. */ system: 'close' | 'delete'; icon?: never; label?: string; } | { system?: never; icon: string; /** Names the action and shows in its Tooltip. */ label: string; } ); const CARD_ACTION_SYSTEM = { close: SystemButton.Close, delete: SystemButton.Delete } as const; /** * A ghost icon-only Button for a Header or Row; its click stays with it, never activating a clickable card. With * `system` it is that `SystemButton` preset, so it takes the preset's icon and translated label. */ const CardAction = forwardRef( ({ system, icon, label, onClick, ...props }, forwardedRef) => { const handleClick = (event: MouseEvent) => { stopPropagation(event); onClick?.(event); }; if (system) { const Preset = CARD_ACTION_SYSTEM[system]; return ; } return (