# Figure

The card around a chart, diagram or layout posted in a conversation, its empty states and its larger view.

Live variants: https://design.arcadia.is/components/figure

```tsx
import {
  Figure, FigureState, FigureView, figureAccessibleName, figureBylineText, fit,
} from "@arcadia/ui";
```

Double-click the chart or choose Open larger.

```tsx
import { useState, useRef } from "react";
import { characterFromId, Figure, FigureView } from "@arcadia/ui";
import {
  Bars, Chart, Grid, XAxis, YAxis, chartColor,
} from "@arcadia/ui/charts";

function ChartWithByline() {
  const [open, setOpen] = useState(false);
  const menuRef = useRef<HTMLButtonElement>(null);
  const info = {
    kind: "chart",
    title: "Weekly active agents",
    by: { kind: "agent", name: "Scout", avatar: characterFromId("agt_scout_figure"), person: { name: "Maya" } },
    source: "Workspace telemetry",
  } as const;
  const chart = <Chart label={info.title} x={["Mon", "Tue", "Wed", "Thu", "Fri"]}><Grid /><XAxis /><YAxis /><Bars label="Agents" color={chartColor(1)} values={[34, 42, 51, 48, 56]} /></Chart>;
  return (
    <>
      <Figure {...info} onOpenLarge={() => setOpen(true)} menuRef={menuRef}>
        {chart}
      </Figure>
      <FigureView {...info} open={open} onOpenChange={setOpen} finalFocus={menuRef}>
        {chart}
      </FigureView>
    </>
  );
}
```

## Accessibility

`Figure` renders a `<figure>` named by `figureAccessibleName(kind, title, by)`, prefixing `title` with the byline when `by` is given. Use the same helper to set an embedded frame's `title` attribute so screen readers announce who posted the drawing before its title. Passing `hideTitle` removes the visual heading while preserving that accessible name and revealing the menu button on focus.

Keyboard order reaches the menu button first, then the drawing inside the well, then any links in the source foot. `FigureView` builds on `DialogContent`: Escape, the close button and pressing outside all close the larger view and return focus to the card's menu button when its `menuRef` is passed as `finalFocus`.

- Tab: Moves from the card menu button to the drawing, then to the source foot
- Enter + Space: Opens the figure menu or activates a state button
- Escape: Closes an open menu or the larger view and returns focus to the card menu button

## API reference

### Figure

- `kind` (`"chart" | "diagram" | "layout"`, required): `"chart"`, `"diagram"` or `"layout"`. A layout parts its child sections with `Separator` and has no larger view or image actions.
- `title` (`string`, required): Required accessible title. Shown in the head unless `hideTitle` is set.
- `hideTitle` (`boolean`, default `false`): When the drawing carries its own title, keeps `title` for assistive technology and shows only the menu button at the card's top end on hover and focus.
- `by` (`Actor`): Who posted it, rendered outside the drawing with `ActorName`. Omit when no byline applies.
- `date` (`ReactNode`): Optional date meta shown in the head for `kind="layout"`.
- `state` (`FigureStateInput`): Replaces the well's drawing with `FigureState` while drawing, failed or removed.
- `actions` (`readonly FigureAction[]`): Menu items in the order the product gives them. For `kind="layout"`, `"open"` and `"image"` items are omitted.
- `source` (`ReactNode`): Source line shown in the foot as `"From "` followed by `source`, at Caption in quiet ink.
- `height` (`number`): Height in CSS pixels held by the well across swaps, capped at `--figure-max-height` (600px). Taller drawings scroll inside.
- `fit` (`boolean`, default `false`): Fits the card to its drawing's own width, capped at 100% (for diagrams). Its width is then the drawing's, so it is no container.
- `onOpenLarge` (`() => void`): Opens the larger view on a double click of the well and adds `"Open larger"` to the menu when no `"open"` action is passed. Ignored for `kind="layout"`.
- `menuLabel` (`string`): Accessible name for the card's menu button. Defaults to `"Chart actions"`, `"Diagram actions"` or `"Layout actions"`.
- `menuRef` (`Ref<HTMLButtonElement>`): Ref to the card's menu button so `FigureView` can restore focus to it on close (`finalFocus`).
- `rootStyle` (`RootStyle`): Typed StyleX override: layout.
- `children` (`ReactNode`): The drawing (a frame, still image or layout sections). Unless the figure fits, it answers to the figure's own width through `@container figure`.

Rest props go to `HTMLAttributes<HTMLElement>`.

### FigureState

- `kind` (`"chart" | "diagram" | "layout"`, required): The kind named in the message: `"chart"`, `"diagram"` or `"layout"`.
- `status` (`"drawing" | "failed" | "removed"`, required): `"drawing"`, `"failed"` or `"removed"`.
- `reason` (`"unresponsive" | "timeout"`, default `"unresponsive"`): Why it was closed when `status="removed"`.
- `problems` (`readonly string[]`): Up to three problem lines shown at Caption when `status="failed"`. Extra entries are omitted.
- `onShowSource` (`() => void`): Opens the drawing's source when `status="failed"`.
- `onCopySource` (`() => void`): Copies the drawing's source when `status="failed"`.
- `onRetry` (`() => void`): Retries drawing when `status="failed"` or `status="removed"`.
- `height` (`number`): Height held in CSS pixels so content below does not shift while drawing or after removal.
- `rootStyle` (`RootStyle`): Typed StyleX override: layout.

Rest props go to `HTMLAttributes<HTMLDivElement>`.

### FigureView

- `kind` (`Exclude<FigureKind, "layout">`, required): `"chart"` or `"diagram"`. Layouts do not open in a larger view.
- `title` (`string`, required): The drawing's title shown in the dialog head and used as its accessible name.
- `by` (`Actor`): Who posted it, rendered in the head with `ActorName`.
- `source` (`ReactNode`): Optional source line shown in the foot as `"From "` followed by `source`.
- `actions` (`readonly FigureAction[]`): Actions for the foot: `"image"` and `"data"` items render as buttons, remaining non-`"open"` items sit in a menu.
- `height` (`number`): Natural height of the drawing in CSS pixels when reported by a frame. Smaller drawings sit at their own height; taller ones scroll inside.
- `state` (`FigureStateInput`): Replaces the drawing with `FigureState` if it reloads, fails or is removed while open.
- `onBodySizeChange` (`(size: FigureViewSize) => void`): Called with the body's inner width and height as the view opens and each time they change, for a renderer that must have the numbers. A frame that fills the body needs none.
- `closeLabel` (`string`, default `"Close"`): Accessible name of the close button.
- `initialFocus` (`FocusTarget`): Where focus moves on open.
- `finalFocus` (`FocusTarget`): Where focus returns on close; pass the card's `menuRef` so focus returns to its menu button.
- `container` (`PortalContainer`, default `document.body`): Portal container.
- `rootStyle` (`RootStyle`): Typed StyleX override for the dialog: layout.
- `children` (`ReactNode`): The drawing shown in the dialog body, a box it fills by layout alone: a frame at `height: 100%` takes its whole height.

Rest props go to `Pick<DialogProps, "open" | "defaultOpen" | "onOpenChange" | "onOpenChangeComplete">`.
