Menu

Components

Description List

A list of terms and their details, such as the fields of a profile or the specs of a product, as plain text, rows or a box.

Introduction

Description List shows things and what is true of them: a profile's name, email and role, the specs of a product, the summary of an order. Each pair is a term and its details, laid out so the eye can run down the terms and read across to the values.

  • A real dl with dt and dd, so a screen reader reads each term with its details and can say how many pairs there are.
  • The term goes beside its details, and stacks above them on a small screen, or always above with layout="vertical".
  • Four looks: plain, divided by lines, in a box, or striped. Three sizes, and one to three columns.
  • A term can have an icon, the details an action such as an "Edit" button, and an empty value shows a dash instead of a hole.
  • It has no client code, so it works in Server Components.

For data with many rows and columns to sort or select, use a table. Use a description list when each row is a label with a value.

Installation

centerUI has no CLI or registry yet: components are copied into your project.

# bun
bun add tailwind-variants tailwind-merge clsx

# pnpm
pnpm add tailwind-variants tailwind-merge clsx

# npm
npm install tailwind-variants tailwind-merge clsx

Manual installation

Copy these files into your project and keep the paths, or adjust the imports.

1. Add the styles

src/types/description-list-styles.tsShow code
import { tv } from "tailwind-variants";

// Styles for DescriptionList. The list is a `dl` and each pair is a `div` (the
// HTML allows it), so the list styles its pairs through child selectors and
// nothing has to be passed down: it stays usable in a Server Component.
export const descriptionListStyles = tv({
  slots: {
    root: "m-0",
    term: "flex min-w-0 items-center gap-2 font-medium text-muted-fg [&_svg]:size-4 [&_svg]:shrink-0",
    details:
      "m-0 flex min-w-0 items-start justify-between gap-3 text-fg [&_a]:font-medium [&_a]:underline [&_a]:underline-offset-4",
    content: "min-w-0 flex-1 break-words",
    empty: "text-muted-fg",
    action: "shrink-0 self-center",
  },
  variants: {
    // The term beside its details, or above them.
    layout: {
      horizontal: {
        root: "[&>div]:grid [&>div]:grid-cols-1 [&>div]:gap-x-6 [&>div]:gap-y-1 sm:[&>div]:grid-cols-[var(--dl-term,12rem)_minmax(0,1fr)]",
      },
      vertical: { root: "[&>div]:flex [&>div]:flex-col [&>div]:gap-1" },
    },
    // How the pairs are told apart.
    variant: {
      plain: {},
      divided: {
        root: "[&>div]:border-b [&>div]:border-border [&>div:last-child]:border-b-0",
      },
      bordered: {
        root: "rounded-xl border px-4 [&>div]:border-b [&>div]:border-border [&>div:last-child]:border-b-0",
      },
      striped: {
        root: "overflow-hidden rounded-xl [&>div]:px-4 [&>div:nth-child(odd)]:bg-muted",
      },
    },
    size: {
      sm: { root: "text-[13px] [&>div]:py-2" },
      md: { root: "text-sm [&>div]:py-3" },
      lg: { root: "text-base [&>div]:py-4" },
    },
    // Pairs side by side, in a grid.
    columns: {
      1: {},
      2: { root: "grid gap-x-10 sm:grid-cols-2" },
      3: { root: "grid gap-x-10 sm:grid-cols-2 lg:grid-cols-3" },
    },
  },
  defaultVariants: {
    layout: "horizontal",
    variant: "plain",
    size: "md",
    columns: 1,
  },
});

2. Add the Description List file

src/components/ui/description-list/description-list.tsxShow code
import type { CSSProperties, ReactNode } from "react";
import { cn } from "@/lib/cn";
import { descriptionListStyles } from "@/types/description-list-styles";

