Skip to content
Unkey Design Docs
Esc
↑↓navigate↵open⌘Jpreview
On this page

Resource list

Resource list components provide the shared layout for filterable lists.

import {
  Button,
  Input,
  ResourceList,
  ResourceListBody,
  ResourceListContent,
  ResourceListFooter,
  ResourceListHeader,
  ResourceListItem,
  ResourceListShowMore,
} from "@unkey/ui";
import { useState } from "react";

const keys = [
  { name: "Production API", id: "key_2tQx8", status: "Active" },
  { name: "Preview API", id: "key_7mLp3", status: "Active" },
  { name: "Legacy integration", id: "key_4nWs1", status: "Disabled" },
];

export default function BasicResourceList() {
  const [query, setQuery] = useState("");
  const [activeOnly, setActiveOnly] = useState(false);
  const [visibleCount, setVisibleCount] = useState(2);
  const filteredKeys = keys.filter(
    (key) =>
      (!activeOnly || key.status === "Active") &&
      `${key.name} ${key.id}`.toLowerCase().includes(query.trim().toLowerCase()),
  );
  const visibleKeys = filteredKeys.slice(0, visibleCount);

  return (
    <ResourceList className="min-h-72">
      <ResourceListHeader>
        <Input
          aria-label="Search API keys"
          placeholder="Search API keys"
          value={query}
          onChange={(event) => setQuery(event.target.value)}
        />
        <Button
          variant="outline"
          size="lg"
          aria-pressed={activeOnly}
          className={activeOnly ? "bg-grayA-3" : undefined}
          onClick={() => setActiveOnly((current) => !current)}
        >
          Active only
        </Button>
      </ResourceListHeader>
      <ResourceListContent>
        <ResourceListBody>
          {visibleKeys.map((key) => (
            <ResourceListItem key={key.id} className="flex items-center gap-4 px-4 py-3">
              <div className="min-w-0 flex-1">
                <div className="truncate font-medium text-gray-12 text-sm">{key.name}</div>
                <div className="font-mono text-gray-9 text-xs">{key.id}</div>
              </div>
              <span className="text-gray-11 text-xs">{key.status}</span>
            </ResourceListItem>
          ))}
          {visibleKeys.length === 0 ? (
            <ResourceListItem className="px-4 py-8 text-center text-gray-10 text-sm">
              No API keys found
            </ResourceListItem>
          ) : null}
        </ResourceListBody>
        {visibleKeys.length < filteredKeys.length ? (
          <ResourceListFooter>
            <ResourceListShowMore onClick={() => setVisibleCount(filteredKeys.length)} />
          </ResourceListFooter>
        ) : null}
      </ResourceListContent>
    </ResourceList>
  );
}

Anatomy

  • ResourceList spaces the controls and content.
  • ResourceListHeader puts the controls on one line.
  • ResourceListSearch sizes the search input.
  • ResourceListContent draws the raised list frame. See Card elevation.
  • ResourceListBody and ResourceListItem render a semantic list.
  • ResourceListRow makes a whole row clickable, with label as its accessible name. Pass expanded while its panel is open.
  • ResourceListGrid lays out cards in one, two or three columns.
  • ResourceListTable draws a list frame with a column header. Each column has an id, a header and a grid width. Pass the pager as footer.
  • ResourceListTableRow puts its cells on the table columns. Pass a link element, such as a <Link>, to make the whole row a link; label sets its accessible name unless the link has its own aria-label, and without label the link needs text (for example sr-only) or an aria-label. Pass onActivate instead for rows that open a panel; label is required then. Pass one or the other, not both.
  • ResourceListFooter holds the Show more button. Use variant="bare" under a grid of cards.
  • ResourceListShowMore is the Show more button. Pass loading while a page loads.
  • ResourceListViewToggle switches between grid and list views. useListView stores the choice in the dashboard.

Keep the resource title and primary action in PageHeader.

Examples

Clickable rows

import {
  Button,
  ResourceList,
  ResourceListBody,
  ResourceListContent,
  ResourceListItem,
  ResourceListRow,
} from "@unkey/ui";
import { useState } from "react";

const environments = [
  { id: "env_prod", name: "production", domain: "acme-api.unkey.app" },
  { id: "env_preview", name: "preview", domain: "preview.acme-api.unkey.app" },
];

