@behavioral-agentic-design-system/core 0.27.0 → 0.29.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -5
- package/scripts/a11y-lint.cjs +78 -9
- package/scripts/build-tokens.cjs +273 -15
- package/scripts/wcag-audit.cjs +37 -7
- package/src/api/layers/file.ts +1 -0
- package/src/app/font-ready.tsx +6 -5
- package/src/app/layout.tsx +54 -3
- package/src/app/preview/components/page.tsx +5735 -2500
- package/src/app/preview/layout.tsx +4 -1
- package/src/app/preview/showcase-filter.tsx +66 -0
- package/src/app/preview/tab-replay.tsx +63 -0
- package/src/app/shell.tsx +91 -18
- package/src/components/customize/save-bar.tsx +19 -6
- package/src/components/docs-tab.tsx +36 -24
- package/src/components/layers-tab.tsx +37 -5
- package/src/components/ui/ai-badge.tsx +74 -0
- package/src/components/ui/alert.tsx +12 -0
- package/src/components/ui/badge.tsx +19 -0
- package/src/components/ui/chat-bubble.tsx +298 -0
- package/src/components/ui/chat-marker.tsx +120 -0
- package/src/components/ui/checkbox.tsx +9 -1
- package/src/components/ui/clipboard.tsx +291 -0
- package/src/components/ui/close-button.tsx +71 -0
- package/src/components/ui/combobox.tsx +62 -3
- package/src/components/ui/container.tsx +65 -0
- package/src/components/ui/copy-button.tsx +5 -1
- package/src/components/ui/data-table.tsx +388 -0
- package/src/components/ui/date-field.tsx +368 -0
- package/src/components/ui/drawer.tsx +334 -0
- package/src/components/ui/file-button.tsx +145 -0
- package/src/components/ui/floating-indicator.tsx +217 -0
- package/src/components/ui/focus-trap.tsx +184 -0
- package/src/components/ui/format-byte.tsx +113 -0
- package/src/components/ui/index.ts +28 -0
- package/src/components/ui/input.tsx +38 -3
- package/src/components/ui/keyboard-shortcut.tsx +146 -0
- package/src/components/ui/loading-dots.tsx +99 -0
- package/src/components/ui/mark.tsx +69 -0
- package/src/components/ui/month-picker.tsx +296 -0
- package/src/components/ui/multi-select.tsx +15 -13
- package/src/components/ui/nav-link.tsx +200 -0
- package/src/components/ui/navigation-rail.tsx +256 -0
- package/src/components/ui/notification.tsx +20 -0
- package/src/components/ui/password-input.tsx +34 -1
- package/src/components/ui/progress.tsx +15 -1
- package/src/components/ui/prose.tsx +62 -0
- package/src/components/ui/result.tsx +151 -0
- package/src/components/ui/rolling-number.tsx +201 -0
- package/src/components/ui/simple-grid.tsx +110 -0
- package/src/components/ui/skeleton.tsx +52 -3
- package/src/components/ui/squircle.tsx +274 -0
- package/src/components/ui/stack.tsx +105 -0
- package/src/components/ui/status-badge.tsx +93 -0
- package/src/components/ui/tabs.tsx +34 -7
- package/src/components/ui/tag.tsx +46 -5
- package/src/components/ui/theme-icon.tsx +138 -0
- package/src/components/ui/timeline.tsx +27 -10
- package/src/components/ui/tooltip.tsx +12 -1
- package/src/components/ui/tree-select.tsx +386 -0
- package/src/components/ui/typing-indicator.tsx +2 -2
- package/src/components/ui/word-rotate.tsx +6 -1
- package/src/components/ui/year-picker.tsx +307 -0
- package/src/data/docs-index.ts +10 -0
- package/src/hooks/use-global-primary-color.ts +57 -6
- package/src/stories/AiBadge.stories.tsx +67 -0
- package/src/stories/Alert.stories.tsx +31 -1
- package/src/stories/Badge.stories.tsx +63 -6
- package/src/stories/ChatBubble.stories.tsx +222 -0
- package/src/stories/ChatMarker.stories.tsx +59 -0
- package/src/stories/Checkbox.stories.tsx +5 -0
- package/src/stories/Clipboard.stories.tsx +136 -0
- package/src/stories/CloseButton.stories.tsx +65 -0
- package/src/stories/Combobox.stories.tsx +74 -3
- package/src/stories/Container.stories.tsx +134 -0
- package/src/stories/CopyButton.stories.tsx +2 -0
- package/src/stories/DataTable.stories.tsx +271 -0
- package/src/stories/DateField.stories.tsx +114 -0
- package/src/stories/Drawer.stories.tsx +211 -0
- package/src/stories/FileButton.stories.tsx +117 -0
- package/src/stories/FloatingIndicator.stories.tsx +343 -0
- package/src/stories/FocusTrap.stories.tsx +218 -0
- package/src/stories/FormatByte.stories.tsx +142 -0
- package/src/stories/Input.stories.tsx +91 -0
- package/src/stories/KeyboardShortcut.stories.tsx +87 -0
- package/src/stories/LoadingDots.stories.tsx +73 -0
- package/src/stories/Mark.stories.tsx +84 -0
- package/src/stories/MonthPicker.stories.tsx +99 -0
- package/src/stories/MultiSelect.stories.tsx +3 -1
- package/src/stories/NavLink.stories.tsx +213 -0
- package/src/stories/NavigationRail.stories.tsx +413 -0
- package/src/stories/Notification.stories.tsx +53 -0
- package/src/stories/PasswordInput.stories.tsx +21 -0
- package/src/stories/Progress.stories.tsx +38 -0
- package/src/stories/Prose.stories.tsx +232 -0
- package/src/stories/Result.stories.tsx +145 -0
- package/src/stories/RollingNumber.stories.tsx +226 -0
- package/src/stories/SimpleGrid.stories.tsx +130 -0
- package/src/stories/Skeleton.stories.tsx +62 -1
- package/src/stories/Squircle.stories.tsx +127 -0
- package/src/stories/Stack.stories.tsx +139 -0
- package/src/stories/StatusBadge.stories.tsx +125 -0
- package/src/stories/Tabs.stories.tsx +56 -1
- package/src/stories/Tag.stories.tsx +31 -0
- package/src/stories/ThemeIcon.stories.tsx +97 -0
- package/src/stories/Timeline.stories.tsx +69 -0
- package/src/stories/Tooltip.stories.tsx +18 -0
- package/src/stories/TreeSelect.stories.tsx +222 -0
- package/src/stories/TypingIndicator.stories.tsx +1 -0
- package/src/stories/WordRotate.stories.tsx +1 -0
- package/src/stories/YearPicker.stories.tsx +100 -0
- package/src/styles.css +289 -15
- package/src/tokens.json +96 -1
- package/LICENSE +0 -21
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { XIcon } from "lucide-react"
|
|
5
|
+
import { cn } from "../../lib/utils"
|
|
6
|
+
|
|
7
|
+
export interface DateFieldProps {
|
|
8
|
+
/** Controlled selected date */
|
|
9
|
+
value?: Date
|
|
10
|
+
/** Fires with a Date when all three segments are filled; undefined when cleared */
|
|
11
|
+
onValueChange?: (date: Date | undefined) => void
|
|
12
|
+
/** Uncontrolled default */
|
|
13
|
+
defaultValue?: Date
|
|
14
|
+
/** Segment placeholders, slash-separated. Defaults to "MM / DD / YYYY" */
|
|
15
|
+
placeholder?: string
|
|
16
|
+
disabled?: boolean
|
|
17
|
+
clearable?: boolean
|
|
18
|
+
/** Earliest selectable date (committed values clamp to this) */
|
|
19
|
+
min?: Date
|
|
20
|
+
/** Latest selectable date (committed values clamp to this) */
|
|
21
|
+
max?: Date
|
|
22
|
+
size?: "sm" | "default" | "lg"
|
|
23
|
+
className?: string
|
|
24
|
+
id?: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const sizeMap = {
|
|
28
|
+
sm: { height: "h-8", px: "px-2.5", text: "text-sm", gap: "gap-1", icon: 14 },
|
|
29
|
+
default: { height: "h-9", px: "px-3", text: "text-sm", gap: "gap-1.5", icon: 16 },
|
|
30
|
+
lg: { height: "h-10", px: "px-4", text: "text-base", gap: "gap-2", icon: 16 },
|
|
31
|
+
} as const
|
|
32
|
+
|
|
33
|
+
function pad(n: number, width: number) {
|
|
34
|
+
return String(n).padStart(width, "0")
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function daysInMonth(year: number | null, month: number | null): number {
|
|
38
|
+
if (month === null) return 31
|
|
39
|
+
// Unknown year: assume a leap year so Feb 29 stays enterable until the year lands.
|
|
40
|
+
return new Date(year ?? 2000, month, 0).getDate()
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function atMidnight(d: Date): Date {
|
|
44
|
+
return new Date(d.getFullYear(), d.getMonth(), d.getDate())
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function isValidDate(d: unknown): d is Date {
|
|
48
|
+
return d instanceof Date && !isNaN(d.getTime())
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
type Segment = "month" | "day" | "year"
|
|
52
|
+
|
|
53
|
+
const SEGMENT_ORDER: Segment[] = ["month", "day", "year"]
|
|
54
|
+
|
|
55
|
+
function DateField({
|
|
56
|
+
value,
|
|
57
|
+
onValueChange,
|
|
58
|
+
defaultValue,
|
|
59
|
+
placeholder = "MM / DD / YYYY",
|
|
60
|
+
disabled = false,
|
|
61
|
+
clearable = false,
|
|
62
|
+
min,
|
|
63
|
+
max,
|
|
64
|
+
size = "default",
|
|
65
|
+
className,
|
|
66
|
+
id,
|
|
67
|
+
}: DateFieldProps) {
|
|
68
|
+
const isControlled = value !== undefined
|
|
69
|
+
const [internalValue, setInternalValue] = React.useState<Date | undefined>(
|
|
70
|
+
isValidDate(defaultValue) ? atMidnight(defaultValue) : undefined
|
|
71
|
+
)
|
|
72
|
+
const resolvedValue = isControlled ? value : internalValue
|
|
73
|
+
const resolvedTime = isValidDate(resolvedValue) ? resolvedValue.getTime() : undefined
|
|
74
|
+
|
|
75
|
+
const [month, setMonth] = React.useState<number | null>(
|
|
76
|
+
isValidDate(resolvedValue) ? resolvedValue.getMonth() + 1 : null
|
|
77
|
+
)
|
|
78
|
+
const [day, setDay] = React.useState<number | null>(
|
|
79
|
+
isValidDate(resolvedValue) ? resolvedValue.getDate() : null
|
|
80
|
+
)
|
|
81
|
+
const [year, setYear] = React.useState<number | null>(
|
|
82
|
+
isValidDate(resolvedValue) ? resolvedValue.getFullYear() : null
|
|
83
|
+
)
|
|
84
|
+
const [active, setActive] = React.useState<Segment | null>(null)
|
|
85
|
+
const digitBuffer = React.useRef("")
|
|
86
|
+
// Set when a partial clear (Backspace on one segment) emits undefined, so the
|
|
87
|
+
// sync effect below doesn't wipe the segments the user is still editing.
|
|
88
|
+
const skipNextClearSync = React.useRef(false)
|
|
89
|
+
|
|
90
|
+
const monthRef = React.useRef<HTMLSpanElement>(null)
|
|
91
|
+
const dayRef = React.useRef<HTMLSpanElement>(null)
|
|
92
|
+
const yearRef = React.useRef<HTMLSpanElement>(null)
|
|
93
|
+
const segmentRefs: Record<Segment, React.RefObject<HTMLSpanElement | null>> = {
|
|
94
|
+
month: monthRef,
|
|
95
|
+
day: dayRef,
|
|
96
|
+
year: yearRef,
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Sync segments when the resolved value changes (controlled update, commit, clear).
|
|
100
|
+
React.useEffect(() => {
|
|
101
|
+
if (resolvedTime !== undefined) {
|
|
102
|
+
const d = new Date(resolvedTime)
|
|
103
|
+
setMonth(d.getMonth() + 1)
|
|
104
|
+
setDay(d.getDate())
|
|
105
|
+
setYear(d.getFullYear())
|
|
106
|
+
} else {
|
|
107
|
+
if (skipNextClearSync.current) {
|
|
108
|
+
skipNextClearSync.current = false
|
|
109
|
+
return
|
|
110
|
+
}
|
|
111
|
+
setMonth(null)
|
|
112
|
+
setDay(null)
|
|
113
|
+
setYear(null)
|
|
114
|
+
}
|
|
115
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
116
|
+
}, [resolvedTime])
|
|
117
|
+
|
|
118
|
+
function emit(date: Date | undefined) {
|
|
119
|
+
if (!isControlled) setInternalValue(date)
|
|
120
|
+
onValueChange?.(date)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Commit when all three segments are filled; clamps day-in-month and min/max. */
|
|
124
|
+
function maybeCommit(m: number | null, d: number | null, y: number | null) {
|
|
125
|
+
if (m === null || d === null || y === null) return
|
|
126
|
+
const clampedDay = Math.min(d, daysInMonth(y, m))
|
|
127
|
+
let date = new Date(y, m - 1, clampedDay)
|
|
128
|
+
if (min && date < atMidnight(min)) date = atMidnight(min)
|
|
129
|
+
if (max && date > atMidnight(max)) date = atMidnight(max)
|
|
130
|
+
setMonth(date.getMonth() + 1)
|
|
131
|
+
setDay(date.getDate())
|
|
132
|
+
setYear(date.getFullYear())
|
|
133
|
+
emit(date)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function clearSegment(seg: Segment) {
|
|
137
|
+
const hadFullValue = month !== null && day !== null && year !== null
|
|
138
|
+
if (seg === "month") setMonth(null)
|
|
139
|
+
else if (seg === "day") setDay(null)
|
|
140
|
+
else setYear(null)
|
|
141
|
+
digitBuffer.current = ""
|
|
142
|
+
if (hadFullValue) {
|
|
143
|
+
skipNextClearSync.current = true
|
|
144
|
+
emit(undefined)
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function handleClear(e: React.MouseEvent) {
|
|
149
|
+
e.stopPropagation()
|
|
150
|
+
setMonth(null)
|
|
151
|
+
setDay(null)
|
|
152
|
+
setYear(null)
|
|
153
|
+
digitBuffer.current = ""
|
|
154
|
+
emit(undefined)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function focusSegment(seg: Segment) {
|
|
158
|
+
segmentRefs[seg].current?.focus()
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function moveFocus(from: Segment, delta: 1 | -1) {
|
|
162
|
+
const idx = SEGMENT_ORDER.indexOf(from)
|
|
163
|
+
const next = SEGMENT_ORDER[idx + delta]
|
|
164
|
+
if (next) focusSegment(next)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function stepSegment(seg: Segment, delta: 1 | -1) {
|
|
168
|
+
const today = new Date()
|
|
169
|
+
if (seg === "month") {
|
|
170
|
+
const base = month ?? today.getMonth() + 1
|
|
171
|
+
const next = month === null ? base : ((base - 1 + delta + 12) % 12) + 1
|
|
172
|
+
setMonth(next)
|
|
173
|
+
maybeCommit(next, day, year)
|
|
174
|
+
} else if (seg === "day") {
|
|
175
|
+
const limit = daysInMonth(year, month)
|
|
176
|
+
const base = day ?? Math.min(today.getDate(), limit)
|
|
177
|
+
const next = day === null ? base : ((base - 1 + delta + limit) % limit) + 1
|
|
178
|
+
setDay(next)
|
|
179
|
+
maybeCommit(month, next, year)
|
|
180
|
+
} else {
|
|
181
|
+
const base = year ?? today.getFullYear()
|
|
182
|
+
const next = year === null ? base : Math.min(9999, Math.max(1, base + delta))
|
|
183
|
+
setYear(next)
|
|
184
|
+
maybeCommit(month, day, next)
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function handleDigit(seg: Segment, digit: number) {
|
|
189
|
+
const newBuf = digitBuffer.current + String(digit)
|
|
190
|
+
const val = parseInt(newBuf, 10)
|
|
191
|
+
if (seg === "month") {
|
|
192
|
+
if (newBuf.length === 1 && digit > 1) {
|
|
193
|
+
// Unambiguous single digit (2-9) → set and advance.
|
|
194
|
+
setMonth(digit)
|
|
195
|
+
maybeCommit(digit, day, year)
|
|
196
|
+
digitBuffer.current = ""
|
|
197
|
+
moveFocus("month", 1)
|
|
198
|
+
} else if (newBuf.length >= 2) {
|
|
199
|
+
const next = Math.min(Math.max(val, 1), 12)
|
|
200
|
+
setMonth(next)
|
|
201
|
+
maybeCommit(next, day, year)
|
|
202
|
+
digitBuffer.current = ""
|
|
203
|
+
moveFocus("month", 1)
|
|
204
|
+
} else {
|
|
205
|
+
setMonth(val === 0 ? null : val)
|
|
206
|
+
digitBuffer.current = newBuf
|
|
207
|
+
}
|
|
208
|
+
} else if (seg === "day") {
|
|
209
|
+
const limit = daysInMonth(year, month)
|
|
210
|
+
if (newBuf.length === 1 && digit > 3) {
|
|
211
|
+
setDay(Math.min(digit, limit))
|
|
212
|
+
maybeCommit(month, Math.min(digit, limit), year)
|
|
213
|
+
digitBuffer.current = ""
|
|
214
|
+
moveFocus("day", 1)
|
|
215
|
+
} else if (newBuf.length >= 2) {
|
|
216
|
+
const next = Math.min(Math.max(val, 1), limit)
|
|
217
|
+
setDay(next)
|
|
218
|
+
maybeCommit(month, next, year)
|
|
219
|
+
digitBuffer.current = ""
|
|
220
|
+
moveFocus("day", 1)
|
|
221
|
+
} else {
|
|
222
|
+
setDay(val === 0 ? null : val)
|
|
223
|
+
digitBuffer.current = newBuf
|
|
224
|
+
}
|
|
225
|
+
} else {
|
|
226
|
+
if (newBuf.length >= 4) {
|
|
227
|
+
const next = Math.min(Math.max(val, 1), 9999)
|
|
228
|
+
setYear(next)
|
|
229
|
+
maybeCommit(month, day, next)
|
|
230
|
+
digitBuffer.current = ""
|
|
231
|
+
} else {
|
|
232
|
+
setYear(val === 0 ? null : val)
|
|
233
|
+
digitBuffer.current = newBuf
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function handleSegmentKeyDown(seg: Segment, e: React.KeyboardEvent) {
|
|
239
|
+
if (e.key === "ArrowUp") {
|
|
240
|
+
e.preventDefault()
|
|
241
|
+
stepSegment(seg, 1)
|
|
242
|
+
} else if (e.key === "ArrowDown") {
|
|
243
|
+
e.preventDefault()
|
|
244
|
+
stepSegment(seg, -1)
|
|
245
|
+
} else if (e.key === "ArrowLeft") {
|
|
246
|
+
e.preventDefault()
|
|
247
|
+
moveFocus(seg, -1)
|
|
248
|
+
} else if (e.key === "ArrowRight") {
|
|
249
|
+
e.preventDefault()
|
|
250
|
+
moveFocus(seg, 1)
|
|
251
|
+
} else if (e.key === "Backspace" || e.key === "Delete") {
|
|
252
|
+
e.preventDefault()
|
|
253
|
+
clearSegment(seg)
|
|
254
|
+
} else if (e.key >= "0" && e.key <= "9") {
|
|
255
|
+
e.preventDefault()
|
|
256
|
+
handleDigit(seg, Number(e.key))
|
|
257
|
+
} else if (e.key !== "Tab" && e.key !== "Shift") {
|
|
258
|
+
digitBuffer.current = ""
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const { height, px, text, gap, icon: iconSize } = sizeMap[size]
|
|
263
|
+
const [phMonth = "MM", phDay = "DD", phYear = "YYYY"] = placeholder
|
|
264
|
+
.split("/")
|
|
265
|
+
.map((s) => s.trim())
|
|
266
|
+
|
|
267
|
+
const segClass = cn(
|
|
268
|
+
"inline-flex items-center justify-center rounded-sm px-0.5 outline-none select-none tabular-nums",
|
|
269
|
+
"focus-visible:ring-2 focus-visible:ring-ring",
|
|
270
|
+
"transition-colors",
|
|
271
|
+
text
|
|
272
|
+
)
|
|
273
|
+
const segStyle: React.CSSProperties = {
|
|
274
|
+
transitionDuration: "var(--duration-fast)",
|
|
275
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
276
|
+
}
|
|
277
|
+
const yearBounds = {
|
|
278
|
+
min: min ? min.getFullYear() : 1,
|
|
279
|
+
max: max ? max.getFullYear() : 9999,
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function renderSegment(
|
|
283
|
+
seg: Segment,
|
|
284
|
+
current: number | null,
|
|
285
|
+
label: string,
|
|
286
|
+
placeholderText: string,
|
|
287
|
+
ariaMin: number,
|
|
288
|
+
ariaMax: number,
|
|
289
|
+
width: 2 | 4
|
|
290
|
+
) {
|
|
291
|
+
return (
|
|
292
|
+
<span
|
|
293
|
+
ref={segmentRefs[seg]}
|
|
294
|
+
role="spinbutton"
|
|
295
|
+
aria-label={label}
|
|
296
|
+
aria-valuenow={current ?? undefined}
|
|
297
|
+
aria-valuemin={ariaMin}
|
|
298
|
+
aria-valuemax={ariaMax}
|
|
299
|
+
aria-valuetext={current === null ? "Empty" : pad(current, width)}
|
|
300
|
+
aria-disabled={disabled || undefined}
|
|
301
|
+
tabIndex={disabled ? -1 : 0}
|
|
302
|
+
className={cn(
|
|
303
|
+
segClass,
|
|
304
|
+
current === null && "text-muted-foreground",
|
|
305
|
+
active === seg && "bg-accent text-accent-foreground"
|
|
306
|
+
)}
|
|
307
|
+
style={segStyle}
|
|
308
|
+
onFocus={() => setActive(seg)}
|
|
309
|
+
onBlur={() => {
|
|
310
|
+
setActive(null)
|
|
311
|
+
digitBuffer.current = ""
|
|
312
|
+
}}
|
|
313
|
+
onKeyDown={(e) => handleSegmentKeyDown(seg, e)}
|
|
314
|
+
>
|
|
315
|
+
{current === null ? placeholderText : pad(current, width)}
|
|
316
|
+
</span>
|
|
317
|
+
)
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return (
|
|
321
|
+
<div
|
|
322
|
+
data-slot="date-field"
|
|
323
|
+
data-size={size}
|
|
324
|
+
id={id}
|
|
325
|
+
role="group"
|
|
326
|
+
aria-label="Date"
|
|
327
|
+
aria-disabled={disabled || undefined}
|
|
328
|
+
className={cn(
|
|
329
|
+
"inline-flex w-full min-w-0 items-center rounded-md border border-input bg-transparent shadow-xs",
|
|
330
|
+
"focus-within:border-ring focus-within:ring-[3px] focus-within:ring-ring/50",
|
|
331
|
+
disabled && "pointer-events-none cursor-not-allowed opacity-50",
|
|
332
|
+
height,
|
|
333
|
+
px,
|
|
334
|
+
gap,
|
|
335
|
+
className
|
|
336
|
+
)}
|
|
337
|
+
>
|
|
338
|
+
<span className={cn("inline-flex flex-1 items-center", gap)}>
|
|
339
|
+
{renderSegment("month", month, "Month", phMonth, 1, 12, 2)}
|
|
340
|
+
<span aria-hidden className={cn("text-muted-foreground", text)}>
|
|
341
|
+
/
|
|
342
|
+
</span>
|
|
343
|
+
{renderSegment("day", day, "Day", phDay, 1, daysInMonth(year, month), 2)}
|
|
344
|
+
<span aria-hidden className={cn("text-muted-foreground", text)}>
|
|
345
|
+
/
|
|
346
|
+
</span>
|
|
347
|
+
{renderSegment("year", year, "Year", phYear, yearBounds.min, yearBounds.max, 4)}
|
|
348
|
+
</span>
|
|
349
|
+
{clearable && (month !== null || day !== null || year !== null) && (
|
|
350
|
+
<button
|
|
351
|
+
type="button"
|
|
352
|
+
aria-label="Clear date"
|
|
353
|
+
onClick={handleClear}
|
|
354
|
+
disabled={disabled}
|
|
355
|
+
className="shrink-0 -mr-1 rounded p-0.5 text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
|
|
356
|
+
style={{
|
|
357
|
+
transitionDuration: "var(--duration-fast)",
|
|
358
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
359
|
+
}}
|
|
360
|
+
>
|
|
361
|
+
<XIcon size={iconSize - 2} strokeWidth={1.5} />
|
|
362
|
+
</button>
|
|
363
|
+
)}
|
|
364
|
+
</div>
|
|
365
|
+
)
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
export { DateField }
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { XIcon } from "lucide-react"
|
|
5
|
+
import * as DrawerPrimitive from "@radix-ui/react-dialog"
|
|
6
|
+
|
|
7
|
+
import { cn } from "../../lib/utils"
|
|
8
|
+
import { usePortalThemeClass } from "../../hooks/use-portal-theme-class"
|
|
9
|
+
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
// Drawer — gesture-driven edge panel
|
|
12
|
+
//
|
|
13
|
+
// Semantically distinct from Sheet:
|
|
14
|
+
// • Defaults to `side="bottom"` (the dominant mobile secondary-content pattern)
|
|
15
|
+
// • Ships a visible drag handle at the top of the panel
|
|
16
|
+
// • Supports pointer-event drag-to-dismiss: dragging >30% of the panel's
|
|
17
|
+
// height in the dismiss direction triggers `onOpenChange(false)`
|
|
18
|
+
// • Spring-back animation cancels the drag when the threshold isn't met
|
|
19
|
+
//
|
|
20
|
+
// Motion: uses tw-animate-css `animate-in` / `animate-out` +
|
|
21
|
+
// `slide-in-from-*` / `slide-out-to-*` for state-change entry/exit (same
|
|
22
|
+
// as Sheet — these are existing keyframe utilities, pattern (a) from the
|
|
23
|
+
// component-build-spec). Gesture drag uses inline transform with
|
|
24
|
+
// `var(--duration-base)` / `var(--ease-spring)` for the spring-back
|
|
25
|
+
// (pattern (c)).
|
|
26
|
+
//
|
|
27
|
+
// No new dependencies — backed by @radix-ui/react-dialog (already installed).
|
|
28
|
+
// ---------------------------------------------------------------------------
|
|
29
|
+
|
|
30
|
+
function Drawer({
|
|
31
|
+
...props
|
|
32
|
+
}: React.ComponentProps<typeof DrawerPrimitive.Root>) {
|
|
33
|
+
return <DrawerPrimitive.Root data-slot="drawer" {...props} />
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function DrawerTrigger({
|
|
37
|
+
...props
|
|
38
|
+
}: React.ComponentProps<typeof DrawerPrimitive.Trigger>) {
|
|
39
|
+
return <DrawerPrimitive.Trigger data-slot="drawer-trigger" {...props} />
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function DrawerClose({
|
|
43
|
+
...props
|
|
44
|
+
}: React.ComponentProps<typeof DrawerPrimitive.Close>) {
|
|
45
|
+
return <DrawerPrimitive.Close data-slot="drawer-close" {...props} />
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function DrawerPortal({
|
|
49
|
+
...props
|
|
50
|
+
}: React.ComponentProps<typeof DrawerPrimitive.Portal>) {
|
|
51
|
+
return <DrawerPrimitive.Portal data-slot="drawer-portal" {...props} />
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function DrawerOverlay({
|
|
55
|
+
className,
|
|
56
|
+
...props
|
|
57
|
+
}: React.ComponentProps<typeof DrawerPrimitive.Overlay>) {
|
|
58
|
+
return (
|
|
59
|
+
<DrawerPrimitive.Overlay
|
|
60
|
+
data-slot="drawer-overlay"
|
|
61
|
+
className={cn(
|
|
62
|
+
"fixed inset-0 z-50 bg-black/40 backdrop-blur-sm data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:animate-in data-[state=open]:fade-in-0",
|
|
63
|
+
className
|
|
64
|
+
)}
|
|
65
|
+
{...props}
|
|
66
|
+
/>
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// ---------------------------------------------------------------------------
|
|
71
|
+
// DrawerContent — the animated panel
|
|
72
|
+
// ---------------------------------------------------------------------------
|
|
73
|
+
|
|
74
|
+
function DrawerContent({
|
|
75
|
+
className,
|
|
76
|
+
children,
|
|
77
|
+
side = "bottom",
|
|
78
|
+
showHandle,
|
|
79
|
+
showCloseButton = true,
|
|
80
|
+
...props
|
|
81
|
+
}: React.ComponentProps<typeof DrawerPrimitive.Content> & {
|
|
82
|
+
/**
|
|
83
|
+
* Which edge the drawer slides from.
|
|
84
|
+
* @default "bottom"
|
|
85
|
+
*/
|
|
86
|
+
side?: "top" | "right" | "bottom" | "left"
|
|
87
|
+
/**
|
|
88
|
+
* Render the centered drag-handle affordance.
|
|
89
|
+
* Defaults to `true` when `side="bottom"`, false for every other side.
|
|
90
|
+
*/
|
|
91
|
+
showHandle?: boolean
|
|
92
|
+
/** Whether to render the built-in close button. @default true */
|
|
93
|
+
showCloseButton?: boolean
|
|
94
|
+
}) {
|
|
95
|
+
// Resolve showHandle default: visible only on bottom by default
|
|
96
|
+
const resolvedShowHandle = showHandle ?? side === "bottom"
|
|
97
|
+
|
|
98
|
+
// Portal theme class — makes theme-scoped CSS variables resolve correctly
|
|
99
|
+
// inside the Radix portal (same mechanism as Sheet / Dialog).
|
|
100
|
+
const portalThemeClass = usePortalThemeClass()
|
|
101
|
+
|
|
102
|
+
// -------------------------------------------------------------------------
|
|
103
|
+
// Gesture state — drag-to-dismiss for touch/pointer devices
|
|
104
|
+
// -------------------------------------------------------------------------
|
|
105
|
+
const panelRef = React.useRef<HTMLDivElement>(null)
|
|
106
|
+
const dragStartRef = React.useRef<number | null>(null)
|
|
107
|
+
const [dragOffset, setDragOffset] = React.useState(0)
|
|
108
|
+
const [isAnimatingBack, setIsAnimatingBack] = React.useState(false)
|
|
109
|
+
|
|
110
|
+
// We need to close the drawer when the drag threshold is met. Radix Dialog
|
|
111
|
+
// exposes `onOpenChange` on the Root. To reach it from DrawerContent we
|
|
112
|
+
// forward the close call through Radix's own Close primitive programmatically
|
|
113
|
+
// by reading the panel's ownerDocument and dispatching an Escape KeyboardEvent
|
|
114
|
+
// — OR more cleanly, by accepting an optional `onGestureDismiss` prop. The
|
|
115
|
+
// simplest zero-prop approach: track a local close-ref via the DrawerContext.
|
|
116
|
+
const closeFnRef = React.useRef<(() => void) | null>(null)
|
|
117
|
+
|
|
118
|
+
// Resolve the dismiss axis and sign based on the side
|
|
119
|
+
function getDragMetrics(e: React.PointerEvent<HTMLDivElement>) {
|
|
120
|
+
const panel = panelRef.current
|
|
121
|
+
if (!panel) return null
|
|
122
|
+
const rect = panel.getBoundingClientRect()
|
|
123
|
+
if (side === "bottom" || side === "top") {
|
|
124
|
+
return { size: rect.height, axis: "y" as const }
|
|
125
|
+
}
|
|
126
|
+
return { size: rect.width, axis: "x" as const }
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function getDragDelta(e: React.PointerEvent<HTMLDivElement>): number {
|
|
130
|
+
if (dragStartRef.current === null) return 0
|
|
131
|
+
if (side === "bottom") return e.clientY - dragStartRef.current // positive = drag down (dismiss)
|
|
132
|
+
if (side === "top") return dragStartRef.current - e.clientY // positive = drag up (dismiss)
|
|
133
|
+
if (side === "right") return e.clientX - dragStartRef.current // positive = drag right (dismiss)
|
|
134
|
+
if (side === "left") return dragStartRef.current - e.clientX // positive = drag left (dismiss)
|
|
135
|
+
return 0
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function handlePointerDown(e: React.PointerEvent<HTMLDivElement>) {
|
|
139
|
+
// Only respond to primary pointer (finger / left mouse)
|
|
140
|
+
if (e.button !== 0 && e.pointerType !== "touch") return
|
|
141
|
+
const coord = side === "bottom" || side === "top" ? e.clientY : e.clientX
|
|
142
|
+
dragStartRef.current = coord
|
|
143
|
+
;(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId)
|
|
144
|
+
setIsAnimatingBack(false)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function handlePointerMove(e: React.PointerEvent<HTMLDivElement>) {
|
|
148
|
+
if (dragStartRef.current === null) return
|
|
149
|
+
const delta = getDragDelta(e)
|
|
150
|
+
// Only allow dragging in the dismiss direction (positive delta)
|
|
151
|
+
setDragOffset(Math.max(0, delta))
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function handlePointerUp(e: React.PointerEvent<HTMLDivElement>) {
|
|
155
|
+
if (dragStartRef.current === null) return
|
|
156
|
+
const delta = getDragDelta(e)
|
|
157
|
+
const metrics = getDragMetrics(e)
|
|
158
|
+
dragStartRef.current = null
|
|
159
|
+
|
|
160
|
+
if (metrics && delta > metrics.size * 0.3) {
|
|
161
|
+
// Threshold met — dismiss. Fire the close via the Radix Close button
|
|
162
|
+
// that we render in the DOM (we'll look it up via the panel ref).
|
|
163
|
+
const closeBtn = panelRef.current?.querySelector<HTMLElement>(
|
|
164
|
+
'[data-slot="drawer-close"]'
|
|
165
|
+
)
|
|
166
|
+
closeBtn?.click()
|
|
167
|
+
} else {
|
|
168
|
+
// Spring back
|
|
169
|
+
setIsAnimatingBack(true)
|
|
170
|
+
setDragOffset(0)
|
|
171
|
+
window.setTimeout(() => setIsAnimatingBack(false), 350)
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Build the translate style for the drag gesture
|
|
176
|
+
function getDragTransformStyle(): React.CSSProperties {
|
|
177
|
+
if (dragOffset === 0 && !isAnimatingBack) return {}
|
|
178
|
+
|
|
179
|
+
let transform = ""
|
|
180
|
+
if (side === "bottom") transform = `translateY(${dragOffset}px)`
|
|
181
|
+
else if (side === "top") transform = `translateY(-${dragOffset}px)`
|
|
182
|
+
else if (side === "right") transform = `translateX(${dragOffset}px)`
|
|
183
|
+
else if (side === "left") transform = `translateX(-${dragOffset}px)`
|
|
184
|
+
|
|
185
|
+
if (isAnimatingBack || dragOffset === 0) {
|
|
186
|
+
return {
|
|
187
|
+
transform,
|
|
188
|
+
transitionProperty: "transform",
|
|
189
|
+
transitionDuration: "var(--duration-base)",
|
|
190
|
+
transitionTimingFunction: "var(--ease-spring)",
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
// While dragging: suppress transitions so the panel follows the finger
|
|
194
|
+
// immediately. `transitionProperty: "none"` is a CSS property toggle,
|
|
195
|
+
// not a hardcoded duration value — safe per the microanimation lint.
|
|
196
|
+
return { transform, transitionProperty: "none" }
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return (
|
|
200
|
+
<DrawerPortal>
|
|
201
|
+
<DrawerOverlay />
|
|
202
|
+
<DrawerPrimitive.Content
|
|
203
|
+
ref={panelRef}
|
|
204
|
+
data-slot="drawer-content"
|
|
205
|
+
className={cn(
|
|
206
|
+
"fixed z-50 flex flex-col gap-4 bg-background shadow-lg",
|
|
207
|
+
// State-change entry/exit — tw-animate-css pattern (a): existing
|
|
208
|
+
// keyframe utilities. `animate-in` / `animate-out` + directional
|
|
209
|
+
// slide combinator utilities from tw-animate-css are all valid.
|
|
210
|
+
"data-[state=open]:animate-in data-[state=closed]:animate-out",
|
|
211
|
+
side === "bottom" && [
|
|
212
|
+
"inset-x-0 bottom-0 h-auto max-h-[85svh] gap-0 overflow-y-auto rounded-t-xl border-t",
|
|
213
|
+
"pb-[max(1rem,env(safe-area-inset-bottom))]",
|
|
214
|
+
"data-[state=open]:slide-in-from-bottom data-[state=closed]:slide-out-to-bottom",
|
|
215
|
+
],
|
|
216
|
+
side === "top" && [
|
|
217
|
+
"inset-x-0 top-0 h-auto border-b",
|
|
218
|
+
"data-[state=open]:slide-in-from-top data-[state=closed]:slide-out-to-top",
|
|
219
|
+
],
|
|
220
|
+
side === "right" && [
|
|
221
|
+
"inset-y-0 right-0 h-full w-3/4 border-l sm:max-w-sm",
|
|
222
|
+
"data-[state=open]:slide-in-from-right data-[state=closed]:slide-out-to-right",
|
|
223
|
+
],
|
|
224
|
+
side === "left" && [
|
|
225
|
+
"inset-y-0 left-0 h-full w-3/4 border-r sm:max-w-sm",
|
|
226
|
+
"data-[state=open]:slide-in-from-left data-[state=closed]:slide-out-to-left",
|
|
227
|
+
],
|
|
228
|
+
portalThemeClass,
|
|
229
|
+
className
|
|
230
|
+
)}
|
|
231
|
+
style={getDragTransformStyle()}
|
|
232
|
+
onPointerDown={handlePointerDown}
|
|
233
|
+
onPointerMove={handlePointerMove}
|
|
234
|
+
onPointerUp={handlePointerUp}
|
|
235
|
+
{...props}
|
|
236
|
+
>
|
|
237
|
+
{/* Drag handle — bottom panels only, centered at top */}
|
|
238
|
+
{resolvedShowHandle && (
|
|
239
|
+
<div
|
|
240
|
+
aria-hidden="true"
|
|
241
|
+
data-slot="drawer-handle"
|
|
242
|
+
className={cn(
|
|
243
|
+
"mx-auto mt-3 mb-1 h-1.5 w-12 shrink-0 cursor-grab rounded-full",
|
|
244
|
+
"bg-muted-foreground/30 transition-colors",
|
|
245
|
+
"hover:bg-muted-foreground/50 active:bg-muted-foreground/60"
|
|
246
|
+
)}
|
|
247
|
+
style={{
|
|
248
|
+
transitionDuration: "var(--duration-fast)",
|
|
249
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
250
|
+
}}
|
|
251
|
+
/>
|
|
252
|
+
)}
|
|
253
|
+
|
|
254
|
+
{children}
|
|
255
|
+
|
|
256
|
+
{/* Built-in close button — 44px hit target, top-4 right-4 */}
|
|
257
|
+
{showCloseButton && (
|
|
258
|
+
<DrawerPrimitive.Close
|
|
259
|
+
data-slot="drawer-close"
|
|
260
|
+
className="absolute top-4 right-4 inline-flex size-11 items-center justify-center rounded-xs opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus:outline-hidden disabled:pointer-events-none [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-8"
|
|
261
|
+
>
|
|
262
|
+
<XIcon strokeWidth={1.5} />
|
|
263
|
+
<span className="sr-only">Close</span>
|
|
264
|
+
</DrawerPrimitive.Close>
|
|
265
|
+
)}
|
|
266
|
+
</DrawerPrimitive.Content>
|
|
267
|
+
</DrawerPortal>
|
|
268
|
+
)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// ---------------------------------------------------------------------------
|
|
272
|
+
// Structural sub-components (parallel API to Sheet)
|
|
273
|
+
// ---------------------------------------------------------------------------
|
|
274
|
+
|
|
275
|
+
function DrawerHeader({ className, ...props }: React.ComponentProps<"div">) {
|
|
276
|
+
return (
|
|
277
|
+
<div
|
|
278
|
+
data-slot="drawer-header"
|
|
279
|
+
// pr-16 reserves space for the absolutely-positioned close button so the
|
|
280
|
+
// title / description can never run underneath it.
|
|
281
|
+
className={cn("flex flex-col gap-1.5 p-4 pr-16", className)}
|
|
282
|
+
{...props}
|
|
283
|
+
/>
|
|
284
|
+
)
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function DrawerFooter({ className, ...props }: React.ComponentProps<"div">) {
|
|
288
|
+
return (
|
|
289
|
+
<div
|
|
290
|
+
data-slot="drawer-footer"
|
|
291
|
+
className={cn("mt-auto flex flex-col gap-2 p-4", className)}
|
|
292
|
+
{...props}
|
|
293
|
+
/>
|
|
294
|
+
)
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function DrawerTitle({
|
|
298
|
+
className,
|
|
299
|
+
...props
|
|
300
|
+
}: React.ComponentProps<typeof DrawerPrimitive.Title>) {
|
|
301
|
+
return (
|
|
302
|
+
<DrawerPrimitive.Title
|
|
303
|
+
data-slot="drawer-title"
|
|
304
|
+
className={cn("font-semibold text-foreground", className)}
|
|
305
|
+
{...props}
|
|
306
|
+
/>
|
|
307
|
+
)
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function DrawerDescription({
|
|
311
|
+
className,
|
|
312
|
+
...props
|
|
313
|
+
}: React.ComponentProps<typeof DrawerPrimitive.Description>) {
|
|
314
|
+
return (
|
|
315
|
+
<DrawerPrimitive.Description
|
|
316
|
+
data-slot="drawer-description"
|
|
317
|
+
className={cn("text-sm text-muted-foreground", className)}
|
|
318
|
+
{...props}
|
|
319
|
+
/>
|
|
320
|
+
)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
export {
|
|
324
|
+
Drawer,
|
|
325
|
+
DrawerTrigger,
|
|
326
|
+
DrawerClose,
|
|
327
|
+
DrawerPortal,
|
|
328
|
+
DrawerOverlay,
|
|
329
|
+
DrawerContent,
|
|
330
|
+
DrawerHeader,
|
|
331
|
+
DrawerFooter,
|
|
332
|
+
DrawerTitle,
|
|
333
|
+
DrawerDescription,
|
|
334
|
+
}
|