Skip to content
Molecules

Workspace switcher

The workspace picker atop a sidebar.

Installation

  1. Run the CLI

    Run pnpm dlx asta-ui@latest init once, then this. The file lands in components/astabyte/ and the colour tokens go into your CSS.

    $ pnpm dlx asta-ui@latest add workspace-switcher

    Or with the shadcn CLI: pnpm dlx shadcn@latest add https://ui.astabyte.com/r/workspace-switcher.json

  2. Or install the package

    Don’t want the file? Import from asta-ui/workspace-switcher and update with pnpm update asta-ui. Add the stylesheet once. More.

    $ pnpm add asta-ui
    import {  } from "asta-ui/workspace-switcher";
  3. Or copy it by hand

    Copy this file. Keep the paths, or fix the @/ imports. Then add the tokens from globals.css.

    "use client";
    
    import { useEffect, useId, useRef, useState, type KeyboardEvent, type ReactNode } from "react";
    
    const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" ");
    
    const KEYFRAMES = `@keyframes asta-ws-in { from { opacity: 0; transform: translateY(-4px) scale(.97) } }
    @media (prefers-reduced-motion: reduce) { .asta-ws-pop { animation: none !important } }`;
    
    export interface Workspace {
      id: string;
      name: string;
      /** A short tag after the name, e.g. "Pro" or "Free". */
      plan?: string;
      /** A logo URL. Falls back to the first letter. */
      logo?: string;
      /** Muted text under the name in the list, e.g. "4 members". */
      meta?: string;
    }
    
    export interface WorkspaceSwitcherProps {
      workspaces: Workspace[];
      value?: string;
      defaultValue?: string;
      onChange?: (id: string, workspace: Workspace) => void;
      /** Shows a "New workspace" row at the bottom. */
      onCreate?: () => void;
      createLabel?: string;
      /** Who is signed in, shown at the top of the menu. */
      account?: { name: string; email?: string };
      /** Extra rows under the list, e.g. settings or sign out. */
      footer?: ReactNode;
      className?: string;
    }
    
    function Logo({ ws, size }: { ws: Workspace; size: "sm" | "md" }) {
      const box = size === "sm" ? "h-6 w-6 rounded-[6px] text-[11.5px]" : "h-8 w-8 rounded-[8px] text-[13px]";
      if (ws.logo) return <img src={ws.logo} alt="" width={32} height={32} loading="lazy" className={cx(box, "shrink-0 object-cover")} />;
      return <span className={cx(box, "grid shrink-0 place-items-center bg-brand font-semibold text-on-brand")}>{ws.name.slice(0, 1).toUpperCase()}</span>;
    }
    
    const Check = () => (
      <svg viewBox="0 0 24 24" className="h-4 w-4 text-brand" fill="none" stroke="currentColor" strokeWidth={2.2} strokeLinecap="round" strokeLinejoin="round" aria-hidden>
        <path d="m5 12.5 4.5 4.5L19 7.5" />
      </svg>
    );
    
    /**
     * The account and workspace picker at the top of an app sidebar. The menu drops in from the trigger, ↑ ↓ move,
     * Enter picks, Escape or a click outside closes it, and the current workspace carries a check.
     */
    export function WorkspaceSwitcher({ workspaces, value, defaultValue, onChange, onCreate, createLabel = "New workspace", account, footer, className }: WorkspaceSwitcherProps) {
      const [inner, setInner] = useState(defaultValue ?? workspaces[0]?.id);
      const current = workspaces.find((w) => w.id === (value ?? inner)) ?? workspaces[0];
      const [open, setOpen] = useState(false);
      const [active, setActive] = useState(0);
      const root = useRef<HTMLDivElement>(null);
      const trigger = useRef<HTMLButtonElement>(null);
      const listRef = useRef<HTMLDivElement>(null);
      const id = useId();
      const count = workspaces.length + (onCreate ? 1 : 0);
    
      useEffect(() => {
        if (!open) return;
        const close = (e: PointerEvent) => !root.current?.contains(e.target as Node) && setOpen(false);
        window.addEventListener("pointerdown", close);
        return () => window.removeEventListener("pointerdown", close);
      }, [open]);
    
      useEffect(() => {
        if (open) listRef.current?.focus();
      }, [open]);
    
      const show = () => {
        setActive(Math.max(0, workspaces.findIndex((w) => w.id === current?.id)));
        setOpen(true);
      };
      const choose = (i: number) => {
        if (i === workspaces.length) onCreate?.();
        else {
          const ws = workspaces[i];
          if (value === undefined) setInner(ws.id);
          onChange?.(ws.id, ws);
        }
        setOpen(false);
        trigger.current?.focus();
      };
    
      const onListKey = (e: KeyboardEvent) => {
        if (e.key === "ArrowDown") setActive((a) => (a + 1) % count);
        else if (e.key === "ArrowUp") setActive((a) => (a - 1 + count) % count);
        else if (e.key === "Home") setActive(0);
        else if (e.key === "End") setActive(count - 1);
        else if (e.key === "Enter" || e.key === " ") choose(active);
        else if (e.key === "Escape" || e.key === "Tab") {
          setOpen(false);
          if (e.key === "Escape") trigger.current?.focus();
          return;
        } else return;
        e.preventDefault();
      };
    
      if (!current) return null;
    
      return (
        <div ref={root} className={cx("relative", className)}>
          <style href="asta-workspace-switcher" precedence="asta">{KEYFRAMES}</style>
          <button
            ref={trigger}
            type="button"
            aria-haspopup="listbox"
            aria-expanded={open}
            aria-controls={open ? id : undefined}
            onClick={() => (open ? setOpen(false) : show())}
            onKeyDown={(e) => {
              if (e.key === "ArrowDown" || e.key === "ArrowUp") {
                e.preventDefault();
                show();
              }
            }}
            className={cx(
              "group/ws flex w-full cursor-pointer items-center gap-2.5 rounded-[9px] px-2 py-1.5 text-left outline-none transition-colors duration-150",
              "hover:bg-[color-mix(in_srgb,var(--color-ink)_6%,transparent)] focus-visible:shadow-[0_0_0_2px_var(--color-brand)]",
              open && "bg-[color-mix(in_srgb,var(--color-ink)_6%,transparent)]",
            )}
          >
            <Logo ws={current} size="sm" />
            <span className="min-w-0 flex-1 truncate text-[13.5px] font-medium text-ink">{current.name}</span>
            {current.plan && <span className="shrink-0 rounded-[5px] bg-[color-mix(in_srgb,var(--color-brand)_12%,transparent)] px-1.5 py-0.5 text-[11px] font-medium text-brand">{current.plan}</span>}
            <svg viewBox="0 0 24 24" className={cx("h-3.5 w-3.5 shrink-0 text-mute transition-transform duration-300 ease-[cubic-bezier(.16,1,.3,1)]", open && "rotate-180")} fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round" strokeLinejoin="round" aria-hidden>
              <path d="m6 9.5 6 6 6-6" />
            </svg>
          </button>
    
          {open && (
            <div
              className={cx(
                "asta-ws-pop absolute left-0 right-0 top-full z-30 mt-1.5 min-w-[240px] origin-top rounded-[12px] bg-paper p-1.5",
                "animate-[asta-ws-in_.2s_cubic-bezier(.16,1,.3,1)_both]",
                "shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_10%,transparent),0_12px_32px_-12px_rgb(0_0_0/0.28),0_4px_10px_-6px_rgb(0_0_0/0.12)]",
              )}
            >
              {account && (
                <div className="px-2.5 pb-2 pt-1.5">
                  <p className="truncate text-[13px] font-medium text-ink">{account.name}</p>
                  {account.email && <p className="truncate text-[12px] text-mute">{account.email}</p>}
                </div>
              )}
              <div
                ref={listRef}
                id={id}
                role="listbox"
                tabIndex={-1}
                aria-label="Workspaces"
                aria-activedescendant={`${id}-${active}`}
                onKeyDown={onListKey}
                className="outline-none"
              >
                {workspaces.map((w, i) => (
                  <div
                    key={w.id}
                    id={`${id}-${i}`}
                    role="option"
                    aria-selected={w.id === current.id}
                    onPointerMove={() => setActive(i)}
                    onClick={() => choose(i)}
                    className={cx("flex cursor-pointer items-center gap-2.5 rounded-[8px] px-2 py-1.5 transition-colors duration-100", active === i && "bg-[color-mix(in_srgb,var(--color-ink)_6%,transparent)]")}
                  >
                    <Logo ws={w} size="md" />
                    <span className="min-w-0 flex-1">
                      <span className="flex items-center gap-1.5">
                        <span className="truncate text-[13px] font-medium text-ink">{w.name}</span>
                        {w.plan && <span className="shrink-0 text-[11px] text-mute">{w.plan}</span>}
                      </span>
                      {w.meta && <span className="block truncate text-[12px] text-mute">{w.meta}</span>}
                    </span>
                    {w.id === current.id && <Check />}
                  </div>
                ))}
                {onCreate && (
                  <div
                    id={`${id}-${workspaces.length}`}
                    role="option"
                    aria-selected={false}
                    onPointerMove={() => setActive(workspaces.length)}
                    onClick={() => choose(workspaces.length)}
                    className={cx("mt-1 flex cursor-pointer items-center gap-2.5 rounded-[8px] px-2 py-1.5 text-[13px] text-ink-2 transition-colors duration-100", active === workspaces.length && "bg-[color-mix(in_srgb,var(--color-ink)_6%,transparent)] text-ink")}
                  >
                    <span className="grid h-8 w-8 place-items-center rounded-[8px] shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_14%,transparent)]">
                      <svg viewBox="0 0 24 24" className="h-4 w-4" fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round" strokeLinejoin="round" aria-hidden>
                        <path d="M12 5v14" />
                        <path d="M5 12h14" />
                      </svg>
                    </span>
                    {createLabel}
                  </div>
                )}
              </div>
              {footer && <div className="mt-1 pt-1">{footer}</div>}
            </div>
          )}
        </div>
      );
    }
    
  4. Or ask your AI agent

    Paste this into Claude Code, Cursor, Codex, v0 or Lovable. Fill in the last line. More in Prompts.

    Add the Astabyte UI Workspace switcher component to my project.
    
    The workspace picker atop a sidebar.
    It includes: account, plan tag, logo or initial, new workspace, arrow keys, click outside.
    
    Install it:
    ```sh
    pnpm dlx asta-ui@latest add workspace-switcher --radix
    ```
    Or with the shadcn CLI:
    ```sh
    pnpm dlx shadcn@latest add https://ui.astabyte.com/r/workspace-switcher.json
    ```
    If the project isn't set up yet, run `pnpm dlx asta-ui@latest init --radix` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4).
    
    Import it like this:
    ```tsx
    import { WorkspaceSwitcher, type Workspace } from "@/components/astabyte/workspace-switcher";
    ```
    
    Usage example and props: https://ui.astabyte.com/workspace-switcher
    Full catalogue for context: https://ui.astabyte.com/llms.txt
    
    Rules:
    - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS.
    - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback.
    - Keep keyboard, focus and ARIA behaviour.
    - Use fake data and fake brand names in examples.
    
    Use it to: [describe where it goes and what it should do]
  5. Or use plain HTML + CSS

    Not on React? Use it from Django, Rails, Laravel, Go html/template, htmx or a static page. Load asta.css and asta.js once (the head.html tab), then paste the markup. The ui.astabyte.com/cdn link is temporary: pin a version from jsDelivr for production. More in HTML + CSS.

    <!-- Needs asta.js: opens the menu and copies the picked workspace name, plan and logo into the trigger. -->
    <div><div style="width:16rem;max-width:100%;border-radius:12px;background:var(--paper);padding:8px">
      <div class="asta-ws" data-asta-dropdown data-asta-ws data-match>
        <button type="button" class="asta-ws-trigger" data-asta-dropdown-trigger aria-haspopup="menu" aria-expanded="false" id="ex-ws-one-trigger" aria-controls="ex-ws-one">
          <span data-ws-logo-out class="asta-ws-logo-sm"><span aria-hidden="true" class="asta-ws-initial">A</span></span>
          <span data-ws-name-out class="asta-ws-name">Astabyte</span>
          <span data-ws-plan-out class="asta-ws-plan">Pro</span>
          <svg class="asta-ws-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m4 6 4 4 4-4"/></svg>
        </button>
        <div role="menu" popover="manual" tabindex="-1" data-asta-mpanel aria-label="Workspaces" id="ex-ws-one" class="asta-float asta-dropdown-panel asta-ws-panel">
          <div class="asta-ws-account"><p>Sam Rivera</p><p>[email protected]</p></div>
          <button type="button" role="menuitemradio" tabindex="-1" aria-checked="true" data-group="workspace" data-value="studio" data-ws-name="Astabyte" data-ws-plan="Pro" class="asta-ws-row"><span data-ws-logo-box class="asta-ws-logo"><span aria-hidden="true" class="asta-ws-initial">A</span></span><span class="asta-ws-row-text"><span><span class="asta-ws-row-name">Astabyte</span><span class="asta-ws-row-plan">Pro</span></span><span class="asta-ws-row-meta">12 members</span></span><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3.5 8.5l3 3 6-7"/></svg></button>
          <button type="button" role="menuitemradio" tabindex="-1" aria-checked="false" data-group="workspace" data-value="labs" data-ws-name="Astabyte Labs" data-ws-plan="" class="asta-ws-row"><span data-ws-logo-box class="asta-ws-logo"><span aria-hidden="true" class="asta-ws-initial">A</span></span><span class="asta-ws-row-text"><span><span class="asta-ws-row-name">Astabyte Labs</span></span><span class="asta-ws-row-meta">3 members</span></span><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3.5 8.5l3 3 6-7"/></svg></button>
          <button type="button" role="menuitemradio" tabindex="-1" aria-checked="false" data-group="workspace" data-value="client" data-ws-name="Client work" data-ws-plan="Team" class="asta-ws-row"><span data-ws-logo-box class="asta-ws-logo"><span aria-hidden="true" class="asta-ws-initial">C</span></span><span class="asta-ws-row-text"><span><span class="asta-ws-row-name">Client work</span><span class="asta-ws-row-plan">Team</span></span><span class="asta-ws-row-meta">8 members</span></span><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3.5 8.5l3 3 6-7"/></svg></button>
          <a role="menuitem" tabindex="-1" href="#workspace-switcher" class="asta-menu-item asta-ws-create"><span class="asta-menu-item-text"><span style="display:inline-flex;align-items:center;gap:10px"><span class="asta-ws-create-icon"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" aria-hidden="true"><path d="M8 3.5v9M3.5 8h9"/></svg></span>New workspace</span></span></a>
          <div class="asta-ws-footer"><div role="separator" class="asta-menu-separator"></div>
            <button type="button" role="menuitem" tabindex="-1" class="asta-menu-item"><span class="asta-menu-item-icon"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"><path d="M6 2.5H3v11h3M10 5l3 3-3 3M13 8H6"/></svg></span><span class="asta-menu-item-text">Sign out</span></button>
          </div>
        </div>
      </div>
    </div></div>

