@spunto/design-system 0.70.0 → 0.71.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -115,6 +115,15 @@ const nextConfig = { transpilePackages: ["@spunto/design-system"] }
115
115
  presentational: it never knows what an entry *does*. For what a list can't say (a
116
116
  submenu, a checkbox item), compose Base UI directly — that's what `WorkerCard`'s
117
117
  `actions` slot is for.
118
+ - _Sidebar_ — the app shell's rail: `SidebarProvider` (open state, ⌘B, cookie, sheet on
119
+ mobile; `mobileCloseKey={pathname}` closes it on navigation) + `Sidebar`
120
+ (`collapsible` offcanvas / icon / none, `variant` sidebar / floating / inset) and its
121
+ parts (`SidebarHeader`/`Content`/`Footer`, `SidebarGroup*`, `SidebarMenu*`,
122
+ `SidebarMenuSub*`, `SidebarTrigger`, `SidebarRail`, `SidebarInset`, `useSidebar`), plus its
123
+ ends: `SidebarBrand` (logo, « Spunto », the product as subtitle), `SidebarOrgSwitcher` (header) and `SidebarUserMenu` (footer: avatar + a menu of entries,
124
+ e.g. account settings and sign out).
125
+ Router-agnostic: a link is `render={<Link />}` on `SidebarMenuButton`. Colors come from
126
+ the `--sidebar*` tokens, so it follows `data-product`.
118
127
  - _Sheet_ — `Sheet` (+`SheetTrigger`/`SheetClose`/`SheetContent`/`SheetHeader`/
119
128
  `SheetFooter`/`SheetTitle`/`SheetDescription`), the same Base UI dialog entering from
120
129
  an edge. `side` (4) decides the edge, `size` (`sm`…`full`) how far it comes in — two
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spunto/design-system",
3
- "version": "0.70.0",
3
+ "version": "0.71.0",
4
4
  "description": "Spunto's shared design system — green tokens (conservatory + sprout), color constants, and UI primitives.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -0,0 +1,211 @@
