opensourceui
GitHubTwitter/X
  1. Home
  2. Components
  3. Dropdowns
  4. Workspace Switcher
Back to Dropdowns

Components / Dropdowns

Workspace Switcher

Free Dropdowns React component — WorkspaceSwitcherDropdown. MIT licensed, copy-paste ready for Next.js and Tailwind CSS.

Rectangular org bar that expands inline — workspace list opens inside the same bordered box, not a floating panel.

Preview

Setup

How to use

Free for personal and commercial use. No UI attribution required. Include the MIT copyright notice when copying component source into your project. Read the MIT license.

  1. 1

    Run in your terminal:

    $npm install clsx tailwind-merge lucide-react
  2. 2

    Copy lib/cn.ts below. Skip if you already have cn().

  3. 3

    Copy the code below and create components/dropdowns/workspace-switcher-dropdown.tsx in your project.

  4. 4

    Import and render:

    Example

    import { WorkspaceSwitcherDropdown } from "@/components/dropdowns/workspace-switcher-dropdown";

    <WorkspaceSwitcherDropdown onWorkspaceChange={(w) => console.log(w.id)} />

lib/cn.ts

1
2
3
4
5
6
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
 
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}

components/dropdowns/workspace-switcher-dropdown.tsx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
"use client";
 
import {
forwardRef,
useEffect,
useId,
useRef,
useState,
cloneElement,
type ComponentPropsWithoutRef,
type ReactElement,
} from "react";
 
import { cn } from "@/lib/cn";
 
import { ChevronsUpDown, Plus } from "lucide-react";
import { NextJs } from "@/icons/brands/next-js";
import { Vercel } from "@/icons/brands/vercel";
import { Github } from "@/icons/brands/github";
 
export type WorkspaceItem = Readonly<{
id: string;
name?: string;
icon?: ReactElement;
plan?: string;
active?: boolean;
separator?: boolean;
}>;
 
export type WorkspaceSwitcherDropdownProps = Readonly<
{
triggerAriaLabel?: string;
menuAriaLabel?: string;
createLabel?: string;
workspaces?: readonly WorkspaceItem[];
onWorkspaceChange?: (workspace: WorkspaceItem) => void;
onCreateWorkspace?: () => void;
} & ComponentPropsWithoutRef<"div">
>;
 
const defaultWorkspaces: readonly WorkspaceItem[] = [
{
id: "appui",
name: "App UI",
icon: <NextJs />,
plan: "Pro",
active: true,
},
{
id: "vercel",
name: "Vercel Team",
icon: <Vercel />,
plan: "Free",
},
{
id: "github",
name: "Open Source",
icon: <Github />,
plan: "Team",
},
{ id: "separator-create", separator: true },
{ id: "create", name: "Create workspace" },
];
 
type WorkspaceRowProps = Readonly<{
item: WorkspaceItem;
onSelect: (item: WorkspaceItem) => void;
createLabel: string;
}>;
 
function WorkspaceRow({ item, onSelect, createLabel }: WorkspaceRowProps) {
if (item.separator) {
return <hr className="border-0 border-t border-neutral-100" />;
}
 
const isCreate = item.id === "create";
 
return (
<button
type="button"
role="menuitem"
aria-label={isCreate ? createLabel : item.name}
aria-current={item.active ? "true" : undefined}
onClick={() => onSelect(item)}
className={cn(
"flex w-full cursor-pointer items-center gap-2.5 px-3 py-2 text-left text-[12px] font-medium transition-colors",
item.active
? "bg-neutral-50 text-neutral-900"
: "text-neutral-700 hover:bg-neutral-50",
)}
>
<span className="flex size-6 shrink-0 items-center justify-center text-neutral-500">
{isCreate ? (
<Plus size={14} strokeWidth={2} />
) : item.icon ? (
cloneElement(
item.icon as ReactElement<{ size?: number }>,
{ size: 14 },
)
) : null}
</span>
<span className="min-w-0 flex-1 truncate">
{isCreate ? createLabel : item.name}
</span>
{!isCreate && item.plan ? (
<span className="shrink-0 text-[10px] text-neutral-400">
{item.plan}
</span>
) : null}
</button>
);
}
 
