@whity/ui 0.0.0-stage → 0.5.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 (169) hide show
  1. package/README.md +107 -3
  2. package/package.json +192 -6
  3. package/src/access-denied.stories.tsx +179 -0
  4. package/src/access-denied.tsx +305 -0
  5. package/src/action-proposal.tsx +302 -0
  6. package/src/actor-chip.tsx +71 -0
  7. package/src/agent-activity.tsx +163 -0
  8. package/src/agent-collaboration.stories.tsx +156 -0
  9. package/src/agent-run-status.tsx +196 -0
  10. package/src/alert-dialog.stories.tsx +45 -0
  11. package/src/alert-dialog.tsx +147 -0
  12. package/src/alert.stories.tsx +91 -0
  13. package/src/alert.tsx +89 -0
  14. package/src/app-sidebar.stories.tsx +195 -0
  15. package/src/app-sidebar.tsx +500 -0
  16. package/src/approval-queue.tsx +108 -0
  17. package/src/audience-group-picker.stories.tsx +198 -0
  18. package/src/audience-group-picker.tsx +362 -0
  19. package/src/audience-people-picker.stories.tsx +102 -0
  20. package/src/audience-people-picker.tsx +279 -0
  21. package/src/avatar.tsx +83 -0
  22. package/src/badge.stories.tsx +105 -0
  23. package/src/badge.tsx +68 -0
  24. package/src/beta-warning.stories.tsx +79 -0
  25. package/src/beta-warning.tsx +189 -0
  26. package/src/bilingual-input.stories.tsx +82 -0
  27. package/src/bilingual-input.tsx +183 -0
  28. package/src/breadcrumb.stories.tsx +51 -0
  29. package/src/breadcrumb.tsx +90 -0
  30. package/src/button.stories.tsx +87 -0
  31. package/src/button.tsx +118 -0
  32. package/src/card.stories.tsx +423 -0
  33. package/src/card.tsx +180 -0
  34. package/src/chart.stories.tsx +66 -0
  35. package/src/chart.tsx +228 -0
  36. package/src/checkbox.stories.tsx +62 -0
  37. package/src/checkbox.tsx +54 -0
  38. package/src/collapsible.stories.tsx +48 -0
  39. package/src/collapsible.tsx +34 -0
  40. package/src/color-card.stories.tsx +61 -0
  41. package/src/color-card.tsx +270 -0
  42. package/src/color-picker.stories.tsx +65 -0
  43. package/src/color-picker.tsx +425 -0
  44. package/src/command-palette.tsx +306 -0
  45. package/src/copy-block.stories.tsx +38 -0
  46. package/src/copy-block.tsx +85 -0
  47. package/src/data-table.stories.tsx +200 -0
  48. package/src/data-table.tsx +1106 -0
  49. package/src/date-picker.stories.tsx +61 -0
  50. package/src/date-picker.tsx +422 -0
  51. package/src/design-cards.stories.tsx +458 -0
  52. package/src/dialog.stories.tsx +48 -0
  53. package/src/dialog.tsx +166 -0
  54. package/src/documents/barcode-svg.stories.tsx +72 -0
  55. package/src/documents/barcode-svg.tsx +79 -0
  56. package/src/dropdown-menu.stories.tsx +77 -0
  57. package/src/dropdown-menu.tsx +271 -0
  58. package/src/editor/behaviour.ts +279 -0
  59. package/src/editor/block-ids.ts +44 -0
  60. package/src/editor/block-list.tsx +101 -0
  61. package/src/editor/block-toolbar.tsx +477 -0
  62. package/src/editor/bubble.tsx +93 -0
  63. package/src/editor/catalogue.ts +103 -0
  64. package/src/editor/collab-rail.tsx +746 -0
  65. package/src/editor/collab.stories.tsx +207 -0
  66. package/src/editor/collab.ts +87 -0
  67. package/src/editor/contract.ts +408 -0
  68. package/src/editor/convert.ts +362 -0
  69. package/src/editor/doc-editor.stories.tsx +98 -0
  70. package/src/editor/doc-editor.tsx +1287 -0
  71. package/src/editor/editor.css +536 -0
  72. package/src/editor/extensions.ts +86 -0
  73. package/src/editor/field-picker.tsx +107 -0
  74. package/src/editor/fields.ts +100 -0
  75. package/src/editor/gutter.tsx +367 -0
  76. package/src/editor/index.ts +22 -0
  77. package/src/editor/label-sheet.tsx +289 -0
  78. package/src/editor/labels.ts +310 -0
  79. package/src/editor/link-tools.tsx +141 -0
  80. package/src/editor/node-views.tsx +450 -0
  81. package/src/editor/page-view.tsx +353 -0
  82. package/src/editor/publish-check.tsx +182 -0
  83. package/src/editor/rich-blocks.stories.tsx +146 -0
  84. package/src/editor/sample.ts +40 -0
  85. package/src/editor/schema-rich.ts +150 -0
  86. package/src/editor/schema.ts +315 -0
  87. package/src/editor/services.tsx +127 -0
  88. package/src/editor/slash.ts +64 -0
  89. package/src/editor/suggesting.ts +372 -0
  90. package/src/editor/suggestions.ts +763 -0
  91. package/src/editor/table-keys.ts +66 -0
  92. package/src/editor/templates.stories.tsx +324 -0
  93. package/src/editor/toolbar.tsx +358 -0
  94. package/src/empty-state.stories.tsx +51 -0
  95. package/src/empty-state.tsx +100 -0
  96. package/src/empty-state.usage-notes.md +30 -0
  97. package/src/form.stories.tsx +112 -0
  98. package/src/form.tsx +176 -0
  99. package/src/globals.css +20 -0
  100. package/src/icon-button.tsx +85 -0
  101. package/src/icon-map.ts +244 -0
  102. package/src/icon.tsx +41 -0
  103. package/src/index.ts +67 -0
  104. package/src/input.stories.tsx +157 -0
  105. package/src/input.tsx +759 -0
  106. package/src/locked-screen.stories.tsx +29 -0
  107. package/src/locked-screen.tsx +61 -0
  108. package/src/mark.tsx +62 -0
  109. package/src/math-text.stories.tsx +33 -0
  110. package/src/math-text.tsx +43 -0
  111. package/src/menu-bar.stories.tsx +198 -0
  112. package/src/menu-bar.tsx +373 -0
  113. package/src/money/currency.ts +230 -0
  114. package/src/page-header.stories.tsx +95 -0
  115. package/src/page-header.tsx +122 -0
  116. package/src/page-shell.stories.tsx +42 -0
  117. package/src/page-shell.tsx +32 -0
  118. package/src/pagination.stories.tsx +45 -0
  119. package/src/pagination.tsx +98 -0
  120. package/src/plugin-card.stories.tsx +180 -0
  121. package/src/plugin-card.tsx +848 -0
  122. package/src/popover.stories.tsx +48 -0
  123. package/src/popover.tsx +55 -0
  124. package/src/radio-group.stories.tsx +26 -0
  125. package/src/radio-group.tsx +44 -0
  126. package/src/route-flow/editor.tsx +1014 -0
  127. package/src/route-flow/model.ts +716 -0
  128. package/src/scope-grant.tsx +139 -0
  129. package/src/screen-too-small.stories.tsx +61 -0
  130. package/src/screen-too-small.tsx +104 -0
  131. package/src/select.stories.tsx +44 -0
  132. package/src/select.tsx +197 -0
  133. package/src/separator.stories.tsx +34 -0
  134. package/src/separator.tsx +34 -0
  135. package/src/sheet.stories.tsx +49 -0
  136. package/src/sheet.tsx +168 -0
  137. package/src/skeleton.stories.tsx +28 -0
  138. package/src/skeleton.tsx +19 -0
  139. package/src/slider.stories.tsx +78 -0
  140. package/src/slider.tsx +300 -0
  141. package/src/spinner.stories.tsx +33 -0
  142. package/src/spinner.tsx +55 -0
  143. package/src/switch.stories.tsx +42 -0
  144. package/src/switch.tsx +41 -0
  145. package/src/switcher.stories.tsx +111 -0
  146. package/src/switcher.tsx +131 -0
  147. package/src/table.stories.tsx +48 -0
  148. package/src/table.tsx +121 -0
  149. package/src/tabs.stories.tsx +74 -0
  150. package/src/tabs.tsx +90 -0
  151. package/src/tag-input.stories.tsx +67 -0
  152. package/src/tag-input.tsx +117 -0
  153. package/src/tenant-logo.tsx +63 -0
  154. package/src/tenant-switcher.stories.tsx +38 -0
  155. package/src/tenant-switcher.tsx +164 -0
  156. package/src/textarea.stories.tsx +27 -0
  157. package/src/textarea.tsx +18 -0
  158. package/src/theme-mode.ts +52 -0
  159. package/src/time-picker.stories.tsx +62 -0
  160. package/src/time-picker.tsx +615 -0
  161. package/src/toggle-group.tsx +138 -0
  162. package/src/tooltip.stories.tsx +43 -0
  163. package/src/tooltip.tsx +73 -0
  164. package/src/top-nav.stories.tsx +58 -0
  165. package/src/top-nav.tsx +324 -0
  166. package/src/undo-bar.tsx +107 -0
  167. package/src/utils.ts +45 -0
  168. package/src/view-toggle.stories.tsx +84 -0
  169. package/src/view-toggle.tsx +110 -0
