@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,296 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react"
|
|
5
|
+
|
|
6
|
+
import { cn } from "../../lib/utils"
|
|
7
|
+
import { Button } from "./button"
|
|
8
|
+
|
|
9
|
+
export interface MonthPickerProps {
|
|
10
|
+
/** Controlled selected month. The day-of-month is normalized to the 1st. */
|
|
11
|
+
value?: Date
|
|
12
|
+
/** Uncontrolled initial selection. */
|
|
13
|
+
defaultValue?: Date
|
|
14
|
+
/** Fires with the first day of the picked month (local time). */
|
|
15
|
+
onChange?: (date: Date) => void
|
|
16
|
+
/** Disables the entire control — year navigation and every month cell. */
|
|
17
|
+
disabled?: boolean
|
|
18
|
+
/**
|
|
19
|
+
* Predicate disabling specific months. Receives the first day of the
|
|
20
|
+
* candidate month (local time) — never rely on the day-of-month.
|
|
21
|
+
*/
|
|
22
|
+
disabledMonths?: (date: Date) => boolean
|
|
23
|
+
className?: string
|
|
24
|
+
id?: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Explicit locale so server and client render identical labels (no hydration
|
|
28
|
+
// mismatch) — same convention as FormatNumber.
|
|
29
|
+
const MONTH_SHORT = Array.from({ length: 12 }, (_, m) =>
|
|
30
|
+
new Date(2000, m, 1).toLocaleString("en-US", { month: "short" })
|
|
31
|
+
)
|
|
32
|
+
const MONTH_LONG = Array.from({ length: 12 }, (_, m) =>
|
|
33
|
+
new Date(2000, m, 1).toLocaleString("en-US", { month: "long" })
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
// 3-column × 4-row month grid — the cross-library consensus shape
|
|
37
|
+
// (Mantine MonthPicker, MUI-X MonthCalendar, Ant Design picker="month",
|
|
38
|
+
// Ark UI DatePicker view=month).
|
|
39
|
+
const ROWS = [
|
|
40
|
+
[0, 1, 2],
|
|
41
|
+
[3, 4, 5],
|
|
42
|
+
[6, 7, 8],
|
|
43
|
+
[9, 10, 11],
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Calendar grid of 12 month cells for a given year — yields a month + year
|
|
48
|
+
* selection without drilling into individual dates. Use for billing periods,
|
|
49
|
+
* report month filters, and archive navigation; for a specific day, use
|
|
50
|
+
* DatePicker instead.
|
|
51
|
+
*
|
|
52
|
+
* ARIA grid pattern: grid → row → gridcell buttons with roving tabindex.
|
|
53
|
+
* Arrow keys move ±1 / ±3 months, Home/End jump to first/last enabled month,
|
|
54
|
+
* PageUp/PageDown page the displayed year.
|
|
55
|
+
*/
|
|
56
|
+
function MonthPicker({
|
|
57
|
+
value,
|
|
58
|
+
defaultValue,
|
|
59
|
+
onChange,
|
|
60
|
+
disabled = false,
|
|
61
|
+
disabledMonths,
|
|
62
|
+
className,
|
|
63
|
+
id,
|
|
64
|
+
}: MonthPickerProps) {
|
|
65
|
+
const isControlled = value !== undefined
|
|
66
|
+
const [internalValue, setInternalValue] = React.useState<Date | undefined>(
|
|
67
|
+
defaultValue
|
|
68
|
+
)
|
|
69
|
+
const selected = isControlled ? value : internalValue
|
|
70
|
+
|
|
71
|
+
const [viewYear, setViewYear] = React.useState<number>(() =>
|
|
72
|
+
(selected ?? new Date()).getFullYear()
|
|
73
|
+
)
|
|
74
|
+
// Roving tabindex: the month index the user last focused, if any.
|
|
75
|
+
const [focusedMonth, setFocusedMonth] = React.useState<number | null>(null)
|
|
76
|
+
const cellRefs = React.useRef<Array<HTMLButtonElement | null>>([])
|
|
77
|
+
|
|
78
|
+
// Follow a controlled value into its year so the selection stays visible.
|
|
79
|
+
const valueTime = value?.getTime()
|
|
80
|
+
React.useEffect(() => {
|
|
81
|
+
if (valueTime !== undefined) {
|
|
82
|
+
setViewYear(new Date(valueTime).getFullYear())
|
|
83
|
+
}
|
|
84
|
+
}, [valueTime])
|
|
85
|
+
|
|
86
|
+
const isMonthDisabled = React.useCallback(
|
|
87
|
+
(month: number) =>
|
|
88
|
+
disabled || (disabledMonths?.(new Date(viewYear, month, 1)) ?? false),
|
|
89
|
+
[disabled, disabledMonths, viewYear]
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
const today = new Date()
|
|
93
|
+
const selectedMonthInView =
|
|
94
|
+
selected && selected.getFullYear() === viewYear ? selected.getMonth() : null
|
|
95
|
+
const todayMonthInView =
|
|
96
|
+
today.getFullYear() === viewYear ? today.getMonth() : null
|
|
97
|
+
|
|
98
|
+
// Which cell carries tabIndex=0: last-focused, else selected, else today's
|
|
99
|
+
// month, else the first enabled month in the displayed year.
|
|
100
|
+
// When every cell is disabled (disabled prop or disabledMonths covers all 12)
|
|
101
|
+
// no button can carry tabIndex=0 — the grid div itself becomes the tab stop.
|
|
102
|
+
const allDisabled = React.useMemo(
|
|
103
|
+
() => Array.from({ length: 12 }, (_, m) => m).every((m) => isMonthDisabled(m)),
|
|
104
|
+
[isMonthDisabled]
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
const defaultTabbable = React.useMemo(() => {
|
|
108
|
+
if (selectedMonthInView !== null && !isMonthDisabled(selectedMonthInView)) {
|
|
109
|
+
return selectedMonthInView
|
|
110
|
+
}
|
|
111
|
+
if (todayMonthInView !== null && !isMonthDisabled(todayMonthInView)) {
|
|
112
|
+
return todayMonthInView
|
|
113
|
+
}
|
|
114
|
+
for (let m = 0; m < 12; m++) {
|
|
115
|
+
if (!isMonthDisabled(m)) return m
|
|
116
|
+
}
|
|
117
|
+
return 0
|
|
118
|
+
}, [selectedMonthInView, todayMonthInView, isMonthDisabled])
|
|
119
|
+
const tabbableMonth =
|
|
120
|
+
focusedMonth !== null && !isMonthDisabled(focusedMonth)
|
|
121
|
+
? focusedMonth
|
|
122
|
+
: defaultTabbable
|
|
123
|
+
|
|
124
|
+
function selectMonth(month: number) {
|
|
125
|
+
if (isMonthDisabled(month)) return
|
|
126
|
+
const next = new Date(viewYear, month, 1)
|
|
127
|
+
if (!isControlled) setInternalValue(next)
|
|
128
|
+
onChange?.(next)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function focusCell(month: number) {
|
|
132
|
+
cellRefs.current[month]?.focus()
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function handleGridKeyDown(event: React.KeyboardEvent<HTMLDivElement>) {
|
|
136
|
+
const from = focusedMonth ?? tabbableMonth
|
|
137
|
+
let delta = 0
|
|
138
|
+
switch (event.key) {
|
|
139
|
+
case "ArrowLeft":
|
|
140
|
+
delta = -1
|
|
141
|
+
break
|
|
142
|
+
case "ArrowRight":
|
|
143
|
+
delta = 1
|
|
144
|
+
break
|
|
145
|
+
case "ArrowUp":
|
|
146
|
+
delta = -3
|
|
147
|
+
break
|
|
148
|
+
case "ArrowDown":
|
|
149
|
+
delta = 3
|
|
150
|
+
break
|
|
151
|
+
case "Home": {
|
|
152
|
+
event.preventDefault()
|
|
153
|
+
for (let m = 0; m < 12; m++) {
|
|
154
|
+
if (!isMonthDisabled(m)) {
|
|
155
|
+
focusCell(m)
|
|
156
|
+
return
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return
|
|
160
|
+
}
|
|
161
|
+
case "End": {
|
|
162
|
+
event.preventDefault()
|
|
163
|
+
for (let m = 11; m >= 0; m--) {
|
|
164
|
+
if (!isMonthDisabled(m)) {
|
|
165
|
+
focusCell(m)
|
|
166
|
+
return
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return
|
|
170
|
+
}
|
|
171
|
+
case "PageUp":
|
|
172
|
+
event.preventDefault()
|
|
173
|
+
if (!disabled) setViewYear((y) => y - 1)
|
|
174
|
+
return
|
|
175
|
+
case "PageDown":
|
|
176
|
+
event.preventDefault()
|
|
177
|
+
if (!disabled) setViewYear((y) => y + 1)
|
|
178
|
+
return
|
|
179
|
+
default:
|
|
180
|
+
return
|
|
181
|
+
}
|
|
182
|
+
event.preventDefault()
|
|
183
|
+
// Skip over disabled cells in the direction of travel.
|
|
184
|
+
let next = from + delta
|
|
185
|
+
while (next >= 0 && next <= 11 && isMonthDisabled(next)) {
|
|
186
|
+
next += delta
|
|
187
|
+
}
|
|
188
|
+
if (next < 0 || next > 11) return
|
|
189
|
+
focusCell(next)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return (
|
|
193
|
+
<div
|
|
194
|
+
id={id}
|
|
195
|
+
data-slot="month-picker"
|
|
196
|
+
data-disabled={disabled || undefined}
|
|
197
|
+
className={cn("w-fit bg-background p-3", className)}
|
|
198
|
+
>
|
|
199
|
+
<div
|
|
200
|
+
data-slot="month-picker-header"
|
|
201
|
+
className="mb-2 flex items-center justify-between gap-1"
|
|
202
|
+
>
|
|
203
|
+
<Button
|
|
204
|
+
type="button"
|
|
205
|
+
variant="ghost"
|
|
206
|
+
size="icon-xl"
|
|
207
|
+
aria-label="Previous year"
|
|
208
|
+
disabled={disabled}
|
|
209
|
+
onClick={() => setViewYear((y) => y - 1)}
|
|
210
|
+
>
|
|
211
|
+
<ChevronLeftIcon strokeWidth={1.5} aria-hidden="true" />
|
|
212
|
+
</Button>
|
|
213
|
+
<span
|
|
214
|
+
aria-live="polite"
|
|
215
|
+
className={cn(
|
|
216
|
+
"text-sm font-medium tabular-nums select-none",
|
|
217
|
+
disabled ? "text-muted-foreground" : "text-foreground"
|
|
218
|
+
)}
|
|
219
|
+
>
|
|
220
|
+
{viewYear}
|
|
221
|
+
</span>
|
|
222
|
+
<Button
|
|
223
|
+
type="button"
|
|
224
|
+
variant="ghost"
|
|
225
|
+
size="icon-xl"
|
|
226
|
+
aria-label="Next year"
|
|
227
|
+
disabled={disabled}
|
|
228
|
+
onClick={() => setViewYear((y) => y + 1)}
|
|
229
|
+
>
|
|
230
|
+
<ChevronRightIcon strokeWidth={1.5} aria-hidden="true" />
|
|
231
|
+
</Button>
|
|
232
|
+
</div>
|
|
233
|
+
<div
|
|
234
|
+
role="grid"
|
|
235
|
+
aria-label="Month picker"
|
|
236
|
+
aria-disabled={disabled || undefined}
|
|
237
|
+
data-slot="month-picker-grid"
|
|
238
|
+
tabIndex={allDisabled ? 0 : -1}
|
|
239
|
+
onKeyDown={handleGridKeyDown}
|
|
240
|
+
className="w-64 space-y-1"
|
|
241
|
+
>
|
|
242
|
+
{ROWS.map((row) => (
|
|
243
|
+
<div key={row[0]} role="row" className="grid grid-cols-3 gap-1">
|
|
244
|
+
{row.map((month) => {
|
|
245
|
+
const isSelected = selectedMonthInView === month
|
|
246
|
+
const isToday = todayMonthInView === month
|
|
247
|
+
const isDisabled = isMonthDisabled(month)
|
|
248
|
+
return (
|
|
249
|
+
<button
|
|
250
|
+
key={month}
|
|
251
|
+
ref={(el) => {
|
|
252
|
+
cellRefs.current[month] = el
|
|
253
|
+
}}
|
|
254
|
+
type="button"
|
|
255
|
+
role="gridcell"
|
|
256
|
+
data-slot="month-picker-cell"
|
|
257
|
+
data-selected={isSelected || undefined}
|
|
258
|
+
data-today={isToday || undefined}
|
|
259
|
+
aria-label={`${MONTH_LONG[month]} ${viewYear}`}
|
|
260
|
+
aria-selected={isSelected}
|
|
261
|
+
aria-disabled={isDisabled || undefined}
|
|
262
|
+
disabled={isDisabled}
|
|
263
|
+
tabIndex={!isDisabled && month === tabbableMonth ? 0 : -1}
|
|
264
|
+
onClick={() => selectMonth(month)}
|
|
265
|
+
onFocus={() => setFocusedMonth(month)}
|
|
266
|
+
// Motion pattern (c): token-driven one-off transition —
|
|
267
|
+
// never Tailwind duration-* classes (silent failure in v4).
|
|
268
|
+
style={{
|
|
269
|
+
transitionDuration: "var(--duration-fast)",
|
|
270
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
271
|
+
}}
|
|
272
|
+
className={cn(
|
|
273
|
+
"inline-flex min-h-11 items-center justify-center rounded-md px-2 py-2 text-sm select-none",
|
|
274
|
+
"transition-colors outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50",
|
|
275
|
+
"disabled:pointer-events-none disabled:opacity-50",
|
|
276
|
+
isSelected
|
|
277
|
+
? "bg-primary text-primary-foreground hover:bg-primary/90"
|
|
278
|
+
: "text-foreground hover:bg-accent hover:text-accent-foreground",
|
|
279
|
+
// Today's month: underline + weight, never color alone.
|
|
280
|
+
isToday &&
|
|
281
|
+
!isSelected &&
|
|
282
|
+
"font-medium text-primary underline underline-offset-4"
|
|
283
|
+
)}
|
|
284
|
+
>
|
|
285
|
+
{MONTH_SHORT[month]}
|
|
286
|
+
</button>
|
|
287
|
+
)
|
|
288
|
+
})}
|
|
289
|
+
</div>
|
|
290
|
+
))}
|
|
291
|
+
</div>
|
|
292
|
+
</div>
|
|
293
|
+
)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export { MonthPicker }
|
|
@@ -120,21 +120,23 @@ function MultiSelect({
|
|
|
120
120
|
<Badge
|
|
121
121
|
key={opt.value}
|
|
122
122
|
variant="secondary"
|
|
123
|
-
className="gap-1
|
|
123
|
+
className={cn("gap-1 font-normal py-0 min-h-6", !disabled && "pr-1")}
|
|
124
124
|
>
|
|
125
125
|
<span className="truncate max-w-[120px]">{opt.label}</span>
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
126
|
+
{!disabled && (
|
|
127
|
+
<button
|
|
128
|
+
type="button"
|
|
129
|
+
aria-label={`Remove ${opt.label}`}
|
|
130
|
+
onClick={(e) => removeAt(opt.value, e)}
|
|
131
|
+
className="pointer-events-auto h-6 w-6 flex items-center justify-center rounded-sm opacity-60 hover:opacity-100 outline-none focus-visible:ring-1 focus-visible:ring-ring transition-opacity"
|
|
132
|
+
style={{
|
|
133
|
+
transitionDuration: "var(--duration-fast)",
|
|
134
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
135
|
+
}}
|
|
136
|
+
>
|
|
137
|
+
<XIcon size={10} strokeWidth={1.5} />
|
|
138
|
+
</button>
|
|
139
|
+
)}
|
|
138
140
|
</Badge>
|
|
139
141
|
))}
|
|
140
142
|
{overflowCount > 0 && (
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { Slot, Slottable } from "@radix-ui/react-slot";
|
|
5
|
+
|
|
6
|
+
import { cn } from "../../lib/utils";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* NavLink — individual sidebar navigation link primitive.
|
|
10
|
+
*
|
|
11
|
+
* Pairs with BADS' Sidebar container component, filling the gap between the
|
|
12
|
+
* full sidebar shell and the individual clickable destination. Sources:
|
|
13
|
+
* Mantine NavLink (icon/badge/nested API), Radix-based composition (asChild
|
|
14
|
+
* for Next.js Link).
|
|
15
|
+
*
|
|
16
|
+
* Active state is CSS-only via `aria-current="page"` — the `active` prop sets
|
|
17
|
+
* the attribute; the attribute selector in the className does the styling.
|
|
18
|
+
* No JS class toggling needed, which means the active mark survives SSR,
|
|
19
|
+
* hydration, and static export intact.
|
|
20
|
+
*
|
|
21
|
+
* Icon slot: enforces 1.5 stroke width via `[&>svg]:stroke-[1.5]` so the
|
|
22
|
+
* consumer only needs to pass a Lucide icon (or any SVG) — no manual
|
|
23
|
+
* strokeWidth wiring required.
|
|
24
|
+
*
|
|
25
|
+
* Badge slot: `ml-auto` pushes it to the trailing edge regardless of label
|
|
26
|
+
* length. Wraps arbitrary ReactNode so the consumer can pass a `<Badge>`,
|
|
27
|
+
* a count string, or a status dot.
|
|
28
|
+
*
|
|
29
|
+
* asChild idiom (same as Button / Link) — when `asChild={true}`, the Slot
|
|
30
|
+
* from @radix-ui/react-slot merges NavLink's props onto the single child
|
|
31
|
+
* element, making `<NavLink asChild><NextLink href="...">…</NextLink></NavLink>`
|
|
32
|
+
* pattern work with no wrapper div.
|
|
33
|
+
*
|
|
34
|
+
* // Basic
|
|
35
|
+
* <NavLink href="/dashboard" icon={<LayoutDashboard />}>Dashboard</NavLink>
|
|
36
|
+
*
|
|
37
|
+
* // Active
|
|
38
|
+
* <NavLink href="/settings" icon={<Settings />} active>Settings</NavLink>
|
|
39
|
+
*
|
|
40
|
+
* // With badge
|
|
41
|
+
* <NavLink href="/inbox" icon={<Bell />} badge="3">Notifications</NavLink>
|
|
42
|
+
*
|
|
43
|
+
* // Nested / indented sub-item
|
|
44
|
+
* <NavLink href="/reports/monthly" nested>Monthly</NavLink>
|
|
45
|
+
*
|
|
46
|
+
* // As Next.js Link
|
|
47
|
+
* <NavLink asChild active={pathname === "/dashboard"}>
|
|
48
|
+
* <NextLink href="/dashboard">Dashboard</NextLink>
|
|
49
|
+
* </NavLink>
|
|
50
|
+
*
|
|
51
|
+
* AAA notes:
|
|
52
|
+
* - Native `<a>` element preserves link semantics; Tab/Shift+Tab navigation
|
|
53
|
+
* is handled by the browser with no extra JS.
|
|
54
|
+
* - `aria-current="page"` is the ARIA pattern for marking the current page
|
|
55
|
+
* in a navigation landmark (SC 2.4.8 Location; ARIA APG).
|
|
56
|
+
* - Target height: `py-2` (8px × 2) + `text-sm` (16px × 1.5 line-height =
|
|
57
|
+
* 24px) = 40px base; min-h-11 (44px) ensures the WCAG 2.2 SC 2.5.5
|
|
58
|
+
* Enhanced floor is always met.
|
|
59
|
+
* - Icon is aria-hidden by convention — consumers should add sr-only text
|
|
60
|
+
* or ensure the label is present alongside the icon.
|
|
61
|
+
*
|
|
62
|
+
* Motion pattern (c) — inline var() for one-off hover transition:
|
|
63
|
+
* transition-colors (explicit property, not transition-all)
|
|
64
|
+
* style={{ transitionDuration: 'var(--duration-fast)', transitionTimingFunction: 'var(--ease-out)' }}
|
|
65
|
+
*
|
|
66
|
+
* @param active - Sets `aria-current="page"` + active styling via CSS attribute selector
|
|
67
|
+
* @param icon - Left slot. Renders at 16px; `[&>svg]:stroke-[1.5]` enforces 1.5 stroke.
|
|
68
|
+
* @param badge - Right slot. Pushed to trailing edge via ml-auto. Pass a Badge, count, or dot.
|
|
69
|
+
* @param nested - Adds `pl-8` indent for sub-navigation items under a parent item.
|
|
70
|
+
* @param asChild - Radix Slot idiom — merge styles onto single child (e.g. Next.js Link).
|
|
71
|
+
*/
|
|
72
|
+
|
|
73
|
+
export interface NavLinkProps extends React.ComponentProps<"a"> {
|
|
74
|
+
/**
|
|
75
|
+
* Marks this link as the current page — sets `aria-current="page"` on the
|
|
76
|
+
* root element and activates the accent-background styling via a CSS
|
|
77
|
+
* attribute selector (no JS class toggling).
|
|
78
|
+
*/
|
|
79
|
+
active?: boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Left icon slot. Expected size: 16px (size-4). The icon container applies
|
|
82
|
+
* `[&>svg]:size-4 [&>svg]:stroke-[1.5]` so stroke width and sizing are
|
|
83
|
+
* enforced at the NavLink level — the consumer only passes the icon node.
|
|
84
|
+
* Use aria-hidden="true" on decorative icons (the default for most nav icons
|
|
85
|
+
* where the text label carries the accessible name).
|
|
86
|
+
*/
|
|
87
|
+
icon?: React.ReactNode;
|
|
88
|
+
/**
|
|
89
|
+
* Right badge slot. Pushed to the trailing edge via `ml-auto`. Pass a BADS
|
|
90
|
+
* `<Badge>`, a plain count string, an unread dot, or any ReactNode. When
|
|
91
|
+
* count-only (no visible text label beside it), add an `aria-label` on the
|
|
92
|
+
* badge element so screen readers announce the full meaning
|
|
93
|
+
* (e.g. `aria-label="3 unread notifications"`).
|
|
94
|
+
*/
|
|
95
|
+
badge?: React.ReactNode;
|
|
96
|
+
/**
|
|
97
|
+
* Indented variant for sub-navigation items nested under a parent item.
|
|
98
|
+
* Applies `pl-8` (32px left padding) to convey the hierarchy visually and
|
|
99
|
+
* spatially — no JavaScript state required, purely a visual/layout signal.
|
|
100
|
+
*/
|
|
101
|
+
nested?: boolean;
|
|
102
|
+
/**
|
|
103
|
+
* When true, renders as the child element instead of the default `<a>` tag.
|
|
104
|
+
* Use for semantic overrides (e.g. `<NavLink asChild><NextLink href="…">…</NextLink></NavLink>`).
|
|
105
|
+
* The Slot from @radix-ui/react-slot merges all NavLink props and className
|
|
106
|
+
* onto the single child element. `Slottable` marks which child carries the
|
|
107
|
+
* user-provided element while icon/badge spans are passed through as
|
|
108
|
+
* additional children of the merged element.
|
|
109
|
+
*/
|
|
110
|
+
asChild?: boolean;
|
|
111
|
+
/**
|
|
112
|
+
* Visually and semantically disables the link. Sets `aria-disabled="true"`
|
|
113
|
+
* and suppresses pointer events so keyboard and screen-reader users are
|
|
114
|
+
* informed without relying on the non-standard HTML `disabled` on `<a>`.
|
|
115
|
+
*/
|
|
116
|
+
disabled?: boolean;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function NavLink({
|
|
120
|
+
active = false,
|
|
121
|
+
icon,
|
|
122
|
+
badge,
|
|
123
|
+
nested = false,
|
|
124
|
+
asChild = false,
|
|
125
|
+
className,
|
|
126
|
+
style,
|
|
127
|
+
children,
|
|
128
|
+
disabled,
|
|
129
|
+
...props
|
|
130
|
+
}: NavLinkProps) {
|
|
131
|
+
const Comp = asChild ? Slot : "a";
|
|
132
|
+
|
|
133
|
+
return (
|
|
134
|
+
<Comp
|
|
135
|
+
data-slot="nav-link"
|
|
136
|
+
aria-current={active ? "page" : undefined}
|
|
137
|
+
aria-disabled={disabled ? "true" : undefined}
|
|
138
|
+
className={cn(
|
|
139
|
+
// Base layout — full-width flex row, token spacing + radius
|
|
140
|
+
"flex min-h-11 w-full items-center gap-2 rounded-md px-3 py-2 text-sm font-medium text-foreground outline-none",
|
|
141
|
+
// Hover state — accent surface + foreground, token-driven transition
|
|
142
|
+
"hover:bg-accent hover:text-accent-foreground",
|
|
143
|
+
// Active / current-page state — CSS-only via aria-current attribute
|
|
144
|
+
// selector. No JS class needed; survives SSR + hydration.
|
|
145
|
+
"aria-[current=page]:bg-accent aria-[current=page]:text-accent-foreground",
|
|
146
|
+
// Focus visible — 2px ring with offset per WCAG 2.2 SC 2.4.13
|
|
147
|
+
"focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
|
|
148
|
+
// Nested / indented sub-item
|
|
149
|
+
nested && "pl-8",
|
|
150
|
+
// Disabled
|
|
151
|
+
disabled && "pointer-events-none opacity-50",
|
|
152
|
+
className
|
|
153
|
+
)}
|
|
154
|
+
// Motion pattern (c): token-driven one-off transition.
|
|
155
|
+
// transition-colors targets background + color changes only (never
|
|
156
|
+
// transition-all — avoids animating focus rings and layout props).
|
|
157
|
+
// Duration + easing resolve at runtime against :root CSS vars generated
|
|
158
|
+
// from tokens.json via pnpm build:tokens. Reduced-motion handled by the
|
|
159
|
+
// base-layer @media rule — no per-component guard needed.
|
|
160
|
+
style={{
|
|
161
|
+
transitionDuration: "var(--duration-fast)",
|
|
162
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
163
|
+
...style,
|
|
164
|
+
}}
|
|
165
|
+
{...props}
|
|
166
|
+
>
|
|
167
|
+
{icon != null && (
|
|
168
|
+
// Icon container — enforces 16px size and 1.5 stroke width at the
|
|
169
|
+
// system level, decoupling consumers from the BADS icon stroke rule.
|
|
170
|
+
<span
|
|
171
|
+
data-slot="nav-link-icon"
|
|
172
|
+
aria-hidden="true"
|
|
173
|
+
className="shrink-0 [&>svg]:size-4 [&>svg]:stroke-[1.5]"
|
|
174
|
+
>
|
|
175
|
+
{icon}
|
|
176
|
+
</span>
|
|
177
|
+
)}
|
|
178
|
+
|
|
179
|
+
{/* Label — when asChild, Slottable marks the user-provided child as the
|
|
180
|
+
merge target for Slot (icon/badge siblings become its children).
|
|
181
|
+
When not asChild, a plain span carries the flex-1 + truncate styling. */}
|
|
182
|
+
{asChild ? (
|
|
183
|
+
<Slottable>{children}</Slottable>
|
|
184
|
+
) : (
|
|
185
|
+
<span className="flex-1 truncate">{children}</span>
|
|
186
|
+
)}
|
|
187
|
+
|
|
188
|
+
{badge != null && (
|
|
189
|
+
// Badge slot — pushed to trailing edge via ml-auto; the `flex-1` on
|
|
190
|
+
// the label span ensures ml-auto resolves relative to the link width
|
|
191
|
+
// not the label width.
|
|
192
|
+
<span data-slot="nav-link-badge" className="ml-auto shrink-0">
|
|
193
|
+
{badge}
|
|
194
|
+
</span>
|
|
195
|
+
)}
|
|
196
|
+
</Comp>
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export { NavLink };
|