// Org switcher — rectangular bar trigger, inline expand menu (same width).
export const WorkspaceSwitcherDropdown = forwardRef<
HTMLDivElement,
WorkspaceSwitcherDropdownProps
>(
(
{
triggerAriaLabel = "Switch workspace",
menuAriaLabel = "Workspace list",
createLabel = "Create workspace",
workspaces = defaultWorkspaces,
onWorkspaceChange,
onCreateWorkspace,
className,
...props
},
ref,
) => {
const [open, setOpen] = useState(false);
const rootRef = useRef<HTMLDivElement>(null);
const menuId = useId();
 
const activeWorkspace =
workspaces.find((w) => w.active && !w.separator) ??
workspaces.find((w) => !w.separator && w.id !== "create");
 
useEffect(() => {
const closeOnOutside = (event: globalThis.MouseEvent) => {
if (!rootRef.current?.contains(event.target as Node)) {
setOpen(false);
}
};
 
const closeOnEscape = (event: KeyboardEvent) => {
if (event.key === "Escape") setOpen(false);
};
 
document.addEventListener("mousedown", closeOnOutside);
document.addEventListener("keydown", closeOnEscape);
return () => {
document.removeEventListener("mousedown", closeOnOutside);
document.removeEventListener("keydown", closeOnEscape);
};
}, []);
 
const handleWorkspaceSelect = (item: WorkspaceItem) => {
setOpen(false);
if (item.id === "create") {
onCreateWorkspace?.();
} else {
onWorkspaceChange?.(item);
}
};
 
const toggleOpen = () => setOpen((prev) => !prev);
 
return (
<div
ref={ref}
data-slot="workspace-switcher-dropdown"
className={cn("relative inline-block font-sans", className)}
{...props}
>
<div
ref={rootRef}
className={cn(
"w-56 overflow-hidden rounded-lg border border-neutral-200 bg-white",
open && "shadow-[0_12px_32px_-12px_rgba(0,0,0,0.1)]",
)}
>
<button
type="button"
aria-label={`${triggerAriaLabel}: ${activeWorkspace?.name ?? "Workspace"}`}
aria-expanded={open}
aria-haspopup="menu"
aria-controls={open ? menuId : undefined}
onClick={toggleOpen}
onKeyDown={(event) => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
toggleOpen();
}
}}
className="flex w-full cursor-pointer items-center gap-2.5 px-3 py-2.5 text-left"
>
<span className="flex size-7 shrink-0 items-center justify-center rounded-md border border-neutral-100 bg-neutral-50">
{activeWorkspace?.icon ? (
cloneElement(
activeWorkspace.icon as ReactElement<{ size?: number }>,
{ size: 15 },
)
) : (
<span className="text-[10px] font-semibold text-neutral-500">
{activeWorkspace?.name?.charAt(0) ?? "W"}
</span>
)}
</span>
 
<span className="min-w-0 flex-1">
<span className="block truncate text-[12px] font-medium text-neutral-800">
{activeWorkspace?.name ?? "Workspace"}
</span>
{activeWorkspace?.plan ? (
<span className="mt-0.5 block truncate text-[10px] text-neutral-400">
{activeWorkspace.plan} plan
</span>
) : null}
</span>
 
<ChevronsUpDown
size={14}
className="shrink-0 text-neutral-400"
/>
</button>
 
{open ? (
<div
id={menuId}
role="menu"
aria-label={menuAriaLabel}
className="border-t border-neutral-100 py-1"
>
{workspaces.map((item) => (
<WorkspaceRow
key={item.id}
item={item}
createLabel={createLabel}
onSelect={handleWorkspaceSelect}
/>
))}
</div>
) : null}
</div>
</div>
);
},
);
 
WorkspaceSwitcherDropdown.displayName = "WorkspaceSwitcherDropdown";
PreviousNext

On this

page

Jump to a section on this page.

  • lib/cn.ts
  • components/dropdowns/workspace-switcher-dropdown.tsx

Sponsor spot · demo preview

Logo

Your brand name

Your tagline or category

A short pitch about your product or service goes here. This is a preview of how sponsor cards will look in this sidebar.

Your call to action

This slot is available. Sponsor Opensource UI and reach developers browsing components every day.