export interface DescriptionListProps {
  /** The `DescriptionItem`s. */
  children: ReactNode;
  /**
   * `horizontal` puts each term beside its details (and above them on a small
   * screen). `vertical` puts it above.
   */
  layout?: "horizontal" | "vertical";
  /**
   * How the pairs are told apart: `plain` (only space), `divided` (a line
   * between them), `bordered` (in a box) or `striped` (every other one tinted).
   */
  variant?: "plain" | "divided" | "bordered" | "striped";
  size?: "sm" | "md" | "lg";
  /** Lay the pairs out in this many columns, from a small screen up. Defaults to 1. */
  columns?: 1 | 2 | 3;
  /** The width of the term column in a `horizontal` list. Any CSS length. Defaults to `12rem`. */
  termWidth?: string;
  className?: string;
}

/**
 * A list of terms and their details: the fields of a profile, the specs of a
 * product, the summary of an order. It renders a real `dl`, so assistive
 * technology reads each term with its details, and it sits in the flow of the
 * page as plain text, in a box, or in rows.
 *
 * It has no client code, so it works in Server Components.
 *
 * @example
 * <DescriptionList variant="divided">
 *   <DescriptionItem label="Name">Ana Wijaya</DescriptionItem>
 *   <DescriptionItem label="Email">ana@example.com</DescriptionItem>
 * </DescriptionList>
 */
export function DescriptionList({
  children,
  layout,
  variant,
  size,
  columns,
  termWidth,
  className,
}: DescriptionListProps) {
  const styles = descriptionListStyles({ layout, variant, size, columns });

  return (
    <dl
      style={
        termWidth ? ({ "--dl-term": termWidth } as CSSProperties) : undefined
      }
      className={cn(styles.root(), className)}
    >
      {children}
    </dl>
  );
}

export interface DescriptionItemProps {
  /**
   * The term: what the details are about. Leave it out to write the `dt` and
   * `dd` yourself, with `DescriptionTerm` and `DescriptionDetails`.
   */
  label?: ReactNode;
  /** The details. Empty or missing shows `emptyText`. */
  children?: ReactNode;
  /** An icon before the term. */
  icon?: ReactNode;
  /** A control at the end of the details, such as an "Edit" button. */
  action?: ReactNode;
  /** What is shown when there are no details. Defaults to an em dash. */
  emptyText?: ReactNode;
  className?: string;
}

const styles = descriptionListStyles();

/**
 * One term and its details. Give it a `label` and the details as children, or
 * compose the `dt` and the `dd` yourself for more than one detail.
 */
export function DescriptionItem({
  label,
  children,
  icon,
  action,
  emptyText = "—",
  className,
}: DescriptionItemProps) {
  if (label === undefined) {
    return <div className={className}>{children}</div>;
  }

  const isEmpty =
    children === undefined ||
    children === null ||
    children === false ||
    children === "";

  return (
    <div className={className}>
      <dt className={styles.term()}>
        {icon}
        {label}
      </dt>
      <dd className={styles.details()}>
        <span className={cn(styles.content(), isEmpty && styles.empty())}>
          {isEmpty ? emptyText : children}
        </span>
        {action ? <span className={styles.action()}>{action}</span> : null}
      </dd>
    </div>
  );
}

/** A term, for an item written by hand. */
export function DescriptionTerm({
  children,
  className,
}: {
  children: ReactNode;
  className?: string;
}) {
  return <dt className={cn(styles.term(), className)}>{children}</dt>;
}

/** Details, for an item written by hand. An item can have several. */
export function DescriptionDetails({
  children,
  className,
}: {
  children: ReactNode;
  className?: string;
}) {
  return <dd className={cn(styles.details(), className)}>{children}</dd>;
}

3. Use it

import {
  DescriptionDetails,
  DescriptionItem,
  DescriptionList,
  DescriptionTerm,
} from "@/components/ui/description-list/description-list";

Anatomy

A list of pairs, each with a term and its details.

<DescriptionList variant="divided">
  <DescriptionItem label="Name" icon={<UserIcon />} action={<Button size="xs">Edit</Button>}>
    Ana Wijaya
  </DescriptionItem>
  <DescriptionItem label="Phone" />