1
+ "use client"
2
+
3
+ import { cloneElement, type ReactElement, type ReactNode } from "react"
4
+ import { CheckIcon, ChevronsUpDownIcon, PlusIcon } from "lucide-react"
5
+
6
+ import { cn } from "../utils"
7
+ import { ActionMenu, type ActionMenuEntry, type ActionMenuLinkRender } from "./action-menu"
8
+ import { SpuntoLogo } from "../brand/spunto-logo"
9
+ import { Avatar, AvatarFallback, AvatarImage } from "./avatar"
10
+
11
+ // The two controls every app puts at the ends of its sidebar: who the workspace is (top) and who
12
+ // is looking at it (bottom). Both are an `ActionMenu` behind a full-width button, so they inherit its
13
+ // contract: entries are data, `href` entries are real anchors, the app's router link comes in through
14
+ // `render.link`, and nothing here knows what an entry does.
15
+
16
+ function Monogram({ name, className }: { name: string; className?: string }) {
17
+ return (
18
+ <span
19
+ className={cn(
20
+ "flex shrink-0 items-center justify-center rounded-md bg-linear-to-br from-primary/25 to-primary/5 font-display font-bold text-primary ring-1 ring-primary/15",
21
+ className
22
+ )}
23
+ aria-hidden
24
+ >
25
+ {name.slice(0, 1).toUpperCase()}
26
+ </span>
27
+ )
28
+ }
29
+
30
+ export interface SidebarOrg {
31
+ id: string
32
+ name: string
33
+ /** Shown under the name of the current org (`owner`, `admin`…). */
34
+ role?: string
35
+ /** Where picking this org goes. */
36
+ href: string
37
+ }
38
+
39
+ export interface SidebarOrgSwitcherProps {
40
+ orgs: SidebarOrg[]
41
+ /** Id of the org the sidebar is showing. */
42
+ currentId: string
43
+ /** Adds a « New organization » entry under the list. */
44
+ newOrg?: { href: string; label?: string }
45
+ /** Role line when the current org has none. @default "organization" */
46
+ fallbackRole?: string
47
+ render?: { link?: ActionMenuLinkRender }
48
+ className?: string
49
+ }
50
+
51
+ /**
52
+ * The organization at the top of the sidebar: its monogram, name and role, and the list of the
53
+ * others behind it. Meant for `SidebarHeader`.
54
+ */
55
+ export function SidebarOrgSwitcher({
56
+ orgs,
57
+ currentId,
58
+ newOrg,
59
+ fallbackRole = "organization",
60
+ render,
61
+ className,
62
+ }: SidebarOrgSwitcherProps) {
63
+ const current = orgs.find((o) => o.id === currentId)
64
+ if (!current) return null
65
+
66
+ const items: ActionMenuEntry[] = [
67
+ ...orgs.map((org) => ({
68
+ key: org.id,
69
+ href: org.href,
70
+ label: (
71
+ <span className="flex items-center gap-2.5">
72
+ <Monogram name={org.name} className="size-6 text-[10px]" />
73
+ <span className="truncate font-medium">{org.name}</span>
74
+ </span>
75
+ ),
76
+ shortcut: org.id === currentId ? <CheckIcon className="size-3.5 text-primary" /> : undefined,
77
+ })),
78
+ newOrg && "separator",
79
+ newOrg && { key: "new-org", href: newOrg.href, icon: PlusIcon, label: newOrg.label ?? "New organization" },
80
+ ]
81
+
82
+ return (
83
+ <ActionMenu
84
+ items={items}
85
+ align="start"
86
+ side="bottom"
87
+ sideOffset={6}
88
+ className="w-60"
89
+ render={render}
90
+ trigger={
91
+ <button
92
+ type="button"
93
+ data-slot="sidebar-org-switcher"
94
+ className={cn(
95
+ "group flex w-full items-center gap-2.5 rounded-md border border-sidebar-border bg-sidebar-accent/40 px-2.5 py-2 text-left outline-none transition-colors hover:bg-sidebar-accent focus-visible:ring-2 focus-visible:ring-sidebar-ring",
96
+ className
97
+ )}
98
+ >
99
+ <Monogram name={current.name} className="size-7 text-xs" />
100
+ <span className="min-w-0 flex-1">
101
+ <span className="block truncate text-[13px] leading-tight font-semibold text-sidebar-foreground">{current.name}</span>
102
+ <span className="mt-0.5 block font-mono text-[9px] leading-tight tracking-[0.16em] text-muted-foreground/60 uppercase">
103
+ {current.role ?? fallbackRole}
104
+ </span>
105
+ </span>
106
+ <ChevronsUpDownIcon className="size-3.5 shrink-0 text-muted-foreground/50" />
107
+ </button>
108
+ }
109
+ />
110
+ )
111
+ }
112
+
113
+ export interface SidebarUser {
114
+ name?: string | null
115
+ email?: string | null
116
+ picture?: string | null
117
+ }
118
+
119
+ export interface SidebarUserMenuProps {
120
+ user: SidebarUser
121
+ /** The menu: account settings, theme, sign out… A destructive last entry is the convention. */
122
+ items: ActionMenuEntry[]
123
+ render?: { link?: ActionMenuLinkRender }
124
+ /** Replaces the default « name / email » block when the sidebar has no room for it. */
125
+ children?: ReactNode
126
+ className?: string
127
+ }
128
+
129
+ /**
130
+ * The signed-in member at the bottom of the sidebar — avatar, name and email — opening a menu of
131
+ * what they can do with their account. Meant for `SidebarFooter`.
132
+ */
133
+ export function SidebarUserMenu({ user, items, render, children, className }: SidebarUserMenuProps) {
134
+ const label = user.name || user.email || ""
135
+
136
+ return (
137
+ <ActionMenu
138
+ items={items}
139
+ align="start"
140
+ side="top"
141
+ sideOffset={6}
142
+ className="w-60"
143
+ render={render}
144
+ trigger={
145
+ <button
146
+ type="button"
147
+ data-slot="sidebar-user-menu"
148
+ className={cn(
149
+ "flex w-full items-center gap-2.5 rounded-md px-2 py-2 text-left outline-none transition-colors hover:bg-sidebar-accent focus-visible:ring-2 focus-visible:ring-sidebar-ring data-popup-open:bg-sidebar-accent",
150
+ className
151
+ )}
152
+ >
153
+ <Avatar size="sm" className="shrink-0">
154
+ <AvatarImage src={user.picture ?? undefined} alt="" />
155
+ <AvatarFallback className="bg-primary/15 font-semibold text-primary">{label.slice(0, 2).toUpperCase()}</AvatarFallback>
156
+ </Avatar>
157
+ {children ?? (
158
+ <span className="min-w-0 flex-1">
159
+ <span className="block truncate text-xs leading-none font-medium text-sidebar-foreground">{user.name ?? user.email}</span>
160
+ {user.name && user.email && (
161
+ <span className="mt-1 block truncate text-[11px] text-muted-foreground/70">{user.email}</span>
162
+ )}
163
+ </span>
164
+ )}
165
+ <ChevronsUpDownIcon className="size-3.5 shrink-0 text-muted-foreground/50" />
166
+ </button>
167
+ }
168
+ />
169
+ )
170
+ }
171
+
172
+ export interface SidebarBrandProps {
173
+ /** The product this app is — « Cloud », « CMS », « Lite »… — as the subtitle under the name. */
174
+ product?: string
175
+ /** The company line. @default "Spunto" */
176
+ name?: string
177
+ /** Replaces the default logo (`SpuntoLogo` in the product's colours). */
178
+ logo?: ReactNode
179
+ /** Makes the whole block a link (to the marketing site, the app's home…). */
180
+ href?: string
181
+ /** Renders the link with the app's router link; without it, a plain anchor. */
182
+ render?: (props: { href: string }) => ReactElement
183
+ className?: string
184
+ }
185
+
186
+ /**
187
+ * The very top of a sidebar: the Spunto mark, « Spunto », and which product this is underneath
188
+ * (`product="Cloud"`, `"CMS"`…) — so every app of the family opens the same way and only the
189
+ * subtitle (and the `data-product` colours) change. Meant for the top of `SidebarHeader`, above the
190
+ * org switcher.
191
+ */
192
+ export function SidebarBrand({ product, name = "Spunto", logo, href, render, className }: SidebarBrandProps) {
193
+ const content = (
194
+ <>
195
+ {logo ?? <SpuntoLogo tone="product" title={name} className="size-8 shrink-0 rounded-lg ring-1 ring-sidebar-border" />}
196
+ <span className="min-w-0">
197
+ <span className="block truncate font-display text-[15px] leading-none tracking-tight text-sidebar-foreground">{name}</span>
198
+ {product && (
199
+ <span className="mt-1 block truncate font-mono text-[10px] leading-none tracking-[0.18em] text-muted-foreground uppercase">
200
+ {product}
201
+ </span>
202
+ )}
203
+ </span>
204
+ </>
205
+ )
206
+ const classes = cn("flex items-center gap-2.5 px-2 py-1 text-left", className)
207
+
208
+ if (!href) return <div data-slot="sidebar-brand" className={classes}>{content}</div>
209
+ const link = render?.({ href }) ?? <a href={href} />
210
+ return cloneElement(link as ReactElement<Record<string, unknown>>, { "data-slot": "sidebar-brand", className: cn(classes, "outline-none focus-visible:ring-2 focus-visible:ring-sidebar-ring rounded-md") }, content)
211
+ }
@@ -0,0 +1,61 @@
1
+ // @vitest-environment jsdom
2
+ import { act, cleanup, render, screen } from "@testing-library/react"
3
+ import { afterEach, describe, expect, it } from "vitest"
4
+ import {
5
+ Sidebar,
6
+ SidebarContent,
7
+ SidebarMenu,
8
+ SidebarMenuButton,
9
+ SidebarMenuItem,
10
+ SidebarProvider,
11
+ useSidebar,
12
+ } from "./sidebar"
13
+
14
+ afterEach(cleanup)
15
+
16
+ function State() {
17
+ const { state, toggleSidebar } = useSidebar()
18
+ return <button onClick={toggleSidebar}>{state}</button>
19
+ }
20
+
21
+ function App({ cookieName }: { cookieName?: string | null }) {
22
+ return (
23
+ <SidebarProvider cookieName={cookieName}>
24
+ <Sidebar collapsible="none">
25
+ <SidebarContent>
26
+ <SidebarMenu>
27
+ <SidebarMenuItem>
28
+ <SidebarMenuButton isActive render={<a href="/agents" />}>
29
+ Agents
30
+ </SidebarMenuButton>
31
+ </SidebarMenuItem>
32
+ </SidebarMenu>
33
+ </SidebarContent>
34
+ </Sidebar>
35
+ <State />
36
+ </SidebarProvider>
37
+ )
38
+ }
39
+
40
+ describe("Sidebar", () => {
41
+ it("renders a menu button as the link it is given", () => {
42
+ render(<App />)
43
+ const link = screen.getByRole("link", { name: "Agents" })
44
+ expect(link.tagName).toBe("A")
45
+ expect(link.hasAttribute("data-active")).toBe(true)
46
+ })
47
+
48
+ it("toggles and remembers the state in its cookie, unless told not to", () => {
49
+ document.cookie = "sidebar_state=; max-age=0"
50
+ render(<App />)
51
+ act(() => screen.getByRole("button", { name: "expanded" }).click())
52
+ expect(screen.getByRole("button", { name: "collapsed" })).toBeTruthy()
53
+ expect(document.cookie).toContain("sidebar_state=false")
54
+
55
+ cleanup()
56
+ document.cookie = "sidebar_state=; max-age=0"
57
+ render(<App cookieName={null} />)
58
+ act(() => screen.getByRole("button", { name: "expanded" }).click())
59
+ expect(document.cookie).not.toContain("sidebar_state")
60
+ })
61
+ })
@@ -0,0 +1,755 @@
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+ import { mergeProps } from "@base-ui/react/merge-props"
5
+ import { useRender } from "@base-ui/react/use-render"
6
+ import { cva, type VariantProps } from "class-variance-authority"
7
+ import { PanelLeftIcon } from "lucide-react"
8
+
9
+ import { cn } from "../utils"
10
+ import { Button } from "./button"
11
+ import { Input } from "./input"
12
+ import { Separator } from "./separator"
13
+ import {
14
+ Sheet,
15
+ SheetContent,
16
+ SheetDescription,
17
+ SheetHeader,
18
+ SheetTitle,
19
+ } from "./sheet"
20
+ import { Skeleton } from "./skeleton"
21
+ import { Tooltip } from "./tooltip"
22
+
23
+ const SIDEBAR_COOKIE_NAME = "sidebar_state"
24
+ const MOBILE_BREAKPOINT = 768
25
+ const SIDEBAR_COOKIE_MAX_AGE = 60 * 60 * 24 * 7
26
+ const SIDEBAR_WIDTH = "16rem"
27
+ const SIDEBAR_WIDTH_MOBILE = "18rem"
28
+ const SIDEBAR_WIDTH_ICON = "3rem"
29
+ const SIDEBAR_KEYBOARD_SHORTCUT = "b"
30
+
31
+ type SidebarContextProps = {
32
+ state: "expanded" | "collapsed"
33
+ open: boolean
34
+ setOpen: (open: boolean) => void
35
+ openMobile: boolean
36
+ setOpenMobile: (open: boolean) => void
37
+ isMobile: boolean
38
+ toggleSidebar: () => void
39
+ }
40
+
41
+ function useIsMobile() {
42
+ const [isMobile, setIsMobile] = React.useState(false)
43
+
44
+ React.useEffect(() => {
45
+ if (typeof window.matchMedia !== "function") return
46
+ const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`)
47
+ const onChange = () => setIsMobile(mql.matches)
48
+ mql.addEventListener("change", onChange)
49
+ onChange()
50
+ return () => mql.removeEventListener("change", onChange)
51
+ }, [])
52
+
53
+ return isMobile
54
+ }
55
+
56
+ const SidebarContext = React.createContext<SidebarContextProps | null>(null)
57
+
58
+ function useSidebar() {
59
+ const context = React.useContext(SidebarContext)
60
+ if (!context) {
61
+ throw new Error("useSidebar must be used within a SidebarProvider.")
62
+ }
63
+
64
+ return context
65
+ }
66
+
67
+ function SidebarProvider({
68
+ defaultOpen = true,
69
+ open: openProp,
70
+ onOpenChange: setOpenProp,
71
+ cookieName = SIDEBAR_COOKIE_NAME,
72
+ mobileCloseKey,
73
+ className,
74
+ style,
75
+ children,
76
+ ...props
77
+ }: React.ComponentProps<"div"> & {
78
+ defaultOpen?: boolean
79
+ open?: boolean
80
+ onOpenChange?: (open: boolean) => void
81
+ /** Cookie that remembers the desktop state (read it server-side for `defaultOpen`). `null` = don't persist. */
82
+ cookieName?: string | null
83
+ /**
84
+ * On mobile the sidebar is a sheet over the page: once a navigation lands, keeping it open hides
85
+ * the page that was just asked for. Pass anything that changes with the route (the pathname) and
86
+ * the sheet closes when it does — the provider has no router of its own, so it works in any app.
87
+ * It also closes at once on a click on a link, even one to the current page.
88
+ */
89
+ mobileCloseKey?: unknown
90
+ }) {
91
+ const isMobile = useIsMobile()
92
+ const [openMobile, setOpenMobile] = React.useState(false)
93
+
94
+ // This is the internal state of the sidebar.
95
+ // We use openProp and setOpenProp for control from outside the component.
96
+ const [_open, _setOpen] = React.useState(defaultOpen)
97
+ const open = openProp ?? _open
98
+ const setOpen = React.useCallback(
99
+ (value: boolean | ((value: boolean) => boolean)) => {
100
+ const openState = typeof value === "function" ? value(open) : value
101
+ if (setOpenProp) {
102
+ setOpenProp(openState)
103
+ } else {
104
+ _setOpen(openState)
105
+ }
106
+
107
+ // This sets the cookie to keep the sidebar state.
108
+ if (cookieName) {
109
+ document.cookie = `${cookieName}=${openState}; path=/; max-age=${SIDEBAR_COOKIE_MAX_AGE}`
110
+ }
111
+ },
112
+ [setOpenProp, open, cookieName]
113
+ )
114
+
115
+ // Helper to toggle the sidebar.
116
+ const toggleSidebar = React.useCallback(() => {
117
+ return isMobile ? setOpenMobile((open) => !open) : setOpen((open) => !open)
118
+ }, [isMobile, setOpen, setOpenMobile])
119
+
120
+ // Covers navigations that don't go through a link too (a `router.push` from an org switcher).
121
+ React.useEffect(() => {
122
+ setOpenMobile(false)
123
+ }, [mobileCloseKey])
124
+
125
+ // Adds a keyboard shortcut to toggle the sidebar.
126
+ React.useEffect(() => {
127
+ const handleKeyDown = (event: KeyboardEvent) => {
128
+ if (
129
+ event.key === SIDEBAR_KEYBOARD_SHORTCUT &&
130
+ (event.metaKey || event.ctrlKey)
131
+ ) {
132
+ event.preventDefault()
133
+ toggleSidebar()
134
+ }
135
+ }
136
+
137
+ window.addEventListener("keydown", handleKeyDown)
138
+ return () => window.removeEventListener("keydown", handleKeyDown)
139
+ }, [toggleSidebar])
140
+
141
+ // We add a state so that we can do data-state="expanded" or "collapsed".
142
+ // This makes it easier to style the sidebar with Tailwind classes.
143
+ const state = open ? "expanded" : "collapsed"
144
+
145
+ const contextValue = React.useMemo<SidebarContextProps>(
146
+ () => ({
147
+ state,
148
+ open,
149
+ setOpen,
150
+ isMobile,
151
+ openMobile,
152
+ setOpenMobile,
153
+ toggleSidebar,
154
+ }),
155
+ [state, open, setOpen, isMobile, openMobile, setOpenMobile, toggleSidebar]
156
+ )
157
+
158
+ return (
159
+ <SidebarContext.Provider value={contextValue}>
160
+ <div
161
+ data-slot="sidebar-wrapper"
162
+ style={
163
+ {
164
+ "--sidebar-width": SIDEBAR_WIDTH,
165
+ "--sidebar-width-icon": SIDEBAR_WIDTH_ICON,
166
+ ...style,
167
+ } as React.CSSProperties
168
+ }
169
+ className={cn(
170
+ "group/sidebar-wrapper flex min-h-svh w-full has-data-[variant=inset]:bg-sidebar",
171
+ className
172
+ )}
173
+ {...props}
174
+ >
175
+ {children}
176
+ </div>
177
+ </SidebarContext.Provider>
178
+ )
179
+ }
180
+
181
+ function Sidebar({
182
+ side = "left",
183
+ variant = "sidebar",
184
+ collapsible = "offcanvas",
185
+ className,
186
+ children,
187
+ dir,
188
+ ...props
189
+ }: React.ComponentProps<"div"> & {
190
+ side?: "left" | "right"
191
+ variant?: "sidebar" | "floating" | "inset"
192
+ collapsible?: "offcanvas" | "icon" | "none"
193
+ }) {
194
+ const { isMobile, state, openMobile, setOpenMobile } = useSidebar()
195
+
196
+ if (collapsible === "none") {
197
+ return (
198
+ <div
199
+ data-slot="sidebar"
200
+ className={cn(
201
+ "flex h-full w-(--sidebar-width) flex-col bg-sidebar text-sidebar-foreground",
202
+ className
203
+ )}
204
+ {...props}
205
+ >
206
+ {children}
207
+ </div>
208
+ )
209
+ }
210
+
211
+ if (isMobile) {
212
+ return (
213
+ <Sheet open={openMobile} onOpenChange={setOpenMobile}>
214
+ <SheetContent
215
+ dir={dir}
216
+ data-sidebar="sidebar"
217
+ data-slot="sidebar"
218
+ data-mobile="true"
219
+ showClose={false}
220
+ className="w-(--sidebar-width) gap-0 border-sidebar-border bg-sidebar p-0 text-sidebar-foreground"
221
+ style={
222
+ {
223
+ "--sidebar-width": SIDEBAR_WIDTH_MOBILE,
224
+ } as React.CSSProperties
225
+ }
226
+ side={side}
227
+ >
228
+ <SheetHeader className="sr-only">
229
+ <SheetTitle>Sidebar</SheetTitle>
230
+ <SheetDescription>Displays the mobile sidebar.</SheetDescription>
231
+ </SheetHeader>
232
+ {/* Fermeture immédiate au clic sur un lien, sans attendre la fin de la navigation
233
+ (et y compris vers la page courante, où le pathname ne changera pas). */}
234
+ <div
235
+ className="flex h-full w-full flex-col"
236
+ onClick={(event) => {
237
+ const link = (event.target as HTMLElement).closest("a[href]")
238
+ if (link && link.getAttribute("target") !== "_blank") {
239
+ setOpenMobile(false)
240
+ }
241
+ }}
242
+ >
243
+ {children}
244
+ </div>
245
+ </SheetContent>
246
+ </Sheet>
247
+ )
248
+ }
249
+
250
+ return (
251
+ <div
252
+ className="group peer hidden text-sidebar-foreground md:block"
253
+ data-state={state}
254
+ data-collapsible={state === "collapsed" ? collapsible : ""}
255
+ data-variant={variant}
256
+ data-side={side}
257
+ data-slot="sidebar"
258
+ >
259
+ {/* This is what handles the sidebar gap on desktop */}
260
+ <div
261
+ data-slot="sidebar-gap"
262
+ className={cn(
263
+ "relative w-(--sidebar-width) bg-transparent transition-[width] duration-200 ease-linear",
264
+ "group-data-[collapsible=offcanvas]:w-0",
265
+ "group-data-[side=right]:rotate-180",
266
+ variant === "floating" || variant === "inset"
267
+ ? "group-data-[collapsible=icon]:w-[calc(var(--sidebar-width-icon)+(--spacing(4)))]"
268
+ : "group-data-[collapsible=icon]:w-(--sidebar-width-icon)"
269
+ )}
270
+ />
271
+ <div
272
+ data-slot="sidebar-container"
273
+ data-side={side}
274
+ className={cn(
275
+ "fixed inset-y-0 z-10 hidden h-svh w-(--sidebar-width) transition-[left,right,width] duration-200 ease-linear data-[side=left]:left-0 data-[side=left]:group-data-[collapsible=offcanvas]:left-[calc(var(--sidebar-width)*-1)] data-[side=right]:right-0 data-[side=right]:group-data-[collapsible=offcanvas]:right-[calc(var(--sidebar-width)*-1)] md:flex",
276
+ // Adjust the padding for floating and inset variants.
277
+ variant === "floating" || variant === "inset"
278
+ ? "p-2 group-data-[collapsible=icon]:w-[calc(var(--sidebar-width-icon)+(--spacing(4))+2px)]"
279
+ : "group-data-[collapsible=icon]:w-(--sidebar-width-icon) group-data-[side=left]:border-r group-data-[side=right]:border-l",
280
+ className
281
+ )}
282
+ {...props}
283
+ >
284
+ <div
285
+ data-sidebar="sidebar"
286
+ data-slot="sidebar-inner"
287
+ className="flex size-full flex-col bg-sidebar group-data-[variant=floating]:rounded-lg group-data-[variant=floating]:shadow-sm group-data-[variant=floating]:ring-1 group-data-[variant=floating]:ring-sidebar-border"
288
+ >
289
+ {children}
290
+ </div>
291
+ </div>
292
+ </div>
293
+ )
294
+ }
295
+
296
+ function SidebarTrigger({
297
+ className,
298
+ onClick,
299
+ ...props
300
+ }: React.ComponentProps<typeof Button>) {
301
+ const { toggleSidebar } = useSidebar()
302
+
303
+ return (
304
+ <Button
305
+ data-sidebar="trigger"
306
+ data-slot="sidebar-trigger"
307
+ variant="ghost"
308
+ size="icon-sm"
309
+ className={cn(className)}
310
+ onClick={(event) => {
311
+ onClick?.(event)
312
+ toggleSidebar()
313
+ }}
314
+ {...props}
315
+ >
316
+ <PanelLeftIcon />
317
+ <span className="sr-only">Toggle Sidebar</span>
318
+ </Button>
319
+ )
320
+ }
321
+
322
+ function SidebarRail({ className, ...props }: React.ComponentProps<"button">) {
323
+ const { toggleSidebar } = useSidebar()
324
+
325
+ return (
326
+ <button
327
+ data-sidebar="rail"
328
+ data-slot="sidebar-rail"
329
+ aria-label="Toggle Sidebar"
330
+ tabIndex={-1}
331
+ onClick={toggleSidebar}
332
+ title="Toggle Sidebar"
333
+ className={cn(
334
+ "absolute inset-y-0 z-20 hidden w-4 transition-all ease-linear group-data-[side=left]:-right-4 group-data-[side=right]:left-0 after:absolute after:inset-y-0 after:start-1/2 after:w-[2px] hover:after:bg-sidebar-border sm:flex ltr:-translate-x-1/2 rtl:-translate-x-1/2",
335
+ "in-data-[side=left]:cursor-w-resize in-data-[side=right]:cursor-e-resize",
336
+ "[[data-side=left][data-state=collapsed]_&]:cursor-e-resize [[data-side=right][data-state=collapsed]_&]:cursor-w-resize",
337
+ "group-data-[collapsible=offcanvas]:translate-x-0 group-data-[collapsible=offcanvas]:after:left-full hover:group-data-[collapsible=offcanvas]:bg-sidebar",
338
+ "[[data-side=left][data-collapsible=offcanvas]_&]:-right-2",
339
+ "[[data-side=right][data-collapsible=offcanvas]_&]:-left-2",
340
+ className
341
+ )}
342
+ {...props}
343
+ />
344
+ )
345
+ }
346
+
347
+ function SidebarInset({ className, ...props }: React.ComponentProps<"main">) {
348
+ return (
349
+ <main
350
+ data-slot="sidebar-inset"
351
+ className={cn(
352
+ "relative flex w-full flex-1 flex-col bg-background md:peer-data-[variant=inset]:m-2 md:peer-data-[variant=inset]:ml-0 md:peer-data-[variant=inset]:rounded-xl md:peer-data-[variant=inset]:shadow-sm md:peer-data-[variant=inset]:peer-data-[state=collapsed]:ml-2",
353
+ className
354
+ )}
355
+ {...props}
356
+ />
357
+ )
358
+ }
359
+
360
+ function SidebarInput({
361
+ className,
362
+ ...props
363
+ }: React.ComponentProps<typeof Input>) {
364
+ return (
365
+ <Input
366
+ data-slot="sidebar-input"
367
+ data-sidebar="input"
368
+ className={cn("h-8 w-full bg-background shadow-none", className)}
369
+ {...props}
370
+ />
371
+ )
372
+ }
373
+
374
+ function SidebarHeader({ className, ...props }: React.ComponentProps<"div">) {
375
+ return (
376
+ <div
377
+ data-slot="sidebar-header"
378
+ data-sidebar="header"
379
+ className={cn("flex flex-col gap-2 p-2", className)}
380
+ {...props}
381
+ />
382
+ )
383
+ }
384
+
385
+ function SidebarFooter({ className, ...props }: React.ComponentProps<"div">) {
386
+ return (
387
+ <div
388
+ data-slot="sidebar-footer"
389
+ data-sidebar="footer"
390
+ className={cn("flex flex-col gap-2 p-2", className)}
391
+ {...props}
392
+ />
393
+ )
394
+ }
395
+
396
+ function SidebarSeparator({
397
+ className,
398
+ ...props
399
+ }: React.ComponentProps<typeof Separator>) {
400
+ return (
401
+ <Separator
402
+ data-slot="sidebar-separator"
403
+ data-sidebar="separator"
404
+ className={cn("mx-2 w-auto bg-sidebar-border", className)}
405
+ {...props}
406
+ />
407
+ )
408
+ }
409
+
410
+ function SidebarContent({ className, ...props }: React.ComponentProps<"div">) {
411
+ return (
412
+ <div
413
+ data-slot="sidebar-content"
414
+ data-sidebar="content"
415
+ className={cn(
416
+ "flex min-h-0 flex-1 flex-col gap-0 overflow-auto group-data-[collapsible=icon]:overflow-hidden",
417
+ className
418
+ )}
419
+ {...props}
420
+ />
421
+ )
422
+ }
423
+
424
+ function SidebarGroup({ className, ...props }: React.ComponentProps<"div">) {
425
+ return (
426
+ <div
427
+ data-slot="sidebar-group"
428
+ data-sidebar="group"
429
+ className={cn("relative flex w-full min-w-0 flex-col p-2", className)}
430
+ {...props}
431
+ />
432
+ )
433
+ }
434
+
435
+ function SidebarGroupLabel({
436
+ className,
437
+ render,
438
+ ...props
439
+ }: useRender.ComponentProps<"div"> & React.ComponentProps<"div">) {
440
+ return useRender({
441
+ defaultTagName: "div",
442
+ props: mergeProps<"div">(
443
+ {
444
+ className: cn(
445
+ "flex h-8 shrink-0 items-center rounded-md px-2 text-xs font-medium text-sidebar-foreground/70 ring-sidebar-ring outline-hidden transition-[margin,opacity] duration-200 ease-linear group-data-[collapsible=icon]:-mt-8 group-data-[collapsible=icon]:opacity-0 focus-visible:ring-2 [&>svg]:size-4 [&>svg]:shrink-0",
446
+ className
447
+ ),
448
+ },
449
+ props
450
+ ),
451
+ render,
452
+ state: {
453
+ slot: "sidebar-group-label",
454
+ sidebar: "group-label",
455
+ },
456
+ })
457
+ }
458
+
459
+ function SidebarGroupAction({
460
+ className,
461
+ render,
462
+ ...props
463
+ }: useRender.ComponentProps<"button"> & React.ComponentProps<"button">) {
464
+ return useRender({
465
+ defaultTagName: "button",
466
+ props: mergeProps<"button">(
467
+ {
468
+ className: cn(
469
+ "absolute top-3.5 right-3 flex aspect-square w-5 items-center justify-center rounded-md p-0 text-sidebar-foreground ring-sidebar-ring outline-hidden transition-transform group-data-[collapsible=icon]:hidden after:absolute after:-inset-2 hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2 md:after:hidden [&>svg]:size-4 [&>svg]:shrink-0",
470
+ className
471
+ ),
472
+ },
473
+ props
474
+ ),
475
+ render,
476
+ state: {
477
+ slot: "sidebar-group-action",
478
+ sidebar: "group-action",
479
+ },
480
+ })
481
+ }
482
+
483
+ function SidebarGroupContent({
484
+ className,
485
+ ...props
486
+ }: React.ComponentProps<"div">) {
487
+ return (
488
+ <div
489
+ data-slot="sidebar-group-content"
490
+ data-sidebar="group-content"
491
+ className={cn("w-full text-sm", className)}
492
+ {...props}
493
+ />
494
+ )
495
+ }
496
+
497
+ function SidebarMenu({ className, ...props }: React.ComponentProps<"ul">) {
498
+ return (
499
+ <ul
500
+ data-slot="sidebar-menu"
501
+ data-sidebar="menu"
502
+ className={cn("flex w-full min-w-0 flex-col gap-0", className)}
503
+ {...props}
504
+ />
505
+ )
506
+ }
507
+
508
+ function SidebarMenuItem({ className, ...props }: React.ComponentProps<"li">) {
509
+ return (
510
+ <li
511
+ data-slot="sidebar-menu-item"
512
+ data-sidebar="menu-item"
513
+ className={cn("group/menu-item relative", className)}
514
+ {...props}
515
+ />
516
+ )
517
+ }
518
+
519
+ const sidebarMenuButtonVariants = cva(
520
+ "peer/menu-button group/menu-button flex w-full items-center gap-2 overflow-hidden rounded-md p-2 text-left text-sm ring-sidebar-ring outline-hidden transition-[width,height,padding] group-has-data-[sidebar=menu-action]/menu-item:pr-8 group-data-[collapsible=icon]:size-8! group-data-[collapsible=icon]:p-2! hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2 active:bg-sidebar-accent active:text-sidebar-accent-foreground disabled:pointer-events-none disabled:opacity-50 aria-disabled:pointer-events-none aria-disabled:opacity-50 data-open:hover:bg-sidebar-accent data-open:hover:text-sidebar-accent-foreground data-active:bg-sidebar-accent data-active:font-medium data-active:text-sidebar-accent-foreground [&_svg]:size-4 [&_svg]:shrink-0 [&>span:last-child]:truncate",
521
+ {
522
+ variants: {
523
+ variant: {
524
+ default: "hover:bg-sidebar-accent hover:text-sidebar-accent-foreground",
525
+ outline:
526
+ "bg-background shadow-[0_0_0_1px_var(--sidebar-border)] hover:bg-sidebar-accent hover:text-sidebar-accent-foreground hover:shadow-[0_0_0_1px_var(--sidebar-accent)]",
527
+ },
528
+ size: {
529
+ default: "h-8 text-sm",
530
+ sm: "h-7 text-xs",
531
+ lg: "h-12 text-sm group-data-[collapsible=icon]:p-0!",
532
+ },
533
+ },
534
+ defaultVariants: {
535
+ variant: "default",
536
+ size: "default",
537
+ },
538
+ }
539
+ )
540
+
541
+ function SidebarMenuButton({
542
+ render,
543
+ isActive = false,
544
+ variant = "default",
545
+ size = "default",
546
+ tooltip,
547
+ className,
548
+ ...props
549
+ }: useRender.ComponentProps<"button"> &
550
+ React.ComponentProps<"button"> & {
551
+ isActive?: boolean
552
+ /** Label shown beside the button while the sidebar is collapsed to icons. */
553
+ tooltip?: React.ReactNode
554
+ } & VariantProps<typeof sidebarMenuButtonVariants>) {
555
+ const { isMobile, state } = useSidebar()
556
+ const comp = useRender({
557
+ defaultTagName: "button",
558
+ props: mergeProps<"button">(
559
+ {
560
+ className: cn(sidebarMenuButtonVariants({ variant, size }), className),
561
+ },
562
+ props
563
+ ),
564
+ render,
565
+ state: {
566
+ slot: "sidebar-menu-button",
567
+ sidebar: "menu-button",
568
+ size,
569
+ active: isActive,
570
+ },
571
+ })
572
+
573
+ if (!tooltip) {
574
+ return comp
575
+ }
576
+
577
+ // Only worth a bubble when the label is hidden: collapsed to icons, on desktop.
578
+ return (
579
+ <Tooltip content={state === "collapsed" && !isMobile ? tooltip : undefined} side="right" align="center">
580
+ {comp as React.ReactElement<Record<string, unknown>>}
581
+ </Tooltip>
582
+ )
583
+ }
584
+
585
+ function SidebarMenuAction({
586
+ className,
587
+ render,
588
+ showOnHover = false,
589
+ ...props
590
+ }: useRender.ComponentProps<"button"> &
591
+ React.ComponentProps<"button"> & {
592
+ showOnHover?: boolean
593
+ }) {
594
+ return useRender({
595
+ defaultTagName: "button",
596
+ props: mergeProps<"button">(
597
+ {
598
+ className: cn(
599
+ "absolute top-1.5 right-1 flex aspect-square w-5 items-center justify-center rounded-md p-0 text-sidebar-foreground ring-sidebar-ring outline-hidden transition-transform group-data-[collapsible=icon]:hidden peer-hover/menu-button:text-sidebar-accent-foreground peer-data-[size=default]/menu-button:top-1.5 peer-data-[size=lg]/menu-button:top-2.5 peer-data-[size=sm]/menu-button:top-1 after:absolute after:-inset-2 hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2 md:after:hidden [&>svg]:size-4 [&>svg]:shrink-0",
600
+ showOnHover &&
601
+ "group-focus-within/menu-item:opacity-100 group-hover/menu-item:opacity-100 peer-data-active/menu-button:text-sidebar-accent-foreground aria-expanded:opacity-100 md:opacity-0",
602
+ className
603
+ ),
604
+ },
605
+ props
606
+ ),
607
+ render,
608
+ state: {
609
+ slot: "sidebar-menu-action",
610
+ sidebar: "menu-action",
611
+ },
612
+ })
613
+ }
614
+
615
+ function SidebarMenuBadge({
616
+ className,
617
+ ...props
618
+ }: React.ComponentProps<"div">) {
619
+ return (
620
+ <div
621
+ data-slot="sidebar-menu-badge"
622
+ data-sidebar="menu-badge"
623
+ className={cn(
624
+ "pointer-events-none absolute right-1 flex h-5 min-w-5 items-center justify-center rounded-md px-1 text-xs font-medium text-sidebar-foreground tabular-nums select-none group-data-[collapsible=icon]:hidden peer-hover/menu-button:text-sidebar-accent-foreground peer-data-[size=default]/menu-button:top-1.5 peer-data-[size=lg]/menu-button:top-2.5 peer-data-[size=sm]/menu-button:top-1 peer-data-active/menu-button:text-sidebar-accent-foreground",
625
+ className
626
+ )}
627
+ {...props}
628
+ />
629
+ )
630
+ }
631
+
632
+ function SidebarMenuSkeleton({
633
+ className,
634
+ showIcon = false,
635
+ ...props
636
+ }: React.ComponentProps<"div"> & {
637
+ showIcon?: boolean
638
+ }) {
639
+ // Random width between 50 to 90%.
640
+ const [width] = React.useState(() => {
641
+ return `${Math.floor(Math.random() * 40) + 50}%`
642
+ })
643
+
644
+ return (
645
+ <div
646
+ data-slot="sidebar-menu-skeleton"
647
+ data-sidebar="menu-skeleton"
648
+ className={cn("flex h-8 items-center gap-2 rounded-md px-2", className)}
649
+ {...props}
650
+ >
651
+ {showIcon && (
652
+ <Skeleton
653
+ className="size-4 rounded-md"
654
+ data-sidebar="menu-skeleton-icon"
655
+ />
656
+ )}
657
+ <Skeleton
658
+ className="h-4 max-w-(--skeleton-width) flex-1"
659
+ data-sidebar="menu-skeleton-text"
660
+ style={
661
+ {
662
+ "--skeleton-width": width,
663
+ } as React.CSSProperties
664
+ }
665
+ />
666
+ </div>
667
+ )
668
+ }
669
+
670
+ function SidebarMenuSub({ className, ...props }: React.ComponentProps<"ul">) {
671
+ return (
672
+ <ul
673
+ data-slot="sidebar-menu-sub"
674
+ data-sidebar="menu-sub"
675
+ className={cn(
676
+ "mx-3.5 flex min-w-0 translate-x-px flex-col gap-1 border-l border-sidebar-border px-2.5 py-0.5 group-data-[collapsible=icon]:hidden",
677
+ className
678
+ )}
679
+ {...props}
680
+ />
681
+ )
682
+ }
683
+
684
+ function SidebarMenuSubItem({
685
+ className,
686
+ ...props
687
+ }: React.ComponentProps<"li">) {
688
+ return (
689
+ <li
690
+ data-slot="sidebar-menu-sub-item"
691
+ data-sidebar="menu-sub-item"
692
+ className={cn("group/menu-sub-item relative", className)}
693
+ {...props}
694
+ />
695
+ )
696
+ }
697
+
698
+ function SidebarMenuSubButton({
699
+ render,
700
+ size = "md",
701
+ isActive = false,
702
+ className,
703
+ ...props
704
+ }: useRender.ComponentProps<"a"> &
705
+ React.ComponentProps<"a"> & {
706
+ size?: "sm" | "md"
707
+ isActive?: boolean
708
+ }) {
709
+ return useRender({
710
+ defaultTagName: "a",
711
+ props: mergeProps<"a">(
712
+ {
713
+ className: cn(
714
+ "flex h-7 min-w-0 -translate-x-px items-center gap-2 overflow-hidden rounded-md px-2 text-sidebar-foreground ring-sidebar-ring outline-hidden group-data-[collapsible=icon]:hidden hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2 active:bg-sidebar-accent active:text-sidebar-accent-foreground disabled:pointer-events-none disabled:opacity-50 aria-disabled:pointer-events-none aria-disabled:opacity-50 data-[size=md]:text-sm data-[size=sm]:text-xs data-active:bg-sidebar-accent data-active:text-sidebar-accent-foreground [&>span:last-child]:truncate [&>svg]:size-4 [&>svg]:shrink-0 [&>svg]:text-sidebar-accent-foreground",
715
+ className
716
+ ),
717
+ },
718
+ props
719
+ ),
720
+ render,
721
+ state: {
722
+ slot: "sidebar-menu-sub-button",
723
+ sidebar: "menu-sub-button",
724
+ size,
725
+ active: isActive,
726
+ },
727
+ })
728
+ }
729
+
730
+ export {
731
+ Sidebar,
732
+ SidebarContent,
733
+ SidebarFooter,
734
+ SidebarGroup,
735
+ SidebarGroupAction,
736
+ SidebarGroupContent,
737
+ SidebarGroupLabel,
738
+ SidebarHeader,
739
+ SidebarInput,
740
+ SidebarInset,
741
+ SidebarMenu,
742
+ SidebarMenuAction,
743
+ SidebarMenuBadge,
744
+ SidebarMenuButton,
745
+ SidebarMenuItem,
746
+ SidebarMenuSkeleton,
747
+ SidebarMenuSub,
748
+ SidebarMenuSubButton,
749
+ SidebarMenuSubItem,
750
+ SidebarProvider,
751
+ SidebarRail,
752
+ SidebarSeparator,
753
+ SidebarTrigger,
754
+ useSidebar,
755
+ }
package/src/index.ts CHANGED
@@ -149,6 +149,49 @@ export {
149
149
  } from "./components/sheet"
150
150
  export type { SheetContentProps, SheetSide, SheetSize } from "./components/sheet"
151
151
 
152
+ // Sidebar — the app shell's left rail: provider (open state, ⌘B, cookie, mobile sheet), the
153
+ // rail itself (`collapsible` offcanvas / icon / none, `variant` sidebar / floating / inset),
154
+ // and the parts that fill it (header, content, groups, menu buttons, sub-menus, footer).
155
+ // Router-agnostic: `render={<Link />}` on a menu button, `mobileCloseKey={pathname}` on the provider.
156
+ export {
157
+ Sidebar,
158
+ SidebarContent,
159
+ SidebarFooter,
160
+ SidebarGroup,
161
+ SidebarGroupAction,
162
+ SidebarGroupContent,
163
+ SidebarGroupLabel,
164
+ SidebarHeader,
165
+ SidebarInput,
166
+ SidebarInset,
167
+ SidebarMenu,
168
+ SidebarMenuAction,
169
+ SidebarMenuBadge,
170
+ SidebarMenuButton,
171
+ SidebarMenuItem,
172
+ SidebarMenuSkeleton,
173
+ SidebarMenuSub,
174
+ SidebarMenuSubButton,
175
+ SidebarMenuSubItem,
176
+ SidebarProvider,
177
+ SidebarRail,
178
+ SidebarSeparator,
179
+ SidebarTrigger,
180
+ useSidebar,
181
+ } from "./components/sidebar"
182
+
183
+ // The two ends of a sidebar: the organization switcher (header) and the signed-in member's menu
184
+ // (footer) — each an `ActionMenu` behind a full-width button, so entries are data and links go through
185
+ // the app's router (`render.link`).
186
+ export { SidebarBrand, SidebarOrgSwitcher, SidebarUserMenu } from "./components/sidebar-menus"
187
+ export type {
188
+ SidebarBrandProps,
189
+ SidebarOrg,
190
+ SidebarOrgSwitcherProps,
191
+ SidebarUser,
192
+ SidebarUserMenuProps,
193
+ } from "./components/sidebar-menus"
194
+
152
195
  // Table — presentational only (no sorting, no pagination): apps own that logic,
153
196
  // what they were duplicating is the markup and the tokens.
154
197
  export {