Usage

The demo above, as code.

"use client";

import { useState } from "react";
import { WorkspaceSwitcher, type Workspace } from "@/components/astabyte/workspace-switcher";

const WORKSPACES: Workspace[] = [
  { id: "studio", name: "Astabyte Studio", plan: "Pro", meta: "12 members" },
  { id: "labs", name: "Astabyte Labs", plan: "Team", meta: "4 members" },
  { id: "me", name: "Personal", plan: "Free", meta: "Just you" },
];

const NAV = ["Recents", "Drafts", "All projects", "Trash"];

export default function WorkspaceSwitcherDemo() {
  const [ws, setWs] = useState("studio");
  const [extra, setExtra] = useState(0);
  const list = [...WORKSPACES, ...Array.from({ length: extra }, (_, i) => ({ id: `new-${i}`, name: `Untitled workspace ${i + 1}`, plan: "Free", meta: "Just you" }))];
  const current = list.find((w) => w.id === ws)!;
  return (
    <div className="flex min-h-[460px] w-full items-start justify-center p-6 sm:p-10">
      <div className="flex h-[380px] w-full max-w-2xl overflow-hidden rounded-[14px] shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_10%,transparent)]">
        <aside className="w-64 shrink-0 bg-paper-2 p-2">
          <WorkspaceSwitcher
            workspaces={list}
            value={ws}
            onChange={setWs}
            onCreate={() => {
              setExtra((n) => n + 1);
              setWs(`new-${extra}`);
            }}
            account={{ name: "Rina Putri", email: "[email protected]" }}
          />
          <nav className="mt-3 space-y-0.5">
            {NAV.map((n, i) => (
              <span key={n} className={`block rounded-[8px] px-2.5 py-1.5 text-[13px] ${i === 0 ? "bg-[color-mix(in_srgb,var(--color-ink)_7%,transparent)] text-ink" : "text-ink-2"}`}>
                {n}
              </span>
            ))}
          </nav>
        </aside>
        <main className="min-w-0 flex-1 bg-paper p-5">
          <p className="text-[15px] font-medium text-ink">{current.name}</p>
          <p className="mt-1 text-[13px] text-mute">{current.meta}. Open the menu on the left, or focus it and press ↓.</p>
        </main>
      </div>
    </div>
  );
}