Skip to content
← Blocks

Team Management

A working team management with illustrative data and frontend interactions.

Open ↗

Make it yours.

Install the editable source and its dependencies into your React + Tailwind project.

Scroll horizontally for long lines
pnpm dlx shadcn@4.0.8 add http://localhost:3000/r/team-management.json

Use it

Scroll horizontally for long lines
"use client";
import { TeamManagement } from "@/components/jez-ui/blocks/team-management";

export default function Example() {
  return (
    <>
      <TeamManagement />
    </>
  );
}

Compose your own

These styled parts are included in the same install. Use children to build your layout; add className only when you want an override.

Scroll horizontally for long lines
"use client";
import {
  TeamManagement,
  TeamManagementError,
  TeamManagementHeading,
  TeamManagementInviteForm,
  TeamManagementMembers,
  TeamManagementStatus,
  TeamManagementReset,
} from "@/components/jez-ui/blocks/team-management";

export default function Example() {
  return (
    <TeamManagement>
      <TeamManagementError />
      <TeamManagementHeading />
      <TeamManagementInviteForm />
      <TeamManagementMembers />
      <TeamManagementStatus />
      <TeamManagementReset />
    </TeamManagement>
  );
}
Scroll horizontally for long lines
import { TeamManagement, TeamManagementTitle, TeamManagementForm, TeamManagementItem, TeamManagementError, TeamManagementHeading, TeamManagementInviteForm, TeamManagementMembers, TeamManagementStatus, TeamManagementReset } from "@/components/jez-ui/blocks/team-management";
Read the composition guide →

Props & types

Scroll horizontally for long lines
export type TeamManagementOptions = {
  className?: string;
  value?: typeof initial;
  defaultValue?: typeof initial;
  onValueChange?: (value: typeof initial) => void;
  items?: typeof TeamManagementDefaultItems;
  onInvite?: (value: string) => void | Promise<void>;
};

export type TeamManagementProps = Omit<
  React.ComponentProps<"section">,
  keyof TeamManagementOptions
> &
  TeamManagementOptions;

TeamManagementProps

Source

View 6 source files

registry/blocks/team-management.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { useAsyncAction } from "../ui/use-async-action";
import { cn } from "../ui/utils";
import { Input } from "../ui/input";
import { Button } from "../ui/button";
import { useControllable } from "../ui/use-controllable";
const initial = [
  { email: "alex@example.com", role: "Owner" },
  { email: "sam@example.com", role: "Editor" },
];
export type TeamManagementOptions = {
  className?: string;
  value?: typeof initial;
  defaultValue?: typeof initial;
  onValueChange?: (value: typeof initial) => void;
  items?: typeof TeamManagementDefaultItems;
  onInvite?: (value: string) => void | Promise<void>;
};
export type TeamManagementProps = Omit<
  React.ComponentProps<"section">,
  keyof TeamManagementOptions
> &
  TeamManagementOptions;
const TeamManagementDefaultItems = ["Owner", "Editor", "Viewer", "Invited"];
function useTeamManagementModel({
  items = TeamManagementDefaultItems,
  className,
  value: controlledValue,
  defaultValue = initial,
  onValueChange,
  onInvite,
  children,
  ...rootProps
}: TeamManagementProps) {
  const action = useAsyncAction();
  const [team, setTeam] = useControllable<typeof initial>(
    controlledValue,
    defaultValue,
    onValueChange,
  );
  const reset = () => setTeam(defaultValue);
  const [email, setEmail] = React.useState("");
  const [status, setStatus] = React.useState("");
  return {
    items,
    className,
    controlledValue,
    defaultValue,
    onValueChange,
    onInvite,
    children,
    rootProps,
    action,
    team,
    setTeam,
    reset,
    email,
    setEmail,
    status,
    setStatus,
  };
}
const TeamManagementCompositionContext = React.createContext<ReturnType<
  typeof useTeamManagementModel
> | null>(null);
function useTeamManagementComposition() {
  const context = React.useContext(TeamManagementCompositionContext);
  if (!context)
    throw new Error("TeamManagement parts must be inside TeamManagement.");
  return context;
}
export function TeamManagement(props: TeamManagementProps) {
  const model = useTeamManagementModel(props);
  const { className, rootProps, children } = model;
  return (
    <TeamManagementCompositionContext.Provider value={model}>
      <section
        {...rootProps}
        className={cn(
          "grid gap-5 rounded-xl border border-border p-6",
          className,
        )}
      >
        {children !== undefined ? (
          children
        ) : (
          <>
            <TeamManagementError />
            <TeamManagementHeading />
            <TeamManagementInviteForm />
            <TeamManagementMembers />
            <TeamManagementStatus />
            <TeamManagementReset />
          </>
        )}
      </section>
    </TeamManagementCompositionContext.Provider>
  );
}

