# DataList (/docs/data-list)





`DataList` shows a record as pairs of a label and a value: "Login: jdoe", "Campus: Paris", "Status: Active". It renders a real `<dl>`, each pair a `<div>` holding a `<dt>` and a `<dd>`, so a screen reader announces it as a description list. It's a pure Server Component, with no state and no `"use client"`.

Pass the rows as `data`, or compose `DataList.Item`, `DataList.Label` and `DataList.Value` when a value needs more than the data form gives it. The two mix: the `data` rows come first, then the children.

```tsx
import { DataList } from "@42-central/ui-react/data-list";

<DataList
  data={[
    { label: "Login", value: "jdoe" },
    { label: "Campus", value: "Paris" },
  ]}
>
  <DataList.Item>
    <DataList.Label>Status</DataList.Label>
    <DataList.Value>
      <Badge color="green" variant="light">Active</Badge>
    </DataList.Value>
  </DataList.Item>
</DataList>;
```

## Playground [#playground]

<story.Playground />

### Full prop browser [#full-prop-browser]

<story.WithControl />

## Values [#values]

A value is any node: text, a [`Badge`](/docs/badge), an [`Avatar`](/docs/avatar), a [`Copy`](/docs/clipboard) button. In a row, the label and the value share their first baseline, so a one-line label stays level with a badge or with the first line of a value that wraps.

<Preview align="block" code="&#x22;<DataList withDivider>\n  <DataList.Item>\n    <DataList.Label>Student</DataList.Label>\n    <DataList.Value>\n      <span className=\&#x22;inline-flex items-center gap-2\&#x22;>\n        <Avatar size=\&#x22;sm\&#x22; name=\&#x22;Jane Doe\&#x22; color=\&#x22;teal\&#x22; />\n        Jane Doe\n      </span>\n    </DataList.Value>\n  </DataList.Item>\n  <DataList.Item>\n    <DataList.Label>Login</DataList.Label>\n    <DataList.Value>\n      <span className=\&#x22;inline-flex items-center gap-1\&#x22;>\n        jdoe\n        <Copy value=\&#x22;jdoe\&#x22; size=\&#x22;xs\&#x22; variant=\&#x22;subtle\&#x22; />\n      </span>\n    </DataList.Value>\n  </DataList.Item>\n  <DataList.Item>\n    <DataList.Label>Status</DataList.Label>\n    <DataList.Value>\n      <Badge color=\&#x22;green\&#x22; variant=\&#x22;light\&#x22;>Active</Badge>\n    </DataList.Value>\n  </DataList.Item>\n  {/* … */}\n</DataList>&#x22;">
  <DataListProfileDemo />
</Preview>

## Orientation [#orientation]

`horizontal`, the default, puts the labels in a column beside their values. Each item is a row of the list's own grid (CSS `subgrid`), so the labels line up down the list whatever their length. `vertical` stacks each label above its value.

