Team members
Members and invites in one table, roles in a menu, an invite that grows in place.
253 lines · React · Tailwind 4
Install
$ bunx shadcn@latest add @blocco/team-membersCode
import { useRef, useState, type FormEvent } from "react";
import { Button } from "@/registry/blocco/ui/button";
import { Check, ChevronDown, UserPlus } from "@/registry/blocco/ui/icons";
import { Input } from "@/registry/blocco/ui/field";
import { Menu, MenuItem, MenuSeparator } from "@/registry/blocco/ui/menu";
import { Badge, Table, Td, Th } from "@/registry/blocco/ui/misc";
import { Spinner } from "@/registry/blocco/ui/spinner";
import { cn } from "@/registry/blocco/ui/cn";
import { useToast } from "@/registry/blocco/ui/toast";
type Role = "Owner" | "Admin" | "Editor" | "Viewer";
const roles: { role: Exclude<Role, "Owner">; hint: string }[] = [
{ role: "Admin", hint: "Manages members and billing" },
{ role: "Editor", hint: "Creates and edits links" },
{ role: "Viewer", hint: "Reads links and stats" },
];
interface Person {
id: string;
name: string;
email: string;
role: Role;
invited: boolean;
}
const initial: Person[] = [
{ id: "1", name: "Ada Lovelace", email: "ada@acme.test", role: "Owner", invited: false },
{ id: "2", name: "Grace Hopper", email: "grace@acme.test", role: "Admin", invited: false },
{ id: "3", name: "Linus Torvalds", email: "linus@acme.test", role: "Editor", invited: false },
{
id: "4",
name: "Margaret Hamilton",
email: "margaret@acme.test",
role: "Editor",
invited: false,
},
{ id: "5", name: "", email: "alan@acme.test", role: "Viewer", invited: true },
];
const EMAIL = /^\S+@\S+\.\S+$/;
function RoleMenu({
person,
onRole,
onRemove,
}: {
person: Person;
onRole: (role: Role) => void;
onRemove: () => void;
}) {
if (person.role === "Owner") return <span className="px-2 text-sm text-muted">Owner</span>;
return (
<Menu
align="end"
trigger={
<span className="inline-flex h-9 items-center gap-1.5 rounded-lg px-2 text-sm transition-colors duration-150 [@media(hover:hover)]:hover:bg-surface-2">
<span className="sr-only">Role for {person.email}: </span>
{person.role}
<ChevronDown size={14} className="text-muted" aria-hidden />
</span>
}
>
{roles.map((r) => (
<MenuItem key={r.role} onClick={() => onRole(r.role)}>
<span className="w-4 shrink-0">
{r.role === person.role && <Check size={13} className="text-accent" />}
</span>
<span className="flex flex-col">
<span>{r.role}</span>
<span className="text-xs text-muted">{r.hint}</span>
</span>
</MenuItem>
))}
<MenuSeparator />
<MenuItem onClick={onRemove} className="text-danger">
<span className="w-4 shrink-0" />
{person.invited ? "Revoke invite" : "Remove member"}
</MenuItem>
</Menu>
);
}
/** The invite button, which widens into an email field with its submit inside. Esc puts it back. */
function InviteGrow({ onInvite }: { onInvite: (email: string) => Promise<void> }) {
const [open, setOpen] = useState(false);
const [value, setValue] = useState("");
const [busy, setBusy] = useState(false);
const [error, setError] = useState("");
const input = useRef<HTMLInputElement>(null);
const trigger = useRef<HTMLButtonElement>(null);
const close = () => {
setOpen(false);
setValue("");
setError("");
requestAnimationFrame(() => trigger.current?.focus());
};
const submit = async (e: FormEvent) => {
e.preventDefault();
if (!EMAIL.test(value.trim())) {
setError("Needs an email");
input.current?.focus();
return;
}
setBusy(true);
try {
await onInvite(value.trim());
} finally {
setBusy(false);
}
close();
};
return (
<form
onSubmit={submit}
noValidate
className={cn(
"relative h-9 max-w-full overflow-hidden rounded-lg transition-[width] duration-200 ease-out motion-reduce:transition-none",
open ? "w-80 sm:w-96" : "w-24",
)}
>
{open ? (
<>
<Input
ref={input}
autoFocus
type="email"
inputMode="email"
autoComplete="email"
aria-label="Email to invite"
aria-invalid={error ? true : undefined}
placeholder={error || "name@company.com"}
value={value}
disabled={busy}
onChange={(e) => {
setValue(e.target.value);
setError("");
}}
onKeyDown={(e) => {
if (e.key === "Escape") {
e.preventDefault();
close();
}
}}
onBlur={(e) => {
if (!value && !e.relatedTarget) close();
}}
className={cn("h-9 pr-24", error && "border-danger placeholder:text-danger")}
/>
<Button type="submit" size="sm" disabled={busy} className="absolute top-1 right-1 h-7">
{busy ? <Spinner /> : null}
Send invite
</Button>
</>
) : (
<Button
ref={trigger}
type="button"
size="md"
className="w-full"
onClick={() => setOpen(true)}
>
<UserPlus size={14} />
Invite
</Button>
)}
</form>
);
}
/** Members and pending invites in one table, roles in a menu, and an invite field that grows out of its button. */
export default function TeamMembers() {
const toast = useToast();
const [people, setPeople] = useState(initial);
const seats = people.length;
const invite = async (email: string) => {
await new Promise((r) => setTimeout(r, 400));
setPeople((p) => [
{ id: crypto.randomUUID(), name: "", email, role: "Viewer", invited: true },
...p,
]);
toast("Invite sent", "info", { code: email });
};
return (
<section className="mx-auto max-w-4xl px-4 py-10 sm:px-6 sm:py-12">
<div className="mb-6 flex flex-wrap items-end justify-between gap-3">
<div>
<h1 className="text-xl font-semibold tracking-tight text-balance">Members</h1>
<p className="tnum mt-1 text-sm text-muted">{seats} of 10 seats used on the Pro plan.</p>
</div>
<InviteGrow onInvite={invite} />
</div>
<Table minWidth="min-w-[34rem]">
<thead>
<tr>
<Th>Member</Th>
<Th className="w-40 text-right">Role</Th>
</tr>
</thead>
<tbody>
{people.map((p) => (
<tr key={p.id}>
<Td>
<div className="flex min-w-0 items-center gap-3">
<span
aria-hidden
className="grid size-8 shrink-0 place-items-center rounded-full bg-surface-2 text-xs font-semibold"
>
{(p.name || p.email)[0].toUpperCase()}
</span>
<div className="min-w-0">
<div className="flex items-center gap-2">
<span className="truncate text-sm font-medium">{p.name || p.email}</span>
{p.invited && <Badge color="butter">Invited</Badge>}
</div>
{p.name && (
<span className="block truncate font-mono text-xs text-muted [overflow-wrap:anywhere]">
{p.email}
</span>
)}
</div>
</div>
</Td>
<Td className="text-right">
<div className="flex justify-end">
<RoleMenu
person={p}
onRole={(role) => {
setPeople((all) => all.map((x) => (x.id === p.id ? { ...x, role } : x)));
toast(`Role set to ${role}`, "info", { code: p.email });
}}
onRemove={() => {
setPeople((all) => all.filter((x) => x.id !== p.id));
toast(p.invited ? "Invite revoked" : "Member removed", "info", {
code: p.email,
});
}}
/>
</div>
</Td>
</tr>
))}
</tbody>
</Table>
</section>
);
}