</DescriptionList>
PartRendered asNotes
DescriptionListdlThe list. Sets the layout, how the pairs are told apart, the size and the columns.
DescriptionItemdiv with dt and ddOne pair: give it a label and the details as children.
TermdtWhat the details are about. It can have an icon.
DetailsddThe value. It can have an action at the end, and shows a dash when it is empty.
DescriptionTerm / DescriptionDetailsdt / ddFor an item written by hand, when a term has more than one detail.

Examples

Usage

Full name
Ana Wijaya
Email
ana@example.com
Role
Product designer
Location
Jakarta, Indonesia

Layout

Horizontal puts each term beside its details. Vertical puts it above. On a small screen a horizontal list stacks too.

Full name
Ana Wijaya
Email
ana@example.com
Role
Product designer
Location
Jakarta, Indonesia
Full name
Ana Wijaya
Email
ana@example.com
Role
Product designer
Location
Jakarta, Indonesia

Variants

Plain is only space. Divided draws a line between the pairs. Bordered puts them in a box. Striped tints every other one, which helps to follow a row in a long list.

Full name
Ana Wijaya
Email
ana@example.com
Role
Product designer
Location
Jakarta, Indonesia
Full name
Ana Wijaya
Email
ana@example.com
Role
Product designer
Location
Jakarta, Indonesia
Full name
Ana Wijaya
Email
ana@example.com
Role
Product designer
Location
Jakarta, Indonesia
Full name
Ana Wijaya
Email
ana@example.com
Role
Product designer
Location
Jakarta, Indonesia

Sizes

Full name
Ana Wijaya
Email
ana@example.com
Full name
Ana Wijaya
Email
ana@example.com
Full name
Ana Wijaya
Email
ana@example.com

Columns

columns lays the pairs out side by side from a small screen up, and they stack on a phone. It works best with a vertical layout.

Full name
Ana Wijaya
Email
ana@example.com
Role
Product designer
Location
Jakarta, Indonesia
Full name
Ana Wijaya
Email
ana@example.com
Role
Product designer
Location
Jakarta, Indonesia
Joined
3 March 2024
Plan
Pro

Term width

In a horizontal list the terms are 12rem wide. termWidth changes it, for short terms or long ones.

Name
Ana Wijaya
Role
Designer
Number of projects you own
12
Storage used this month
4.2 GB

Icons

An icon before the term, to find a row at a glance.

Name
Ana Wijaya
Email
ana@example.com
Notifications
Weekly summary
Last seen
2 hours ago

Actions

A control at the end of the details, for what can be done with a value. It stays at the right, and the details wrap beside it.

Email
ana@example.com
Password
••••••••••

Empty values

An item with no details shows a dash, in a muted color, or the text you give in emptyText. The row does not collapse.

Name
Ana Wijaya
Phone
—
Website
Not added yet

Rich content

The details can hold anything: a badge, a link, an avatar with a name. Links are underlined.

Status
Active
Owner
Ana Wijaya

Several details

A term can have more than one detail. Write the item yourself with DescriptionTerm and DescriptionDetails, without a label.

Emails
ana@example.com
ana.wijaya@work.example
Role
Product designer

In a card

A summary in a card, in a small size with lines between the pairs.

Order #1042

Placed on 3 March

Status
Paid
Items
3
Shipping
Jakarta, 2 to 3 days
Total
$248.00

Component API

Description List is made of the parts below.

DescriptionList

PropTypeDefault
childrenDescriptionItem[]- (required)
layout"horizontal" | "vertical""horizontal"
variant"plain" | "divided" | "bordered" | "striped""plain"
size"sm" | "md" | "lg""md"
columns1 | 2 | 31
termWidthstring (a CSS length)"12rem"
classNamestring-

DescriptionItem

PropTypeDefault
labelReactNode-
childrenReactNode-
iconReactNode-
actionReactNode-
emptyTextReactNode"—"
classNamestring-

DescriptionTerm and DescriptionDetails take children and className.