<Preview align="block" code="`<DataList orientation=&#x22;vertical&#x22; data={profile} />`">
  <DataList
    orientation="vertical"
    className="w-full max-w-md"
    data="[
    { label: &#x22;Login&#x22;, value: &#x22;jdoe&#x22; },
    { label: &#x22;Campus&#x22;, value: &#x22;Paris&#x22; },
    { label: &#x22;Email&#x22;, value: &#x22;jdoe@student.42.fr&#x22; },
  ]"
  />
</Preview>

### Responsive [#responsive]

`responsive` stacks the items while the list is narrower than 24rem and lays them out as rows from there. It measures the list itself, not the viewport, so the same list switches in a narrow sidebar and stays horizontal in a wide main column. Resize the frame below to see it switch.

<Preview align="block" code="`<DataList orientation=&#x22;responsive&#x22; withDivider data={profile} />`">
  <div className="w-full max-w-xl resize-x overflow-auto rounded-md border border-brand-900/20 p-4 dark:border-white/15">
    <DataList
      orientation="responsive"
      data="[
      { label: &#x22;Login&#x22;, value: &#x22;jdoe&#x22; },
      { label: &#x22;Campus&#x22;, value: &#x22;Paris&#x22; },
      { label: &#x22;Cursus&#x22;, value: &#x22;42cursus · level 7.42&#x22; },
      { label: &#x22;Email&#x22;, value: &#x22;jdoe@student.42.fr&#x22; },
    ]"
    />
  </div>
</Preview>

The switch is a CSS container query, so it works without JavaScript and in a Server Component. It gives the list `container-type: inline-size`, which means the list's width can't come from its content: it fills its parent like any block. In a row that shrinks to fit (a flex item with no width), give it a width or `flex-1`, or it collapses.

## Label width [#label-width]

By default the label column is as wide as the widest label, up to 40% of the list (`fit-content(40%)`); a longer label wraps. `labelWidth` sets the column instead. A number is in pixels, and a string is any grid track size: `"10rem"`, `"30%"`, `"max-content"`, `"minmax(8rem, 1fr)"`. It only applies to rows, so a vertical list ignores it.

<Preview align="block" code="`<DataList labelWidth=&#x22;12rem&#x22; data={profile} />`">
  <DataList
    labelWidth="12rem"
    className="w-full max-w-md"
    data="[
    { label: &#x22;Login&#x22;, value: &#x22;jdoe&#x22; },
    { label: &#x22;Campus&#x22;, value: &#x22;Paris&#x22; },
  ]"
  />
</Preview>

## Dividers [#dividers]

`withDivider` draws a hairline between items, never above the first or below the last, so the list sits flush in a card.

<Preview align="block" code="&#x22;<Card>\n  <DataList withDivider data={profile} />\n</Card>&#x22;">
  <Card className="w-full max-w-md">
    <DataList
      data="[
      { label: &#x22;Login&#x22;, value: &#x22;jdoe&#x22; },
      { label: &#x22;Campus&#x22;, value: &#x22;Paris&#x22; },
      { label: &#x22;Email&#x22;, value: &#x22;jdoe@student.42.fr&#x22; },
    ]"
    />
  </Card>
</Preview>

## Sizes [#sizes]

`size` sets the font size on the same scale as [`Text`](/docs/text) (`md` is `text-base`), and the gaps that go with it. The default is `md`.

<Preview align="block" code="&#x22;<DataList size=\&#x22;xs\&#x22; data={profile} />\n<DataList size=\&#x22;lg\&#x22; data={profile} />&#x22;">
  <div className="grid w-full gap-6 sm:grid-cols-2">
    <DataList size="xs" data="[{ label: &#x22;Login&#x22;, value: &#x22;jdoe&#x22; }, { label: &#x22;Campus&#x22;, value: &#x22;Paris&#x22; }]" />

    <DataList size="lg" data="[{ label: &#x22;Login&#x22;, value: &#x22;jdoe&#x22; }, { label: &#x22;Campus&#x22;, value: &#x22;Paris&#x22; }]" />
  </div>
</Preview>

## Muted labels [#muted-labels]

Labels are the secondary text colour at medium weight, a step below the values, which are the primary colour. `muted` steps them further back, to the muted shade at normal weight, for a page where the values matter more than their names.

<Preview align="block" code="`<DataList muted data={profile} />`">
  <DataList
    className="w-full max-w-md"
    data="[
    { label: &#x22;Login&#x22;, value: &#x22;jdoe&#x22; },
    { label: &#x22;Campus&#x22;, value: &#x22;Paris&#x22; },
  ]"
  />
</Preview>

## Accessibility [#accessibility]

* The list is a `<dl>`. Each item is a `<div>` holding one `<dt>` (the label) and one `<dd>` (the value), the grouping HTML allows inside a `<dl>`. Screen readers announce the list, its number of items, and each term with its description.
* Keep one `DataList.Label` per item, and put several values in one `DataList.Value` rather than adding `<dd>`s.
* `DataList.Item` must be a direct child of the list, as HTML requires, so don't wrap items in another element.
* The layout uses logical properties, so the label column moves to the right under `dir="rtl"`.

## Anatomy [#anatomy]

* **`DataList`**: the `<dl>`, the grid both columns live on. It takes every layout prop.
* **`DataList.Item`**: a `<div>`, one label/value pair.
* **`DataList.Label`**: the `<dt>`.
* **`DataList.Value`**: the `<dd>`.

Every part takes `testId`, rendered as `data-testid` on its element. A `data` row takes one too: it lands on the row's item, and its label and value get `-label` and `-value` after it (`login`, `login-label`, `login-value`). See [Testing](/docs/testing).

```tsx
<DataList testId="profile" data={[{ label: "Login", value: "jdoe", testId: "login" }]} />;

screen.getByTestId("login-value"); // the <dd>
```

## API [#api]

`DataList` accepts every `dl` prop plus:

<TypeTable
  type="{
  data: { type: &#x22;DataListEntry[]&#x22;, description: &#x22;Rows to render, before any children. Each becomes an item with its label and value.&#x22; },
  orientation: { type: '&#x22;horizontal&#x22; | &#x22;vertical&#x22; | &#x22;responsive&#x22;', default: '&#x22;horizontal&#x22;', description: &#x22;Label beside its value, label above its value, or stacked below a 24rem-wide list and horizontal from there (a container query on the list).&#x22; },
  labelWidth: { type: &#x22;number | string&#x22;, default: '&#x22;fit-content(40%)&#x22;', description: &#x22;The label column's grid track. A number is in pixels. Rows only.&#x22; },
  size: { type: '&#x22;xs&#x22; | &#x22;sm&#x22; | &#x22;md&#x22; | &#x22;lg&#x22; | &#x22;xl&#x22;', default: '&#x22;md&#x22;', description: &#x22;Font size, on the Text scale, and the matching gaps.&#x22; },
  withDivider: { type: &#x22;boolean&#x22;, default: &#x22;false&#x22;, description: &#x22;Draws a hairline between items.&#x22; },
  muted: { type: &#x22;boolean&#x22;, default: &#x22;false&#x22;, description: &#x22;Labels in the muted shade at normal weight, instead of the secondary shade at medium weight.&#x22; },
  asChild: { type: &#x22;boolean&#x22;, default: &#x22;false&#x22;, description: &#x22;Render the list as its only child, with the data rows inside that element before its own content.&#x22; },
  testId: { type: &#x22;string&#x22;, description: &#x22;Renders as data-testid on the <dl>.&#x22; },
}"
/>

`DataListEntry`:

<TypeTable
  type="{
  label: { type: &#x22;ReactNode&#x22;, required: true, description: &#x22;The term, in a <dt>.&#x22; },
  value: { type: &#x22;ReactNode&#x22;, required: true, description: &#x22;The description, in a <dd>. Any node.&#x22; },
  id: { type: &#x22;string&#x22;, description: &#x22;React key. Defaults to the label when it's a string, else the row's index.&#x22; },
  testId: { type: &#x22;string&#x22;, description: &#x22;data-testid on the item; its label and value get -label and -value suffixes.&#x22; },
}"
/>

`DataList.Item`, `DataList.Label` and `DataList.Value` take every prop of their element (`div`, `dt`, `dd`), plus `asChild` and `testId`.