@@ -0,0 +1,373 @@
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+ import { Menubar as MenubarPrimitive } from "radix-ui"
5
+ import { IconCheck, IconChevronRight } from "@tabler/icons-react"
6
+
7
+ import { cn, useIsDarkMode } from "./utils"
8
+
9
+ /**
10
+ * A desktop-application menu bar (File / Edit / Insert / …), as found in Google
11
+ * Docs, Word and every canvas editor — the home for a command set far too large
12
+ * for a single toolbar row.
13
+ *
14
+ * Two layers, mirroring `dropdown-menu.tsx`:
15
+ *
16
+ * 1. Styled primitive parts (`MenuBarRoot`, `MenuBarItem`, …) over Radix
17
+ * `Menubar`, which supplies the behaviour that makes a menu bar feel native
18
+ * and stays accessible: roving tab focus across the menus, arrow-key
19
+ * traversal, hover-to-switch once a menu is open, character typeahead,
20
+ * Escape/click-outside dismissal and focus restoration.
21
+ * 2. A DECLARATIVE renderer — `<MenuBar menus={…} />` — that draws a plain
22
+ * `MenuBarMenu[]` model. Prefer this: it lets an app describe its commands
23
+ * once (a command registry) and render them here, in a toolbar and in a
24
+ * shortcuts sheet without restating any of them. Every node carries its own
25
+ * `disabled`/`checked` state, so the model is the single source of truth for
26
+ * whether a command is currently available.
27
+ */
28
+
29
+ // ── the declarative model ───────────────────────────────────────────────────
30
+
31
+ /** A command: runs `onSelect` when chosen. */
32
+ export interface MenuBarActionNode {
33
+ kind?: "item"
34
+ id: string
35
+ label: React.ReactNode
36
+ icon?: React.ReactNode
37
+ /** Display-only shortcut hint, e.g. "Ctrl+S". Binding it is the app's job. */
38
+ shortcut?: string
39
+ disabled?: boolean
40
+ /** Renders in the destructive tone (delete, remove). */
41
+ destructive?: boolean
42
+ onSelect: () => void
43
+ }
44
+
45
+ /** A toggle: shows a tick when `checked`. */
46
+ export interface MenuBarCheckboxNode {
47
+ kind: "checkbox"
48
+ id: string
49
+ label: React.ReactNode
50
+ checked: boolean
51
+ shortcut?: string
52
+ disabled?: boolean
53
+ onCheckedChange: (checked: boolean) => void
54
+ }
55
+
56
+ /** A nested menu. An empty `items` renders a disabled placeholder, never a
57
+ * dead end that opens onto nothing. */
58
+ export interface MenuBarSubmenuNode {
59
+ kind: "submenu"
60
+ id: string
61
+ label: React.ReactNode
62
+ icon?: React.ReactNode
63
+ disabled?: boolean
64
+ items: MenuBarNode[]
65
+ /** Shown (disabled) when `items` is empty. */
66
+ emptyLabel?: React.ReactNode
67
+ }
68
+
69
+ export interface MenuBarSeparatorNode {
70
+ kind: "separator"
71
+ id: string
72
+ }
73
+
74
+ /** A non-interactive heading, for grouping inside a long menu. */
75
+ export interface MenuBarLabelNode {
76
+ kind: "label"
77
+ id: string
78
+ label: React.ReactNode
79
+ }
80
+
81
+ export type MenuBarNode =
82
+ | MenuBarActionNode
83
+ | MenuBarCheckboxNode
84
+ | MenuBarSubmenuNode
85
+ | MenuBarSeparatorNode
86
+ | MenuBarLabelNode
87
+
88
+ export interface MenuBarMenu {
89
+ id: string
90
+ label: React.ReactNode
91
+ disabled?: boolean
92
+ items: MenuBarNode[]
93
+ }
94
+
95
+ // ── styled primitive parts ──────────────────────────────────────────────────
96
+
97
+ function MenuBarRoot({ className, ...props }: React.ComponentProps<typeof MenubarPrimitive.Root>) {
98
+ return (
99
+ <MenubarPrimitive.Root
100
+ data-slot="menu-bar"
101
+ className={cn("flex items-center gap-0.5", className)}
102
+ {...props}
103
+ />
104
+ )
105
+ }
106
+
107
+ function MenuBarMenuRoot({ ...props }: React.ComponentProps<typeof MenubarPrimitive.Menu>) {
108
+ return <MenubarPrimitive.Menu data-slot="menu-bar-menu" {...props} />
109
+ }
110
+
111
+ function MenuBarTrigger({ className, ...props }: React.ComponentProps<typeof MenubarPrimitive.Trigger>) {
112
+ return (
113
+ <MenubarPrimitive.Trigger
114
+ data-slot="menu-bar-trigger"
115
+ className={cn(
116
+ "flex select-none items-center rounded-md px-2 py-1 text-xs font-medium text-foreground outline-hidden hover:bg-accent hover:text-accent-foreground focus-visible:ring-2 focus-visible:ring-ring/40 data-open:bg-accent data-open:text-accent-foreground data-disabled:pointer-events-none data-disabled:opacity-50",
117
+ className
118
+ )}
119
+ {...props}
120
+ />
121
+ )
122
+ }
123
+
124
+ function MenuBarContent({
125
+ className,
126
+ align = "start",
127
+ sideOffset = 4,
128
+ ...props
129
+ }: React.ComponentProps<typeof MenubarPrimitive.Content>) {
130
+ const isDark = useIsDarkMode()
131
+
132
+ return (
133
+ <MenubarPrimitive.Portal>
134
+ <MenubarPrimitive.Content
135
+ data-slot="menu-bar-content"
136
+ align={align}
137
+ sideOffset={sideOffset}
138
+ className={cn(
139
+ isDark && "dark",
140
+ "z-50 max-h-(--radix-menubar-content-available-height) min-w-52 origin-(--radix-menubar-content-transform-origin) overflow-x-hidden overflow-y-auto rounded-lg border border-border bg-popover p-1 text-popover-foreground shadow-md duration-100 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95",
141
+ className
142
+ )}
143
+ {...props}
144
+ />
145
+ </MenubarPrimitive.Portal>
146
+ )
147
+ }
148
+
149
+ function MenuBarItem({
150
+ className,
151
+ variant = "default",
152
+ ...props
153
+ }: React.ComponentProps<typeof MenubarPrimitive.Item> & { variant?: "default" | "destructive" }) {
154
+ return (
155
+ <MenubarPrimitive.Item
156
+ data-slot="menu-bar-item"
157
+ data-variant={variant}
158
+ className={cn(
159
+ "group/menu-bar-item relative flex min-h-7 cursor-default items-center gap-2 rounded-md px-2 py-1 text-xs/relaxed outline-hidden select-none focus:bg-accent focus:text-accent-foreground not-data-[variant=destructive]:focus:**:text-accent-foreground data-[variant=destructive]:text-destructive data-[variant=destructive]:focus:bg-destructive/10 data-[variant=destructive]:focus:text-destructive dark:data-[variant=destructive]:focus:bg-destructive/20 data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-3.5 data-[variant=destructive]:*:[svg]:text-destructive",
160
+ className
161
+ )}
162
+ {...props}
163
+ />
164
+ )
165
+ }
166
+
167
+ function MenuBarCheckboxItem({
168
+ className,
169
+ children,
170
+ checked,
171
+ ...props
172
+ }: React.ComponentProps<typeof MenubarPrimitive.CheckboxItem>) {
173
+ return (
174
+ <MenubarPrimitive.CheckboxItem
175
+ data-slot="menu-bar-checkbox-item"
176
+ checked={checked}
177
+ className={cn(
178
+ "group/menu-bar-item relative flex min-h-7 cursor-default items-center gap-2 rounded-md py-1 pe-8 ps-2 text-xs/relaxed outline-hidden select-none focus:bg-accent focus:text-accent-foreground focus:**:text-accent-foreground data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-3.5",
179
+ className
180
+ )}
181
+ {...props}
182
+ >
183
+ <span className="pointer-events-none absolute end-2 flex items-center justify-center">
184
+ <MenubarPrimitive.ItemIndicator>
185
+ <IconCheck />
186
+ </MenubarPrimitive.ItemIndicator>
187
+ </span>
188
+ {children}
189
+ </MenubarPrimitive.CheckboxItem>
190
+ )
191
+ }
192
+
193
+ function MenuBarLabel({ className, ...props }: React.ComponentProps<typeof MenubarPrimitive.Label>) {
194
+ return (
195
+ <MenubarPrimitive.Label
196
+ data-slot="menu-bar-label"
197
+ className={cn("px-2 py-1 text-[0.625rem] font-semibold uppercase tracking-wide text-muted-foreground", className)}
198
+ {...props}
199
+ />
200
+ )
201
+ }
202
+
203
+ function MenuBarSeparator({ className, ...props }: React.ComponentProps<typeof MenubarPrimitive.Separator>) {
204
+ return (
205
+ <MenubarPrimitive.Separator
206
+ data-slot="menu-bar-separator"
207
+ className={cn("-mx-1 my-1 h-px bg-border/50", className)}
208
+ {...props}
209
+ />
210
+ )
211
+ }
212
+
213
+ function MenuBarShortcut({ className, ...props }: React.ComponentProps<"span">) {
214
+ return (
215
+ <span
216
+ data-slot="menu-bar-shortcut"
217
+ className={cn(
218
+ "ms-auto ps-4 text-[0.625rem] tracking-widest text-muted-foreground group-focus/menu-bar-item:text-accent-foreground",
219
+ className
220
+ )}
221
+ {...props}
222
+ />
223
+ )
224
+ }
225
+
226
+ function MenuBarSub({ ...props }: React.ComponentProps<typeof MenubarPrimitive.Sub>) {
227
+ return <MenubarPrimitive.Sub data-slot="menu-bar-sub" {...props} />
228
+ }
229
+
230
+ function MenuBarSubTrigger({
231
+ className,
232
+ children,
233
+ ...props
234
+ }: React.ComponentProps<typeof MenubarPrimitive.SubTrigger>) {
235
+ return (
236
+ <MenubarPrimitive.SubTrigger
237
+ data-slot="menu-bar-sub-trigger"
238
+ className={cn(
239
+ "flex min-h-7 cursor-default items-center gap-2 rounded-md px-2 py-1 text-xs/relaxed outline-hidden select-none focus:bg-accent focus:text-accent-foreground focus:**:text-accent-foreground data-open:bg-accent data-open:text-accent-foreground data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-3.5",
240
+ className
241
+ )}
242
+ {...props}
243
+ >
244
+ {children}
245
+ <IconChevronRight className="ms-auto rtl:rotate-180" />
246
+ </MenubarPrimitive.SubTrigger>
247
+ )
248
+ }
249
+
250
+ function MenuBarSubContent({ className, ...props }: React.ComponentProps<typeof MenubarPrimitive.SubContent>) {
251
+ const isDark = useIsDarkMode()
252
+
253
+ return (
254
+ <MenubarPrimitive.Portal>
255
+ <MenubarPrimitive.SubContent
256
+ data-slot="menu-bar-sub-content"
257
+ className={cn(
258
+ isDark && "dark",
259
+ "z-50 max-h-(--radix-menubar-content-available-height) min-w-44 origin-(--radix-menubar-content-transform-origin) overflow-x-hidden overflow-y-auto rounded-lg border border-border bg-popover p-1 text-popover-foreground shadow-md duration-100 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95",
260
+ className
261
+ )}
262
+ {...props}
263
+ />
264
+ </MenubarPrimitive.Portal>
265
+ )
266
+ }
267
+
268
+ // ── the declarative renderer ────────────────────────────────────────────────
269
+
270
+ /** Render one model node (recursively, for submenus). */
271
+ function MenuBarNodes({ items, emptyLabel }: { items: MenuBarNode[]; emptyLabel?: React.ReactNode }) {
272
+ if (items.length === 0) {
273
+ return <MenuBarItem disabled>{emptyLabel ?? "Nothing here yet"}</MenuBarItem>
274
+ }
275
+ return (
276
+ <>
277
+ {items.map((node) => {
278
+ if ("kind" in node && node.kind === "separator") {
279
+ return <MenuBarSeparator key={node.id} />
280
+ }
281
+ if ("kind" in node && node.kind === "label") {
282
+ return <MenuBarLabel key={node.id}>{node.label}</MenuBarLabel>
283
+ }
284
+ if ("kind" in node && node.kind === "checkbox") {
285
+ return (
286
+ <MenuBarCheckboxItem
287
+ key={node.id}
288
+ data-testid={`menu-item-${node.id}`}
289
+ checked={node.checked}
290
+ disabled={node.disabled}
291
+ // Radix closes the menu on select by default; a toggle is more
292
+ // useful kept open so several can be flipped in one visit.
293
+ onSelect={(e) => e.preventDefault()}
294
+ onCheckedChange={node.onCheckedChange}
295
+ >
296
+ {node.label}
297
+ {node.shortcut && <MenuBarShortcut>{node.shortcut}</MenuBarShortcut>}
298
+ </MenuBarCheckboxItem>
299
+ )
300
+ }
301
+ if ("kind" in node && node.kind === "submenu") {
302
+ return (
303
+ <MenuBarSub key={node.id}>
304
+ <MenuBarSubTrigger data-testid={`menu-item-${node.id}`} disabled={node.disabled}>
305
+ {node.icon}
306
+ {node.label}
307
+ </MenuBarSubTrigger>
308
+ <MenuBarSubContent>
309
+ <MenuBarNodes items={node.items} emptyLabel={node.emptyLabel} />
310
+ </MenuBarSubContent>
311
+ </MenuBarSub>
312
+ )
313
+ }
314
+ const item = node as MenuBarActionNode
315
+ return (
316
+ <MenuBarItem
317
+ key={item.id}
318
+ data-testid={`menu-item-${item.id}`}
319
+ disabled={item.disabled}
320
+ variant={item.destructive ? "destructive" : "default"}
321
+ onSelect={item.onSelect}
322
+ >
323
+ {item.icon}
324
+ {item.label}
325
+ {item.shortcut && <MenuBarShortcut>{item.shortcut}</MenuBarShortcut>}
326
+ </MenuBarItem>
327
+ )
328
+ })}
329
+ </>
330
+ )
331
+ }
332
+
333
+ export interface MenuBarProps extends Omit<React.ComponentProps<typeof MenubarPrimitive.Root>, "children"> {
334
+ menus: MenuBarMenu[]
335
+ }
336
+
337
+ /**
338
+ * Draw a whole menu bar from a `MenuBarMenu[]` model. Each menu gets a
339
+ * `data-testid` of `menu-<id>` and each item `menu-item-<id>`, so a command can
340
+ * be driven end-to-end in tests by the same id the registry declares it with.
341
+ */
342
+ function MenuBar({ menus, ...props }: MenuBarProps) {
343
+ return (
344
+ <MenuBarRoot {...props}>
345
+ {menus.map((menu) => (
346
+ <MenuBarMenuRoot key={menu.id}>
347
+ <MenuBarTrigger data-testid={`menu-${menu.id}`} disabled={menu.disabled}>
348
+ {menu.label}
349
+ </MenuBarTrigger>
350
+ <MenuBarContent>
351
+ <MenuBarNodes items={menu.items} />
352
+ </MenuBarContent>
353
+ </MenuBarMenuRoot>
354
+ ))}
355
+ </MenuBarRoot>
356
+ )
357
+ }
358
+
359
+ export {
360
+ MenuBar,
361
+ MenuBarRoot,
362
+ MenuBarMenuRoot,
363
+ MenuBarTrigger,
364
+ MenuBarContent,
365
+ MenuBarItem,
366
+ MenuBarCheckboxItem,
367
+ MenuBarLabel,
368
+ MenuBarSeparator,
369
+ MenuBarShortcut,
370
+ MenuBarSub,
371
+ MenuBarSubTrigger,
372
+ MenuBarSubContent,
373
+ }
@@ -0,0 +1,230 @@
1
+ /**
2
+ * Minor units, major units, and the exponent that separates them — the client
3
+ * half of `Whity\Core\Money\Currency`.
4
+ *
5
+ * WHY THERE IS A CLIENT HALF AT ALL. Amounts cross the wire as integers of
6
+ * minor units, which is the only representation that does not lose money to
7
+ * floating point. Turning 5000 into something a person reads therefore happens
8
+ * here, and it cannot be done without knowing that the Jordanian dinar has
9
+ * THREE decimal places: 5000 fils is 5.000 JOD, not 50.00. Dividing by 100 —
10
+ * which is what every hand-rolled formatter does — shows every customer an
11
+ * amount ten times too large.
12
+ *
13
+ * WHY `Intl` IS NOT THE SOURCE OF THE EXPONENT. It is the obvious idea — the
14
+ * engine ships ISO 4217 data, so why transcribe a table? — and it is wrong.
15
+ * `Intl.NumberFormat(...).resolvedOptions().maximumFractionDigits` reports
16
+ * CLDR's DISPLAY convention, not the currency's official exponent, and the two
17
+ * disagree for thirteen currencies whose minor unit is worthless in practice.
18
+ * ICU says the Iraqi dinar has 0 decimal places; ISO 4217 says 3. Lebanese
19
+ * pounds, Afghanis, Serbian dinars and ten others are the same story with 0
20
+ * against 2.
21
+ *
22
+ * Deriving the exponent from `Intl` here would therefore have made the client
23
+ * disagree with the server about what an IQD amount MEANS — by a factor of a
24
+ * thousand — while every JOD and USD test passed. So the table below is the
25
+ * authority, it is ISO's numbers, and it is identical to the PHP one by
26
+ * construction; a test asserts the two tables entry-for-entry and separately
27
+ * pins the ICU divergences so they cannot change unnoticed.
28
+ *
29
+ * `Intl` is still used for what it is actually good at: placing symbols and
30
+ * digits for a locale. `formatMoney` forces the fraction digits to OUR
31
+ * exponent, so the figure is ISO-correct even where CLDR would round it away.
32
+ *
33
+ * WHAT IS DELIBERATELY SPLIT IN TWO. `toDecimalString` is exact — integer
34
+ * arithmetic only, correct for any amount an integer can hold. `formatMoney`
35
+ * is the locale-aware one, and it is what screens use: `Intl` places the symbol
36
+ * and picks the digits, which is how the Arabic build gets Arabic-Indic
37
+ * numerals and correct RTL placement without this file knowing anything about
38
+ * either.
39
+ */
40
+
41
+ /** Two decimal places, which is every active ISO 4217 code not listed below. */
42
+ const DEFAULT_EXPONENT = 2;
43
+
44
+ /**
45
+ * Every active ISO 4217 currency whose exponent is not two. THE AUTHORITY, not
46
+ * a fallback: see the note above on why `Intl` cannot play this role. Identical
47
+ * to the PHP table by construction — `currency-exponent-parity.test.ts` asserts
48
+ * it entry for entry.
49
+ */
50
+ const EXPONENT_EXCEPTIONS: Readonly<Record<string, number>> = {
51
+ BHD: 3, IQD: 3, JOD: 3, KWD: 3, LYD: 3, OMR: 3, TND: 3,
52
+ BIF: 0, CLP: 0, DJF: 0, GNF: 0, ISK: 0, JPY: 0, KMF: 0, KRW: 0, PYG: 0,
53
+ RWF: 0, UGX: 0, UYI: 0, VND: 0, VUV: 0, XAF: 0, XOF: 0, XPF: 0,
54
+ CLF: 4, UYW: 4,
55
+ };
56
+
57
+ const exponentCache = new Map<string, number>();
58
+
59
+ /** Uppercased and trimmed, or `null` when it is not a three-letter code. */
60
+ function normalize(currency: string): string | null {
61
+ const code = currency.trim().toUpperCase();
62
+ return /^[A-Z]{3}$/.test(code) ? code : null;
63
+ }
64
+
65
+ /**
66
+ * The currency's ISO 4217 exponent.
67
+ *
68
+ * Throws on a malformed code rather than assuming two, because an amount
69
+ * rendered against a guessed currency is wrong in a way nobody can see.
70
+ */
71
+ export function currencyExponent(currency: string): number {
72
+ const code = normalize(currency);
73
+ if (code === null) {
74
+ throw new Error(`Not an ISO 4217 currency code: '${currency}'`);
75
+ }
76
+
77
+ const cached = exponentCache.get(code);
78
+ if (cached !== undefined) return cached;
79
+
80
+ // The table, never Intl — see the module note. Asking the engine here would
81
+ // silently make this disagree with the server for IQD, LBP and eleven others.
82
+ const exponent = EXPONENT_EXCEPTIONS[code] ?? DEFAULT_EXPONENT;
83
+
84
+ exponentCache.set(code, exponent);
85
+ return exponent;
86
+ }
87
+
88
+ /** 1000 for JOD, 100 for USD, 1 for JPY. */
89
+ export function minorUnitsPerMajor(currency: string): number {
90
+ return 10 ** currencyExponent(currency);
91
+ }
92
+
93
+ /**
94
+ * Minor units as an exact decimal string: `5000` JOD becomes `"5.000"`.
95
+ *
96
+ * Integer arithmetic throughout, so this is correct for every amount a
97
+ * JavaScript integer holds and never introduces a fractional artefact of its
98
+ * own. No currency code is appended — that is the caller's or `Intl`'s job.
99
+ */
100
+ export function toDecimalString(minorUnits: number, currency: string): string {
101
+ if (!Number.isInteger(minorUnits)) {
102
+ throw new Error(
103
+ `Expected an integer of minor units, got ${minorUnits}. A fractional ` +
104
+ 'minor unit is not an amount of money.',
105
+ );
106
+ }
107
+
108
+ const exponent = currencyExponent(currency);
109
+ if (exponent === 0) return String(minorUnits);
110
+
111
+ // The sign is taken off first: -1 fil must render "-0.001", and leaving the
112
+ // minus in the arithmetic produces "0.-01".
113
+ const sign = minorUnits < 0 ? '-' : '';
114
+ const magnitude = Math.abs(minorUnits);
115
+ const divisor = 10 ** exponent;
116
+
117
+ const major = Math.floor(magnitude / divisor);
118
+ const minor = magnitude % divisor;
119
+
120
+ return `${sign}${major}.${String(minor).padStart(exponent, '0')}`;
121
+ }
122
+
123
+ /**
124
+ * The amount as a person in this locale reads it — symbol, grouping, digits and
125
+ * direction all decided by `Intl`.
126
+ *
127
+ * Pass the UI's active locale so the Arabic build renders Arabic-Indic numerals
128
+ * and places the currency correctly for RTL; this file must not try to do
129
+ * either itself.
130
+ *
131
+ * PRECISION NOTE: the exact decimal string is handed to `Intl` directly where
132
+ * the engine accepts one (ES2023), so no precision is lost. Where it does not,
133
+ * this falls back to a `Number`, which is exact below 2^53 — about nine
134
+ * quadrillion fils, or nine trillion dinars. `toDecimalString` has no such
135
+ * bound if an exact rendering is what is needed.
136
+ */
137
+ export function formatMoney(
138
+ minorUnits: number,
139
+ currency: string,
140
+ locale?: string,
141
+ ): string {
142
+ const code = normalize(currency);
143
+ if (code === null) {
144
+ throw new Error(`Not an ISO 4217 currency code: '${currency}'`);
145
+ }
146
+
147
+ const decimal = toDecimalString(minorUnits, code);
148
+ const exponent = currencyExponent(code);
149
+
150
+ let formatter: Intl.NumberFormat;
151
+ try {
152
+ formatter = new Intl.NumberFormat(locale, {
153
+ style: 'currency',
154
+ currency: code,
155
+ minimumFractionDigits: exponent,
156
+ maximumFractionDigits: exponent,
157
+ });
158
+ } catch {
159
+ // Unknown to this engine's ICU data: show the exact figure and the code
160
+ // rather than nothing.
161
+ return `${decimal} ${code}`;
162
+ }
163
+
164
+ try {
165
+ // Intl.NumberFormat v3 accepts a decimal string, which keeps full
166
+ // precision. Older engines throw or coerce, hence the fallback.
167
+ return formatter.format(decimal as unknown as number);
168
+ } catch {
169
+ return formatter.format(Number(decimal));
170
+ }
171
+ }
172
+
173
+ /**
174
+ * Read a human-entered major-unit amount into minor units.
175
+ *
176
+ * Mirrors the PHP `Currency::parse` exactly, and that exactness is the point:
177
+ * if the client accepted what the server refuses, the user would see a
178
+ * validation error on an amount the form told them was fine; if it accepted
179
+ * what the server silently rounds, they would be billed something they did not
180
+ * type.
181
+ *
182
+ * So: more precision than the currency has is REFUSED, not rounded, and group
183
+ * separators are refused rather than guessed at — "1.234,56" and "1,234.56"
184
+ * are the same string to different halves of the world and mean amounts a
185
+ * thousand times apart.
186
+ */
187
+ export function parseMoney(input: string, currency: string): number {
188
+ const code = normalize(currency);
189
+ if (code === null) {
190
+ throw new Error(`Not an ISO 4217 currency code: '${currency}'`);
191
+ }
192
+
193
+ const exponent = currencyExponent(code);
194
+ const trimmed = input.trim();
195
+
196
+ if (trimmed === '') {
197
+ throw new Error(`Cannot read an empty amount as ${code}.`);
198
+ }
199
+
200
+ const match = /^(-?)(\d+)(?:\.(\d*))?$/.exec(trimmed);
201
+ if (match === null) {
202
+ throw new Error(
203
+ `Cannot read "${input}" as ${code}: expected digits with at most one "." ` +
204
+ 'and an optional leading "-". Group and decimal separators are ambiguous ' +
205
+ 'between locales and are refused rather than guessed at.',
206
+ );
207
+ }
208
+
209
+ const [, sign, whole, fraction = ''] = match;
210
+
211
+ if (fraction.length > exponent) {
212
+ throw new Error(
213
+ `Cannot read "${input}" as ${code}: ${code} has ${exponent} decimal ` +
214
+ `place${exponent === 1 ? '' : 's'}, and this has ${fraction.length}. ` +
215
+ 'Refusing to round an amount somebody typed.',
216
+ );
217
+ }
218
+
219
+ const digits = whole + fraction.padEnd(exponent, '0');
220
+ const value = Number(`${sign}${digits}`);
221
+
222
+ if (!Number.isSafeInteger(value)) {
223
+ throw new Error(
224
+ `Cannot read "${input}" as ${code}: ${digits} minor units is beyond the ` +
225
+ 'range in which an integer is exact.',
226
+ );
227
+ }
228
+
229
+ return value;
230
+ }
@@ -0,0 +1,95 @@
1
+ import type { Meta, StoryObj } from "@storybook/react-vite"
2
+ import { IconDownload, IconPlus, IconUsers } from "@tabler/icons-react"
3
+
4
+ import { PageHeader } from "./page-header"
5
+ import { Button } from "./button"
6
+ import { Breadcrumb } from "./breadcrumb"
7
+ import { Badge } from "./badge"
8
+ import { Tabs, TabsList, TabsTrigger } from "./tabs"
9
+
10
+ const meta = {
11
+ title: "Layout/PageHeader",
12
+ component: PageHeader,
13
+ tags: ["autodocs"],
14
+ args: {
15
+ title: "Team Members",
16
+ description: "Manage who has access to this workspace and configure permission roles.",
17
+ },
18
+ } satisfies Meta<typeof PageHeader>
19
+
20
+ export default meta
21
+ type Story = StoryObj<typeof meta>
22
+
23
+ export const Default: Story = {}
24
+
25
+ export const WithAction: Story = {
26
+ args: {
27
+ action: (
28
+ <div className="flex items-center gap-2">
29
+ <Button variant="outline" size="sm">
30
+ <IconDownload className="size-3.5" />
31
+ Export CSV
32
+ </Button>
33
+ <Button size="sm">
34
+ <IconPlus className="size-3.5" />
35
+ Invite Member
36
+ </Button>
37
+ </div>
38
+ ),
39
+ },
40
+ }
41
+
42
+ export const RichHeaderWithIconAndBadge: Story = {
43
+ args: {
44
+ icon: <IconUsers />,
45
+ badge: <Badge variant="secondary">24 Active</Badge>,
46
+ breadcrumb: <Breadcrumb items={[{ label: "Organization", href: "#" }, { label: "Team Members" }]} />,
47
+ action: (
48
+ <Button size="sm">
49
+ <IconPlus className="size-3.5" />
50
+ Invite Member
51
+ </Button>
52
+ ),
53
+ },
54
+ }
55
+
56
+ export const CardVariant: Story = {
57
+ args: {
58
+ variant: "card",
59
+ icon: <IconUsers />,
60
+ badge: <Badge variant="outline">Enterprise</Badge>,
61
+ breadcrumb: <Breadcrumb items={[{ label: "Dashboard", href: "#" }, { label: "Settings" }, { label: "Team" }]} />,
62
+ action: (
63
+ <Button size="sm">
64
+ <IconPlus className="size-3.5" />
65
+ Add Member
66
+ </Button>
67
+ ),
68
+ },
69
+ }
70
+
71
+ export const WithEmbeddedTabs: Story = {
72
+ render: () => (
73
+ <PageHeader
74
+ title="User Management"
75
+ description="View, edit, and audit system users and role permissions."
76
+ icon={<IconUsers />}
77
+ badge={<Badge variant="secondary">v2.4.0</Badge>}
78
+ breadcrumb={<Breadcrumb items={[{ label: "Admin", href: "#" }, { label: "Users" }]} />}
79
+ action={
80
+ <Button size="sm">
81
+ <IconPlus className="size-3.5" />
82
+ New User
83
+ </Button>
84
+ }
85
+ >
86
+ <Tabs defaultValue="all" className="w-full">
87
+ <TabsList>
88
+ <TabsTrigger value="all">All Users</TabsTrigger>
89
+ <TabsTrigger value="admins">Administrators</TabsTrigger>
90
+ <TabsTrigger value="pending">Pending Invites</TabsTrigger>
91
+ </TabsList>
92
+ </Tabs>
93
+ </PageHeader>
94
+ ),
95
+ }