@whity/ui 0.5.0 → 1.0.1

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 (208) hide show
  1. package/README.md +107 -107
  2. package/package.json +223 -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 +125 -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 +399 -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 +1687 -1106
  54. package/src/date-picker.stories.tsx +61 -61
  55. package/src/date-picker.tsx +457 -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 +59 -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 +92 -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-input.tsx +252 -0
  151. package/src/phone-tab-bar.tsx +152 -0
  152. package/src/plugin-card.stories.tsx +208 -180
  153. package/src/plugin-card.tsx +931 -848
  154. package/src/popover.stories.tsx +48 -48
  155. package/src/popover.tsx +55 -55
  156. package/src/radio-group.stories.tsx +26 -26
  157. package/src/radio-group.tsx +44 -44
  158. package/src/role-summary.stories.tsx +143 -0
  159. package/src/role-summary.tsx +120 -0
  160. package/src/route-flow/editor.tsx +1087 -1014
  161. package/src/route-flow/model.ts +771 -716
  162. package/src/save-bar.tsx +94 -0
  163. package/src/scope-grant.tsx +139 -139
  164. package/src/screen-too-small.stories.tsx +61 -61
  165. package/src/screen-too-small.tsx +104 -104
  166. package/src/search-field.tsx +65 -0
  167. package/src/select.stories.tsx +44 -44
  168. package/src/select.tsx +197 -197
  169. package/src/separator.stories.tsx +34 -34
  170. package/src/separator.tsx +34 -34
  171. package/src/sheet.stories.tsx +49 -49
  172. package/src/sheet.tsx +168 -168
  173. package/src/skeleton.stories.tsx +28 -28
  174. package/src/skeleton.tsx +19 -19
  175. package/src/slider.stories.tsx +78 -78
  176. package/src/slider.tsx +300 -300
  177. package/src/spinner.stories.tsx +33 -33
  178. package/src/spinner.tsx +55 -55
  179. package/src/stepper.tsx +85 -0
  180. package/src/switch.stories.tsx +42 -42
  181. package/src/switch.tsx +41 -41
  182. package/src/switcher.stories.tsx +111 -111
  183. package/src/switcher.tsx +131 -131
  184. package/src/system-state.tsx +55 -0
  185. package/src/table.stories.tsx +48 -48
  186. package/src/table.tsx +121 -121
  187. package/src/tabs.stories.tsx +74 -74
  188. package/src/tabs.tsx +90 -90
  189. package/src/tag-input.stories.tsx +67 -67
  190. package/src/tag-input.tsx +117 -117
  191. package/src/tenant-logo.tsx +63 -63
  192. package/src/tenant-switcher.stories.tsx +38 -38
  193. package/src/tenant-switcher.tsx +185 -164
  194. package/src/textarea.stories.tsx +27 -27
  195. package/src/textarea.tsx +18 -18
  196. package/src/theme-mode.ts +74 -52
  197. package/src/time-grid.tsx +163 -0
  198. package/src/time-picker.stories.tsx +62 -62
  199. package/src/time-picker.tsx +616 -615
  200. package/src/toggle-group.tsx +139 -138
  201. package/src/tooltip.stories.tsx +43 -43
  202. package/src/tooltip.tsx +73 -73
  203. package/src/top-nav.stories.tsx +58 -58
  204. package/src/top-nav.tsx +324 -324
  205. package/src/undo-bar.tsx +107 -107
  206. package/src/utils.ts +45 -45
  207. package/src/view-toggle.stories.tsx +84 -84
  208. package/src/view-toggle.tsx +111 -110
