List row
FreeThe dense row every list shares: a status node, avatar or logo, a title with one grey line, and a value or time. Runs shimmer while they work, open from the keyboard and fold out to show the work.
npx shadcn@latest add https://pixilui.com/r/list-row.jsonPreview
import { useRef, useState } from "react";
import { IconAdd } from "@/components/pixilui/icons/add";
import { IconForward } from "@/components/pixilui/icons/forward";
import { IconSearch } from "@/components/pixilui/icons/search";
import { KeyValueList } from "@/components/pixilui/key-value-list";
import { StatusDot, type StatusDotState } from "@/components/pixilui/status-dot";
import { ListRowAdd, ListRowList, ListRow, addRow } from "@/components/pixilui/list-row";
// The first option of each knob is the demo's default; the docs preview starts there.
export const knobs = {
size: ["md", "sm"],
} as const;
/* The real marks of the tools the sources name (their own colors), as the lock draws them (locked/rows, rows.icons.js). */
const MARKS: Record<string, { viewBox: string; html: string }> = {
"notion": { viewBox: "0 0 256 268", html: "<path fill=\"#fff\" d=\"M16.092 11.538L164.09.608c18.179-1.56 22.85-.508 34.28 7.801l47.243 33.282C253.406 47.414 256 48.975 256 55.207v182.527c0 11.439-4.155 18.205-18.696 19.24L65.44 267.378c-10.913.517-16.11-1.043-21.825-8.327L8.826 213.814C2.586 205.487 0 199.254 0 191.97V29.726c0-9.352 4.155-17.153 16.092-18.188\"/><path d=\"M164.09.608L16.092 11.538C4.155 12.573 0 20.374 0 29.726v162.245c0 7.284 2.585 13.516 8.826 21.843l34.789 45.237c5.715 7.284 10.912 8.844 21.825 8.327l171.864-10.404c14.532-1.035 18.696-7.801 18.696-19.24V55.207c0-5.911-2.336-7.614-9.21-12.66l-1.185-.856L198.37 8.409C186.94.1 182.27-.952 164.09.608M69.327 52.22c-14.033.945-17.216 1.159-25.186-5.323L23.876 30.778c-2.06-2.086-1.026-4.69 4.163-5.207l142.274-10.395c11.947-1.043 18.17 3.12 22.842 6.758l24.401 17.68c1.043.525 3.638 3.637.517 3.637L71.146 52.095zm-16.36 183.954V81.222c0-6.767 2.077-9.887 8.3-10.413L230.02 60.93c5.724-.517 8.31 3.12 8.31 9.879v153.917c0 6.767-1.044 12.49-10.387 13.008l-161.487 9.361c-9.343.517-13.489-2.594-13.489-10.921M212.377 89.53c1.034 4.681 0 9.362-4.681 9.897l-7.783 1.542v114.404c-6.758 3.637-12.981 5.715-18.18 5.715c-8.308 0-10.386-2.604-16.609-10.396l-50.898-80.079v77.476l16.1 3.646s0 9.362-12.989 9.362l-35.814 2.077c-1.043-2.086 0-7.284 3.63-8.318l9.351-2.595V109.823l-12.98-1.052c-1.044-4.68 1.55-11.439 8.826-11.965l38.426-2.585l52.958 81.113v-71.76l-13.498-1.552c-1.043-5.733 3.111-9.896 8.3-10.404z\"/>" }, // check-registry-ignore: a product mark keeps its own colors
"google-drive": { viewBox: "0 0 256 238", html: "<defs><linearGradient id=\"SVGjzk6ueva\" x1=\"86.924%\" x2=\"7.63%\" y1=\"94.294%\" y2=\"45.952%\"><stop offset=\"9%\" stop-color=\"#ffe921\"/><stop offset=\"100%\" stop-color=\"#fec700\"/></linearGradient><linearGradient id=\"SVGb0f7AcFJ\" x1=\"99.538%\" x2=\"23.437%\" y1=\"93.901%\" y2=\"54.033%\"><stop offset=\"15%\" stop-color=\"#a9a8ff\"/><stop offset=\"33%\" stop-color=\"#6d97ff\"/><stop offset=\"48%\" stop-color=\"#3186ff\"/></linearGradient><linearGradient id=\"SVGxMwz6dYk\" x1=\"87.128%\" x2=\"-.639%\" y1=\"51.518%\" y2=\"93.078%\"><stop offset=\"55%\" stop-color=\"#0ebc5f\"/><stop offset=\"85%\" stop-color=\"#78c9ff\"/></linearGradient><path id=\"SVGAiIt3btS\" d=\"M77.303 29.27c22.531-39.026 78.863-39.027 101.394 0l69.373 120.158c22.531 39.027-5.633 87.81-50.698 87.81H58.628c-45.065 0-73.23-48.783-50.698-87.81z\"/></defs><mask id=\"SVG6FLXsceq\" fill=\"#fff\"><use href=\"#SVGAiIt3btS\"/></mask><g mask=\"url(#SVG6FLXsceq)\"><path fill=\"url(#SVGjzk6ueva)\" d=\"M341.719 295.937H200.166l-29.293-50.735l70.777-122.589z\" transform=\"translate(-42.87 -58.67)\"/><path fill=\"url(#SVGb0f7AcFJ)\" d=\"m0 295.916l100.069-173.325v.003l-29.282 50.723h58.57l70.783 122.596l-200.138.001z\" transform=\"translate(-42.87 -58.67)\"/><path fill=\"url(#SVGxMwz6dYk)\" d=\"m170.881 0l70.781 122.6l-29.286 50.726H70.812z\" transform=\"translate(-42.87 -58.67)\"/></g>" }, // check-registry-ignore: a product mark keeps its own colors
"github": { viewBox: "0 0 256 250", html: "<path fill=\"currentColor\" d=\"M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0m-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931m6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66m4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08m7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27m9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622m10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868m10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403\"/>" }, // check-registry-ignore: a product mark keeps its own colors
"slack": { viewBox: "0 0 256 256", html: "<path fill=\"#e01e5a\" d=\"M53.841 161.32c0 14.832-11.987 26.82-26.819 26.82S.203 176.152.203 161.32c0-14.831 11.987-26.818 26.82-26.818H53.84zm13.41 0c0-14.831 11.987-26.818 26.819-26.818s26.819 11.987 26.819 26.819v67.047c0 14.832-11.987 26.82-26.82 26.82c-14.83 0-26.818-11.988-26.818-26.82z\"/><path fill=\"#36c5f0\" d=\"M94.07 53.638c-14.832 0-26.82-11.987-26.82-26.819S79.239 0 94.07 0s26.819 11.987 26.819 26.819v26.82zm0 13.613c14.832 0 26.819 11.987 26.819 26.819s-11.987 26.819-26.82 26.819H26.82C11.987 120.889 0 108.902 0 94.069c0-14.83 11.987-26.818 26.819-26.818z\"/><path fill=\"#2eb67d\" d=\"M201.55 94.07c0-14.832 11.987-26.82 26.818-26.82s26.82 11.988 26.82 26.82s-11.988 26.819-26.82 26.819H201.55zm-13.41 0c0 14.832-11.988 26.819-26.82 26.819c-14.831 0-26.818-11.987-26.818-26.82V26.82C134.502 11.987 146.489 0 161.32 0s26.819 11.987 26.819 26.819z\"/><path fill=\"#ecb22e\" d=\"M161.32 201.55c14.832 0 26.82 11.987 26.82 26.818s-11.988 26.82-26.82 26.82c-14.831 0-26.818-11.988-26.818-26.82V201.55zm0-13.41c-14.831 0-26.818-11.988-26.818-26.82c0-14.831 11.987-26.818 26.819-26.818h67.25c14.832 0 26.82 11.987 26.82 26.819s-11.988 26.819-26.82 26.819z\"/>" }, // check-registry-ignore: a product mark keeps its own colors
"stripe": { viewBox: "0 0 24 24", html: "<path fill=\"#635BFF\" d=\"M13.976 9.15c-2.172-.806-3.356-1.426-3.356-2.409c0-.831.683-1.305 1.901-1.305c2.227 0 4.515.858 6.09 1.631l.89-5.494C18.252.975 15.697 0 12.165 0C9.667 0 7.589.654 6.104 1.872C4.56 3.147 3.757 4.992 3.757 7.218c0 4.039 2.467 5.76 6.476 7.219c2.585.92 3.445 1.574 3.445 2.583c0 .98-.84 1.545-2.354 1.545c-1.875 0-4.965-.921-6.99-2.109l-.9 5.555C5.175 22.99 8.385 24 11.714 24c2.641 0 4.843-.624 6.328-1.813c1.664-1.305 2.525-3.236 2.525-5.732c0-4.128-2.524-5.851-6.594-7.305z\"/>" }, // check-registry-ignore: a product mark keeps its own colors
"zendesk": { viewBox: "0 0 256 195", html: "<path fill=\"#03363d\" d=\"M118.25 51.233v142.773H0zm0-51.233c0 32.644-26.481 59.125-59.125 59.125S0 32.645 0 0zm19.5 194.006c0-32.677 26.449-59.125 59.125-59.125S256 161.361 256 194.006zm0-51.266V0H256z\"/>" }, // check-registry-ignore: a product mark keeps its own colors
"hubspot": { viewBox: "0 0 24 24", html: "<path fill=\"#FF7A59\" d=\"M18.164 7.93V5.084a2.2 2.2 0 0 0 1.267-1.978v-.067A2.2 2.2 0 0 0 17.238.845h-.067a2.2 2.2 0 0 0-2.193 2.193v.067a2.2 2.2 0 0 0 1.252 1.973l.013.006v2.852a6.2 6.2 0 0 0-2.969 1.31l.012-.01l-7.828-6.095A2.497 2.497 0 1 0 4.3 4.656l-.012.006l7.697 5.991a6.2 6.2 0 0 0-1.038 3.446a6.2 6.2 0 0 0 1.147 3.607l-.013-.02l-2.342 2.343a2 2 0 0 0-.58-.095h-.002a2.033 2.033 0 1 0 2.033 2.033a2 2 0 0 0-.1-.595l.005.014l2.317-2.317a6.247 6.247 0 1 0 4.782-11.134l-.036-.005zm-.964 9.378a3.206 3.206 0 1 1 3.215-3.207v.002a3.206 3.206 0 0 1-3.207 3.207z\"/>" }, // check-registry-ignore: a product mark keeps its own colors
"snowflake": { viewBox: "0 0 24 24", html: "<path fill=\"#29b5e8\" d=\"M7.602 12.4c.038-.151.076-.304.076-.456 0-.114-.038-.228-.038-.342-.114-.343-.304-.647-.646-.838l-4.87-2.777c-.685-.38-1.56-.152-1.94.533-.381.685-.153 1.56.532 1.94l2.701 1.56-2.701 1.56c-.685.38-.913 1.256-.533 1.94.38.685 1.256.914 1.94.533l4.832-2.777c.343-.267.571-.533.647-.876zm1.332 2.626c-.266-.038-.57.038-.837.19l-4.832 2.777c-.685.38-.913 1.256-.532 1.94.38.686 1.255.914 1.94.533l2.701-1.56v3.12c0 .8.647 1.408 1.446 1.408.799 0 1.407-.647 1.407-1.408v-5.592c0-.761-.57-1.37-1.293-1.408zm4.946-6.088c.266.038.57-.038.837-.19l4.832-2.777c.685-.38.913-1.256.532-1.94-.38-.686-1.255-.914-1.94-.533l-2.701 1.56V1.975c0-.799-.647-1.408-1.446-1.408-.799 0-1.446.609-1.446 1.408V7.53c0 .76.609 1.37 1.332 1.407zM3.265 5.97l4.832 2.777c.266.152.533.19.837.19.723-.038 1.331-.684 1.331-1.407V1.975c0-.799-.646-1.408-1.407-1.408-.799 0-1.446.647-1.446 1.408v3.12l-2.701-1.56c-.685-.38-1.56-.152-1.94.533-.419.646-.19 1.521.494 1.902zm9.093 6.011a.412.412 0 00-.114-.266l-.57-.571a.346.346 0 00-.267-.114.412.412 0 00-.266.114l-.571.57a.411.411 0 00-.114.267c0 .076.038.19.114.267l.57.57a.345.345 0 00.267.114c.076 0 .19-.038.266-.114l.571-.57a.412.412 0 00.114-.267zm1.598.533L11.94 14.53c-.039.038-.153.114-.229.114h-.608a.411.411 0 01-.267-.114L8.82 12.514a.408.408 0 01-.076-.229v-.608c0-.076.038-.19.114-.267l2.016-2.016a.41.41 0 01.267-.114h.608a.41.41 0 01.267.114l2.016 2.016a.347.347 0 01.114.267v.608c-.076.077-.114.19-.19.229zm5.593 5.44l-4.832-2.777c-.266-.152-.57-.19-.837-.152-.723.038-1.332.684-1.332 1.408v5.554c0 .8.647 1.408 1.408 1.408.799 0 1.446-.647 1.446-1.408v-3.12l2.7 1.56c.686.38 1.561.152 1.941-.533.419-.646.19-1.521-.494-1.94zm2.549-7.533l-2.701 1.56 2.7 1.56c.686.38.914 1.256.533 1.94-.38.685-1.255.913-1.94.533l-4.832-2.778a1.644 1.644 0 01-.647-.798c-.037-.153-.076-.305-.076-.457 0-.114.039-.228.039-.342.114-.343.342-.647.646-.837l4.832-2.778c.685-.38 1.56-.152 1.94.533.457.609.19 1.484-.494 1.864\"/>" }, // check-registry-ignore: a product mark keeps its own colors
};
const Mark = ({ name }: { name: string }) => <svg viewBox={MARKS[name]!.viewBox} aria-hidden="true" dangerouslySetInnerHTML={{ __html: MARKS[name]!.html }} />;
const RUNS: { state: StatusDotState; title: string; sub: string; time: string }[] = [
{ state: "running", title: "Refund for Dev Patel", sub: "Reading 14 invoices", time: "46s" },
{ state: "waiting", title: "Refund for Lena Ortiz", sub: "Waiting for your approval", time: "3m ago" },
{ state: "done", title: "Seat audit for Hana Ito", sub: "Removed 3 unused seats", time: "12m ago" },
{ state: "failed", title: "Sync invoices to Snowflake", sub: "Timed out after 2.3s", time: "26m ago" },
];
const SOURCES = [
{ logo: "notion", title: "Product wiki", kind: "pages", docs: "1,204", time: "4m ago", reads: "Pages shared with Acme Support", by: "Maya Rao · Sep 12" },
{ logo: "google-drive", title: "Contracts", kind: "files", docs: "318", time: "12m ago", reads: "The Contracts folder, read only", by: "Dev Patel · Aug 30" },
{ logo: "github", title: "acme/billing", kind: "issues", docs: "412", time: "1h ago", reads: "Open and closed issues, no code", by: "Hana Ito · Oct 3" },
{ logo: "slack", title: "#support", kind: "messages", docs: "96", time: "6d ago", reads: "The last 90 days of #support", by: "Maya Rao · Jul 21" },
];
const ADDABLE = [
{ value: "stripe", label: "Stripe", meta: "payments" },
{ value: "zendesk", label: "Zendesk", meta: "tickets" },
{ value: "hubspot", label: "HubSpot", meta: "contacts" },
{ value: "snowflake", label: "Snowflake", meta: "tables" },
];
/**
* Two lists, as the lock keeps them: runs (the running row shimmers, one opens), and connected sources, each with its
* tool's mark, folding out to say what the agents read, over an add row that turns into a search picker: pick an app
* and it lands as a source, the rows below gliding.
*/
export default function ListRowDemo({ size = "md" }: KnobValues<typeof knobs>) {
const [open, setOpen] = useState<string | null>(null);
const [added, setAdded] = useState<typeof ADDABLE>([]);
const list = useRef<HTMLDivElement>(null);
return (
<div className="flex w-full max-w-lg flex-col gap-4">
<div className="rounded-panel bg-surface py-4 elevation-raised">
<ListRowList size={size}>
{RUNS.map((r) => (
<ListRow
key={r.title}
aria-label={r.title}
state={r.state === "running" || r.state === "failed" ? r.state : undefined}
lead={<StatusDot state={r.state} />}
title={r.title}
sub={r.sub}
time={r.time}
current={open === r.title}
onOpen={() => setOpen(r.title)}
/>
))}
</ListRowList>
</div>
<div className="rounded-panel bg-surface py-4 elevation-raised">
<ListRowList ref={list} size="sm">
{SOURCES.map((s) => (
<ListRow
key={s.title}
aria-label={s.title}
lead={<span className="pixilui-list-row-logo"><Mark name={s.logo} /></span>}
title={s.title}
meta={s.kind}
value={s.docs}
time={s.time}
chevron={<IconForward />}
detail={
<KeyValueList
items={[
{ label: "Agents read", value: s.reads },
{ label: "Connected by", value: s.by },
]}
/>
}
/>
))}
{added.map((a) => (
<ListRow key={a.value} aria-label={a.label} lead={<span className="pixilui-list-row-logo"><Mark name={a.value} /></span>} title={a.label} meta={a.meta} time="Just now" />
))}
<ListRowAdd
label="Add a source"
icon={<IconAdd />}
placeholder="Search apps"
searchIcon={<IconSearch />}
options={ADDABLE.filter((a) => !added.includes(a)).map((a) => ({ ...a, logo: <Mark name={a.value} /> }))}
onPick={(_, option) => addRow(list.current!, () => setAdded((x) => [...x, ADDABLE.find((a) => a.value === option.value)!]))}
/>
</ListRowList>
</div>
</div>
);
}When to use
Use it when
- Add in place: an add row at the end of a list that turns into a search picker (connect a source, invite someone).
- A list in a panel that grows into a table when the panel is wide enough (deals by stage).
- Runs, logs, members, sources and activity: one mark, a title, one grey line and one value or time per row.
- A row that opens a page (onOpen): Enter opens it, Up and Down move between rows.
- A row that folds out (detail): what an agent read, who approved it, what it did, as a key-value list.
- kind="timeline" for an activity log: a dot on one line, the time, the title, who did it and a note.
Use something else when
- Records with several columns: use the sortable table.
- Agent avatars, names, durations and chevrons on every run row: one mark, one line, one value ("way too much overwhelming").
- A settings form: use a field or a switch row.
Installation
npx shadcn@latest add https://pixilui.com/r/list-row.jsonAdds list-row.tsx with field.tsx, and installs clsx and tailwind-merge. The URL works in any app with a components.json, with no registry to add first.
"use client";
import { forwardRef, useEffect, useId, useLayoutEffect, useRef, useState, type ComponentPropsWithoutRef, type CSSProperties, type KeyboardEvent, type MouseEvent, type ReactNode } from "react";
import { flushSync } from "react-dom";
import { cn } from "./lib/utils";
import { arrive, flip, fold, leave, resize } from "./lib/motion/surface";
import { prefersReducedMotion } from "./lib/motion/reduced-motion";
import { Field, FieldControl, FieldInput, FieldPrefix } from "./field";
import { safeAnimate } from "./lib/motion/animate";
/*
List rows (B12, locked 1 Dec 2026, "Lock rows"): the row every list shares, for runs, logs, members and sources. A port of
rows.js's list half. Look and motion are foundation.css section 8 (.pixilui-list, .pixilui-list-row): lively breathes with
soft inset lines and a rounded wash; quiet is compact, square and full width with one line between rows. A row that opens
is focusable (Enter opens it, Up and Down move between rows, no motion); a row that folds out opens its detail (.pixilui-fold).
A row that mounts after the list arrives with the system's arrive (a small rise out of a blur). Running wears the locked text
shimmer on its second line; failed turns it red.
The moves rows.js makes (Rows.add, append, remove and the grow switch) are here as functions on the elements, so they work for
any list or table: addRow, appendRows and removeRow take a commit (your state change, run inside flushSync) and glide the rest
with the system's flip, resize, arrive and leave; watchGrow is the list that grows into a table (ListRowList grow). ListRowAdd
is the add row that turns into a search picker (the locked search field and the locked menu's gliding highlight).
*/
// rows by their roles (a list's listitems, a table body's rows), so the moves work on any list or table markup
const ROWS = ':scope > [role="listitem"], :scope > [role="row"]';
/** A table's rows live in its body (its rowgroup); a list's in the list. */
const boxOf = (el: HTMLElement) => (el.getAttribute("role") === "table" ? el.querySelector<HTMLElement>(':scope > [role="rowgroup"]') ?? el : el);
const rowsIn = (box: HTMLElement) => [...box.querySelectorAll<HTMLElement>(ROWS)];
/** What glides when a row comes or goes: every part of the list (rows, their details, group bands, the add row), not a template. */
const partsIn = (box: HTMLElement) => [...box.children].filter((n): n is HTMLElement => n instanceof HTMLElement && !(n instanceof HTMLTemplateElement));
const commitNow = (commit: () => void) => () => flushSync(commit);
export type RowMoveOptions = {
/** The change came from the keyboard: it just happens, nothing moves. */
keyboard?: boolean;
};
/**
* A row arrives: run `commit` (the state change that renders it), and the rows below glide down while the new one rises in
* (rows.js Rows.add). `list` is a ListRowList or a SortableTable.
*/
export function addRow(list: HTMLElement, commit: () => void, { keyboard = false }: RowMoveOptions = {}) {
const box = boxOf(list);
const before = new Set(box.children);
const glide = partsIn(box);
if (keyboard) commitNow(commit)();
else flip(glide, commitNow(commit), box);
if (keyboard) return;
for (const row of rowsIn(box)) if (!before.has(row)) arrive(row);
}
/** Rows arrive at the end, one stagger apart, while the box glides to its new height (rows.js Rows.append: Load more). */
export function appendRows(list: HTMLElement, commit: () => void, { keyboard = false }: RowMoveOptions = {}) {
const box = boxOf(list);
const before = new Set(box.children);
if (keyboard) commitNow(commit)();
else resize(box, commitNow(commit));
if (keyboard) return;
rowsIn(box)
.filter((r) => !before.has(r))
.forEach((r, i) => arrive(r, i));
}
/**
* A row leaves and the rest close the gap (rows.js Rows.remove): it fades into a blur, then `commit` takes it out and the rows
* below glide up. Without `commit` the element is removed from the page. Resolves once it has gone.
*/
export async function removeRow(row: HTMLElement, commit?: () => void, { keyboard = false }: RowMoveOptions = {}): Promise<void> {
const box = row.parentElement;
if (!box) return;
const others = rowsIn(box).filter((r) => r !== row);
if (!keyboard) await leave(row);
const go = commit ? commitNow(commit) : () => row.remove();
if (keyboard) go();
else flip(others, go, box);
}
/**
* The list that grows into a table (rows.js wireGrow): at --pixilui-row-grow-at wide the list takes data-wide, the parts marked
* [data-glide] slide from where they were to their columns (the system's travel), and the cells that only fit when wide
* ([data-wide-only]) arrive. Narrowing reverses. data-instant on the list (a keyboard resize) switches without motion. Returns a
* function that stops watching.
*/
export function watchGrow(list: HTMLElement): () => void {
// no observer (a test DOM, an old browser): the list stays a list
if (typeof ResizeObserver === "undefined") return () => {};
const at = () => parseFloat(getComputedStyle(list).getPropertyValue("--pixilui-row-grow-at")) || 600;
let wide: boolean | null = null;
const apply = (w: boolean, glide: boolean) => {
const moving = glide ? [...list.querySelectorAll<HTMLElement>("[data-glide]")].slice(0, 80) : [];
const before = new Map(moving.map((n) => [n, n.getBoundingClientRect()]));
list.toggleAttribute("data-wide", w);
if (!glide || prefersReducedMotion()) return;
for (const n of moving) {
const a = before.get(n)!;
const b = n.getBoundingClientRect();
const dx = a.left - b.left;
const dy = a.top - b.top;
if (Math.abs(dx) < 0.5 && Math.abs(dy) < 0.5) continue;
const cs = getComputedStyle(n);
safeAnimate(n, [{ transform: `translate(${dx}px, ${dy}px)` }, { transform: "none" }], {
duration: parseFloat(cs.getPropertyValue("--pixilui-dur-travel")) || 0,
easing: cs.getPropertyValue("--pixilui-ease-out").trim(),
});
}
if (w) [...list.querySelectorAll<HTMLElement>("[data-wide-only]")].forEach((c, i) => arrive(c, Math.min(i % 6, 5)));
};
const ro = new ResizeObserver(([e]) => {
if (!e) return;
const w = e.contentRect.width >= at();
if (w === wide) return;
const first = wide === null;
wide = w;
apply(w, !first && !list.hasAttribute("data-instant"));
});
ro.observe(list);
return () => ro.disconnect();
}
export type ListRowListProps = ComponentPropsWithoutRef<"div"> & {
/** sm: compact rows (44 lively, 36 quiet). */
size?: "md" | "sm";
/** Quiet: a line above the first row, for a list that spans its card. */
edge?: boolean;
/** No lines between rows. */
lines?: boolean;
/** timeline: a dot on one line, the time, the title, who did it and a note. */
kind?: "list" | "timeline";
/** Grows into a table when it is growAt wide: parts marked data-glide slide to their columns, data-wide-only cells arrive. */
grow?: boolean;
/** The width the list grows into a table at ("600px", the default). */
growAt?: string;
/** The table's columns once grown, as grid tracks ("24px minmax(0, 1fr) 96px 120px 96px 40px"). */
wideColumns?: string;
/** The width is changing from the keyboard: switch without motion. */
instant?: boolean;
};
/** The list: rows, details, an empty line. Up and Down move between rows that open. */
export const ListRowList = forwardRef<HTMLDivElement, ListRowListProps>(function ListRowList(
{ size = "md", edge, lines = true, kind = "list", grow, growAt, wideColumns, instant, className, style, onKeyDown, ...rest },
ref,
) {
const list = useRef<HTMLDivElement | null>(null);
useEffect(() => (grow && list.current ? watchGrow(list.current) : undefined), [grow]);
const keys = (e: KeyboardEvent<HTMLDivElement>) => {
onKeyDown?.(e);
const row = (e.target as HTMLElement).closest<HTMLElement>(".pixilui-list-row[data-href]");
if (!row || e.target !== row) return;
const d = e.key === "ArrowDown" ? 1 : e.key === "ArrowUp" ? -1 : 0;
if (!d) return;
const rows = [...e.currentTarget.querySelectorAll<HTMLElement>(".pixilui-list-row[data-href]")];
const next = rows[rows.indexOf(row) + d];
if (next) {
e.preventDefault();
next.focus();
}
};
const vars = { ...(growAt ? { "--pixilui-row-grow-at": growAt } : null), ...(wideColumns ? { "--pixilui-row-wide-cols": wideColumns } : null), ...style } as CSSProperties;
return (
<div
ref={(el) => {
list.current = el;
if (typeof ref === "function") ref(el);
else if (ref) ref.current = el;
}}
role="list"
className={cn("pixilui-list font-body", className)}
data-size={size === "sm" ? "sm" : undefined}
data-edge={edge ? "" : undefined}
data-lines={lines ? undefined : "off"}
data-kind={kind === "timeline" ? "timeline" : undefined}
data-grow={grow ? "" : undefined}
data-instant={instant ? "" : undefined}
style={vars}
onKeyDown={keys}
{...rest}
/>
);
});
/** A row's status: running shimmers its second line, failed turns it red; any other value is a hook for your own styles. */
export type ListRowState = "running" | "failed" | (string & {});
export type ListRowProps = Omit<ComponentPropsWithoutRef<"div">, "title"> & {
/** What leads the row: a status node, an avatar, a logo or a checkbox (24). */
lead?: ReactNode;
/** The row's name. */
title?: ReactNode;
/** A quiet word beside the title ("pages"). */
meta?: ReactNode;
/** The grey second line ("Reading 14 invoices"). */
sub?: ReactNode;
/** A number that lines up down the list (tabular). */
value?: ReactNode;
/** A short time ("2h ago"), lined up at the end. */
time?: ReactNode;
/** stale writes the time in the warning color. */
stale?: boolean;
/** Anything else at the end (a tag, a button, a menu), before the value and time. */
end?: ReactNode;
/** Anything after the time (an owner's avatar in a list that grows into a table). */
afterTime?: ReactNode;
/** In a list that grows: the meta becomes its own column and glides there, and so does the time. */
columns?: boolean;
/** A chevron at the very end (an arrow icon): shows on hover, or while open. */
chevron?: ReactNode;
/** The row's own body instead of title, meta and sub (the timeline's time, title, actor and note). */
children?: ReactNode;
/** running shimmers the grey line; failed turns it red; any other value is a hook (data-state). */
state?: ListRowState;
/** A row that opens: focusable, Enter and a click run it. */
onOpen?: (event: MouseEvent<HTMLDivElement> | KeyboardEvent<HTMLDivElement>) => void;
/** The row that is open now (aria-current). */
current?: boolean;
/** Picked (aria-selected). */
selected?: boolean;
/** Off: grey, no hover. */
disabled?: boolean;
/** What the row folds out to (a ListRowDetail's content); the row becomes a toggle with aria-expanded. */
detail?: ReactNode;
/** Folded out, controlled. */
expanded?: boolean;
/** Folded out at first, uncontrolled. */
defaultExpanded?: boolean;
/** Runs when the row folds out or in. */
onExpandedChange?: (expanded: boolean) => void;
/** Arrives with the system's arrive when it mounts (a row added to a list that is already on screen). */
arrive?: boolean;
};
/** The dense row every list shares: a lead, a title with its grey line, and a value or time at the end. */
export const ListRow = forwardRef<HTMLDivElement, ListRowProps>(function ListRow(
{ lead, title, meta, sub, value, time, stale, end, afterTime, columns, chevron, children, state, onOpen, current, selected, disabled, detail, expanded: expandedProp, defaultExpanded = false, onExpandedChange, arrive: arrives, className, onClick, onKeyDown, ...rest },
ref,
) {
const row = useRef<HTMLDivElement | null>(null);
const detailRef = useRef<HTMLDivElement | null>(null);
const [inner, setInner] = useState(defaultExpanded);
const expanded = expandedProp ?? inner;
const opens = !!detail;
const keyboard = useRef(false);
useLayoutEffect(() => {
const d = detailRef.current;
if (!d) return;
if (keyboard.current) d.style.transition = "none"; // keyboard actions never animate
fold(d, expanded);
if (keyboard.current) requestAnimationFrame(() => requestAnimationFrame(() => d.style.removeProperty("transition")));
keyboard.current = false;
}, [expanded]);
useEffect(() => {
if (arrives && row.current) arrive(row.current);
// only when it mounts
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const toggle = (kb: boolean) => {
keyboard.current = kb;
if (expandedProp === undefined) setInner(!expanded);
onExpandedChange?.(!expanded);
};
const hasEnd = value != null || time != null || end != null || afterTime != null || chevron != null;
return (
<>
<div
ref={(el) => {
row.current = el;
if (typeof ref === "function") ref(el);
else if (ref) ref.current = el;
}}
role="listitem"
className={cn("pixilui-list-row", className)}
data-href={onOpen ? "" : undefined}
data-state={state}
tabIndex={onOpen || opens ? 0 : undefined}
aria-current={current ? "true" : undefined}
aria-selected={selected === undefined ? undefined : selected}
aria-disabled={disabled ? "true" : undefined}
aria-expanded={opens ? expanded : undefined}
onClick={(e) => {
onClick?.(e);
if (disabled) return;
if (opens && !(e.target as HTMLElement).closest("button, a, .pixilui-dropdown")) toggle(e.detail === 0);
else if (onOpen && !(e.target as HTMLElement).closest("button, a")) onOpen(e);
}}
onKeyDown={(e) => {
onKeyDown?.(e);
if (e.target !== e.currentTarget || disabled || (e.key !== "Enter" && e.key !== " ")) return;
if (e.nativeEvent.isComposing) return;
e.preventDefault();
if (opens) toggle(true);
else onOpen?.(e);
}}
{...rest}
>
{lead != null ? <span className="pixilui-list-row-lead">{lead}</span> : null}
<span className="pixilui-list-row-body">
{children ?? (
<>
<span className="pixilui-list-row-line">
<span className="pixilui-list-row-title">{title}</span>
{meta != null ? (
<span className="pixilui-list-row-meta" data-col={columns ? "" : undefined} data-glide={columns ? "" : undefined}>
{meta}
</span>
) : null}
</span>
{sub != null ? <span className={cn("pixilui-list-row-sub", state === "running" && "pixilui-text-shimmer")}>{sub}</span> : null}
</>
)}
</span>
{hasEnd ? (
<span className="pixilui-list-row-end">
{end}
{value != null ? <span className="pixilui-list-row-value">{value}</span> : null}
{time != null ? (
<span className="pixilui-list-row-time" data-stale={stale ? "" : undefined} data-glide={columns ? "" : undefined}>
{time}
</span>
) : null}
{afterTime}
{chevron != null ? <span className="pixilui-list-row-chev">{chevron}</span> : null}
</span>
) : null}
</div>
{opens ? <ListRowDetail ref={detailRef}>{detail}</ListRowDetail> : null}
</>
);
});
/** What a row folds out to: grey labels over values (KeyValueList), a note, a few actions. Folds with the system's fold. */
export const ListRowDetail = forwardRef<HTMLDivElement, ComponentPropsWithoutRef<"div">>(function ListRowDetail({ className, children, ...rest }, ref) {
return (
<div ref={ref} className={cn("pixilui-list-detail pixilui-fold", className)} data-state="closed" {...rest}>
<div>
<div className="pixilui-list-detail-body">{children}</div>
</div>
</div>
);
});
/** An empty list: one line and an action. */
export const ListRowEmpty = forwardRef<HTMLDivElement, ComponentPropsWithoutRef<"div">>(function ListRowEmpty({ className, ...rest }, ref) {
return <div ref={ref} className={cn("pixilui-list-empty", className)} {...rest} />;
});
export type ListRowGroupProps = Omit<ComponentPropsWithoutRef<"div">, "children"> & {
/** The group's mark: a small status node. */
mark?: ReactNode;
/** The group's name ("Proposal"). */
label: ReactNode;
/** How many rows it holds: a small count. */
count?: ReactNode;
/** In a list that grows: the column labels, shown once the list is wide ("Value", "People", "Close", and "" for an actions column). */
columns?: ReactNode[];
};
/** A group band over its rows (a stage, a day): its mark, its name and a count; the column labels once the list grows. */
export const ListRowGroup = forwardRef<HTMLDivElement, ListRowGroupProps>(function ListRowGroup({ mark, label, count, columns, className, ...rest }, ref) {
return (
<div ref={ref} role="presentation" className={cn("pixilui-list-group", className)} {...rest}>
<span className="pixilui-list-group-label">
{mark != null ? <span className="pixilui-list-group-mark">{mark}</span> : null}
<span>{label}</span>
{count != null ? (
<span className="pixilui-count" data-size="sm">
{count}
</span>
) : null}
</span>
{columns?.map((c, i) => (
<span key={i} className="pixilui-list-group-col" data-wide-only="">
{c}
</span>
))}
</div>
);
});
export type ListRowAddOption = {
/** What onPick reports. */
value: string;
/** The option's name; the search matches it. */
label: string;
/** A logo tile before the name (16). */
logo?: ReactNode;
/** Grey words at the end ("Chat"). */
meta?: ReactNode;
};
export type ListRowAddProps = Omit<ComponentPropsWithoutRef<"button">, "children" | "onSelect"> & {
/** The add row's words ("Add a source"). */
label: ReactNode;
/** The add row's icon (a plus). */
icon?: ReactNode;
/** The search field's placeholder and name ("Search apps"). */
placeholder: string;
/** The search field's icon. */
searchIcon?: ReactNode;
/** What can be added: each a row in the picker's menu. */
options: ListRowAddOption[];
/** Runs with the picked option's value, after the picker has turned back into the add row. */
onPick?: (value: string, option: ListRowAddOption) => void;
/** Open as a picker at first (a preview). */
defaultOpen?: boolean;
};
/**
* Add in place (rows.js picker): the add row turns into a search field over the locked menu. Typing filters the options, Up and
* Down move the gliding highlight, Enter or a click picks, Esc or leaving the field turns it back. Pair onPick with addRow to land
* the new row.
*/
export const ListRowAdd = forwardRef<HTMLButtonElement, ListRowAddProps>(function ListRowAdd(
{ label, icon, placeholder, searchIcon, options, onPick, defaultOpen = false, className, onClick, ...rest },
ref,
) {
const [open, setOpen] = useState(defaultOpen);
const [query, setQuery] = useState("");
// the picker opens with its first option under the highlight
const [active, setActive] = useState<string | null>(() => (defaultOpen ? (options[0]?.value ?? null) : null));
const button = useRef<HTMLButtonElement | null>(null);
const input = useRef<HTMLInputElement | null>(null);
const menu = useRef<HTMLDivElement | null>(null);
const jump = useRef(true);
const refocus = useRef(false);
const base = useId();
const idOf = (i: number) => `${base}-opt-${i}`;
const q = query.trim().toLowerCase();
const shown = options.filter((o) => !q || o.label.toLowerCase().includes(q));
// the locked menu's highlight, moved the way its own script moves it
useLayoutEffect(() => {
const m = menu.current;
if (!open || !m) return;
const hl = m.querySelector<HTMLElement>(":scope > .pixilui-dropdown-highlight");
const it = active == null ? null : m.querySelector<HTMLElement>(`:scope > .pixilui-dropdown-item[data-value="${CSS.escape(active)}"]`);
if (!it || it.hidden) {
m.removeAttribute("data-hl");
return;
}
m.toggleAttribute("data-jump", jump.current || !m.hasAttribute("data-hl"));
m.style.setProperty("--pixilui-dropdown-y", `${it.offsetTop}px`);
if (hl) hl.style.height = `${it.offsetHeight}px`;
m.toggleAttribute("data-hl", true);
input.current?.setAttribute("aria-activedescendant", it.id);
jump.current = false;
const raf = requestAnimationFrame(() => m.removeAttribute("data-jump"));
return () => cancelAnimationFrame(raf);
}, [open, active, query]);
useLayoutEffect(() => {
if (open) input.current?.focus();
else if (refocus.current) button.current?.focus({ preventScroll: true });
refocus.current = false;
}, [open]);
const move = (value: string | null, isJump: boolean) => {
jump.current = isJump;
setActive(value);
};
const done = (pick: ListRowAddOption | null) => {
refocus.current = true;
flushSync(() => {
setOpen(false);
setQuery("");
setActive(null);
});
if (pick) onPick?.(pick.value, pick);
};
if (!open) {
return (
<button
ref={(el) => {
button.current = el;
if (typeof ref === "function") ref(el);
else if (ref) ref.current = el;
}}
type="button"
className={cn("pixilui-list-add", className)}
onClick={(e) => {
onClick?.(e);
if (e.defaultPrevented) return;
jump.current = true;
setActive(options[0]?.value ?? null);
setOpen(true);
}}
{...rest}
>
<span className="pixilui-list-row-lead">{icon}</span>
<span>{label}</span>
</button>
);
}
return (
<div className="pixilui-list-picker">
<Field kind="search" size="sm">
<FieldControl>
<FieldPrefix>{searchIcon}</FieldPrefix>
<FieldInput
ref={input}
type="search"
placeholder={placeholder}
aria-label={placeholder}
role="combobox"
aria-expanded="true"
aria-autocomplete="list"
autoComplete="off"
value={query}
onChange={(e) => {
const next = e.target.value;
const nq = next.trim().toLowerCase();
setQuery(next);
move(options.find((o) => !nq || o.label.toLowerCase().includes(nq))?.value ?? null, true);
}}
onKeyDown={(e) => {
if (e.nativeEvent.isComposing) return;
const i = shown.findIndex((o) => o.value === active);
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
e.preventDefault();
if (!shown.length) return;
const n = shown[(i + (e.key === "ArrowDown" ? 1 : -1) + shown.length) % shown.length]!;
move(n.value, false);
} else if (e.key === "Enter") {
e.preventDefault();
const it = shown.find((o) => o.value === active);
if (it) done(it);
} else if (e.key === "Escape") {
e.preventDefault();
done(null);
}
}}
onBlur={() =>
setTimeout(() => {
if (input.current?.isConnected) done(null);
}, 0)
}
/>
</FieldControl>
</Field>
<div
ref={menu}
className="pixilui-dropdown-menu"
role="listbox"
data-in=""
onMouseMove={(e) => {
const it = (e.target as HTMLElement).closest<HTMLElement>(".pixilui-dropdown-item");
if (it && it.dataset.value !== active) move(it.dataset.value ?? null, false);
}}
onMouseDown={(e) => e.preventDefault()}
onClick={(e) => {
const it = (e.target as HTMLElement).closest<HTMLElement>(".pixilui-dropdown-item");
const o = it && options.find((x) => x.value === it.dataset.value);
if (o) done(o);
}}
>
<span className="pixilui-dropdown-highlight" aria-hidden="true" />
{options.map((o, i) => (
<div
key={o.value}
className="pixilui-dropdown-item"
role="option"
data-value={o.value}
id={idOf(i)}
aria-selected={active === o.value && shown.includes(o)}
hidden={!shown.includes(o)}
>
{o.logo != null ? <span className="pixilui-list-row-logo">{o.logo}</span> : null}
<span className="pixilui-dropdown-text">{o.label}</span>
{o.meta != null ? <span className="pixilui-dropdown-meta">{o.meta}</span> : null}
</div>
))}
</div>
</div>
);
});
It uses Field: add it the same way. Then npm i clsx tailwind-merge, and add the foundation once per app, as in Installation.
Usage
import { ListRow } from "@/components/pixilui/list-row";
<ListRow onOpen={onOpen} onExpandedChange={onExpandedChange}>Save changes</ListRow>Fills its container; the title and grey line truncate, the end stays whole.
API reference
leadReactNode–What leads the row: a status node, an avatar, a logo or a checkbox (24).titleReactNode–The row's name.metaReactNode–A quiet word beside the title ("pages").subReactNode–The grey second line ("Reading 14 invoices").valueReactNode–A number that lines up down the list (tabular).timeReactNode–A short time ("2h ago"), lined up at the end.staleboolean–stale writes the time in the warning color.endReactNode–Anything else at the end (a tag, a button, a menu), before the value and time.afterTimeReactNode–Anything after the time (an owner's avatar in a list that grows into a table).columnsboolean–In a list that grows: the meta becomes its own column and glides there, and so does the time.chevronReactNode–A chevron at the very end (an arrow icon): shows on hover, or while open.stateListRowState–running shimmers the grey line; failed turns it red; any other value is a hook (data-state).onOpen(event: MouseEvent<HTMLDivElement> | KeyboardEvent<HTMLDivElement>) => void–A row that opens: focusable, Enter and a click run it.currentboolean–The row that is open now (aria-current).selectedboolean–Picked (aria-selected).disabledboolean–Off: grey, no hover.detailReactNode–What the row folds out to (a ListRowDetail's content); the row becomes a toggle with aria-expanded.expandedboolean–Folded out, controlled.defaultExpandedbooleanfalseFolded out at first, uncontrolled.onExpandedChange(expanded: boolean) => void–Runs when the row folds out or in.arriveboolean–Arrives with the system's arrive when it mounts (a row added to a list that is already on screen).childrenReactNode–The row's own body instead of title, meta and sub (the timeline's time, title, actor and note).Also takes every native <div> attribute except title, and a ref.
Keyboard
Accessibility
- List and listitem roles; a row that opens is focusable and named by aria-label; a row that folds out has aria-expanded.
- Open is aria-current, picked is aria-selected, off is aria-disabled. Failed is words in red, never only the color.
- Focus is a 1px ring, the plain ring the fields use.
Motion
- Hover washes the row in 150ms (lively: rounded, the lines beside it step back; quiet: square and full width).
- A running row's grey line carries the locked text shimmer.
- A detail folds with the system's fold; a row added later arrives with the system's arrive (a 2 to 3px rise out of a 3px blur) while the rows below glide down (flip).
- A removed row fades into a blur, a hair smaller, then the rest glide up to close the gap. Load more's rows arrive 35ms apart while the list glides to its new height.
- Growing into a table: the value and time slide to their columns over 420ms, and the wide-only cells arrive.
- The add row's picker: the menu's highlight glides between options (150ms), and jumps on the first.
- Keyboard actions never animate. Reduced motion turns every transition off.