@whity/ui 0.5.0 → 1.0.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.
Files changed (205) hide show
  1. package/README.md +107 -107
  2. package/package.json +221 -192
  3. package/src/access-denied.stories.tsx +179 -179
  4. package/src/access-denied.tsx +305 -305
  5. package/src/action-proposal.tsx +338 -302
  6. package/src/actor-chip.tsx +71 -71
  7. package/src/agent-activity.tsx +163 -163
  8. package/src/agent-collaboration.stories.tsx +156 -156
  9. package/src/agent-run-status.tsx +196 -196
  10. package/src/alert-dialog.stories.tsx +45 -45
  11. package/src/alert-dialog.tsx +147 -147
  12. package/src/alert.stories.tsx +91 -91
  13. package/src/alert.tsx +89 -89
  14. package/src/app-sidebar.stories.tsx +195 -195
  15. package/src/app-sidebar.tsx +604 -500
  16. package/src/approval-queue.tsx +109 -108
  17. package/src/audience-group-picker.stories.tsx +198 -198
  18. package/src/audience-group-picker.tsx +362 -362
  19. package/src/audience-people-picker.stories.tsx +102 -102
  20. package/src/audience-people-picker.tsx +279 -279
  21. package/src/avatar-pattern.ts +138 -0
  22. package/src/avatar.tsx +125 -83
  23. package/src/badge.stories.tsx +105 -105
  24. package/src/badge.tsx +68 -68
  25. package/src/beta-warning.stories.tsx +79 -79
  26. package/src/beta-warning.tsx +189 -189
  27. package/src/bilingual-input.stories.tsx +82 -82
  28. package/src/bilingual-input.tsx +188 -183
  29. package/src/board.tsx +216 -0
  30. package/src/breadcrumb.stories.tsx +51 -51
  31. package/src/breadcrumb.tsx +90 -90
  32. package/src/built-on-whity.tsx +52 -0
  33. package/src/button.stories.tsx +87 -87
  34. package/src/button.tsx +123 -118
  35. package/src/card.stories.tsx +423 -423
  36. package/src/card.tsx +180 -180
  37. package/src/chart.stories.tsx +66 -66
  38. package/src/chart.tsx +228 -228
  39. package/src/checkbox.stories.tsx +62 -62
  40. package/src/checkbox.tsx +54 -54
  41. package/src/collapsible.stories.tsx +48 -48
  42. package/src/collapsible.tsx +34 -34
  43. package/src/color-card.stories.tsx +61 -61
  44. package/src/color-card.tsx +270 -270
  45. package/src/color-picker.stories.tsx +65 -65
  46. package/src/color-picker.tsx +425 -425
  47. package/src/column-matcher.tsx +84 -0
  48. package/src/command-palette.tsx +398 -306
  49. package/src/confirm-action.tsx +98 -0
  50. package/src/copy-block.stories.tsx +38 -38
  51. package/src/copy-block.tsx +85 -85
  52. package/src/data-table.stories.tsx +200 -200
  53. package/src/data-table.tsx +1686 -1106
  54. package/src/date-picker.stories.tsx +61 -61
  55. package/src/date-picker.tsx +456 -422
  56. package/src/design-cards.stories.tsx +551 -458
  57. package/src/dialog.stories.tsx +48 -48
  58. package/src/dialog.tsx +179 -166
  59. package/src/display-preferences.tsx +50 -0
  60. package/src/documents/barcode-svg.stories.tsx +72 -72
  61. package/src/documents/barcode-svg.tsx +79 -79
  62. package/src/dropdown-menu.stories.tsx +77 -77
  63. package/src/dropdown-menu.tsx +271 -271
  64. package/src/editor/agent-panel.tsx +222 -0
  65. package/src/editor/agents.stories.tsx +81 -0
  66. package/src/editor/agents.ts +55 -0
  67. package/src/editor/behaviour.ts +279 -279
  68. package/src/editor/block-ids.ts +44 -44
  69. package/src/editor/block-list.tsx +104 -101
  70. package/src/editor/block-toolbar.tsx +490 -477
  71. package/src/editor/bubble.tsx +119 -93
  72. package/src/editor/catalogue.ts +169 -103
  73. package/src/editor/collab-rail.tsx +746 -746
  74. package/src/editor/collab.stories.tsx +207 -207
  75. package/src/editor/collab.ts +87 -87
  76. package/src/editor/contract.ts +476 -408
  77. package/src/editor/convert.ts +414 -362
  78. package/src/editor/doc-editor.stories.tsx +98 -98
  79. package/src/editor/doc-editor.tsx +1492 -1287
  80. package/src/editor/editor.css +636 -536
  81. package/src/editor/extensions.ts +86 -86
  82. package/src/editor/field-bar.tsx +154 -0
  83. package/src/editor/field-picker.tsx +109 -107
  84. package/src/editor/fields.ts +100 -100
  85. package/src/editor/gutter.tsx +443 -367
  86. package/src/editor/index.ts +23 -22
  87. package/src/editor/label-sheet.tsx +289 -289
  88. package/src/editor/labels.ts +394 -310
  89. package/src/editor/link-tools.tsx +141 -141
  90. package/src/editor/node-views.tsx +461 -450
  91. package/src/editor/page-view.tsx +372 -353
  92. package/src/editor/phone.tsx +310 -0
  93. package/src/editor/plugin-block-view.tsx +230 -0
  94. package/src/editor/plugin-blocks.stories.tsx +96 -0
  95. package/src/editor/plugin-blocks.ts +52 -0
  96. package/src/editor/publish-check.tsx +201 -182
  97. package/src/editor/rich-blocks.stories.tsx +146 -146
  98. package/src/editor/sample.ts +40 -40
  99. package/src/editor/save-block-dialog.tsx +141 -0
  100. package/src/editor/saved-block-view.tsx +205 -0
  101. package/src/editor/saved-blocks.stories.tsx +105 -0
  102. package/src/editor/saved-parts.ts +192 -0
  103. package/src/editor/schema-rich.ts +156 -150
  104. package/src/editor/schema.ts +368 -315
  105. package/src/editor/services.tsx +205 -127
  106. package/src/editor/slash.ts +64 -64
  107. package/src/editor/suggesting.ts +372 -372
  108. package/src/editor/suggestions.ts +763 -763
  109. package/src/editor/table-keys.ts +66 -66
  110. package/src/editor/templates.stories.tsx +324 -324
  111. package/src/editor/toolbar.tsx +364 -358
  112. package/src/empty-state.stories.tsx +51 -51
  113. package/src/empty-state.tsx +126 -100
  114. package/src/empty-state.usage-notes.md +30 -30
  115. package/src/form.stories.tsx +112 -112
  116. package/src/form.tsx +176 -176
  117. package/src/globals.css +20 -20
  118. package/src/icon-button.tsx +85 -85
  119. package/src/icon-map.ts +335 -244
  120. package/src/icon.tsx +41 -41
  121. package/src/index.ts +90 -67
  122. package/src/input.stories.tsx +157 -157
  123. package/src/input.tsx +768 -759
  124. package/src/kpi-strip.tsx +47 -0
  125. package/src/localized-text.ts +76 -0
  126. package/src/locked-screen.stories.tsx +29 -29
  127. package/src/locked-screen.tsx +75 -61
  128. package/src/lockup.tsx +42 -0
  129. package/src/lottie.tsx +441 -0
  130. package/src/mark.tsx +62 -62
  131. package/src/math-text.stories.tsx +33 -33
  132. package/src/math-text.tsx +43 -43
  133. package/src/menu-bar.stories.tsx +198 -198
  134. package/src/menu-bar.tsx +373 -373
  135. package/src/message-thread.tsx +181 -0
  136. package/src/money/currency.ts +244 -230
  137. package/src/money/money-input.tsx +131 -0
  138. package/src/money/money.tsx +33 -0
  139. package/src/open-on-computer.tsx +150 -0
  140. package/src/otp-input.tsx +161 -0
  141. package/src/page-header.stories.tsx +95 -95
  142. package/src/page-header.tsx +122 -122
  143. package/src/page-shell.stories.tsx +42 -42
  144. package/src/page-shell.tsx +32 -32
  145. package/src/page-states.tsx +277 -0
  146. package/src/pagination.stories.tsx +45 -45
  147. package/src/pagination.tsx +98 -98
  148. package/src/paper-sheet.stories.tsx +88 -0
  149. package/src/paper-sheet.tsx +85 -0
  150. package/src/phone-tab-bar.tsx +152 -0
  151. package/src/plugin-card.stories.tsx +208 -180
  152. package/src/plugin-card.tsx +931 -848
  153. package/src/popover.stories.tsx +48 -48
  154. package/src/popover.tsx +55 -55
  155. package/src/radio-group.stories.tsx +26 -26
  156. package/src/radio-group.tsx +44 -44
  157. package/src/route-flow/editor.tsx +1087 -1014
  158. package/src/route-flow/model.ts +771 -716
  159. package/src/save-bar.tsx +94 -0
  160. package/src/scope-grant.tsx +139 -139
  161. package/src/screen-too-small.stories.tsx +61 -61
  162. package/src/screen-too-small.tsx +104 -104
  163. package/src/search-field.tsx +65 -0
  164. package/src/select.stories.tsx +44 -44
  165. package/src/select.tsx +197 -197
  166. package/src/separator.stories.tsx +34 -34
  167. package/src/separator.tsx +34 -34
  168. package/src/sheet.stories.tsx +49 -49
  169. package/src/sheet.tsx +168 -168
  170. package/src/skeleton.stories.tsx +28 -28
  171. package/src/skeleton.tsx +19 -19
  172. package/src/slider.stories.tsx +78 -78
  173. package/src/slider.tsx +300 -300
  174. package/src/spinner.stories.tsx +33 -33
  175. package/src/spinner.tsx +55 -55
  176. package/src/stepper.tsx +85 -0
  177. package/src/switch.stories.tsx +42 -42
  178. package/src/switch.tsx +41 -41
  179. package/src/switcher.stories.tsx +111 -111
  180. package/src/switcher.tsx +131 -131
  181. package/src/system-state.tsx +55 -0
  182. package/src/table.stories.tsx +48 -48
  183. package/src/table.tsx +121 -121
  184. package/src/tabs.stories.tsx +74 -74
  185. package/src/tabs.tsx +90 -90
  186. package/src/tag-input.stories.tsx +67 -67
  187. package/src/tag-input.tsx +117 -117
  188. package/src/tenant-logo.tsx +63 -63
  189. package/src/tenant-switcher.stories.tsx +38 -38
  190. package/src/tenant-switcher.tsx +164 -164
  191. package/src/textarea.stories.tsx +27 -27
  192. package/src/textarea.tsx +18 -18
  193. package/src/theme-mode.ts +52 -52
  194. package/src/time-grid.tsx +163 -0
  195. package/src/time-picker.stories.tsx +62 -62
  196. package/src/time-picker.tsx +615 -615
  197. package/src/toggle-group.tsx +138 -138
  198. package/src/tooltip.stories.tsx +43 -43
  199. package/src/tooltip.tsx +73 -73
  200. package/src/top-nav.stories.tsx +58 -58
  201. package/src/top-nav.tsx +324 -324
  202. package/src/undo-bar.tsx +107 -107
  203. package/src/utils.ts +45 -45
  204. package/src/view-toggle.stories.tsx +84 -84
  205. package/src/view-toggle.tsx +110 -110