export default function ResourceListRows() {
  const [opened, setOpened] = useState<string | null>(null);

  return (
    <ResourceList>
      <ResourceListContent>
        <ResourceListBody>
          {environments.map((env) => (
            <ResourceListItem key={env.id}>
              <ResourceListRow
                label={`Open ${env.name}`}
                onActivate={() => setOpened(env.id)}
                expanded={opened === env.id}
                className="flex items-center gap-4"
              >
                <span className="min-w-0 flex-1 truncate font-medium text-gray-12 text-sm">
                  {env.name}
                </span>
                <span className="font-mono text-gray-11 text-xs">{env.domain}</span>
                <Button variant="outline" size="sm" className="relative">
                  Copy
                </Button>
              </ResourceListRow>
            </ResourceListItem>
          ))}
        </ResourceListBody>
      </ResourceListContent>
    </ResourceList>
  );
}

Grid and list views

import {
  ResourceList,
  ResourceListBody,
  ResourceListFooter,
  ResourceListGrid,
  ResourceListHeader,
  ResourceListSearch,
  ResourceListShowMore,
  ResourceListTable,
  ResourceListTableRow,
  type ResourceListView,
  ResourceListViewToggle,
  SearchInput,
} from "@unkey/ui";
import { useState } from "react";

const columns = [
  { id: "name", header: "Keyspace", width: "minmax(0,1fr)" },
  { id: "id", header: "ID", width: "minmax(0,1fr)" },
];

const keyspaces = [
  { id: "ks_2tQx8", name: "Production API" },
  { id: "ks_7mLp3", name: "Preview API" },
  { id: "ks_4nWs1", name: "Internal tools" },
];

export default function ResourceListViewToggleExample() {
  const [query, setQuery] = useState("");
  const [view, setView] = useState<ResourceListView>("grid");
  const [visibleCount, setVisibleCount] = useState(2);
  const matches = keyspaces.filter((keyspace) =>
    keyspace.name.toLowerCase().includes(query.trim().toLowerCase()),
  );
  const shown = matches.slice(0, visibleCount);
  const showMore =
    shown.length < matches.length ? (
      <ResourceListShowMore onClick={() => setVisibleCount(matches.length)} />
    ) : null;

  return (
    <ResourceList>
      <ResourceListHeader>
        <ResourceListSearch>
          <SearchInput
            value={query}
            onValueChange={setQuery}
            label="Search keyspaces"
            placeholder="Search keyspaces"
          />
        </ResourceListSearch>
        <ResourceListViewToggle className="md:ml-auto" value={view} onValueChange={setView} />
      </ResourceListHeader>
      {view === "grid" ? (
        <>
          <ResourceListGrid>
            {shown.map((keyspace) => (
              <div key={keyspace.id} className="rounded-lg border bg-raised p-5 shadow-xs">
                <div className="truncate font-medium text-gray-12 text-sm">{keyspace.name}</div>
                <div className="font-mono text-gray-11 text-xs">{keyspace.id}</div>
              </div>
            ))}
          </ResourceListGrid>
          {showMore ? <ResourceListFooter variant="bare">{showMore}</ResourceListFooter> : null}
        </>
      ) : (
        <ResourceListTable
          columns={columns}
          minWidth={360}
          footer={showMore ? <ResourceListFooter>{showMore}</ResourceListFooter> : null}
        >
          <ResourceListBody>
            {shown.map((keyspace) => (
              <ResourceListTableRow
                key={keyspace.id}
                link={
                  <a href={`#${keyspace.id}`}>
                    <span className="sr-only">View {keyspace.name}</span>
                  </a>
                }
              >
                <span className="truncate font-medium text-sm">{keyspace.name}</span>
                <span className="font-mono text-gray-11">{keyspace.id}</span>
              </ResourceListTableRow>
            ))}
          </ResourceListBody>
        </ResourceListTable>
      )}
    </ResourceList>
  );
}

Best practices

Header order

Put search first, then filters, then ResourceListViewToggle. Give the toggle md:ml-auto to push it to the right edge.

Interactive cells

Give a copy button or other interactive cell in a clickable row the relative class, or the row takes its clicks. In a ResourceListTableRow, use relative z-10.