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
ResourceListspaces the controls and content.ResourceListHeaderputs the controls on one line.ResourceListSearchsizes the search input.ResourceListContentdraws the raised list frame. See Card elevation.ResourceListBodyandResourceListItemrender a semantic list.ResourceListRowmakes a whole row clickable, withlabelas its accessible name. Passexpandedwhile its panel is open.ResourceListGridlays out cards in one, two or three columns.ResourceListTabledraws a list frame with a column header. Each column has anid, aheaderand a gridwidth. Pass the pager asfooter.ResourceListTableRowputs its cells on the table columns. Pass alinkelement, such as a<Link>, to make the whole row a link;labelsets its accessible name unless the link has its ownaria-label, and withoutlabelthe link needs text (for examplesr-only) or anaria-label. PassonActivateinstead for rows that open a panel;labelis required then. Pass one or the other, not both.ResourceListFooterholds the Show more button. Usevariant="bare"under a grid of cards.ResourceListShowMoreis the Show more button. Passloadingwhile a page loads.ResourceListViewToggleswitches between grid and list views.useListViewstores 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.