@@ -1,615 +1,616 @@
1
- "use client"
2
-
3
- import * as React from "react"
4
- import {
5
- IconChevronDown,
6
- IconClock,
7
- IconInfoCircle,
8
- IconX,
9
- } from "@tabler/icons-react"
10
-
11
- import { cn } from "./utils"
12
- import { Popover, PopoverContent, PopoverTrigger } from "./popover"
13
- import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "./tooltip"
14
-
15
- export interface TimePickerProps {
16
- /**
17
- * Selected time string e.g. "09:30 AM" or "14:30".
18
- */
19
- value?: string
20
- /**
21
- * Default time string.
22
- */
23
- defaultValue?: string
24
- /**
25
- * Callback fired when time changes or is cleared.
26
- */
27
- onChange?: (time: string | null) => void
28
- /**
29
- * Field label text displayed statically above the input field.
30
- */
31
- label?: React.ReactNode
32
- /**
33
- * Displays a required asterisk (*) next to the label.
34
- */
35
- required?: boolean
36
- /**
37
- * Tooltip message displayed in an interactive info icon next to the label.
38
- */
39
- tooltip?: React.ReactNode
40
- /**
41
- * Small informational note rendered below the input field.
42
- */
43
- helperText?: React.ReactNode
44
- /**
45
- * Error text rendered below the input field in destructive red.
46
- */
47
- errorText?: React.ReactNode
48
- /**
49
- * Placeholder text when no time is selected. Defaults to "Select time...".
50
- */
51
- placeholder?: string
52
- /** Accessible name for the clear button. */
53
- clearLabel?: string
54
- /** Accessible name for a tooltip trigger. Defaults to "More information". */
55
- tooltipTriggerLabel?: string
56
- /** Heading above the time columns. */
57
- timeHeadingLabel?: string
58
- /** Meridiem column labels. */
59
- amLabel?: string
60
- pmLabel?: string
61
- /** Hint under the columns explaining the scroll interaction. */
62
- scrollHint?: string
63
- /** Footer button text. */
64
- nowLabel?: string
65
- clearButtonLabel?: string
66
- /**
67
- * Time display format. Defaults to "12h".
68
- */
69
- format?: "12h" | "24h"
70
- /**
71
- * Minute step intervals e.g. 1, 5, 15, 30. Defaults to 1 for full granularity.
72
- */
73
- minuteStep?: number
74
- /**
75
- * Disables the time picker control.
76
- */
77
- disabled?: boolean
78
- /**
79
- * Allows clearing the selected time. Defaults to true.
80
- */
81
- clearable?: boolean
82
- /**
83
- * Custom container class name.
84
- */
85
- className?: string
86
- id?: string
87
- name?: string
88
- }
89
-
90
- function parseTimeString(val?: string, format: "12h" | "24h" = "12h") {
91
- if (!val) return { hour: "09", minute: "00", period: "AM" }
92
- const match12 = val.match(/^(\d{1,2}):(\d{2})\s*(AM|PM)?$/i)
93
- if (match12) {
94
- let h = parseInt(match12[1], 10)
95
- let p = (match12[3] || "AM").toUpperCase()
96
- if (format === "24h") {
97
- return {
98
- hour: String(h).padStart(2, "0"),
99
- minute: match12[2],
100
- period: "AM",
101
- }
102
- }
103
- if (h === 0) h = 12
104
- if (h > 12) {
105
- h = h - 12
106
- p = "PM"
107
- }
108
- return {
109
- hour: String(h).padStart(2, "0"),
110
- minute: match12[2],
111
- period: p,
112
- }
113
- }
114
- return { hour: "09", minute: "00", period: "AM" }
115
- }
116
-
117
- const HOURS_12 = Array.from({ length: 12 }, (_, i) => String(i + 1).padStart(2, "0"))
118
- const HOURS_24 = Array.from({ length: 24 }, (_, i) => String(i).padStart(2, "0"))
119
-
120
- const TimePicker = React.forwardRef<HTMLDivElement, TimePickerProps>(
121
- (
122
- {
123
- value,
124
- defaultValue,
125
- onChange,
126
- label,
127
- required,
128
- tooltip,
129
- helperText,
130
- errorText,
131
- placeholder = "Select time...",
132
- clearLabel = "Clear time",
133
- tooltipTriggerLabel = "More information",
134
- timeHeadingLabel = "Time",
135
- amLabel = "AM",
136
- pmLabel = "PM",
137
- scrollHint = "Scroll wheel or click numbers to cycle time",
138
- nowLabel = "Now",
139
- clearButtonLabel = "Clear",
140
- format = "12h",
141
- minuteStep = 1,
142
- disabled,
143
- clearable = true,
144
- className,
145
- id,
146
- name,
147
- },
148
- ref
149
- ) => {
150
- const [selectedTime, setSelectedTime] = React.useState<string | null>(() => {
151
- return value ?? defaultValue ?? null
152
- })
153
- const [activeFormat, setActiveFormat] = React.useState<"12h" | "24h">(format)
154
- const [open, setOpen] = React.useState(false)
155
-
156
- React.useEffect(() => {
157
- if (value !== undefined) {
158
- setSelectedTime(value)
159
- }
160
- }, [value])
161
-
162
- React.useEffect(() => {
163
- setActiveFormat(format)
164
- }, [format])
165
-
166
- const parsed = parseTimeString(selectedTime || undefined, activeFormat)
167
- const [hour, setHour] = React.useState(parsed.hour)
168
- const [minute, setMinute] = React.useState(parsed.minute)
169
- const [period, setPeriod] = React.useState(parsed.period)
170
-
171
- React.useEffect(() => {
172
- if (selectedTime) {
173
- const p = parseTimeString(selectedTime, activeFormat)
174
- setHour(p.hour)
175
- setMinute(p.minute)
176
- setPeriod(p.period)
177
- }
178
- }, [selectedTime, activeFormat])
179
-
180
- const minutesList = React.useMemo(() => {
181
- const list: string[] = []
182
- for (let i = 0; i < 60; i += minuteStep) {
183
- list.push(String(i).padStart(2, "0"))
184
- }
185
- return list
186
- }, [minuteStep])
187
-
188
- const hoursList = activeFormat === "24h" ? HOURS_24 : HOURS_12
189
-
190
- const updateTime = (newHour: string, newMinute: string, newPeriod: string, fmt = activeFormat) => {
191
- setHour(newHour)
192
- setMinute(newMinute)
193
- setPeriod(newPeriod)
194
- const formatted = fmt === "24h" ? `${newHour}:${newMinute}` : `${newHour}:${newMinute} ${newPeriod}`
195
- setSelectedTime(formatted)
196
- onChange?.(formatted)
197
- }
198
-
199
- const incrementHour = (h: string, p: string, fmt: "12h" | "24h") => {
200
- let nextH = h
201
- let nextP = p
202
- if (fmt === "12h") {
203
- const list = HOURS_12
204
- const currIdx = list.indexOf(h)
205
- const nextIdx = (currIdx + 1) % list.length
206
- nextH = list[nextIdx]
207
- if (h === "12" && nextH === "01") {
208
- nextP = p === "AM" ? "PM" : "AM"
209
- }
210
- } else {
211
- const list = HOURS_24
212
- const currIdx = list.indexOf(h)
213
- const nextIdx = (currIdx + 1) % list.length
214
- nextH = list[nextIdx]
215
- }
216
- return { nextH, nextP }
217
- }
218
-
219
- const decrementHour = (h: string, p: string, fmt: "12h" | "24h") => {
220
- let nextH = h
221
- let nextP = p
222
- if (fmt === "12h") {
223
- const list = HOURS_12
224
- const currIdx = list.indexOf(h)
225
- const prevIdx = (currIdx - 1 + list.length) % list.length
226
- nextH = list[prevIdx]
227
- if (h === "01" && nextH === "12") {
228
- nextP = p === "AM" ? "PM" : "AM"
229
- }
230
- } else {
231
- const list = HOURS_24
232
- const currIdx = list.indexOf(h)
233
- const prevIdx = (currIdx - 1 + list.length) % list.length
234
- nextH = list[prevIdx]
235
- }
236
- return { nextH, nextP }
237
- }
238
-
239
- const handleHourWheel = (e: React.WheelEvent) => {
240
- e.preventDefault()
241
- if (e.deltaY > 0) {
242
- const { nextH, nextP } = incrementHour(hour, period, activeFormat)
243
- updateTime(nextH, minute, nextP)
244
- } else if (e.deltaY < 0) {
245
- const { nextH, nextP } = decrementHour(hour, period, activeFormat)
246
- updateTime(nextH, minute, nextP)
247
- }
248
- }
249
-
250
- const handleMinuteWheel = (e: React.WheelEvent) => {
251
- e.preventDefault()
252
- const currIdx = minutesList.indexOf(minute)
253
- if (currIdx === -1) return
254
- if (e.deltaY > 0) {
255
- if (currIdx === minutesList.length - 1) {
256
- const nextM = minutesList[0]
257
- const { nextH, nextP } = incrementHour(hour, period, activeFormat)
258
- updateTime(nextH, nextM, nextP)
259
- } else {
260
- updateTime(hour, minutesList[currIdx + 1], period)
261
- }
262
- } else if (e.deltaY < 0) {
263
- if (currIdx === 0) {
264
- const nextM = minutesList[minutesList.length - 1]
265
- const { nextH, nextP } = decrementHour(hour, period, activeFormat)
266
- updateTime(nextH, nextM, nextP)
267
- } else {
268
- updateTime(hour, minutesList[currIdx - 1], period)
269
- }
270
- }
271
- }
272
-
273
- const handleClear = (e: React.MouseEvent) => {
274
- e.stopPropagation()
275
- setSelectedTime(null)
276
- onChange?.(null)
277
- }
278
-
279
- const handleSetNow = () => {
280
- const now = new Date()
281
- let h = now.getHours()
282
- const m = String(Math.floor(now.getMinutes() / minuteStep) * minuteStep).padStart(2, "0")
283
- let p = "AM"
284
- if (activeFormat === "12h") {
285
- p = h >= 12 ? "PM" : "AM"
286
- h = h % 12 || 12
287
- }
288
- const formattedH = String(h).padStart(2, "0")
289
- updateTime(formattedH, m, p)
290
- }
291
-
292
- const renderDrumColumn = (
293
- colLabel: string,
294
- items: string[],
295
- selectedVal: string,
296
- onSelect: (val: string) => void,
297
- onWheel: (e: React.WheelEvent) => void
298
- ) => {
299
- const currIdx = items.indexOf(selectedVal) >= 0 ? items.indexOf(selectedVal) : 0
300
- const N = items.length
301
-
302
- const top2Idx = (currIdx - 2 + N) % N
303
- const top1Idx = (currIdx - 1 + N) % N
304
- const centerIdx = currIdx
305
- const bot1Idx = (currIdx + 1) % N
306
- const bot2Idx = (currIdx + 2) % N
307
-
308
- return (
309
- <div className="flex flex-col items-center gap-1">
310
- <span className="text-[0.625rem] font-semibold text-muted-foreground uppercase">{colLabel}</span>
311
- <div
312
- onWheel={onWheel}
313
- className="relative flex flex-col items-center justify-center w-16 select-none rounded-xl border border-border/60 bg-card p-1 shadow-2xs"
314
- >
315
- {/* Permanent Center Selection Lens */}
316
- <div className="pointer-events-none absolute inset-x-1 top-1/2 h-8 -translate-y-1/2 rounded-lg bg-primary text-primary-foreground shadow-2xs" />
317
-
318
- {/* Row 1 (-2) */}
319
- <button
320
- type="button"
321
- onClick={() => onSelect(items[top2Idx])}
322
- className="flex h-6 w-full items-center justify-center text-[0.6875rem] font-medium text-muted-foreground/40 transition-colors hover:text-foreground"
323
- >
324
- {items[top2Idx]}
325
- </button>
326
-
327
- {/* Row 2 (-1) */}
328
- <button
329
- type="button"
330
- onClick={() => onSelect(items[top1Idx])}
331
- className="flex h-6 w-full items-center justify-center text-xs font-medium text-muted-foreground/70 transition-colors hover:text-foreground"
332
- >
333
- {items[top1Idx]}
334
- </button>
335
-
336
- {/* Row 3 (CENTERED LENS) */}
337
- <div className="relative z-10 flex h-8 w-full items-center justify-center text-sm font-semibold text-primary-foreground">
338
- {items[centerIdx]}
339
- </div>
340
-
341
- {/* Row 4 (+1) */}
342
- <button
343
- type="button"
344
- onClick={() => onSelect(items[bot1Idx])}
345
- className="flex h-6 w-full items-center justify-center text-xs font-medium text-muted-foreground/70 transition-colors hover:text-foreground"
346
- >
347
- {items[bot1Idx]}
348
- </button>
349
-
350
- {/* Row 5 (+2) */}
351
- <button
352
- type="button"
353
- onClick={() => onSelect(items[bot2Idx])}
354
- className="flex h-6 w-full items-center justify-center text-[0.6875rem] font-medium text-muted-foreground/40 transition-colors hover:text-foreground"
355
- >
356
- {items[bot2Idx]}
357
- </button>
358
- </div>
359
- </div>
360
- )
361
- }
362
-
363
- const hasHeader = label || tooltip || required
364
- const hasFooter = helperText || errorText
365
-
366
- const triggerButton = (
367
- <Popover open={open} onOpenChange={setOpen}>
368
- <PopoverTrigger asChild>
369
- <button
370
- type="button"
371
- id={id}
372
- disabled={disabled}
373
- className={cn(
374
- "relative flex h-7 w-full items-center justify-start rounded-md border border-input bg-card ps-8 pe-8 py-0.5 text-xs font-medium text-foreground transition-colors outline-none focus-visible:border-ring focus-visible:ring-2 focus-visible:ring-ring/30 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-2 aria-invalid:ring-destructive/20",
375
- !selectedTime && "text-muted-foreground font-normal",
376
- className
377
- )}
378
- >
379
- <div className="absolute start-2.5 flex items-center justify-center size-3.5 text-muted-foreground pointer-events-none shrink-0 z-10">
380
- <IconClock className="size-3.5 shrink-0" aria-hidden="true" />
381
- </div>
382
-
383
- <span className="truncate">{selectedTime || placeholder}</span>
384
-
385
- <div className="absolute end-2.5 flex items-center justify-center gap-1 shrink-0 z-10">
386
- {clearable && selectedTime && !disabled && (
387
- <span
388
- role="button"
389
- tabIndex={-1}
390
- onClick={handleClear}
391
- aria-label={clearLabel}
392
- className="rounded p-0.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
393
- >
394
- <IconX className="size-3.5" aria-hidden="true" />
395
- </span>
396
- )}
397
- <IconChevronDown className="size-3.5 text-muted-foreground transition-transform duration-200 group-data-[state=open]:rotate-180" />
398
- </div>
399
- </button>
400
- </PopoverTrigger>
401
- <PopoverContent align="start" className="w-64 p-3 shadow-lg border border-border bg-popover text-popover-foreground rounded-xl">
402
- {/* Header & 12H/24H format selector + AM/PM toggle */}
403
- <div className="flex items-center justify-between pb-2 mb-2 border-b border-border/50">
404
- <span className="text-xs font-semibold text-foreground">{timeHeadingLabel}</span>
405
- <div className="flex items-center gap-1.5">
406
- <div className="flex items-center rounded-md bg-muted p-0.5 text-[0.625rem] font-semibold">
407
- <button
408
- type="button"
409
- onClick={() => {
410
- setActiveFormat("12h")
411
- if (activeFormat === "24h") {
412
- let h = parseInt(hour, 10)
413
- const p = h >= 12 ? "PM" : "AM"
414
- h = h % 12 || 12
415
- updateTime(String(h).padStart(2, "0"), minute, p, "12h")
416
- }
417
- }}
418
- className={cn(
419
- "px-1.5 py-0.5 rounded transition-colors",
420
- activeFormat === "12h" ? "bg-card text-foreground shadow-2xs font-semibold" : "text-muted-foreground hover:text-foreground"
421
- )}
422
- >
423
- 12H
424
- </button>
425
- <button
426
- type="button"
427
- onClick={() => {
428
- setActiveFormat("24h")
429
- if (activeFormat === "12h") {
430
- let h = parseInt(hour, 10)
431
- if (period === "PM" && h < 12) h += 12
432
- if (period === "AM" && h === 12) h = 0
433
- updateTime(String(h).padStart(2, "0"), minute, "AM", "24h")
434
- }
435
- }}
436
- className={cn(
437
- "px-1.5 py-0.5 rounded transition-colors",
438
- activeFormat === "24h" ? "bg-card text-foreground shadow-2xs font-semibold" : "text-muted-foreground hover:text-foreground"
439
- )}
440
- >
441
- 24H
442
- </button>
443
- </div>
444
-
445
- {activeFormat === "12h" && (
446
- <div className="flex items-center rounded-md bg-muted p-0.5 text-[0.625rem] font-semibold">
447
- <button
448
- type="button"
449
- onClick={() => updateTime(hour, minute, "AM", activeFormat)}
450
- className={cn(
451
- "px-1.5 py-0.5 rounded transition-colors",
452
- period === "AM" ? "bg-card text-foreground shadow-2xs font-semibold" : "text-muted-foreground hover:text-foreground"
453
- )}
454
- >
455
- {amLabel}
456
- </button>
457
- <button
458
- type="button"
459
- onClick={() => updateTime(hour, minute, "PM", activeFormat)}
460
- className={cn(
461
- "px-1.5 py-0.5 rounded transition-colors",
462
- period === "PM" ? "bg-card text-foreground shadow-2xs font-semibold" : "text-muted-foreground hover:text-foreground"
463
- )}
464
- >
465
- {pmLabel}
466
- </button>
467
- </div>
468
- )}
469
- </div>
470
- </div>
471
-
472
- {/* Fixed Drum Wheel Columns: Selected Value ALWAYS Centered */}
473
- <div className="flex items-center justify-center gap-4 py-1">
474
- {renderDrumColumn(
475
- "Hour",
476
- hoursList,
477
- hour,
478
- (h) => {
479
- // When selecting a new hour in 12H mode, check if we rolled from 12->01 or 01->12
480
- if (activeFormat === "12h") {
481
- let p = period
482
- if (hour === "12" && h === "01") p = period === "AM" ? "PM" : "AM"
483
- if (hour === "01" && h === "12") p = period === "AM" ? "PM" : "AM"
484
- updateTime(h, minute, p)
485
- } else {
486
- updateTime(h, minute, period)
487
- }
488
- },
489
- handleHourWheel
490
- )}
491
- <span className="text-sm font-semibold text-muted-foreground pt-5">:</span>
492
- {renderDrumColumn(
493
- "Minute",
494
- minutesList,
495
- minute,
496
- (m) => updateTime(hour, m, period),
497
- handleMinuteWheel
498
- )}
499
- </div>
500
-
501
- <p className="mt-1 text-center text-[0.625rem] text-muted-foreground">
502
- {scrollHint}
503
- </p>
504
-
505
- {/* Footer Now / Clear */}
506
- <div className="flex items-center justify-between pt-2 mt-2 border-t border-border/50">
507
- <button
508
- type="button"
509
- onClick={handleSetNow}
510
- className="text-[0.6875rem] font-semibold text-primary hover:underline"
511
- >
512
- {nowLabel}
513
- </button>
514
- {selectedTime && (
515
- <button
516
- type="button"
517
- onClick={() => {
518
- setSelectedTime(null)
519
- onChange?.(null)
520
- setOpen(false)
521
- }}
522
- className="text-[0.6875rem] text-muted-foreground hover:text-foreground"
523
- >
524
- {clearButtonLabel}
525
- </button>
526
- )}
527
- </div>
528
- </PopoverContent>
529
- </Popover>
530
- )
531
-
532
- if (name && selectedTime) {
533
- return (
534
- <div ref={ref} className="w-full">
535
- <input type="hidden" name={name} value={selectedTime} />
536
- {!hasHeader && !hasFooter ? (
537
- triggerButton
538
- ) : (
539
- <div className="flex flex-col gap-1 w-full text-start">
540
- {hasHeader && (
541
- <div className="flex items-center gap-1 text-xs font-semibold text-foreground">
542
- {label && <span>{label}</span>}
543
- {required && <span className="text-destructive font-semibold ms-0.5">*</span>}
544
- {tooltip && (
545
- <TooltipProvider>
546
- <Tooltip>
547
- <TooltipTrigger asChild>
548
- <button
549
- type="button"
550
- aria-label={tooltipTriggerLabel}
551
- className="inline-flex items-center text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring rounded p-0.5"
552
- >
553
- <IconInfoCircle className="size-3.5 shrink-0" aria-hidden="true" />
554
- </button>
555
- </TooltipTrigger>
556
- <TooltipContent>{tooltip}</TooltipContent>
557
- </Tooltip>
558
- </TooltipProvider>
559
- )}
560
- </div>
561
- )}
562
- {triggerButton}
563
- {errorText ? (
564
- <p className="text-[0.6875rem] font-medium text-destructive">{errorText}</p>
565
- ) : (
566
- helperText && <p className="text-[0.6875rem] text-muted-foreground">{helperText}</p>
567
- )}
568
- </div>
569
- )}
570
- </div>
571
- )
572
- }
573
-
574
- if (!hasHeader && !hasFooter) {
575
- return <div ref={ref} className="w-full">{triggerButton}</div>
576
- }
577
-
578
- return (
579
- <div ref={ref} className="flex flex-col gap-1 w-full text-start">
580
- {hasHeader && (
581
- <div className="flex items-center gap-1 text-xs font-semibold text-foreground">
582
- {label && <span>{label}</span>}
583
- {required && <span className="text-destructive font-semibold ms-0.5">*</span>}
584
- {tooltip && (
585
- <TooltipProvider>
586
- <Tooltip>
587
- <TooltipTrigger asChild>
588
- <button
589
- type="button"
590
- aria-label={tooltipTriggerLabel}
591
- className="inline-flex items-center text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring rounded p-0.5"
592
- >
593
- <IconInfoCircle className="size-3.5 shrink-0" aria-hidden="true" />
594
- </button>
595
- </TooltipTrigger>
596
- <TooltipContent>{tooltip}</TooltipContent>
597
- </Tooltip>
598
- </TooltipProvider>
599
- )}
600
- </div>
601
- )}
602
- {triggerButton}
603
- {errorText ? (
604
- <p className="text-[0.6875rem] font-medium text-destructive">{errorText}</p>
605
- ) : (
606
- helperText && <p className="text-[0.6875rem] text-muted-foreground">{helperText}</p>
607
- )}
608
- </div>
609
- )
610
- }
611
- )
612
-
613
- TimePicker.displayName = "TimePicker"
614
-
615
- export { TimePicker }
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+ import {
5
+ IconChevronDown,
6
+ IconClock,
7
+ IconInfoCircle,
8
+ IconX,
9
+ } from "@tabler/icons-react"
10
+
11
+ import { cn } from "./utils"
12
+ import { Popover, PopoverContent, PopoverTrigger } from "./popover"
13
+ import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "./tooltip"
14
+
15
+ export interface TimePickerProps {
16
+ /**
17
+ * Selected time string e.g. "09:30 AM" or "14:30".
18
+ */
19
+ value?: string
20
+ /**
21
+ * Default time string.
22
+ */
23
+ defaultValue?: string
24
+ /**
25
+ * Callback fired when time changes or is cleared.
26
+ */
27
+ onChange?: (time: string | null) => void
28
+ /**
29
+ * Field label text displayed statically above the input field.
30
+ */
31
+ label?: React.ReactNode
32
+ /**
33
+ * Displays a required asterisk (*) next to the label.
34
+ */
35
+ required?: boolean
36
+ /**
37
+ * Tooltip message displayed in an interactive info icon next to the label.
38
+ */
39
+ tooltip?: React.ReactNode
40
+ /**
41
+ * Small informational note rendered below the input field.
42
+ */
43
+ helperText?: React.ReactNode
44
+ /**
45
+ * Error text rendered below the input field in destructive red.
46
+ */
47
+ errorText?: React.ReactNode
48
+ /**
49
+ * Placeholder text when no time is selected. Defaults to "Select time...".
50
+ */
51
+ placeholder?: string
52
+ /** Accessible name for the clear button. */
53
+ clearLabel?: string
54
+ /** Accessible name for a tooltip trigger. Defaults to "More information". */
55
+ tooltipTriggerLabel?: string
56
+ /** Heading above the time columns. */
57
+ timeHeadingLabel?: string
58
+ /** Meridiem column labels. */
59
+ amLabel?: string
60
+ pmLabel?: string
61
+ /** Hint under the columns explaining the scroll interaction. */
62
+ scrollHint?: string
63
+ /** Footer button text. */
64
+ nowLabel?: string
65
+ clearButtonLabel?: string
66
+ /**
67
+ * Time display format. Defaults to "12h".
68
+ */
69
+ format?: "12h" | "24h"
70
+ /**
71
+ * Minute step intervals e.g. 1, 5, 15, 30. Defaults to 1 for full granularity.
72
+ */
73
+ minuteStep?: number
74
+ /**
75
+ * Disables the time picker control.
76
+ */
77
+ disabled?: boolean
78
+ /**
79
+ * Allows clearing the selected time. Defaults to true.
80
+ */
81
+ clearable?: boolean
82
+ /**
83
+ * Custom container class name.
84
+ */
85
+ className?: string
86
+ id?: string
87
+ name?: string
88
+ }
89
+
90
+ function parseTimeString(val?: string, format: "12h" | "24h" = "12h") {
91
+ if (!val) return { hour: "09", minute: "00", period: "AM" }
92
+ const match12 = val.match(/^(\d{1,2}):(\d{2})\s*(AM|PM)?$/i)
93
+ if (match12) {
94
+ let h = parseInt(match12[1], 10)
95
+ let p = (match12[3] || "AM").toUpperCase()
96
+ if (format === "24h") {
97
+ return {
98
+ hour: String(h).padStart(2, "0"),
99
+ minute: match12[2],
100
+ period: "AM",
101
+ }
102
+ }
103
+ if (h === 0) h = 12
104
+ if (h > 12) {
105
+ h = h - 12
106
+ p = "PM"
107
+ }
108
+ return {
109
+ hour: String(h).padStart(2, "0"),
110
+ minute: match12[2],
111
+ period: p,
112
+ }
113
+ }
114
+ return { hour: "09", minute: "00", period: "AM" }
115
+ }
116
+
117
+ const HOURS_12 = Array.from({ length: 12 }, (_, i) => String(i + 1).padStart(2, "0"))
118
+ const HOURS_24 = Array.from({ length: 24 }, (_, i) => String(i).padStart(2, "0"))
119
+
120
+ const TimePicker = React.forwardRef<HTMLDivElement, TimePickerProps>(
121
+ (
122
+ {
123
+ value,
124
+ defaultValue,
125
+ onChange,
126
+ label,
127
+ required,
128
+ tooltip,
129
+ helperText,
130
+ errorText,
131
+ placeholder = "Select time...",
132
+ clearLabel = "Clear time",
133
+ tooltipTriggerLabel = "More information",
134
+ timeHeadingLabel = "Time",
135
+ amLabel = "AM",
136
+ pmLabel = "PM",
137
+ scrollHint = "Scroll wheel or click numbers to cycle time",
138
+ nowLabel = "Now",
139
+ clearButtonLabel = "Clear",
140
+ format = "12h",
141
+ minuteStep = 1,
142
+ disabled,
143
+ clearable = true,
144
+ className,
145
+ id,
146
+ name,
147
+ },
148
+ ref
149
+ ) => {
150
+ const [selectedTime, setSelectedTime] = React.useState<string | null>(() => {
151
+ return value ?? defaultValue ?? null
152
+ })
153
+ const [activeFormat, setActiveFormat] = React.useState<"12h" | "24h">(format)
154
+ const [open, setOpen] = React.useState(false)
155
+
156
+ React.useEffect(() => {
157
+ if (value !== undefined) {
158
+ setSelectedTime(value)
159
+ }
160
+ }, [value])
161
+
162
+ React.useEffect(() => {
163
+ setActiveFormat(format)
164
+ }, [format])
165
+
166
+ const parsed = parseTimeString(selectedTime || undefined, activeFormat)
167
+ const [hour, setHour] = React.useState(parsed.hour)
168
+ const [minute, setMinute] = React.useState(parsed.minute)
169
+ const [period, setPeriod] = React.useState(parsed.period)
170
+
171
+ React.useEffect(() => {
172
+ if (selectedTime) {
173
+ const p = parseTimeString(selectedTime, activeFormat)
174
+ setHour(p.hour)
175
+ setMinute(p.minute)
176
+ setPeriod(p.period)
177
+ }
178
+ }, [selectedTime, activeFormat])
179
+
180
+ const minutesList = React.useMemo(() => {
181
+ const list: string[] = []
182
+ for (let i = 0; i < 60; i += minuteStep) {
183
+ list.push(String(i).padStart(2, "0"))
184
+ }
185
+ return list
186
+ }, [minuteStep])
187
+
188
+ const hoursList = activeFormat === "24h" ? HOURS_24 : HOURS_12
189
+
190
+ const updateTime = (newHour: string, newMinute: string, newPeriod: string, fmt = activeFormat) => {
191
+ setHour(newHour)
192
+ setMinute(newMinute)
193
+ setPeriod(newPeriod)
194
+ const formatted = fmt === "24h" ? `${newHour}:${newMinute}` : `${newHour}:${newMinute} ${newPeriod}`
195
+ setSelectedTime(formatted)
196
+ onChange?.(formatted)
197
+ }
198
+
199
+ const incrementHour = (h: string, p: string, fmt: "12h" | "24h") => {
200
+ let nextH = h
201
+ let nextP = p
202
+ if (fmt === "12h") {
203
+ const list = HOURS_12
204
+ const currIdx = list.indexOf(h)
205
+ const nextIdx = (currIdx + 1) % list.length
206
+ nextH = list[nextIdx]
207
+ if (h === "12" && nextH === "01") {
208
+ nextP = p === "AM" ? "PM" : "AM"
209
+ }
210
+ } else {
211
+ const list = HOURS_24
212
+ const currIdx = list.indexOf(h)
213
+ const nextIdx = (currIdx + 1) % list.length
214
+ nextH = list[nextIdx]
215
+ }
216
+ return { nextH, nextP }
217
+ }
218
+
219
+ const decrementHour = (h: string, p: string, fmt: "12h" | "24h") => {
220
+ let nextH = h
221
+ let nextP = p
222
+ if (fmt === "12h") {
223
+ const list = HOURS_12
224
+ const currIdx = list.indexOf(h)
225
+ const prevIdx = (currIdx - 1 + list.length) % list.length
226
+ nextH = list[prevIdx]
227
+ if (h === "01" && nextH === "12") {
228
+ nextP = p === "AM" ? "PM" : "AM"
229
+ }
230
+ } else {
231
+ const list = HOURS_24
232
+ const currIdx = list.indexOf(h)
233
+ const prevIdx = (currIdx - 1 + list.length) % list.length
234
+ nextH = list[prevIdx]
235
+ }
236
+ return { nextH, nextP }
237
+ }
238
+
239
+ const handleHourWheel = (e: React.WheelEvent) => {
240
+ e.preventDefault()
241
+ if (e.deltaY > 0) {
242
+ const { nextH, nextP } = incrementHour(hour, period, activeFormat)
243
+ updateTime(nextH, minute, nextP)
244
+ } else if (e.deltaY < 0) {
245
+ const { nextH, nextP } = decrementHour(hour, period, activeFormat)
246
+ updateTime(nextH, minute, nextP)
247
+ }
248
+ }
249
+
250
+ const handleMinuteWheel = (e: React.WheelEvent) => {
251
+ e.preventDefault()
252
+ const currIdx = minutesList.indexOf(minute)
253
+ if (currIdx === -1) return
254
+ if (e.deltaY > 0) {
255
+ if (currIdx === minutesList.length - 1) {
256
+ const nextM = minutesList[0]
257
+ const { nextH, nextP } = incrementHour(hour, period, activeFormat)
258
+ updateTime(nextH, nextM, nextP)
259
+ } else {
260
+ updateTime(hour, minutesList[currIdx + 1], period)
261
+ }
262
+ } else if (e.deltaY < 0) {
263
+ if (currIdx === 0) {
264
+ const nextM = minutesList[minutesList.length - 1]
265
+ const { nextH, nextP } = decrementHour(hour, period, activeFormat)
266
+ updateTime(nextH, nextM, nextP)
267
+ } else {
268
+ updateTime(hour, minutesList[currIdx - 1], period)
269
+ }
270
+ }
271
+ }
272
+
273
+ const handleClear = (e: React.MouseEvent) => {
274
+ e.stopPropagation()
275
+ setSelectedTime(null)
276
+ onChange?.(null)
277
+ }
278
+
279
+ const handleSetNow = () => {
280
+ const now = new Date()
281
+ let h = now.getHours()
282
+ const m = String(Math.floor(now.getMinutes() / minuteStep) * minuteStep).padStart(2, "0")
283
+ let p = "AM"
284
+ if (activeFormat === "12h") {
285
+ p = h >= 12 ? "PM" : "AM"
286
+ h = h % 12 || 12
287
+ }
288
+ const formattedH = String(h).padStart(2, "0")
289
+ updateTime(formattedH, m, p)
290
+ }
291
+
292
+ const renderDrumColumn = (
293
+ colLabel: string,
294
+ items: string[],
295
+ selectedVal: string,
296
+ onSelect: (val: string) => void,
297
+ onWheel: (e: React.WheelEvent) => void
298
+ ) => {
299
+ const currIdx = items.indexOf(selectedVal) >= 0 ? items.indexOf(selectedVal) : 0
300
+ const N = items.length
301
+
302
+ const top2Idx = (currIdx - 2 + N) % N
303
+ const top1Idx = (currIdx - 1 + N) % N
304
+ const centerIdx = currIdx
305
+ const bot1Idx = (currIdx + 1) % N
306
+ const bot2Idx = (currIdx + 2) % N
307
+
308
+ return (
309
+ <div className="flex flex-col items-center gap-1">
310
+ <span className="text-xs font-semibold text-muted-foreground uppercase">{colLabel}</span>
311
+ <div
312
+ onWheel={onWheel}
313
+ className="relative flex flex-col items-center justify-center w-16 select-none rounded-xl border border-border/60 bg-card p-1 shadow-2xs"
314
+ >
315
+ {/* Permanent Center Selection Lens */}
316
+ <div className="pointer-events-none absolute inset-x-1 top-1/2 h-8 -translate-y-1/2 rounded-lg bg-primary text-primary-foreground shadow-2xs" />
317
+
318
+ {/* Row 1 (-2) */}
319
+ <button
320
+ type="button"
321
+ onClick={() => onSelect(items[top2Idx])}
322
+ className="flex h-6 w-full items-center justify-center text-xs font-medium text-muted-foreground/40 transition-colors hover:text-foreground"
323
+ >
324
+ {items[top2Idx]}
325
+ </button>
326
+
327
+ {/* Row 2 (-1) */}
328
+ <button
329
+ type="button"
330
+ onClick={() => onSelect(items[top1Idx])}
331
+ className="flex h-6 w-full items-center justify-center text-xs font-medium text-muted-foreground/70 transition-colors hover:text-foreground"
332
+ >
333
+ {items[top1Idx]}
334
+ </button>
335
+
336
+ {/* Row 3 (CENTERED LENS) */}
337
+ <div className="relative z-10 flex h-8 w-full items-center justify-center text-sm font-semibold text-primary-foreground">
338
+ {items[centerIdx]}
339
+ </div>
340
+
341
+ {/* Row 4 (+1) */}
342
+ <button
343
+ type="button"
344
+ onClick={() => onSelect(items[bot1Idx])}
345
+ className="flex h-6 w-full items-center justify-center text-xs font-medium text-muted-foreground/70 transition-colors hover:text-foreground"
346
+ >
347
+ {items[bot1Idx]}
348
+ </button>
349
+
350
+ {/* Row 5 (+2) */}
351
+ <button
352
+ type="button"
353
+ onClick={() => onSelect(items[bot2Idx])}
354
+ className="flex h-6 w-full items-center justify-center text-xs font-medium text-muted-foreground/40 transition-colors hover:text-foreground"
355
+ >
356
+ {items[bot2Idx]}
357
+ </button>
358
+ </div>
359
+ </div>
360
+ )
361
+ }
362
+
363
+ const hasHeader = label || tooltip || required
364
+ const hasFooter = helperText || errorText
365
+
366
+ const triggerButton = (
367
+ <Popover open={open} onOpenChange={setOpen}>
368
+ <PopoverTrigger asChild>
369
+ <button
370
+ type="button"
371
+ id={id}
372
+ data-slot="time-picker-trigger"
373
+ disabled={disabled}
374
+ className={cn(
375
+ "relative flex h-7 w-full items-center justify-start rounded-md border border-input bg-card ps-8 pe-8 py-0.5 text-xs font-medium text-foreground transition-colors outline-none focus-visible:border-ring focus-visible:ring-2 focus-visible:ring-ring/30 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-2 aria-invalid:ring-destructive/20",
376
+ !selectedTime && "text-muted-foreground font-normal",
377
+ className
378
+ )}
379
+ >
380
+ <div className="absolute start-2.5 flex items-center justify-center size-3.5 text-muted-foreground pointer-events-none shrink-0 z-10">
381
+ <IconClock className="size-3.5 shrink-0" aria-hidden="true" />
382
+ </div>
383
+
384
+ <span className="truncate">{selectedTime || placeholder}</span>
385
+
386
+ <div className="absolute end-2.5 flex items-center justify-center gap-1 shrink-0 z-10">
387
+ {clearable && selectedTime && !disabled && (
388
+ <span
389
+ role="button"
390
+ tabIndex={-1}
391
+ onClick={handleClear}
392
+ aria-label={clearLabel}
393
+ className="rounded p-0.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
394
+ >
395
+ <IconX className="size-3.5" aria-hidden="true" />
396
+ </span>
397
+ )}
398
+ <IconChevronDown className="size-3.5 text-muted-foreground transition-transform duration-200 group-data-[state=open]:rotate-180" />
399
+ </div>
400
+ </button>
401
+ </PopoverTrigger>
402
+ <PopoverContent align="start" className="w-64 p-3 shadow-lg border border-border bg-popover text-popover-foreground rounded-xl">
403
+ {/* Header & 12H/24H format selector + AM/PM toggle */}
404
+ <div className="flex items-center justify-between pb-2 mb-2 border-b border-border/50">
405
+ <span className="text-xs font-semibold text-foreground">{timeHeadingLabel}</span>
406
+ <div className="flex items-center gap-1.5">
407
+ <div className="flex items-center rounded-md bg-muted p-0.5 text-xs font-semibold">
408
+ <button
409
+ type="button"
410
+ onClick={() => {
411
+ setActiveFormat("12h")
412
+ if (activeFormat === "24h") {
413
+ let h = parseInt(hour, 10)
414
+ const p = h >= 12 ? "PM" : "AM"
415
+ h = h % 12 || 12
416
+ updateTime(String(h).padStart(2, "0"), minute, p, "12h")
417
+ }
418
+ }}
419
+ className={cn(
420
+ "px-1.5 py-0.5 rounded transition-colors",
421
+ activeFormat === "12h" ? "bg-card text-foreground shadow-2xs font-semibold" : "text-muted-foreground hover:text-foreground"
422
+ )}
423
+ >
424
+ 12H
425
+ </button>
426
+ <button
427
+ type="button"
428
+ onClick={() => {
429
+ setActiveFormat("24h")
430
+ if (activeFormat === "12h") {
431
+ let h = parseInt(hour, 10)
432
+ if (period === "PM" && h < 12) h += 12
433
+ if (period === "AM" && h === 12) h = 0
434
+ updateTime(String(h).padStart(2, "0"), minute, "AM", "24h")
435
+ }
436
+ }}
437
+ className={cn(
438
+ "px-1.5 py-0.5 rounded transition-colors",
439
+ activeFormat === "24h" ? "bg-card text-foreground shadow-2xs font-semibold" : "text-muted-foreground hover:text-foreground"
440
+ )}
441
+ >
442
+ 24H
443
+ </button>
444
+ </div>
445
+
446
+ {activeFormat === "12h" && (
447
+ <div className="flex items-center rounded-md bg-muted p-0.5 text-xs font-semibold">
448
+ <button
449
+ type="button"
450
+ onClick={() => updateTime(hour, minute, "AM", activeFormat)}
451
+ className={cn(
452
+ "px-1.5 py-0.5 rounded transition-colors",
453
+ period === "AM" ? "bg-card text-foreground shadow-2xs font-semibold" : "text-muted-foreground hover:text-foreground"
454
+ )}
455
+ >
456
+ {amLabel}
457
+ </button>
458
+ <button
459
+ type="button"
460
+ onClick={() => updateTime(hour, minute, "PM", activeFormat)}
461
+ className={cn(
462
+ "px-1.5 py-0.5 rounded transition-colors",
463
+ period === "PM" ? "bg-card text-foreground shadow-2xs font-semibold" : "text-muted-foreground hover:text-foreground"
464
+ )}
465
+ >
466
+ {pmLabel}
467
+ </button>
468
+ </div>
469
+ )}
470
+ </div>
471
+ </div>
472
+
473
+ {/* Fixed Drum Wheel Columns: Selected Value ALWAYS Centered */}
474
+ <div className="flex items-center justify-center gap-4 py-1">
475
+ {renderDrumColumn(
476
+ "Hour",
477
+ hoursList,
478
+ hour,
479
+ (h) => {
480
+ // When selecting a new hour in 12H mode, check if we rolled from 12->01 or 01->12
481
+ if (activeFormat === "12h") {
482
+ let p = period
483
+ if (hour === "12" && h === "01") p = period === "AM" ? "PM" : "AM"
484
+ if (hour === "01" && h === "12") p = period === "AM" ? "PM" : "AM"
485
+ updateTime(h, minute, p)
486
+ } else {
487
+ updateTime(h, minute, period)
488
+ }
489
+ },
490
+ handleHourWheel
491
+ )}
492
+ <span className="text-sm font-semibold text-muted-foreground pt-5">:</span>
493
+ {renderDrumColumn(
494
+ "Minute",
495
+ minutesList,
496
+ minute,
497
+ (m) => updateTime(hour, m, period),
498
+ handleMinuteWheel
499
+ )}
500
+ </div>
501
+
502
+ <p className="mt-1 text-center text-xs text-muted-foreground">
503
+ {scrollHint}
504
+ </p>
505
+
506
+ {/* Footer Now / Clear */}
507
+ <div className="flex items-center justify-between pt-2 mt-2 border-t border-border/50">
508
+ <button
509
+ type="button"
510
+ onClick={handleSetNow}
511
+ className="text-xs font-semibold text-primary hover:underline"
512
+ >
513
+ {nowLabel}
514
+ </button>
515
+ {selectedTime && (
516
+ <button
517
+ type="button"
518
+ onClick={() => {
519
+ setSelectedTime(null)
520
+ onChange?.(null)
521
+ setOpen(false)
522
+ }}
523
+ className="text-xs text-muted-foreground hover:text-foreground"
524
+ >
525
+ {clearButtonLabel}
526
+ </button>
527
+ )}
528
+ </div>
529
+ </PopoverContent>
530
+ </Popover>
531
+ )
532
+
533
+ if (name && selectedTime) {
534
+ return (
535
+ <div ref={ref} className="w-full">
536
+ <input type="hidden" name={name} value={selectedTime} />
537
+ {!hasHeader && !hasFooter ? (
538
+ triggerButton
539
+ ) : (
540
+ <div className="flex flex-col gap-1 w-full text-start">
541
+ {hasHeader && (
542
+ <div className="flex items-center gap-1 text-xs font-semibold text-foreground">
543
+ {label && <span>{label}</span>}
544
+ {required && <span className="text-destructive font-semibold ms-0.5">*</span>}
545
+ {tooltip && (
546
+ <TooltipProvider>
547
+ <Tooltip>
548
+ <TooltipTrigger asChild>
549
+ <button
550
+ type="button"
551
+ aria-label={tooltipTriggerLabel}
552
+ className="inline-flex items-center text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring rounded p-0.5"
553
+ >
554
+ <IconInfoCircle className="size-3.5 shrink-0" aria-hidden="true" />
555
+ </button>
556
+ </TooltipTrigger>
557
+ <TooltipContent>{tooltip}</TooltipContent>
558
+ </Tooltip>
559
+ </TooltipProvider>
560
+ )}
561
+ </div>
562
+ )}
563
+ {triggerButton}
564
+ {errorText ? (
565
+ <p className="text-xs font-medium text-destructive">{errorText}</p>
566
+ ) : (
567
+ helperText && <p className="text-xs text-muted-foreground">{helperText}</p>
568
+ )}
569
+ </div>
570
+ )}
571
+ </div>
572
+ )
573
+ }
574
+
575
+ if (!hasHeader && !hasFooter) {
576
+ return <div ref={ref} className="w-full">{triggerButton}</div>
577
+ }
578
+
579
+ return (
580
+ <div ref={ref} className="flex flex-col gap-1 w-full text-start">
581
+ {hasHeader && (
582
+ <div className="flex items-center gap-1 text-xs font-semibold text-foreground">
583
+ {label && <span>{label}</span>}
584
+ {required && <span className="text-destructive font-semibold ms-0.5">*</span>}
585
+ {tooltip && (
586
+ <TooltipProvider>
587
+ <Tooltip>
588
+ <TooltipTrigger asChild>
589
+ <button
590
+ type="button"
591
+ aria-label={tooltipTriggerLabel}
592
+ className="inline-flex items-center text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring rounded p-0.5"
593
+ >
594
+ <IconInfoCircle className="size-3.5 shrink-0" aria-hidden="true" />
595
+ </button>
596
+ </TooltipTrigger>
597
+ <TooltipContent>{tooltip}</TooltipContent>
598
+ </Tooltip>
599
+ </TooltipProvider>
600
+ )}
601
+ </div>
602
+ )}
603
+ {triggerButton}
604
+ {errorText ? (
605
+ <p className="text-xs font-medium text-destructive">{errorText}</p>
606
+ ) : (
607
+ helperText && <p className="text-xs text-muted-foreground">{helperText}</p>
608
+ )}
609
+ </div>
610
+ )
611
+ }
612
+ )
613
+
614
+ TimePicker.displayName = "TimePicker"
615
+
616
+ export { TimePicker }