@@ -1,500 +1,604 @@
1
- import * as React from "react"
2
- import {
3
- IconChevronDown,
4
- IconChevronLeft,
5
- IconChevronRight,
6
- IconMenu2,
7
- IconSearch,
8
- IconX,
9
- } from "@tabler/icons-react"
10
-
11
- import { cn } from "./utils"
12
-
13
- /**
14
- * PRESENTATIONAL app-shell sidebar only — a controlled, data-driven nav
15
- * chrome. Fetching nav items (RBAC/tenant-filtered), branding, tenant/team
16
- * switching, and theme/direction toggles belong to the app-specific
17
- * composed wrapper (e.g. web/components/sidebar.tsx's `Sidebar`), which
18
- * renders this component and passes it `groups` + slot content — the same
19
- * split Breadcrumb already draws between itself and route-derived crumbs.
20
- *
21
- * Multi-tenant + multi-team: `tenantSwitcher` and `teamSwitcher` are
22
- * independent optional slots (see each prop's doc below) built from the
23
- * shared `Switcher` primitive — a user might have many tenants and no
24
- * teams, one tenant and many teams, both, or neither; which nav `groups`
25
- * to pass is entirely the composed caller's decision (e.g. re-fetch/swap
26
- * `groups` when the active team changes), this component has no concept
27
- * of "tenant" or "team" itself.
28
- *
29
- * PLATFORM NOTE: `groups`/`items` are plain data (label + href + optional
30
- * icon) — the same shape a Flutter or Tauri-native nav rail would consume;
31
- * this component owns only the desktop-web collapse/expand + mobile
32
- * slide-over chrome around that data.
33
- *
34
- * I18N: every user-visible string is a prop with an English default, and
35
- * nothing here imports a translator — the composed wrapper passes already
36
- * translated text, so the same component serves a client with no i18n layer
37
- * and one with a full catalogue.
38
- */
39
-
40
- export interface AppSidebarNavItem {
41
- id: string
42
- label: string
43
- href: string
44
- icon?: React.ReactNode
45
- /** Marks the item as the current page (styling only — routing is the caller's job). */
46
- active?: boolean
47
- /**
48
- * Things waiting for the person (Inbox, Approvals). A pill beside the label;
49
- * in the rail, a dot on the icon (AppShellCollapsed). Zero or absent shows nothing.
50
- */
51
- count?: number
52
- /** Accessible text for the count, e.g. "3 waiting". Defaults to the number. */
53
- countLabel?: string
54
- }
55
-
56
- export interface AppSidebarNavGroup {
57
- id: string
58
- /**
59
- * Optional group heading, hidden entirely when the sidebar is collapsed.
60
- * A group WITHOUT a label is never collapsible — there is no header to
61
- * click — so its items always render. Use that for a short trailing set
62
- * that must stay reachable regardless of collapse state.
63
- */
64
- label?: string
65
- items: AppSidebarNavItem[]
66
- }
67
-
68
- export interface AppSidebarProps {
69
- /** Branding/logo slot, rendered above the switchers/nav. */
70
- header?: React.ReactNode
71
- /**
72
- * Optional tenant-switcher slot (e.g. built from the shared `Switcher`
73
- * primitive). A multi-tenant client renders one here; a single-tenant
74
- * client omits it entirely — this component has no opinion on tenants.
75
- * If provided, keep it in sync with `collapsed` (pass this same prop's
76
- * value to the `Switcher`/custom control you render here) so it matches
77
- * the sidebar's icon-only state.
78
- */
79
- tenantSwitcher?: React.ReactNode
80
- /**
81
- * Optional team-switcher slot, independent of `tenantSwitcher` — a user
82
- * may have many teams and one tenant (show only this), many tenants and
83
- * no teams (show only that), both, or neither. Whether to render either
84
- * slot at all is entirely the composed caller's call; this component
85
- * just reserves the layout position and stacks whatever it's given.
86
- */
87
- teamSwitcher?: React.ReactNode
88
- groups: AppSidebarNavGroup[]
89
- /** User menu / theme toggle slot, rendered below the nav. */
90
- footer?: React.ReactNode
91
- /** Controlled collapsed (icon-only) state. Uncontrolled if omitted. */
92
- collapsed?: boolean
93
- /** Called when the user clicks the collapse toggle. */
94
- onCollapsedChange?: (collapsed: boolean) => void
95
- /** Controlled mobile open state. Uncontrolled if omitted. */
96
- mobileOpen?: boolean
97
- onMobileOpenChange?: (open: boolean) => void
98
- /** Custom link component (e.g. Next.js <Link>) — defaults to a plain <a>. */
99
- linkComponent?: React.ElementType
100
- /**
101
- * Turn group headings into disclosure buttons. Groups the user has not
102
- * opened render their items HIDDEN rather than unmounted — see the
103
- * `hidden` attribute in the group body below for why that distinction is
104
- * load-bearing.
105
- */
106
- collapsibleGroups?: boolean
107
- /**
108
- * Controlled set of open group ids. Uncontrolled if omitted, in which
109
- * case exactly one group starts open: the one holding the active item
110
- * (falling back to the first group when nothing is active). Navigating to
111
- * another group's page opens that group, without closing anything the
112
- * user opened by hand.
113
- */
114
- expandedGroupIds?: readonly string[]
115
- onExpandedGroupsChange?: (expanded: readonly string[]) => void
116
- /** Render a filter box above the nav that narrows items by label. */
117
- searchable?: boolean
118
- /** Controlled search query. Uncontrolled if omitted. */
119
- searchValue?: string
120
- onSearchChange?: (value: string) => void
121
- /** Placeholder AND accessible name for the filter box. Defaults to "Search". */
122
- searchPlaceholder?: string
123
- /** Accessible name for the button that empties the filter box. */
124
- clearSearchLabel?: string
125
- /** Shown in place of the nav when a query matches no item. */
126
- searchNoResultsLabel?: string
127
- /** Accessible name for the navigation landmark. Defaults to "Main". */
128
- navLabel?: string
129
- /** Accessible name for the button that opens the mobile drawer. */
130
- openNavLabel?: string
131
- /** Accessible name for the button that closes the mobile drawer. */
132
- closeNavLabel?: string
133
- /** Text of the collapse toggle. Defaults to "Collapse". */
134
- collapseLabel?: string
135
- /**
136
- * Accessible names for the collapse toggle in each direction. Separate
137
- * from `collapseLabel`, which is the visible text beside the icon.
138
- */
139
- collapseAriaLabel?: string
140
- expandAriaLabel?: string
141
- className?: string
142
- }
143
-
144
- /**
145
- * True between 768 and 1023px, where the sidebar is the 56px rail whatever the
146
- * collapse toggle says (register 4.5). Below 768 it is the drawer; from 1024
147
- * the person's own choice applies. Exported so the composed wrapper can size
148
- * its own slot content (logo, switcher, footer) to the same state.
149
- */
150
- export function useForcedRail(): boolean {
151
- const [forced, setForced] = React.useState(false)
152
- React.useEffect(() => {
153
- if (typeof window === "undefined" || typeof window.matchMedia !== "function") return
154
- const query = window.matchMedia("(min-width: 768px) and (max-width: 1023.98px)")
155
- const update = () => setForced(query.matches)
156
- update()
157
- query.addEventListener?.("change", update)
158
- return () => query.removeEventListener?.("change", update)
159
- }, [])
160
- return forced
161
- }
162
-
163
- /** Case-insensitive substring match, tolerant of surrounding whitespace. */
164
- function matches(label: string, normalizedQuery: string): boolean {
165
- return label.toLocaleLowerCase().includes(normalizedQuery)
166
- }
167
-
168
- export function AppSidebar({
169
- header,
170
- tenantSwitcher,
171
- teamSwitcher,
172
- groups,
173
- footer,
174
- collapsed: collapsedProp,
175
- onCollapsedChange,
176
- mobileOpen: mobileOpenProp,
177
- onMobileOpenChange,
178
- linkComponent,
179
- collapsibleGroups = false,
180
- expandedGroupIds,
181
- onExpandedGroupsChange,
182
- searchable = false,
183
- searchValue,
184
- onSearchChange,
185
- searchPlaceholder = "Search",
186
- clearSearchLabel = "Clear search",
187
- searchNoResultsLabel = "No matching pages",
188
- navLabel = "Main",
189
- openNavLabel = "Open navigation",
190
- closeNavLabel = "Close navigation",
191
- collapseLabel = "Collapse",
192
- collapseAriaLabel = "Collapse sidebar",
193
- expandAriaLabel = "Expand sidebar",
194
- className,
195
- }: AppSidebarProps) {
196
- const [collapsedState, setCollapsedState] = React.useState(false)
197
- const forcedRail = useForcedRail()
198
- const collapsed = (forcedRail && !(mobileOpenProp ?? false)) || (collapsedProp ?? collapsedState)
199
- const setCollapsed = (next: boolean) => {
200
- setCollapsedState(next)
201
- onCollapsedChange?.(next)
202
- }
203
-
204
- const [mobileOpenState, setMobileOpenState] = React.useState(false)
205
- const mobileOpen = mobileOpenProp ?? mobileOpenState
206
- const setMobileOpen = (next: boolean) => {
207
- setMobileOpenState(next)
208
- onMobileOpenChange?.(next)
209
- }
210
-
211
- const [queryState, setQueryState] = React.useState("")
212
- const query = searchValue ?? queryState
213
- const setQuery = (next: string) => {
214
- if (searchValue === undefined) setQueryState(next)
215
- onSearchChange?.(next)
216
- }
217
- const normalizedQuery = query.trim().toLocaleLowerCase()
218
- const searching = normalizedQuery !== ""
219
-
220
- // The group holding the current page, which is what starts open. Falls back
221
- // to the first group so an unmatched route (a detail page no nav item owns)
222
- // still shows something rather than a stack of closed headers.
223
- const openByDefaultGroupId = React.useMemo(() => {
224
- const active = groups.find((group) => group.items.some((item) => item.active))
225
- return active?.id ?? groups[0]?.id ?? null
226
- }, [groups])
227
-
228
- const [expandedState, setExpandedState] = React.useState<readonly string[]>([])
229
- const expanded = expandedGroupIds ?? expandedState
230
-
231
- // `groups` typically arrives empty on first render and fills in after the
232
- // nav fetch resolves, so the default cannot be computed in useState's
233
- // initializer — it has to react to the id appearing. Keyed on the id alone:
234
- // a group the user opened by hand survives navigation within it, and only a
235
- // move to a DIFFERENT group re-narrows the sidebar.
236
- React.useEffect(() => {
237
- if (expandedGroupIds !== undefined) return
238
- if (openByDefaultGroupId === null) return
239
- setExpandedState((prev) =>
240
- prev.includes(openByDefaultGroupId) ? prev : [openByDefaultGroupId]
241
- )
242
- }, [openByDefaultGroupId, expandedGroupIds])
243
-
244
- const toggleGroup = (id: string) => {
245
- const next = expanded.includes(id)
246
- ? expanded.filter((groupId) => groupId !== id)
247
- : [...expanded, id]
248
- if (expandedGroupIds === undefined) setExpandedState(next)
249
- onExpandedGroupsChange?.(next)
250
- }
251
-
252
- const Link = linkComponent ?? "a"
253
-
254
- // While a query is active the disclosure state is bypassed entirely: the
255
- // point of the box is to reach an item without knowing which group holds
256
- // it. Groups with no match drop out so the result list stays short.
257
- const rendered = React.useMemo(() => {
258
- if (!searching) {
259
- return groups.map((group) => ({ group, items: group.items }))
260
- }
261
- return groups
262
- .map((group) => ({
263
- group,
264
- items: group.items.filter((item) => matches(item.label, normalizedQuery)),
265
- }))
266
- .filter((entry) => entry.items.length > 0)
267
- }, [groups, searching, normalizedQuery])
268
-
269
- // A group is open when it cannot be closed (no header to click, or the
270
- // feature is off), when the icon rail hides the headers anyway, while a
271
- // query is bypassing disclosure, or when the user/default opened it.
272
- const isGroupOpen = (group: AppSidebarNavGroup): boolean =>
273
- !collapsibleGroups ||
274
- !group.label ||
275
- collapsed ||
276
- searching ||
277
- expanded.includes(group.id)
278
-
279
- const nav = (
280
- <nav
281
- data-slot="app-sidebar-nav"
282
- aria-label={navLabel}
283
- className={cn("flex-1 space-y-3 overflow-x-hidden overflow-y-auto px-3 py-3", collapsed && "px-2")}
284
- >
285
- {rendered.map(({ group, items }, index) => {
286
- const open = isGroupOpen(group)
287
- const showHeading = Boolean(group.label) && !collapsed
288
- const isDisclosure = showHeading && collapsibleGroups && !searching
289
- const bodyId = `app-sidebar-group-${group.id}`
290
-
291
- return (
292
- <div
293
- key={group.id}
294
- data-slot="app-sidebar-group"
295
- className={cn("grid gap-px", collapsed && index > 0 && "border-t border-sidebar-border pt-2")}
296
- >
297
- {showHeading && isDisclosure && (
298
- <button
299
- type="button"
300
- data-slot="app-sidebar-group-toggle"
301
- data-group-id={group.id}
302
- aria-expanded={open}
303
- aria-controls={bodyId}
304
- onClick={() => toggleGroup(group.id)}
305
- className="flex w-full items-center gap-1 rounded-md px-2 py-1 text-[0.6875rem] font-semibold tracking-wider text-muted-foreground uppercase outline-none hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2 focus-visible:ring-ring"
306
- >
307
- {open ? (
308
- <IconChevronDown className="size-3 shrink-0" />
309
- ) : (
310
- <IconChevronRight className="size-3 shrink-0 rtl:rotate-180" />
311
- )}
312
- <span className="truncate">{group.label}</span>
313
- </button>
314
- )}
315
- {showHeading && !isDisclosure && (
316
- <div className="px-2 py-1 text-[0.6875rem] font-semibold tracking-wider text-muted-foreground uppercase">
317
- {group.label}
318
- </div>
319
- )}
320
- {/*
321
- A closed group's items are HIDDEN, not unmounted.
322
-
323
- `hidden` is the correct disclosure semantic — collapsed content
324
- must leave the accessibility tree, or a screen reader announces
325
- links the sighted user cannot see. It follows that a role/name
326
- query cannot find an item in a closed group, in Playwright as
327
- much as in Testing Library.
328
-
329
- Keeping them mounted is still deliberate: a DOM/CSS query can
330
- tell "this group is closed" (present, hidden) from "RBAC removed
331
- this link for this role" (absent). Unmounting would collapse
332
- both into "absent", and any suite asserting absence as proof of
333
- permission filtering would keep passing after that filtering
334
- broke. Either query by CSS or open the group first.
335
- */}
336
- <div id={bodyId} hidden={!open} className="grid gap-px">
337
- {items.map((item) => {
338
- const count = item.count && item.count > 0 ? item.count : 0
339
- const countText = item.countLabel ?? String(count)
340
- return (
341
- <Link
342
- key={item.id}
343
- href={item.href}
344
- aria-current={item.active ? "page" : undefined}
345
- title={collapsed ? item.label : undefined}
346
- aria-label={collapsed && count > 0 ? `${item.label}, ${countText}` : undefined}
347
- className={cn(
348
- "relative flex items-center gap-2.5 rounded-md px-2 py-[7px] text-sm font-medium transition-colors outline-none focus-visible:ring-2 focus-visible:ring-ring",
349
- item.active
350
- ? "bg-sidebar-primary text-sidebar-primary-foreground"
351
- : "text-sidebar-foreground hover:bg-sidebar-accent hover:text-sidebar-accent-foreground",
352
- collapsed && "mx-auto h-9 w-10 justify-center px-0 py-0"
353
- )}
354
- >
355
- {item.icon && <span className="shrink-0 [&_svg]:size-5">{item.icon}</span>}
356
- {!collapsed && <span className="min-w-0 flex-1 truncate">{item.label}</span>}
357
- {count > 0 && !collapsed && (
358
- <span
359
- data-slot="app-sidebar-count"
360
- className="rounded-full bg-primary px-1.5 py-px text-[0.6875rem] leading-4 font-semibold text-primary-foreground tabular-nums"
361
- >
362
- <span aria-hidden="true">{count}</span>
363
- <span className="sr-only">{countText}</span>
364
- </span>
365
- )}
366
- {count > 0 && collapsed && (
367
- <span
368
- data-slot="app-sidebar-count-dot"
369
- aria-hidden="true"
370
- className="absolute end-2 top-[7px] size-[7px] rounded-full bg-primary ring-2 ring-sidebar"
371
- />
372
- )}
373
- </Link>
374
- )
375
- })}
376
- </div>
377
- </div>
378
- )
379
- })}
380
- {searching && rendered.length === 0 && (
381
- <p data-slot="app-sidebar-no-results" className="px-2 py-1 text-xs/relaxed text-muted-foreground">
382
- {searchNoResultsLabel}
383
- </p>
384
- )}
385
- </nav>
386
- )
387
-
388
- // Hidden in the icon rail: a 4rem-wide column has nowhere to put a text
389
- // field, and the collapse toggle is right there to widen it again.
390
- const search = searchable && !collapsed && (
391
- <div className="px-2.5 pt-2.5">
392
- <div className="relative flex items-center">
393
- <IconSearch className="pointer-events-none absolute start-2 size-3.5 text-muted-foreground" />
394
- <input
395
- type="search"
396
- data-slot="app-sidebar-search"
397
- value={query}
398
- onChange={(event) => setQuery(event.target.value)}
399
- onKeyDown={(event) => {
400
- if (event.key === "Escape") setQuery("")
401
- }}
402
- placeholder={searchPlaceholder}
403
- aria-label={searchPlaceholder}
404
- className="h-8 w-full rounded-md border border-sidebar-border bg-background/50 ps-7 pe-7 text-xs/relaxed text-sidebar-foreground outline-none placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-2 focus-visible:ring-ring/30 [&::-webkit-search-cancel-button]:hidden"
405
- />
406
- {query !== "" && (
407
- <button
408
- type="button"
409
- aria-label={clearSearchLabel}
410
- onClick={() => setQuery("")}
411
- className="absolute end-1 flex size-6 items-center justify-center rounded-md text-sidebar-foreground/70 outline-none hover:bg-sidebar-accent focus-visible:ring-2 focus-visible:ring-ring/30"
412
- >
413
- <IconX className="size-3.5" />
414
- </button>
415
- )}
416
- </div>
417
- </div>
418
- )
419
-
420
- return (
421
- <>
422
- {/* Mobile trigger — shown only below the md breakpoint; the sidebar
423
- itself is fixed off-canvas until toggled open. */}
424
- <button
425
- type="button"
426
- aria-label={openNavLabel}
427
- onClick={() => setMobileOpen(true)}
428
- className="fixed start-3 top-3 z-40 flex size-8 items-center justify-center rounded-md border border-border bg-card outline-none focus-visible:border-ring focus-visible:ring-2 focus-visible:ring-ring/30 md:hidden"
429
- >
430
- <IconMenu2 className="size-4" />
431
- </button>
432
-
433
- {mobileOpen && (
434
- <div
435
- aria-hidden="true"
436
- onClick={() => setMobileOpen(false)}
437
- className="fixed inset-0 z-40 bg-foreground/20 md:hidden"
438
- />
439
- )}
440
-
441
- <aside
442
- data-slot="app-sidebar"
443
- data-collapsed={collapsed || undefined}
444
- className={cn(
445
- "fixed inset-y-0 start-0 z-50 flex h-screen w-60 -translate-x-full flex-col border-e border-sidebar-border bg-sidebar text-sidebar-foreground transition-all duration-200 rtl:translate-x-full",
446
- mobileOpen && "translate-x-0 rtl:translate-x-0",
447
- "md:sticky md:top-0 md:z-auto md:h-screen md:shrink-0 md:translate-x-0 rtl:md:translate-x-0",
448
- collapsed && "md:w-14",
449
- className
450
- )}
451
- >
452
- <button
453
- type="button"
454
- aria-label={closeNavLabel}
455
- onClick={() => setMobileOpen(false)}
456
- className="absolute end-2 top-2.5 z-20 flex size-7 items-center justify-center rounded-md text-sidebar-foreground/70 outline-none hover:bg-sidebar-accent focus-visible:ring-2 focus-visible:ring-ring/30 md:hidden"
457
- >
458
- <IconX className="size-4" />
459
- </button>
460
-
461
- {(header || tenantSwitcher || teamSwitcher) && (
462
- <div className={cn("flex flex-col gap-3 px-3 pt-4 pe-10 md:pe-3", collapsed && "items-center md:px-2")}>
463
- {header && <div className="px-1.5 pt-0.5 pb-1.5">{header}</div>}
464
- {(tenantSwitcher || teamSwitcher) && (
465
- <div className="flex flex-col gap-1.5">
466
- {tenantSwitcher}
467
- {teamSwitcher}
468
- </div>
469
- )}
470
- </div>
471
- )}
472
-
473
- {search}
474
-
475
- {nav}
476
-
477
- <button
478
- type="button"
479
- aria-label={collapsed ? expandAriaLabel : collapseAriaLabel}
480
- onClick={() => setCollapsed(!collapsed)}
481
- className={cn(
482
- "hidden items-center justify-center gap-1.5 border-t border-sidebar-border px-3 py-2 text-xs text-muted-foreground outline-none hover:bg-sidebar-accent focus-visible:ring-2 focus-visible:ring-ring",
483
- !forcedRail && "md:flex"
484
- )}
485
- >
486
- {collapsed ? (
487
- <IconChevronRight className="size-3.5 rtl:rotate-180" />
488
- ) : (
489
- <>
490
- <IconChevronLeft className="size-3.5 rtl:rotate-180" />
491
- <span>{collapseLabel}</span>
492
- </>
493
- )}
494
- </button>
495
-
496
- {footer && <div className="border-t border-sidebar-border px-3 py-3">{footer}</div>}
497
- </aside>
498
- </>
499
- )
500
- }
1
+ import * as React from "react"
2
+ import {
3
+ IconChevronDown,
4
+ IconChevronLeft,
5
+ IconChevronRight,
6
+ IconMenu2,
7
+ IconSearch,
8
+ IconX,
9
+ } from "@tabler/icons-react"
10
+
11
+ import { cn } from "./utils"
12
+
13
+ /**
14
+ * PRESENTATIONAL app-shell sidebar only — a controlled, data-driven nav
15
+ * chrome. Fetching nav items (RBAC/tenant-filtered), branding, tenant/team
16
+ * switching, and theme/direction toggles belong to the app-specific
17
+ * composed wrapper (e.g. web/components/sidebar.tsx's `Sidebar`), which
18
+ * renders this component and passes it `groups` + slot content — the same
19
+ * split Breadcrumb already draws between itself and route-derived crumbs.
20
+ *
21
+ * Multi-tenant + multi-team: `tenantSwitcher` and `teamSwitcher` are
22
+ * independent optional slots (see each prop's doc below) built from the
23
+ * shared `Switcher` primitive — a user might have many tenants and no
24
+ * teams, one tenant and many teams, both, or neither; which nav `groups`
25
+ * to pass is entirely the composed caller's decision (e.g. re-fetch/swap
26
+ * `groups` when the active team changes), this component has no concept
27
+ * of "tenant" or "team" itself.
28
+ *
29
+ * PLATFORM NOTE: `groups`/`items` are plain data (label + href + optional
30
+ * icon) — the same shape a Flutter or Tauri-native nav rail would consume;
31
+ * this component owns only the desktop-web collapse/expand + mobile
32
+ * slide-over chrome around that data.
33
+ *
34
+ * I18N: every user-visible string is a prop with an English default, and
35
+ * nothing here imports a translator — the composed wrapper passes already
36
+ * translated text, so the same component serves a client with no i18n layer
37
+ * and one with a full catalogue.
38
+ */
39
+
40
+ export interface AppSidebarNavItem {
41
+ id: string
42
+ label: string
43
+ href: string
44
+ icon?: React.ReactNode
45
+ /** Marks the item as the current page (styling only — routing is the caller's job). */
46
+ active?: boolean
47
+ /**
48
+ * Things waiting for the person (Inbox, Approvals). A pill beside the label;
49
+ * in the rail, a dot on the icon (AppShellCollapsed). Zero or absent shows nothing.
50
+ */
51
+ count?: number
52
+ /** Accessible text for the count, e.g. "3 waiting". Defaults to the number. */
53
+ countLabel?: string
54
+ }
55
+
56
+ export interface AppSidebarNavGroup {
57
+ id: string
58
+ /**
59
+ * Optional group heading, hidden entirely when the sidebar is collapsed.
60
+ * A group WITHOUT a label is never collapsible — there is no header to
61
+ * click — so its items always render. Use that for a short trailing set
62
+ * that must stay reachable regardless of collapse state.
63
+ */
64
+ label?: string
65
+ items: AppSidebarNavItem[]
66
+ }
67
+
68
+ export interface AppSidebarProps {
69
+ /** Branding/logo slot, rendered above the switchers/nav. */
70
+ header?: React.ReactNode
71
+ /**
72
+ * Optional tenant-switcher slot (e.g. built from the shared `Switcher`
73
+ * primitive). A multi-tenant client renders one here; a single-tenant
74
+ * client omits it entirely — this component has no opinion on tenants.
75
+ * If provided, keep it in sync with `collapsed` (pass this same prop's
76
+ * value to the `Switcher`/custom control you render here) so it matches
77
+ * the sidebar's icon-only state.
78
+ */
79
+ tenantSwitcher?: React.ReactNode
80
+ /**
81
+ * Optional team-switcher slot, independent of `tenantSwitcher` — a user
82
+ * may have many teams and one tenant (show only this), many tenants and
83
+ * no teams (show only that), both, or neither. Whether to render either
84
+ * slot at all is entirely the composed caller's call; this component
85
+ * just reserves the layout position and stacks whatever it's given.
86
+ */
87
+ teamSwitcher?: React.ReactNode
88
+ groups: AppSidebarNavGroup[]
89
+ /** User menu / theme toggle slot, rendered below the nav. */
90
+ footer?: React.ReactNode
91
+ /** Controlled collapsed (icon-only) state. Uncontrolled if omitted. */
92
+ collapsed?: boolean
93
+ /** Called when the user clicks the collapse toggle. */
94
+ onCollapsedChange?: (collapsed: boolean) => void
95
+ /** Controlled mobile open state. Uncontrolled if omitted. */
96
+ mobileOpen?: boolean
97
+ /**
98
+ * Under 640px the host shows the phone tab bar (`PwNav`, `PhoneTabBar`)
99
+ * instead: the drawer and its open button leave the phone layout, and More
100
+ * lists the same groups. 640–767px keeps the drawer, 768–1023px the rail.
101
+ */
102
+ phoneTabBar?: boolean
103
+ /**
104
+ * Groups pinned at the foot, above `footer`, outside the scrolling list
105
+ * (`PwShortSidebar`, PW.10): on a short window Settings stays put with the
106
+ * account row while only the links between them scroll.
107
+ */
108
+ pinnedGroupIds?: readonly string[]
109
+ onMobileOpenChange?: (open: boolean) => void
110
+ /** Custom link component (e.g. Next.js <Link>) — defaults to a plain <a>. */
111
+ linkComponent?: React.ElementType
112
+ /**
113
+ * Turn group headings into disclosure buttons. Groups the user has not
114
+ * opened render their items HIDDEN rather than unmounted — see the
115
+ * `hidden` attribute in the group body below for why that distinction is
116
+ * load-bearing.
117
+ */
118
+ collapsibleGroups?: boolean
119
+ /**
120
+ * Controlled set of open group ids. Uncontrolled if omitted, in which
121
+ * case exactly one group starts open: the one holding the active item
122
+ * (falling back to the first group when nothing is active). Navigating to
123
+ * another group's page opens that group, without closing anything the
124
+ * user opened by hand.
125
+ */
126
+ expandedGroupIds?: readonly string[]
127
+ onExpandedGroupsChange?: (expanded: readonly string[]) => void
128
+ /** Render a filter box above the nav that narrows items by label. */
129
+ searchable?: boolean
130
+ /** Controlled search query. Uncontrolled if omitted. */
131
+ searchValue?: string
132
+ onSearchChange?: (value: string) => void
133
+ /** Placeholder AND accessible name for the filter box. Defaults to "Search". */
134
+ searchPlaceholder?: string
135
+ /** Accessible name for the button that empties the filter box. */
136
+ clearSearchLabel?: string
137
+ /** Shown in place of the nav when a query matches no item. */
138
+ searchNoResultsLabel?: string
139
+ /** Accessible name for the navigation landmark. Defaults to "Main". */
140
+ navLabel?: string
141
+ /** Accessible name for the button that opens the mobile drawer. */
142
+ openNavLabel?: string
143
+ /** Accessible name for the button that closes the mobile drawer. */
144
+ closeNavLabel?: string
145
+ /** Text of the collapse toggle. Defaults to "Collapse". */
146
+ collapseLabel?: string
147
+ /**
148
+ * Accessible names for the collapse toggle in each direction. Separate
149
+ * from `collapseLabel`, which is the visible text beside the icon.
150
+ */
151
+ collapseAriaLabel?: string
152
+ expandAriaLabel?: string
153
+ className?: string
154
+ }
155
+
156
+ /**
157
+ * True between 768 and 1023px, where the sidebar is the 56px rail whatever the
158
+ * collapse toggle says (register 4.5). Below 768 it is the drawer; from 1024
159
+ * the person's own choice applies. Exported so the composed wrapper can size
160
+ * its own slot content (logo, switcher, footer) to the same state.
161
+ */
162
+ export function useForcedRail(): boolean {
163
+ const [forced, setForced] = React.useState(false)
164
+ React.useEffect(() => {
165
+ if (typeof window === "undefined" || typeof window.matchMedia !== "function") return
166
+ const query = window.matchMedia("(min-width: 768px) and (max-width: 1023.98px)")
167
+ const update = () => setForced(query.matches)
168
+ update()
169
+ query.addEventListener?.("change", update)
170
+ return () => query.removeEventListener?.("change", update)
171
+ }, [])
172
+ return forced
173
+ }
174
+
175
+ /** Case-insensitive substring match, tolerant of surrounding whitespace. */
176
+ /**
177
+ * True below 768px, where the sidebar is an off-canvas drawer. A closed
178
+ * drawer is only moved off the screen, so it must also be made inert or its
179
+ * links stay in the tab order and in a screen reader's reading order.
180
+ */
181
+ function usePhoneDrawer(): boolean {
182
+ const [phone, setPhone] = React.useState(false)
183
+ React.useEffect(() => {
184
+ if (typeof window === "undefined" || typeof window.matchMedia !== "function") return
185
+ const query = window.matchMedia("(max-width: 767.98px)")
186
+ const update = () => setPhone(query.matches)
187
+ update()
188
+ query.addEventListener?.("change", update)
189
+ return () => query.removeEventListener?.("change", update)
190
+ }, [])
191
+ return phone
192
+ }
193
+
194
+ function matches(label: string, normalizedQuery: string): boolean {
195
+ return label.toLocaleLowerCase().includes(normalizedQuery)
196
+ }
197
+
198
+ export function AppSidebar({
199
+ header,
200
+ tenantSwitcher,
201
+ teamSwitcher,
202
+ groups,
203
+ footer,
204
+ collapsed: collapsedProp,
205
+ onCollapsedChange,
206
+ mobileOpen: mobileOpenProp,
207
+ phoneTabBar = false,
208
+ pinnedGroupIds,
209
+ onMobileOpenChange,
210
+ linkComponent,
211
+ collapsibleGroups = false,
212
+ expandedGroupIds,
213
+ onExpandedGroupsChange,
214
+ searchable = false,
215
+ searchValue,
216
+ onSearchChange,
217
+ searchPlaceholder = "Search",
218
+ clearSearchLabel = "Clear search",
219
+ searchNoResultsLabel = "No matching pages",
220
+ navLabel = "Main",
221
+ openNavLabel = "Open navigation",
222
+ closeNavLabel = "Close navigation",
223
+ collapseLabel = "Collapse",
224
+ collapseAriaLabel = "Collapse sidebar",
225
+ expandAriaLabel = "Expand sidebar",
226
+ className,
227
+ }: AppSidebarProps) {
228
+ const [collapsedState, setCollapsedState] = React.useState(false)
229
+ const forcedRail = useForcedRail()
230
+ const collapsed = (forcedRail && !(mobileOpenProp ?? false)) || (collapsedProp ?? collapsedState)
231
+ const setCollapsed = (next: boolean) => {
232
+ setCollapsedState(next)
233
+ onCollapsedChange?.(next)
234
+ }
235
+
236
+ const [mobileOpenState, setMobileOpenState] = React.useState(false)
237
+ const mobileOpen = mobileOpenProp ?? mobileOpenState
238
+ const phoneDrawer = usePhoneDrawer()
239
+ const setMobileOpen = (next: boolean) => {
240
+ setMobileOpenState(next)
241
+ onMobileOpenChange?.(next)
242
+ }
243
+
244
+ // The drawer is modal on a phone (a scrim covers the page), so Escape
245
+ // closes it like every other modal layer.
246
+ React.useEffect(() => {
247
+ if (!mobileOpen) return
248
+ const onKey = (event: KeyboardEvent) => {
249
+ if (event.key === "Escape") setMobileOpen(false)
250
+ }
251
+ window.addEventListener("keydown", onKey)
252
+ return () => window.removeEventListener("keydown", onKey)
253
+ // eslint-disable-next-line react-hooks/exhaustive-deps
254
+ }, [mobileOpen])
255
+
256
+ const [queryState, setQueryState] = React.useState("")
257
+ const query = searchValue ?? queryState
258
+ const setQuery = (next: string) => {
259
+ if (searchValue === undefined) setQueryState(next)
260
+ onSearchChange?.(next)
261
+ }
262
+ const normalizedQuery = query.trim().toLocaleLowerCase()
263
+ const searching = normalizedQuery !== ""
264
+
265
+ // The group holding the current page, which is what starts open. Falls back
266
+ // to the first group so an unmatched route (a detail page no nav item owns)
267
+ // still shows something rather than a stack of closed headers.
268
+ const openByDefaultGroupId = React.useMemo(() => {
269
+ const active = groups.find((group) => group.items.some((item) => item.active))
270
+ return active?.id ?? groups[0]?.id ?? null
271
+ }, [groups])
272
+
273
+ const [expandedState, setExpandedState] = React.useState<readonly string[]>([])
274
+ const expanded = expandedGroupIds ?? expandedState
275
+
276
+ // `groups` typically arrives empty on first render and fills in after the
277
+ // nav fetch resolves, so the default cannot be computed in useState's
278
+ // initializer — it has to react to the id appearing. Keyed on the id alone:
279
+ // a group the user opened by hand survives navigation within it, and only a
280
+ // move to a DIFFERENT group re-narrows the sidebar to that one.
281
+ React.useEffect(() => {
282
+ if (expandedGroupIds !== undefined) return
283
+ if (openByDefaultGroupId === null) return
284
+ // Only the current group stays open (section 21): moving to another group
285
+ // folds the rest.
286
+ setExpandedState([openByDefaultGroupId])
287
+ }, [openByDefaultGroupId, expandedGroupIds])
288
+
289
+ const toggleGroup = (id: string) => {
290
+ // Opening a group folds the others; only the current group stays open.
291
+ const next = expanded.includes(id) ? expanded.filter((groupId) => groupId !== id) : [id]
292
+ if (expandedGroupIds === undefined) setExpandedState(next)
293
+ onExpandedGroupsChange?.(next)
294
+ }
295
+
296
+ const Link = linkComponent ?? "a"
297
+
298
+ // While a query is active the disclosure state is bypassed entirely: the
299
+ // point of the box is to reach an item without knowing which group holds
300
+ // it. Groups with no match drop out so the result list stays short.
301
+ const pinned = React.useMemo(
302
+ () => (searching ? [] : groups.filter((group) => pinnedGroupIds?.includes(group.id))),
303
+ [groups, pinnedGroupIds, searching]
304
+ )
305
+ const rendered = React.useMemo(() => {
306
+ if (!searching) {
307
+ return groups
308
+ .filter((group) => !pinnedGroupIds?.includes(group.id))
309
+ .map((group) => ({ group, items: group.items }))
310
+ }
311
+ return groups
312
+ .map((group) => ({
313
+ group,
314
+ items: group.items.filter((item) => matches(item.label, normalizedQuery)),
315
+ }))
316
+ .filter((entry) => entry.items.length > 0)
317
+ }, [groups, searching, normalizedQuery, pinnedGroupIds])
318
+
319
+ // A group is open when it cannot be closed (no header to click, or the
320
+ // feature is off), when the icon rail hides the headers anyway, while a
321
+ // query is bypassing disclosure, or when the user/default opened it.
322
+ const isGroupOpen = (group: AppSidebarNavGroup): boolean =>
323
+ !collapsibleGroups ||
324
+ !group.label ||
325
+ collapsed ||
326
+ searching ||
327
+ expanded.includes(group.id)
328
+
329
+ const renderItem = (item: AppSidebarNavItem) => {
330
+ const count = item.count && item.count > 0 ? item.count : 0
331
+ const countText = item.countLabel ?? String(count)
332
+ return (
333
+ <Link
334
+ key={item.id}
335
+ href={item.href}
336
+ aria-current={item.active ? "page" : undefined}
337
+ title={collapsed ? item.label : undefined}
338
+ aria-label={collapsed && count > 0 ? `${item.label}, ${countText}` : undefined}
339
+ className={cn(
340
+ "relative flex items-center gap-2.5 rounded-md px-2 py-[7px] text-sm font-medium transition-colors outline-none focus-visible:ring-2 focus-visible:ring-ring max-md:min-h-11",
341
+ item.active
342
+ ? "bg-sidebar-primary text-sidebar-primary-foreground"
343
+ : "text-sidebar-foreground hover:bg-sidebar-accent hover:text-sidebar-accent-foreground",
344
+ collapsed && "mx-auto h-9 w-10 justify-center px-0 py-0"
345
+ )}
346
+ >
347
+ {item.icon && <span className="shrink-0 [&_svg]:size-5">{item.icon}</span>}
348
+ {!collapsed && <span className="min-w-0 flex-1 truncate">{item.label}</span>}
349
+ {count > 0 && !collapsed && (
350
+ <span
351
+ data-slot="app-sidebar-count"
352
+ className="rounded-full bg-primary px-1.5 py-px text-xs leading-4 font-semibold text-primary-foreground tabular-nums"
353
+ >
354
+ <span aria-hidden="true">{count}</span>
355
+ <span className="sr-only">{countText}</span>
356
+ </span>
357
+ )}
358
+ {count > 0 && collapsed && (
359
+ <span
360
+ data-slot="app-sidebar-count-dot"
361
+ aria-hidden="true"
362
+ className="absolute end-2 top-[7px] size-[7px] rounded-full bg-primary ring-2 ring-sidebar"
363
+ />
364
+ )}
365
+ </Link>
366
+ )
367
+ }
368
+
369
+ // Only the links scroll (PwShortSidebar, PW.10, WHIT-827): the logo and
370
+ // switchers at the top and the pinned groups and footer at the bottom stay
371
+ // put on any window height. A soft fade at either end says there is more;
372
+ // once scrolled, a line appears under the top. The current page is scrolled
373
+ // into view when the page opens.
374
+ const navRef = React.useRef<HTMLElement>(null)
375
+ const [edges, setEdges] = React.useState({ top: true, bottom: true })
376
+ const measure = React.useCallback(() => {
377
+ const el = navRef.current
378
+ if (!el) return
379
+ const top = el.scrollTop <= 1
380
+ const bottom = el.scrollTop + el.clientHeight >= el.scrollHeight - 1
381
+ setEdges((prev) => (prev.top === top && prev.bottom === bottom ? prev : { top, bottom }))
382
+ }, [])
383
+ const activeId = React.useMemo(
384
+ () => groups.flatMap((group) => group.items).find((item) => item.active)?.id,
385
+ [groups]
386
+ )
387
+ React.useEffect(() => {
388
+ const el = navRef.current
389
+ if (!el) return
390
+ el.querySelector<HTMLElement>('[aria-current="page"]')?.scrollIntoView?.({ block: "nearest" })
391
+ measure()
392
+ if (typeof ResizeObserver === "undefined") return
393
+ const observer = new ResizeObserver(measure)
394
+ observer.observe(el)
395
+ return () => observer.disconnect()
396
+ }, [activeId, measure, expanded, collapsed])
397
+
398
+ const nav = (
399
+ <div className={cn("relative flex min-h-0 flex-1 flex-col", !edges.top && "border-t border-sidebar-border")}>
400
+ <nav
401
+ ref={navRef}
402
+ onScroll={measure}
403
+ data-slot="app-sidebar-nav"
404
+ aria-label={navLabel}
405
+ className={cn("min-h-0 flex-1 space-y-3 overflow-x-hidden overflow-y-auto overscroll-contain px-3 py-3", collapsed && "px-2")}
406
+ >
407
+ {rendered.map(({ group, items }, index) => {
408
+ const open = isGroupOpen(group)
409
+ const showHeading = Boolean(group.label) && !collapsed
410
+ const isDisclosure = showHeading && collapsibleGroups && !searching
411
+ const bodyId = `app-sidebar-group-${group.id}`
412
+
413
+ return (
414
+ <div
415
+ key={group.id}
416
+ data-slot="app-sidebar-group"
417
+ className={cn("grid gap-px", collapsed && index > 0 && "border-t border-sidebar-border pt-2")}
418
+ >
419
+ {showHeading && isDisclosure && (
420
+ <button
421
+ type="button"
422
+ data-slot="app-sidebar-group-toggle"
423
+ data-group-id={group.id}
424
+ aria-expanded={open}
425
+ aria-controls={bodyId}
426
+ onClick={() => toggleGroup(group.id)}
427
+ className="flex min-h-8 w-full items-center gap-1 rounded-md px-2 py-1 max-md:min-h-11 text-xs font-medium text-muted-foreground outline-none hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2 focus-visible:ring-ring"
428
+ >
429
+ {open ? (
430
+ <IconChevronDown className="size-3 shrink-0" />
431
+ ) : (
432
+ <IconChevronRight className="size-3 shrink-0 rtl:rotate-180" />
433
+ )}
434
+ <span className="truncate">{group.label}</span>
435
+ </button>
436
+ )}
437
+ {showHeading && !isDisclosure && (
438
+ <div className="px-2 py-1 text-xs font-medium text-muted-foreground">
439
+ {group.label}
440
+ </div>
441
+ )}
442
+ {/*
443
+ A closed group's items are HIDDEN, not unmounted.
444
+
445
+ `hidden` is the correct disclosure semantic — collapsed content
446
+ must leave the accessibility tree, or a screen reader announces
447
+ links the sighted user cannot see. It follows that a role/name
448
+ query cannot find an item in a closed group, in Playwright as
449
+ much as in Testing Library.
450
+
451
+ Keeping them mounted is still deliberate: a DOM/CSS query can
452
+ tell "this group is closed" (present, hidden) from "RBAC removed
453
+ this link for this role" (absent). Unmounting would collapse
454
+ both into "absent", and any suite asserting absence as proof of
455
+ permission filtering would keep passing after that filtering
456
+ broke. Either query by CSS or open the group first.
457
+ */}
458
+ <div id={bodyId} hidden={!open} className="grid gap-px">
459
+ {items.map(renderItem)}
460
+ </div>
461
+ </div>
462
+ )
463
+ })}
464
+ {searching && rendered.length === 0 && (
465
+ <p data-slot="app-sidebar-no-results" className="px-2 py-1 text-xs/relaxed text-muted-foreground">
466
+ {searchNoResultsLabel}
467
+ </p>
468
+ )}
469
+ </nav>
470
+ {!edges.top && (
471
+ <div aria-hidden="true" data-slot="app-sidebar-fade-top" className="pointer-events-none absolute inset-x-0 top-0 h-6 bg-gradient-to-b from-sidebar to-transparent" />
472
+ )}
473
+ {!edges.bottom && (
474
+ <div aria-hidden="true" data-slot="app-sidebar-fade-bottom" className="pointer-events-none absolute inset-x-0 bottom-0 h-6 bg-gradient-to-t from-sidebar to-transparent" />
475
+ )}
476
+ </div>
477
+ )
478
+
479
+ const pinnedSection = pinned.length > 0 && (
480
+ <div data-slot="app-sidebar-pinned" className={cn("grid shrink-0 gap-px border-t border-sidebar-border px-3 py-2", collapsed && "px-2")}>
481
+ {pinned.flatMap((group) => group.items).map(renderItem)}
482
+ </div>
483
+ )
484
+
485
+ // Hidden in the icon rail: a 4rem-wide column has nowhere to put a text
486
+ // field, and the collapse toggle is right there to widen it again.
487
+ const search = searchable && !collapsed && (
488
+ <div className="shrink-0 px-2.5 pt-2.5">
489
+ <div className="relative flex items-center">
490
+ <IconSearch className="pointer-events-none absolute start-2 size-3.5 text-muted-foreground" />
491
+ <input
492
+ type="search"
493
+ data-slot="app-sidebar-search"
494
+ value={query}
495
+ onChange={(event) => setQuery(event.target.value)}
496
+ onKeyDown={(event) => {
497
+ if (event.key === "Escape") setQuery("")
498
+ }}
499
+ placeholder={searchPlaceholder}
500
+ aria-label={searchPlaceholder}
501
+ className="h-8 w-full rounded-md border border-sidebar-border bg-background/50 ps-7 pe-7 max-md:h-11 max-md:text-sm text-xs/relaxed text-sidebar-foreground outline-none placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-2 focus-visible:ring-ring/30 [&::-webkit-search-cancel-button]:hidden"
502
+ />
503
+ {query !== "" && (
504
+ <button
505
+ type="button"
506
+ aria-label={clearSearchLabel}
507
+ onClick={() => setQuery("")}
508
+ className="absolute end-1 flex size-6 items-center justify-center rounded-md text-sidebar-foreground/70 outline-none hover:bg-sidebar-accent focus-visible:ring-2 focus-visible:ring-ring/30"
509
+ >
510
+ <IconX className="size-3.5" />
511
+ </button>
512
+ )}
513
+ </div>
514
+ </div>
515
+ )
516
+
517
+ return (
518
+ <>
519
+ {/* Mobile trigger — shown only below the md breakpoint; the sidebar
520
+ itself is fixed off-canvas until toggled open. */}
521
+ <button
522
+ type="button"
523
+ aria-label={openNavLabel}
524
+ onClick={() => setMobileOpen(true)}
525
+ className={cn(
526
+ "fixed start-3 top-[calc(0.75rem+env(safe-area-inset-top))] z-40 flex size-11 items-center justify-center rounded-control-lg border border-border bg-card outline-none focus-visible:border-ring focus-visible:ring-2 focus-visible:ring-ring/30 md:hidden",
527
+ phoneTabBar && "max-sm:hidden"
528
+ )}
529
+ >
530
+ <IconMenu2 className="size-5" />
531
+ </button>
532
+
533
+ {mobileOpen && (
534
+ <div
535
+ aria-hidden="true"
536
+ onClick={() => setMobileOpen(false)}
537
+ className="fixed inset-0 z-40 bg-overlay md:hidden"
538
+ />
539
+ )}
540
+
541
+ <aside
542
+ data-slot="app-sidebar"
543
+ data-collapsed={collapsed || undefined}
544
+ inert={phoneDrawer && !mobileOpen ? true : undefined}
545
+ className={cn(
546
+ "fixed inset-y-0 start-0 z-50 flex h-dvh overflow-hidden w-[min(85vw,20rem)] -translate-x-full flex-col border-e border-sidebar-border bg-sidebar pt-[env(safe-area-inset-top)] pb-[env(safe-area-inset-bottom)] text-sidebar-foreground transition-all duration-200 rtl:translate-x-full md:w-60 md:pt-0 md:pb-0",
547
+ mobileOpen && "translate-x-0 rtl:translate-x-0",
548
+ "md:sticky md:top-0 md:z-auto md:h-auto md:self-stretch md:shrink-0 md:translate-x-0 rtl:md:translate-x-0",
549
+ collapsed && "md:w-14",
550
+ phoneTabBar && "max-sm:hidden",
551
+ className
552
+ )}
553
+ >
554
+ <button
555
+ type="button"
556
+ aria-label={closeNavLabel}
557
+ onClick={() => setMobileOpen(false)}
558
+ className="absolute end-2 top-[calc(0.5rem+env(safe-area-inset-top))] z-20 flex size-11 items-center justify-center rounded-control-lg text-sidebar-foreground/70 outline-none hover:bg-sidebar-accent focus-visible:ring-2 focus-visible:ring-ring/30 md:hidden"
559
+ >
560
+ <IconX className="size-5" />
561
+ </button>
562
+
563
+ {(header || tenantSwitcher || teamSwitcher) && (
564
+ <div className={cn("flex shrink-0 flex-col gap-3 px-3 pt-4 pe-14 md:pe-3", collapsed && "items-center md:px-2")}>
565
+ {header && <div className="px-1.5 pt-0.5 pb-1.5">{header}</div>}
566
+ {(tenantSwitcher || teamSwitcher) && (
567
+ <div className="flex flex-col gap-1.5">
568
+ {tenantSwitcher}
569
+ {teamSwitcher}
570
+ </div>
571
+ )}
572
+ </div>
573
+ )}
574
+
575
+ {search}
576
+
577
+ {nav}
578
+
579
+ {pinnedSection}
580
+
581
+ <button
582
+ type="button"
583
+ aria-label={collapsed ? expandAriaLabel : collapseAriaLabel}
584
+ onClick={() => setCollapsed(!collapsed)}
585
+ className={cn(
586
+ "hidden shrink-0 items-center justify-center gap-1.5 border-t border-sidebar-border px-3 py-2 text-xs text-muted-foreground outline-none hover:bg-sidebar-accent focus-visible:ring-2 focus-visible:ring-ring",
587
+ !forcedRail && "md:flex"
588
+ )}
589
+ >
590
+ {collapsed ? (
591
+ <IconChevronRight className="size-3.5 rtl:rotate-180" />
592
+ ) : (
593
+ <>
594
+ <IconChevronLeft className="size-3.5 rtl:rotate-180" />
595
+ <span>{collapseLabel}</span>
596
+ </>
597
+ )}
598
+ </button>
599
+
600
+ {footer && <div className="shrink-0 border-t border-sidebar-border px-3 py-3">{footer}</div>}
601
+ </aside>
602
+ </>
603
+ )
604
+ }