Switcher

Switcher dropdown for the current workspace, project, account or team: an outline button showing the current option with avatar and badge opens a menu of grouped radio items with descriptions and an optional create item. Controlled or uncontrolled.

Installation

npx shadcn@latest add @01orbital/switcher

Usage

"use client"

import { useState } from "react"

import { Switcher } from "@/components/switcher/switcher"
import { DemoLabel } from "@/components/text/demo-label"

// The copy: change text here, not in the markup.
const COPY = {
  active: "Projects",
  archived: "Archived",
  create: "New project",
  untitled: (n: number) => `Untitled project ${n}`,
  draft: "Draft",
  live: "Live",
  caption:
    "Open: the current project on the button, the others grouped in the menu",
} as const

const PROJECTS = [
  {
    id: "tin-roof",
    name: "Tin Roof",
    description: "Field recordings, 14 scenes",
    badge: COPY.live,
  },
  { id: "vesper", name: "Vesper Dusk", description: "Ambient score, 6 scenes" },
  {
    id: "low-tide",
    name: "Low Tide",
    description: "Podcast intro, 2 scenes",
    badge: COPY.draft,
  },
]

const ARCHIVED = [
  { id: "glasshouse", name: "Glasshouse", description: "Closed in October" },
]

/** A project picker: the current project on the button, the others grouped
 * in the menu, and New project adds one and switches to it. */
export default function SwitcherDemo() {
  const [projects, setProjects] = useState(PROJECTS)
  const [value, setValue] = useState(PROJECTS[0].id)

  return (
    <div className="grid w-full max-w-sm gap-4 p-6">
      <DemoLabel>{COPY.caption}</DemoLabel>
      <Switcher
        defaultOpen
        groups={[
          { label: COPY.active, options: projects },
          { label: COPY.archived, options: ARCHIVED },
        ]}
        value={value}
        onValueChange={setValue}
        createLabel={COPY.create}
        onCreate={() => {
          const id = `project-${projects.length + 1}`
          setProjects((current) => [
            ...current,
            {
              id,
              name: COPY.untitled(current.length - PROJECTS.length + 1),
              description: COPY.draft,
            },
          ])
          setValue(id)
        }}
      />
    </div>
  )
}

This example installs with everything it uses:

npx shadcn@latest add @01orbital/switcher-demo

API reference

A picker for what the app is showing: a workspace, project, account or team. An outline button shows the current option with its avatar and badge, and opens a menu of grouped options, with an optional create item last. Controlled with value and onValueChange, or seeded with defaultValue. The props not listed here go to the trigger button.

Prop

Type

It also accepts the props of the Base UI part it wraps, and the HTML attributes of the element it renders.

Dependencies

npm: @phosphor-icons/react, cn. The CLI installs them for you.

Requires these pieces. The CLI adds each one that is missing and won't replace a file you already have unless you choose to:

The shadcn parts here are their Base UI versions, and this piece uses Base UI's API. A Radix version you already have won't work with it.

Used in blocks

One block is built with Switcher.

Last updated on