export function TeamManagementTitle({
  className,
  ...props
}: React.ComponentProps<"h2">) {
  return (
    <h2
      data-slot="team-management-title"
      className={cn("text-lg font-semibold", className)}
      {...props}
    />
  );
}
export function TeamManagementForm({
  className,
  ...props
}: React.ComponentProps<"form">) {
  return (
    <form
      data-slot="team-management-form"
      className={cn("flex gap-2", className)}
      {...props}
    />
  );
}

export function TeamManagementItem({
  className,
  ...props
}: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="team-management-item"
      className={cn(
        "flex flex-wrap items-center justify-between gap-3 border-b border-border py-3 text-sm",
        className,
      )}
      {...props}
    />
  );
}

export function TeamManagementError({ children }: React.PropsWithChildren) {
  const { action } = useTeamManagementComposition();
  return children === undefined
    ? action.error && <p role="alert">{action.error}</p>
    : children;
}
export function TeamManagementHeading({
  children,
  ...props
}: Partial<React.ComponentProps<"div">> & { children?: React.ReactNode }) {
  const { team } = useTeamManagementComposition();
  return (
    <div {...props}>
      {children === undefined ? (
        <>
          <TeamManagementTitle>
            Team members{" "}
            <span className="ml-1 text-sm text-muted-foreground">
              {team.length}
            </span>
          </TeamManagementTitle>
          <p className="mt-1 text-sm text-muted-foreground">
            Manage who has access to this workspace.
          </p>
        </>
      ) : (
        children
      )}
    </div>
  );
}
export function TeamManagementInviteForm({
  children,
  ...props
}: Partial<React.ComponentProps<typeof TeamManagementForm>> & {
  children?: React.ReactNode;
}) {
  const { onInvite, action, team, setTeam, email, setEmail, setStatus } =
    useTeamManagementComposition();
  const defaultonSubmit: NonNullable<
    React.ComponentProps<typeof TeamManagementForm>["onSubmit"]
  > = (e) => {
    e.preventDefault();
    if (team.some((t) => t.email === email)) {
      setStatus("This person is already on the team.");
      return;
    }
    void action.run(async () => {
      await onInvite?.(email);
      setTeam((t) => [...t, { email, role: "Invited" }]);
      setEmail("");
      setStatus(
        onInvite
          ? "Invitation completed."
          : "Added locally. No invitation email was sent.",
      );
    });
  };
  return (
    <TeamManagementForm
      {...props}
      onSubmit={(event) => {
        props.onSubmit?.(event);
        if (!event.defaultPrevented) defaultonSubmit(event);
      }}
    >
      {children === undefined ? (
        <>
          <Input
            type="email"
            required
            aria-label="Invite email"
            value={email}
            onChange={(e) => setEmail(e.target.value)}
            placeholder="teammate@example.com"
          />
          <Button
            type="submit"
            disabled={action.pending}
            loading={action.pending}
          >
            Invite
          </Button>
        </>
      ) : (
        children
      )}
    </TeamManagementForm>
  );
}
export function TeamManagementMembers({ children }: React.PropsWithChildren) {
  const { items, team, setTeam } = useTeamManagementComposition();
  return children === undefined
    ? team.map((t) => (
        <TeamManagementItem key={t.email}>
          <span className="flex min-w-0 items-center gap-3">
            <span className="grid size-9 shrink-0 place-items-center rounded-full bg-muted text-xs font-medium">
              {t.email.slice(0, 2).toUpperCase()}
            </span>
            <span className="min-w-0">
              <span className="block truncate font-medium">
                {t.email.split("@")[0]}
              </span>
              <span className="text-xs text-muted-foreground">{t.email}</span>
            </span>
          </span>
          <select
            disabled={t.role === "Owner"}
            aria-label={`Role for ${t.email}`}
            value={t.role}
            onChange={(e) =>
              setTeam((v) =>
                v.map((x) =>
                  x.email === t.email ? { ...x, role: e.target.value } : x,
                ),
              )
            }
            className="rounded-lg border border-border bg-background px-3 py-2 text-xs"
          >
            {items.map((r) => (
              <option key={r}>{r}</option>
            ))}
          </select>
        </TeamManagementItem>
      ))
    : children;
}
export function TeamManagementStatus({ children }: React.PropsWithChildren) {
  const { status } = useTeamManagementComposition();
  return children === undefined
    ? status && (
        <p role="status" className="text-sm">
          {status}
        </p>
      )
    : children;
}
export function TeamManagementReset({
  children,
  ...props
}: Partial<React.ComponentProps<typeof Button>> & {
  children?: React.ReactNode;
}) {
  const { reset } = useTeamManagementComposition();
  const defaultonClick: NonNullable<
    React.ComponentProps<typeof Button>["onClick"]
  > = reset;
  return (
    <Button
      variant="ghost"
      {...props}
      className={cn("justify-self-start", props.className)}
      onClick={(event) => {
        props.onClick?.(event);
        if (!event.defaultPrevented) defaultonClick(event);
      }}
    >
      {children === undefined ? "Reset changes" : children}
    </Button>
  );
}

registry/ui/use-async-action.ts

