aiellie

Search

Find a component, template or page

GitHub
Components

Branches Menu

A small button that shows the branch checked out, opening a searchable list of branches with a row pinned at the bottom that creates and checks out the branch typed into the search.

Open full screen
fig. 01 · open it full screen from the corner

Installation

npx shadcn@latest add "@aiellie/branches-menu"

shadcn needs the @aiellie registry in components.json first. npx aiellie add branches-menu adds it on its first run, then installs the same way.

Branches menu picks the git branch a task works on. The trigger shows the branch checked out; the menu lists the others with a search, and whatever is typed into the search can become a new branch from the row pinned under the list.

Getting started

  1. Pass the branches and the one checked out

    It is controlled. onValueChange checks out an existing branch and onCreate gets the name typed into the search.

    components/task-branch.tsx
    "use client"
    
    import * as React from "react"
    
    import { BranchesMenu } from "@/components/aiellie/branches-menu"
    
    export function TaskBranch({ initial }: { initial: string[] }) {
      const [branches, setBranches] = React.useState(initial)
      const [branch, setBranch] = React.useState(initial[0])
    
      return (
        <BranchesMenu
          branches={branches}
          value={branch}
          onValueChange={setBranch}
          onCreate={(name) => {
            setBranches((all) => [name, ...all])
            setBranch(name)
          }}
        />
      )
    }

Anatomy

<button data-slot="branches-menu"> {/* render, a small ghost Button by default */}
  <svg /> {/* a branch */}
  <span class="sr-only">{label}: </span>
  {value}
</button>

<div data-slot="menu-content">
  <input /> {/* the search, which is also the new branch's name */}
  <div data-slot="menu-radio-group">
    <div data-slot="menu-radio-item" /> {/* one per branch, the checked-out one ticked */}
  </div>
  <div data-slot="menu-footer">
    <div data-slot="menu-item" /> {/* Create and check out “name” */}
  </div>
</div>

Branch names are set in mono. The create row is pinned under the list, so the search never filters it out; it stays disabled until the search holds a name no branch has, since an existing name would check that branch out instead. Choosing a branch closes the menu.

Examples

A different trigger

render replaces the trigger's button, here with an outlined one. The menu opens from wherever it sits.

Something other than a branch

label is read out before the value, so a screen reader hears what is being chosen. Change it when the list holds something else, like worktrees.

<BranchesMenu label="Worktree" {...props} />

API reference

BranchesMenu

PropTypeDefaultDescription
branchesstring[]requiredEvery branch, in the order to list them.
valuestringrequiredThe branch checked out.
onValueChange(value: string) => voidrequiredCalled with the branch chosen.
onCreate(name: string) => voidrequiredCalled with the name typed into the search, to create that branch and check it out.
labelstring"Branch"What is being chosen, read out ahead of the branch's name.
renderReactElement<Button variant="ghost" size="sm" />The trigger's element.
side / alignMenuContent placementWhere the menu opens.

All other MenuTrigger props are forwarded to the trigger.