Scroll horizontally for long lines
"use client";
import * as React from "react";
/** Keeps duplicate submissions out and reports failures without committing local success. */
export function useAsyncAction() {
  const [pending, setPending] = React.useState(false);
  const [error, setError] = React.useState<string>();
  const busy = React.useRef(false);
  async function run(action: () => void | Promise<void>) {
    if (busy.current) return false;
    busy.current = true;
    setPending(true);
    setError(undefined);
    try {
      await action();
      return true;
    } catch (error) {
      setError(
        error instanceof Error
          ? error.message
          : "The action failed. Please try again.",
      );
      return false;
    } finally {
      busy.current = false;
      setPending(false);
    }
  }
  return { run, pending, error };
}

registry/ui/utils.ts

Scroll horizontally for long lines
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...values: ClassValue[]) {
  return twMerge(clsx(values));
}

registry/ui/input.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { cn } from "./utils";
export function Input({ className, ...props }: React.ComponentProps<"input">) {
  return (
    <input
      className={cn(
        "block h-10 w-full min-w-0 rounded-lg border border-border bg-background px-3 text-sm leading-normal shadow-[0_1px_2px_#00000004] transition-[border-color,box-shadow] placeholder:text-muted-foreground/75 hover:border-foreground/25 focus:border-primary focus:outline-none focus:ring-3 focus:ring-primary/10 aria-invalid:border-danger aria-invalid:ring-danger/10 disabled:cursor-not-allowed disabled:bg-muted/50 disabled:opacity-50",
        className,
      )}
      {...props}
    />
  );
}

registry/ui/button.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { Slot } from "radix-ui";
import { LoaderCircle } from "lucide-react";
import { cn } from "./utils";
export type ButtonProps = React.ComponentProps<"button"> & {
  variant?: "primary" | "secondary" | "outline" | "ghost" | "danger";
  size?: "sm" | "md" | "lg";
  loading?: boolean;
  asChild?: boolean;
};
export function Button({
  variant = "primary",
  size = "md",
  loading,
  asChild,
  className,
  children,
  disabled,
  ...props
}: ButtonProps) {
  const Comp = asChild ? Slot.Root : "button";
  return (
    <Comp
      type={asChild ? undefined : "button"}
      disabled={asChild ? undefined : disabled || loading}
      aria-disabled={disabled || loading || undefined}
      aria-busy={loading || undefined}
      className={cn(
        "relative inline-flex shrink-0 items-center justify-center gap-2 whitespace-nowrap rounded-lg border border-transparent text-sm font-medium leading-none transition-[background-color,border-color,box-shadow,transform] duration-150 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary active:scale-[.98] disabled:pointer-events-none disabled:opacity-45 aria-disabled:pointer-events-none [&_svg]:shrink-0",
        {
          "bg-primary text-primary-foreground shadow-[inset_0_1px_0_#ffffff26,0_1px_2px_#00000012] hover:bg-primary/90":
            variant === "primary",
          "border-border/60 bg-muted text-foreground hover:bg-muted/70":
            variant === "secondary",
          "border-border bg-background text-foreground shadow-sm hover:bg-muted/60":
            variant === "outline",
          "text-muted-foreground hover:bg-muted hover:text-foreground":
            variant === "ghost",
          "bg-danger text-danger-foreground hover:bg-danger/90":
            variant === "danger",
        },
        {
          "h-8 px-3 text-xs": size === "sm",
          "h-10 px-4": size === "md",
          "h-12 px-6": size === "lg",
        },
        className,
      )}
      {...props}
    >
      {asChild ? (
        children
      ) : (
        <>
          <span
            className={cn(
              "inline-flex min-w-0 flex-1 items-center justify-center gap-2",
              loading && "invisible",
            )}
          >
            {children}
          </span>
          {loading && (
            <LoaderCircle
              aria-hidden="true"
              className="absolute size-4 animate-spin motion-reduce:animate-none"
            />
          )}
        </>
      )}
    </Comp>
  );
}

registry/ui/use-controllable.ts

Scroll horizontally for long lines
"use client";
import { useRef, useState, type SetStateAction } from "react";
/** Controlled or instance-local state. Functional updates also work before a rerender. */
export function useControllable<T>(
  value: T | undefined,
  initial: T,
  onChange?: (value: T) => void,
) {
  const [local, setLocal] = useState(initial);
  const current = useRef(value === undefined ? local : value);
  current.current = value === undefined ? local : value;
  return [
    current.current,
    (action: SetStateAction<T>) => {
      const next =
        typeof action === "function"
          ? (action as (previous: T) => T)(current.current)
          : action;
      current.current = next;
      if (value === undefined) setLocal(next);
      onChange?.(next);
    },
  ] as const;
}

Dependencies

clsx@2.1.1 · tailwind-merge@3.5.0 · radix-ui@1.6.7 · lucide-react@0.577.0

Accessibility & behaviour

Provide meaningful labels and preserve keyboard focus styles. Check contrast when customising theme colours.

Read the accessibility and performance guide →