@behavioral-agentic-design-system/core 0.39.1 → 0.42.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/README.md +2 -0
- package/package.json +26 -5
- package/scripts/a11y-lint.cjs +88 -8
- package/scripts/bads-components.mjs +118 -0
- package/scripts/build-tokens.cjs +133 -12
- package/scripts/check-stories.mjs +1 -1
- package/scripts/health-report.mjs +16 -2
- package/scripts/lint-component-source.mjs +100 -0
- package/scripts/lint-microanimation.mjs +267 -0
- package/scripts/lint-slop.mjs +25 -1
- package/scripts/size-audit.cjs +79 -0
- package/scripts/source-of-truth.cjs +58 -0
- package/scripts/ux-smoke.mjs +438 -0
- package/scripts/wcag-audit.cjs +121 -6
- package/src/api/layers/file.ts +1 -0
- package/src/app/opengraph-image.tsx +5 -0
- package/src/app/preview/components/page.tsx +2237 -2
- package/src/app/shell.tsx +17 -16
- package/src/components/Foundations/Motion.stories.tsx +1 -0
- package/src/components/customize/brand-mark-section.tsx +4 -2
- package/src/components/customize/design-controls.tsx +17 -2
- package/src/components/customize/discipline-section.tsx +3 -6
- package/src/components/customize/font-pickers.tsx +1 -1
- package/src/components/customize/letter-spacing.tsx +1 -1
- package/src/components/customize/save-bar.tsx +2 -2
- package/src/components/customize/theme-io.tsx +7 -7
- package/src/components/customize/type-scale.tsx +1 -1
- package/src/components/customize-tab.tsx +7 -4
- package/src/components/home-tab.tsx +3 -0
- package/src/components/studio/studio.tsx +153 -0
- package/src/components/ui/accordion.tsx +7 -0
- package/src/components/ui/alert.tsx +1 -0
- package/src/components/ui/anchor.tsx +37 -3
- package/src/components/ui/announcement-bar.tsx +256 -0
- package/src/components/ui/attachment.tsx +211 -0
- package/src/components/ui/badge.tsx +1 -0
- package/src/components/ui/callout.tsx +218 -0
- package/src/components/ui/chart.tsx +5 -14
- package/src/components/ui/code.tsx +1 -1
- package/src/components/ui/color-picker.tsx +2 -2
- package/src/components/ui/format-relative-time.tsx +139 -0
- package/src/components/ui/gauge-chart.tsx +238 -0
- package/src/components/ui/grid.tsx +255 -0
- package/src/components/ui/heatmap.tsx +256 -0
- package/src/components/ui/icon.tsx +108 -0
- package/src/components/ui/index.ts +17 -0
- package/src/components/ui/infinite-scroll.tsx +103 -0
- package/src/components/ui/json-input.tsx +351 -0
- package/src/components/ui/navigation-progress.tsx +281 -0
- package/src/components/ui/number-input.tsx +1 -4
- package/src/components/ui/page-header.tsx +63 -0
- package/src/components/ui/password-input.tsx +2 -1
- package/src/components/ui/password-strength-input.tsx +288 -0
- package/src/components/ui/pill.tsx +140 -0
- package/src/components/ui/popover.tsx +27 -4
- package/src/components/ui/portal.tsx +111 -0
- package/src/components/ui/presence.tsx +172 -0
- package/src/components/ui/progress-stack.tsx +258 -0
- package/src/components/ui/scroll-progress.tsx +100 -0
- package/src/components/ui/select.tsx +89 -4
- package/src/components/ui/separator.tsx +1 -0
- package/src/components/ui/slider.tsx +83 -3
- package/src/components/ui/snackbar.tsx +393 -0
- package/src/components/ui/sortable-list.tsx +342 -0
- package/src/components/ui/sparkline.tsx +2 -14
- package/src/components/ui/squircle.tsx +1 -1
- package/src/components/ui/table.tsx +1 -1
- package/src/components/ui/textarea.tsx +28 -1
- package/src/components/ui/tour.tsx +1 -3
- package/src/data/docs-index.ts +25 -0
- package/src/hooks/use-reduced-motion.ts +6 -0
- package/src/lib/clamp.ts +2 -0
- package/src/stories/Accordion.stories.tsx +59 -0
- package/src/stories/Alert.stories.tsx +3 -1
- package/src/stories/Anchor.stories.tsx +54 -0
- package/src/stories/AnnouncementBar.stories.tsx +261 -0
- package/src/stories/Attachment.stories.tsx +189 -0
- package/src/stories/Badge.stories.tsx +3 -0
- package/src/stories/Calendar.stories.tsx +1 -1
- package/src/stories/Callout.stories.tsx +219 -0
- package/src/stories/Chart.stories.tsx +8 -0
- package/src/stories/Code.stories.tsx +1 -0
- package/src/stories/ColorPicker.stories.tsx +24 -0
- package/src/stories/FormatRelativeTime.stories.tsx +116 -0
- package/src/stories/GaugeChart.stories.tsx +163 -0
- package/src/stories/Grid.stories.tsx +197 -0
- package/src/stories/Heatmap.stories.tsx +172 -0
- package/src/stories/Icon.stories.tsx +175 -0
- package/src/stories/InfiniteScroll.stories.tsx +225 -0
- package/src/stories/JsonInput.stories.tsx +212 -0
- package/src/stories/MultiSelect.stories.tsx +1 -1
- package/src/stories/NavigationProgress.stories.tsx +159 -0
- package/src/stories/NumberInput.stories.tsx +1 -0
- package/src/stories/PageHeader.stories.tsx +102 -0
- package/src/stories/PasswordInput.stories.tsx +4 -0
- package/src/stories/PasswordStrengthInput.stories.tsx +211 -0
- package/src/stories/Pill.stories.tsx +201 -0
- package/src/stories/Popover.stories.tsx +29 -1
- package/src/stories/Portal.stories.tsx +191 -0
- package/src/stories/Presence.stories.tsx +228 -0
- package/src/stories/ProgressStack.stories.tsx +287 -0
- package/src/stories/ScrollProgress.stories.tsx +117 -0
- package/src/stories/Select.stories.tsx +25 -0
- package/src/stories/Separator.stories.tsx +3 -0
- package/src/stories/Slider.stories.tsx +54 -5
- package/src/stories/Snackbar.stories.tsx +315 -0
- package/src/stories/SortableList.stories.tsx +263 -0
- package/src/stories/Sparkline.stories.tsx +3 -0
- package/src/stories/Squircle.stories.tsx +1 -0
- package/src/stories/Table.stories.tsx +1 -0
- package/src/stories/Textarea.stories.tsx +18 -0
- package/src/stories/Tour.stories.tsx +1 -0
- package/src/stories/story-groups.json +26 -6
- package/src/styles.css +133 -12
- package/LICENSE +0 -21
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { cva, type VariantProps } from "class-variance-authority"
|
|
5
|
+
|
|
6
|
+
import { cn } from "../../lib/utils"
|
|
7
|
+
|
|
8
|
+
// ─── Token cycling ────────────────────────────────────────────────────────────
|
|
9
|
+
//
|
|
10
|
+
// Default segment colors cycle through the --chart-1..5 family so a
|
|
11
|
+
// ProgressStack reads as part of the same system as BADS charts. Override
|
|
12
|
+
// per-segment via the `color` field.
|
|
13
|
+
|
|
14
|
+
const CHART_COLORS = [
|
|
15
|
+
"var(--chart-1)",
|
|
16
|
+
"var(--chart-2)",
|
|
17
|
+
"var(--chart-3)",
|
|
18
|
+
"var(--chart-4)",
|
|
19
|
+
"var(--chart-5)",
|
|
20
|
+
] as const
|
|
21
|
+
|
|
22
|
+
// ─── Track variants ───────────────────────────────────────────────────────────
|
|
23
|
+
|
|
24
|
+
const progressStackVariants = cva(
|
|
25
|
+
"relative flex w-full overflow-hidden rounded-full bg-secondary",
|
|
26
|
+
{
|
|
27
|
+
variants: {
|
|
28
|
+
/**
|
|
29
|
+
* Track height (peer parity: Mantine Progress size, Bootstrap
|
|
30
|
+
* progress-sm/-lg). Thickness changes only the track — legend typography
|
|
31
|
+
* is size-independent.
|
|
32
|
+
*
|
|
33
|
+
* sm = 4px (dense list rows, table cells)
|
|
34
|
+
* default = 8px (standard dashboard surface)
|
|
35
|
+
* lg = 16px (hero / storage panels)
|
|
36
|
+
*/
|
|
37
|
+
size: {
|
|
38
|
+
sm: "h-1",
|
|
39
|
+
default: "h-2.5",
|
|
40
|
+
lg: "h-4",
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
defaultVariants: {
|
|
44
|
+
size: "default",
|
|
45
|
+
},
|
|
46
|
+
}
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
// ─── Types ────────────────────────────────────────────────────────────────────
|
|
50
|
+
|
|
51
|
+
export interface ProgressStackSegment {
|
|
52
|
+
/**
|
|
53
|
+
* Raw numeric value. Proportioned against the sum of all segment values, or
|
|
54
|
+
* against `max` on the root when provided.
|
|
55
|
+
*/
|
|
56
|
+
value: number
|
|
57
|
+
/** Human-readable label for the legend and screen readers. */
|
|
58
|
+
label: string
|
|
59
|
+
/**
|
|
60
|
+
* Explicit CSS color for this segment. Accepts any CSS color (hex, hsl(),
|
|
61
|
+
* or a variable reference such as `"var(--chart-3)"`). Defaults to cycling
|
|
62
|
+
* through `--chart-1` to `--chart-5`.
|
|
63
|
+
*/
|
|
64
|
+
color?: string
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface ProgressStackProps
|
|
68
|
+
extends Omit<React.ComponentProps<"div">, "children">,
|
|
69
|
+
VariantProps<typeof progressStackVariants> {
|
|
70
|
+
/**
|
|
71
|
+
* The segments that make up the stacked bar. Required — at least one
|
|
72
|
+
* segment is needed for a meaningful render.
|
|
73
|
+
*/
|
|
74
|
+
segments: ProgressStackSegment[]
|
|
75
|
+
/**
|
|
76
|
+
* Total value the segments are measured against. Defaults to the sum of all
|
|
77
|
+
* segment values so the bar is always full. Pass an explicit `max` when the
|
|
78
|
+
* segments may not fill the bar — for example, 70 GB used across categories
|
|
79
|
+
* out of a 128 GB disk renders 70/128 of the track filled with the rest
|
|
80
|
+
* shown as the empty track.
|
|
81
|
+
*/
|
|
82
|
+
max?: number
|
|
83
|
+
/**
|
|
84
|
+
* Renders a color-coded legend row below the bar: a colored swatch, the
|
|
85
|
+
* segment label, and optionally the formatted value when `showValues` is
|
|
86
|
+
* also true. Default `false`.
|
|
87
|
+
*/
|
|
88
|
+
showLegend?: boolean
|
|
89
|
+
/**
|
|
90
|
+
* When `showLegend` is true, also renders the formatted percentage (or
|
|
91
|
+
* custom `formatValue` output) beside each legend label. Default `false`.
|
|
92
|
+
*/
|
|
93
|
+
showValues?: boolean
|
|
94
|
+
/**
|
|
95
|
+
* Custom value-formatting function for the legend. Receives the segment's
|
|
96
|
+
* raw value and the computed total. Defaults to `"N%"` (nearest integer).
|
|
97
|
+
*/
|
|
98
|
+
formatValue?: (value: number, total: number) => string
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// ─── Component ────────────────────────────────────────────────────────────────
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* ProgressStack — a multi-segment stacked horizontal bar showing a
|
|
105
|
+
* proportional breakdown across categories.
|
|
106
|
+
*
|
|
107
|
+
* Distinct from:
|
|
108
|
+
* - `Progress` — single determinate/indeterminate fill with Radix semantics
|
|
109
|
+
* - `Meter` — a gauge for where one value falls within a known range
|
|
110
|
+
* - `CircularProgress` — a ring variant of the single-fill pattern
|
|
111
|
+
*
|
|
112
|
+
* Use ProgressStack for **categorical breakdowns** where the visual
|
|
113
|
+
* decomposition is the point: storage used by type (Documents / Media /
|
|
114
|
+
* System), budget allocation by team, skill proficiency across domains.
|
|
115
|
+
*
|
|
116
|
+
* Sources: Mantine (Progress with `sections` array), Bootstrap stacked
|
|
117
|
+
* progress, shadcn dashboard card examples.
|
|
118
|
+
*
|
|
119
|
+
* Accessibility: the track is `role="img"` with an `aria-label`. A
|
|
120
|
+
* visually-hidden description list below the track announces each segment's
|
|
121
|
+
* label and percentage to screen readers. Never rely on color alone — the
|
|
122
|
+
* legend and the sr-only list both carry the labels.
|
|
123
|
+
*
|
|
124
|
+
* Motion: each segment animates its width from 0 to its final percentage on
|
|
125
|
+
* mount via a CSS transition keyed to `--duration-slow` + `--ease-out`.
|
|
126
|
+
* Segments stagger by 40 ms per index. Reduced-motion users get the final
|
|
127
|
+
* state immediately — the base-layer `prefers-reduced-motion` rule collapses
|
|
128
|
+
* the transition to 0.01 ms.
|
|
129
|
+
*/
|
|
130
|
+
function ProgressStack({
|
|
131
|
+
className,
|
|
132
|
+
size,
|
|
133
|
+
segments,
|
|
134
|
+
max,
|
|
135
|
+
showLegend = false,
|
|
136
|
+
showValues = false,
|
|
137
|
+
formatValue,
|
|
138
|
+
"aria-label": ariaLabel,
|
|
139
|
+
...props
|
|
140
|
+
}: ProgressStackProps & {
|
|
141
|
+
/**
|
|
142
|
+
* Accessible name for the whole bar. Required — without it screen readers
|
|
143
|
+
* have no way to announce what this chart represents. Pass a short
|
|
144
|
+
* description, e.g. `"Storage breakdown"` or `"Q3 budget allocation"`.
|
|
145
|
+
*/
|
|
146
|
+
"aria-label": string
|
|
147
|
+
}) {
|
|
148
|
+
const total = max ?? segments.reduce((acc, s) => acc + s.value, 0)
|
|
149
|
+
const safeTotal = total === 0 ? 1 : total
|
|
150
|
+
const fmt = formatValue ?? ((v: number, t: number) => `${Math.round((v / t) * 100)}%`)
|
|
151
|
+
|
|
152
|
+
// Mount animation: start at 0-width, transition to final width on the
|
|
153
|
+
// first animation frame so the browser paints the 0-width state first and
|
|
154
|
+
// the entrance always plays.
|
|
155
|
+
const [mounted, setMounted] = React.useState(false)
|
|
156
|
+
React.useEffect(() => {
|
|
157
|
+
const id = requestAnimationFrame(() => setMounted(true))
|
|
158
|
+
return () => cancelAnimationFrame(id)
|
|
159
|
+
}, [])
|
|
160
|
+
|
|
161
|
+
return (
|
|
162
|
+
<div
|
|
163
|
+
data-slot="progress-stack-root"
|
|
164
|
+
className={cn("flex flex-col gap-3", className)}
|
|
165
|
+
{...props}
|
|
166
|
+
>
|
|
167
|
+
{/* Visual track — role="img" with the aria-label as the accessible name */}
|
|
168
|
+
<div
|
|
169
|
+
data-slot="progress-stack"
|
|
170
|
+
role="img"
|
|
171
|
+
aria-label={ariaLabel}
|
|
172
|
+
className={cn(progressStackVariants({ size }), "gap-0.5")}
|
|
173
|
+
>
|
|
174
|
+
{segments.map((seg, idx) => {
|
|
175
|
+
const pct = (seg.value / safeTotal) * 100
|
|
176
|
+
const color = seg.color ?? CHART_COLORS[idx % CHART_COLORS.length]
|
|
177
|
+
return (
|
|
178
|
+
<div
|
|
179
|
+
key={idx}
|
|
180
|
+
data-slot="progress-stack-segment"
|
|
181
|
+
data-index={idx}
|
|
182
|
+
aria-hidden
|
|
183
|
+
// First / last get the outer radius so the track feels like one
|
|
184
|
+
// continuous bar; middle segments are square (gap handles the
|
|
185
|
+
// visual break). rounded-sm on inner segments matches the track
|
|
186
|
+
// height at each size without looking lumpy.
|
|
187
|
+
className="h-full flex-none rounded-sm first:rounded-l-full last:rounded-r-full"
|
|
188
|
+
style={{
|
|
189
|
+
width: mounted ? `${pct}%` : "0%",
|
|
190
|
+
backgroundColor: color,
|
|
191
|
+
transitionProperty: "width",
|
|
192
|
+
transitionDuration: "var(--duration-slow)",
|
|
193
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
194
|
+
// Stagger each segment 40 ms later than the previous so they
|
|
195
|
+
// grow in sequence rather than all at once.
|
|
196
|
+
transitionDelay: mounted ? `${idx * 40}ms` : "0ms",
|
|
197
|
+
// Minimum rendered width so a very-small segment is always
|
|
198
|
+
// visible rather than rendering as an invisible sliver. 4px is
|
|
199
|
+
// enough for the color swatch to read.
|
|
200
|
+
minWidth: mounted && pct > 0 ? "4px" : "0px",
|
|
201
|
+
}}
|
|
202
|
+
/>
|
|
203
|
+
)
|
|
204
|
+
})}
|
|
205
|
+
</div>
|
|
206
|
+
|
|
207
|
+
{/* Screen-reader segment list — visually hidden, but gives AT users the
|
|
208
|
+
same per-segment breakdown the sighted legend provides. Placed after
|
|
209
|
+
the track so it follows naturally when the bar is focused / navigated. */}
|
|
210
|
+
<ul
|
|
211
|
+
aria-label={`${ariaLabel} breakdown`}
|
|
212
|
+
className="sr-only"
|
|
213
|
+
role="list"
|
|
214
|
+
>
|
|
215
|
+
{segments.map((seg, idx) => (
|
|
216
|
+
<li key={idx}>
|
|
217
|
+
{seg.label}: {fmt(seg.value, safeTotal)}
|
|
218
|
+
</li>
|
|
219
|
+
))}
|
|
220
|
+
</ul>
|
|
221
|
+
|
|
222
|
+
{/* Legend (opt-in) — aria-hidden because the sr-only list above already
|
|
223
|
+
carries the same information for screen readers. */}
|
|
224
|
+
{showLegend && (
|
|
225
|
+
<div
|
|
226
|
+
data-slot="progress-stack-legend"
|
|
227
|
+
aria-hidden
|
|
228
|
+
className="flex flex-wrap gap-x-4 gap-y-1.5"
|
|
229
|
+
>
|
|
230
|
+
{segments.map((seg, idx) => {
|
|
231
|
+
const color = seg.color ?? CHART_COLORS[idx % CHART_COLORS.length]
|
|
232
|
+
return (
|
|
233
|
+
<div
|
|
234
|
+
key={idx}
|
|
235
|
+
data-slot="progress-stack-legend-item"
|
|
236
|
+
className="flex items-center gap-1.5 text-sm"
|
|
237
|
+
>
|
|
238
|
+
<span
|
|
239
|
+
aria-hidden
|
|
240
|
+
className="size-2.5 shrink-0 rounded-sm"
|
|
241
|
+
style={{ backgroundColor: color }}
|
|
242
|
+
/>
|
|
243
|
+
<span className="text-muted-foreground">{seg.label}</span>
|
|
244
|
+
{showValues && (
|
|
245
|
+
<span className="font-medium tabular-nums text-foreground">
|
|
246
|
+
{fmt(seg.value, safeTotal)}
|
|
247
|
+
</span>
|
|
248
|
+
)}
|
|
249
|
+
</div>
|
|
250
|
+
)
|
|
251
|
+
})}
|
|
252
|
+
</div>
|
|
253
|
+
)}
|
|
254
|
+
</div>
|
|
255
|
+
)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export { ProgressStack, progressStackVariants }
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { cn } from "../../lib/utils"
|
|
5
|
+
|
|
6
|
+
export interface ScrollProgressProps {
|
|
7
|
+
/**
|
|
8
|
+
* Element to track scroll progress for.
|
|
9
|
+
* When omitted, tracks the window (document scroll).
|
|
10
|
+
*/
|
|
11
|
+
containerRef?: React.RefObject<HTMLElement | null>
|
|
12
|
+
/**
|
|
13
|
+
* Where to render the bar. "page" = fixed at top of viewport (default for
|
|
14
|
+
* window tracking). "sticky" = sticky inside a scrolling container.
|
|
15
|
+
* "inline" = renders in flow — position it yourself.
|
|
16
|
+
*/
|
|
17
|
+
position?: "fixed" | "sticky" | "inline"
|
|
18
|
+
/** Override the bar height. Default "2px". */
|
|
19
|
+
height?: string
|
|
20
|
+
/** Override the filled color. Default `var(--primary)`. */
|
|
21
|
+
color?: string
|
|
22
|
+
/** Override the track (background) color. Default `var(--border)`. */
|
|
23
|
+
trackColor?: string
|
|
24
|
+
className?: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Scroll-position progress indicator. Shows how far the user has scrolled
|
|
29
|
+
* through the tracked element or the whole page as a thin colored bar.
|
|
30
|
+
*
|
|
31
|
+
* Sources: GitHub PR scroll indicator, Mantine ScrollProgress RFC, shadcn
|
|
32
|
+
* community recipe (2026-Q3), Next.js nprogress-style indicators.
|
|
33
|
+
*
|
|
34
|
+
* Reduced-motion: the bar updates immediately without any `transition`
|
|
35
|
+
* (the base layer's 0.01ms override would silently fire here anyway).
|
|
36
|
+
*/
|
|
37
|
+
function ScrollProgress({
|
|
38
|
+
containerRef,
|
|
39
|
+
position = "fixed",
|
|
40
|
+
height = "2px",
|
|
41
|
+
color,
|
|
42
|
+
trackColor,
|
|
43
|
+
className,
|
|
44
|
+
}: ScrollProgressProps) {
|
|
45
|
+
const [progress, setProgress] = React.useState(0)
|
|
46
|
+
|
|
47
|
+
React.useEffect(() => {
|
|
48
|
+
function calculate() {
|
|
49
|
+
if (containerRef?.current) {
|
|
50
|
+
const el = containerRef.current
|
|
51
|
+
const scrollTop = el.scrollTop
|
|
52
|
+
const scrollHeight = el.scrollHeight - el.clientHeight
|
|
53
|
+
setProgress(scrollHeight > 0 ? (scrollTop / scrollHeight) * 100 : 0)
|
|
54
|
+
} else {
|
|
55
|
+
const scrollTop = window.scrollY || document.documentElement.scrollTop
|
|
56
|
+
const docHeight =
|
|
57
|
+
document.documentElement.scrollHeight -
|
|
58
|
+
document.documentElement.clientHeight
|
|
59
|
+
setProgress(docHeight > 0 ? (scrollTop / docHeight) * 100 : 0)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const target = containerRef?.current ?? window
|
|
64
|
+
target.addEventListener("scroll", calculate, { passive: true })
|
|
65
|
+
calculate()
|
|
66
|
+
return () => target.removeEventListener("scroll", calculate)
|
|
67
|
+
}, [containerRef])
|
|
68
|
+
|
|
69
|
+
return (
|
|
70
|
+
<div
|
|
71
|
+
data-slot="scroll-progress"
|
|
72
|
+
role="progressbar"
|
|
73
|
+
aria-label="Page scroll progress"
|
|
74
|
+
aria-valuenow={Math.round(progress)}
|
|
75
|
+
aria-valuemin={0}
|
|
76
|
+
aria-valuemax={100}
|
|
77
|
+
className={cn(
|
|
78
|
+
"left-0 top-0 w-full overflow-hidden",
|
|
79
|
+
position === "fixed" && "fixed z-50",
|
|
80
|
+
position === "sticky" && "sticky z-10",
|
|
81
|
+
position === "inline" && "relative",
|
|
82
|
+
className
|
|
83
|
+
)}
|
|
84
|
+
style={{
|
|
85
|
+
height,
|
|
86
|
+
backgroundColor: trackColor ?? "var(--border)",
|
|
87
|
+
}}
|
|
88
|
+
>
|
|
89
|
+
<div
|
|
90
|
+
className="h-full"
|
|
91
|
+
style={{
|
|
92
|
+
width: `${progress}%`,
|
|
93
|
+
backgroundColor: color ?? "var(--primary)",
|
|
94
|
+
}}
|
|
95
|
+
/>
|
|
96
|
+
</div>
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export { ScrollProgress }
|
|
@@ -1,16 +1,69 @@
|
|
|
1
1
|
"use client"
|
|
2
2
|
|
|
3
3
|
import * as React from "react"
|
|
4
|
-
import { CheckIcon, ChevronDownIcon, ChevronUpIcon } from "lucide-react"
|
|
4
|
+
import { CheckIcon, ChevronDownIcon, ChevronUpIcon, XIcon } from "lucide-react"
|
|
5
5
|
import * as SelectPrimitive from "@radix-ui/react-select"
|
|
6
6
|
|
|
7
7
|
import { cn } from "../../lib/utils"
|
|
8
8
|
import { usePortalThemeClass } from "../../hooks/use-portal-theme-class"
|
|
9
9
|
|
|
10
|
+
// Context threads clearable state from Select root to SelectTrigger
|
|
11
|
+
// without prop-drilling through compound-component consumers.
|
|
12
|
+
const SelectClearContext = React.createContext<{
|
|
13
|
+
clearable: boolean
|
|
14
|
+
hasValue: boolean
|
|
15
|
+
onClear: () => void
|
|
16
|
+
}>({ clearable: false, hasValue: false, onClear: () => {} })
|
|
17
|
+
|
|
10
18
|
function Select({
|
|
19
|
+
clearable,
|
|
20
|
+
value,
|
|
21
|
+
defaultValue,
|
|
22
|
+
onValueChange,
|
|
11
23
|
...props
|
|
12
|
-
}: React.ComponentProps<typeof SelectPrimitive.Root>
|
|
13
|
-
|
|
24
|
+
}: React.ComponentProps<typeof SelectPrimitive.Root> & {
|
|
25
|
+
/**
|
|
26
|
+
* Shows an × button in the trigger to clear the current selection.
|
|
27
|
+
* Works in both controlled (pass `value` + `onValueChange`) and
|
|
28
|
+
* uncontrolled modes. In controlled mode, `onValueChange` is called
|
|
29
|
+
* with `""` on clear — reset your state variable to `""` or `undefined`.
|
|
30
|
+
*
|
|
31
|
+
* Peer parity: Mantine clearable, Chakra UI clearable, Ant Design allowClear.
|
|
32
|
+
*/
|
|
33
|
+
clearable?: boolean
|
|
34
|
+
}) {
|
|
35
|
+
// Track internal value only when in uncontrolled + clearable mode.
|
|
36
|
+
const [internalValue, setInternalValue] = React.useState<string | undefined>(
|
|
37
|
+
defaultValue
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
const resolvedHasValue = clearable
|
|
41
|
+
? Boolean(value !== undefined ? value : internalValue)
|
|
42
|
+
: false
|
|
43
|
+
|
|
44
|
+
function handleValueChange(v: string) {
|
|
45
|
+
if (clearable && value === undefined) setInternalValue(v)
|
|
46
|
+
onValueChange?.(v)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function handleClear() {
|
|
50
|
+
if (value === undefined) setInternalValue(undefined)
|
|
51
|
+
onValueChange?.("")
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<SelectClearContext.Provider
|
|
56
|
+
value={{ clearable: !!clearable, hasValue: resolvedHasValue, onClear: handleClear }}
|
|
57
|
+
>
|
|
58
|
+
<SelectPrimitive.Root
|
|
59
|
+
data-slot="select"
|
|
60
|
+
value={value}
|
|
61
|
+
defaultValue={defaultValue}
|
|
62
|
+
onValueChange={handleValueChange}
|
|
63
|
+
{...props}
|
|
64
|
+
/>
|
|
65
|
+
</SelectClearContext.Provider>
|
|
66
|
+
)
|
|
14
67
|
}
|
|
15
68
|
|
|
16
69
|
function SelectGroup({
|
|
@@ -33,12 +86,16 @@ function SelectTrigger({
|
|
|
33
86
|
}: React.ComponentProps<typeof SelectPrimitive.Trigger> & {
|
|
34
87
|
size?: "sm" | "default" | "lg" | "xl"
|
|
35
88
|
}) {
|
|
36
|
-
|
|
89
|
+
const { clearable, hasValue, onClear } = React.useContext(SelectClearContext)
|
|
90
|
+
|
|
91
|
+
const trigger = (
|
|
37
92
|
<SelectPrimitive.Trigger
|
|
38
93
|
data-slot="select-trigger"
|
|
39
94
|
data-size={size}
|
|
40
95
|
className={cn(
|
|
41
96
|
"flex w-fit items-center justify-between gap-2 rounded-md border border-input bg-transparent px-3 py-2 text-sm whitespace-nowrap shadow-xs transition-[color,box-shadow] outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-destructive/20 data-[placeholder]:text-muted-foreground data-[size=sm]:h-8 data-[size=default]:h-9 data-[size=lg]:h-10 data-[size=xl]:h-11 data-[size=xl]:text-base *:data-[slot=select-value]:line-clamp-1 *:data-[slot=select-value]:flex *:data-[slot=select-value]:items-center *:data-[slot=select-value]:gap-2 dark:bg-input/30 dark:hover:bg-input/50 dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 [&_svg:not([class*='text-'])]:text-muted-foreground",
|
|
97
|
+
// reserve horizontal space for the clear button when it will render
|
|
98
|
+
clearable && hasValue && "pr-7",
|
|
42
99
|
className
|
|
43
100
|
)}
|
|
44
101
|
{...props}
|
|
@@ -49,6 +106,34 @@ function SelectTrigger({
|
|
|
49
106
|
</SelectPrimitive.Icon>
|
|
50
107
|
</SelectPrimitive.Trigger>
|
|
51
108
|
)
|
|
109
|
+
|
|
110
|
+
if (!clearable) return trigger
|
|
111
|
+
|
|
112
|
+
return (
|
|
113
|
+
// Relative wrapper enables the clear button to be positioned absolutely
|
|
114
|
+
// OUTSIDE the trigger button — nesting buttons is invalid HTML and some
|
|
115
|
+
// browsers silently discard nested button clicks.
|
|
116
|
+
<div className="relative inline-flex">
|
|
117
|
+
{trigger}
|
|
118
|
+
{hasValue && (
|
|
119
|
+
<button
|
|
120
|
+
type="button"
|
|
121
|
+
aria-label="Clear selection"
|
|
122
|
+
onClick={(e) => {
|
|
123
|
+
e.stopPropagation()
|
|
124
|
+
onClear()
|
|
125
|
+
}}
|
|
126
|
+
className="absolute right-7 top-1/2 z-10 flex size-4 -translate-y-1/2 items-center justify-center rounded text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1"
|
|
127
|
+
style={{
|
|
128
|
+
transitionDuration: "var(--duration-fast)",
|
|
129
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
130
|
+
}}
|
|
131
|
+
>
|
|
132
|
+
<XIcon strokeWidth={1.5} className="size-3" />
|
|
133
|
+
</button>
|
|
134
|
+
)}
|
|
135
|
+
</div>
|
|
136
|
+
)
|
|
52
137
|
}
|
|
53
138
|
|
|
54
139
|
function SelectContent({
|
|
@@ -5,6 +5,7 @@ import * as SeparatorPrimitive from "@radix-ui/react-separator"
|
|
|
5
5
|
|
|
6
6
|
import { cn } from "../../lib/utils"
|
|
7
7
|
|
|
8
|
+
// @recipe-note: Separator always renders a `<div>` (both the label and no-label paths). Nesting it inside a `<p>` is invalid HTML and throws React hydration error #418 at runtime even though `next build` reports success — put it between block elements, not inside one.
|
|
8
9
|
type SeparatorProps = React.ComponentProps<typeof SeparatorPrimitive.Root> & {
|
|
9
10
|
/**
|
|
10
11
|
* Optional label rendered inline, splitting the rule into two segments (an
|
|
@@ -11,6 +11,12 @@ import { cn } from "../../lib/utils"
|
|
|
11
11
|
*/
|
|
12
12
|
type SliderMark = number | { value: number; label?: React.ReactNode }
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* When the per-thumb value bubble shows. `auto` = on hover / focus / drag
|
|
16
|
+
* (drag focuses the thumb in Radix), `always` = persistent, `never` = off.
|
|
17
|
+
*/
|
|
18
|
+
type SliderValueLabelMode = "auto" | "always" | "never"
|
|
19
|
+
|
|
14
20
|
function Slider({
|
|
15
21
|
className,
|
|
16
22
|
defaultValue,
|
|
@@ -19,6 +25,9 @@ function Slider({
|
|
|
19
25
|
max = 100,
|
|
20
26
|
orientation = "horizontal",
|
|
21
27
|
marks,
|
|
28
|
+
valueLabel = "never",
|
|
29
|
+
valueLabelFormat,
|
|
30
|
+
onValueChange,
|
|
22
31
|
"aria-label": ariaLabel,
|
|
23
32
|
"aria-labelledby": ariaLabelledby,
|
|
24
33
|
...props
|
|
@@ -29,6 +38,17 @@ function Slider({
|
|
|
29
38
|
* the slider's default rendering when omitted.
|
|
30
39
|
*/
|
|
31
40
|
marks?: SliderMark[]
|
|
41
|
+
/**
|
|
42
|
+
* Thumb-anchored current-value bubble (Mantine `label`, MUI
|
|
43
|
+
* `valueLabelDisplay`, Ant `tooltip`). Default `never` — the slider renders
|
|
44
|
+
* byte-identically to before when omitted. The bubble is `aria-hidden`:
|
|
45
|
+
* the thumb already announces `aria-valuenow`, and a string returned by
|
|
46
|
+
* `valueLabelFormat` is mirrored to `aria-valuetext` so screen readers hear
|
|
47
|
+
* the same formatted value.
|
|
48
|
+
*/
|
|
49
|
+
valueLabel?: SliderValueLabelMode
|
|
50
|
+
/** Formats the bubble text, e.g. `(v) => \`$${v}\``. Defaults to the raw number. */
|
|
51
|
+
valueLabelFormat?: (value: number) => React.ReactNode
|
|
32
52
|
}) {
|
|
33
53
|
const _values = React.useMemo(
|
|
34
54
|
() =>
|
|
@@ -40,6 +60,22 @@ function Slider({
|
|
|
40
60
|
[value, defaultValue, min, max]
|
|
41
61
|
)
|
|
42
62
|
|
|
63
|
+
// Mirror the live values so the value bubble can render in uncontrolled
|
|
64
|
+
// mode (Radix owns the state; we only tap onValueChange). Controlled mode
|
|
65
|
+
// reads `value` directly.
|
|
66
|
+
const [tracked, setTracked] = React.useState<number[]>(
|
|
67
|
+
() => value ?? defaultValue ?? [min]
|
|
68
|
+
)
|
|
69
|
+
const currentValues = value ?? tracked
|
|
70
|
+
const handleValueChange = React.useCallback(
|
|
71
|
+
(next: number[]) => {
|
|
72
|
+
setTracked(next)
|
|
73
|
+
onValueChange?.(next)
|
|
74
|
+
},
|
|
75
|
+
[onValueChange]
|
|
76
|
+
)
|
|
77
|
+
const showValueLabel = valueLabel !== "never"
|
|
78
|
+
|
|
43
79
|
const _marks = React.useMemo(
|
|
44
80
|
() =>
|
|
45
81
|
(marks ?? []).map((m) =>
|
|
@@ -61,10 +97,15 @@ function Slider({
|
|
|
61
97
|
min={min}
|
|
62
98
|
max={max}
|
|
63
99
|
orientation={orientation}
|
|
100
|
+
onValueChange={handleValueChange}
|
|
64
101
|
className={cn(
|
|
65
102
|
"relative flex w-full touch-none items-center select-none data-[disabled]:opacity-50 data-[orientation=vertical]:h-full data-[orientation=vertical]:min-h-44 data-[orientation=vertical]:w-auto data-[orientation=vertical]:flex-col",
|
|
66
103
|
// Reserve room for mark labels so they never overlap following content.
|
|
67
104
|
hasLabels && (isVertical ? "mr-12" : "mb-7"),
|
|
105
|
+
// A persistent value bubble sits above (or beside) the thumb — reserve
|
|
106
|
+
// room for it the same way. `auto` bubbles are transient, like a
|
|
107
|
+
// tooltip, so they may overlap and reserve nothing.
|
|
108
|
+
valueLabel === "always" && (isVertical ? "mr-16" : "mt-9"),
|
|
68
109
|
className
|
|
69
110
|
)}
|
|
70
111
|
{...props}
|
|
@@ -150,14 +191,53 @@ function Slider({
|
|
|
150
191
|
ariaLabel && _values.length > 1
|
|
151
192
|
? `${ariaLabel} ${index === 0 ? "minimum" : "maximum"}`
|
|
152
193
|
: ariaLabel
|
|
194
|
+
const rawValue = currentValues[index] ?? min
|
|
195
|
+
const formatted = valueLabelFormat ? valueLabelFormat(rawValue) : rawValue
|
|
196
|
+
// Only a string/number formatter result can be announced; a ReactNode
|
|
197
|
+
// label falls back to Radix's own aria-valuenow.
|
|
198
|
+
const valueText =
|
|
199
|
+
valueLabelFormat &&
|
|
200
|
+
(typeof formatted === "string" || typeof formatted === "number")
|
|
201
|
+
? String(formatted)
|
|
202
|
+
: undefined
|
|
153
203
|
return (
|
|
154
204
|
<SliderPrimitive.Thumb
|
|
155
205
|
data-slot="slider-thumb"
|
|
156
206
|
key={index}
|
|
157
207
|
aria-label={thumbLabel}
|
|
158
208
|
aria-labelledby={ariaLabelledby}
|
|
159
|
-
|
|
160
|
-
|
|
209
|
+
aria-valuetext={valueText}
|
|
210
|
+
className="group/thumb relative block size-8 shrink-0 rounded-full border-2 border-primary bg-white shadow-sm ring-ring/50 transition-[color,box-shadow] hover:ring-4 focus-visible:ring-4 focus-visible:outline-hidden disabled:pointer-events-none disabled:opacity-50"
|
|
211
|
+
>
|
|
212
|
+
{showValueLabel && (
|
|
213
|
+
// Inverted surface, same audited pair Tooltip uses
|
|
214
|
+
// (bg-foreground / text-background). aria-hidden: the thumb
|
|
215
|
+
// announces the value. Pattern-c motion for the fade + lift.
|
|
216
|
+
<span
|
|
217
|
+
data-slot="slider-value-label"
|
|
218
|
+
aria-hidden
|
|
219
|
+
className={cn(
|
|
220
|
+
"pointer-events-none absolute rounded-md bg-foreground px-2 py-0.5 text-xs font-medium whitespace-nowrap tabular-nums text-background shadow-sm transition-[opacity,transform]",
|
|
221
|
+
isVertical
|
|
222
|
+
? "top-1/2 left-full ml-3 -translate-y-1/2"
|
|
223
|
+
: "bottom-full left-1/2 mb-2 -translate-x-1/2",
|
|
224
|
+
valueLabel === "auto" &&
|
|
225
|
+
cn(
|
|
226
|
+
"opacity-0 group-hover/thumb:opacity-100 group-focus/thumb:opacity-100",
|
|
227
|
+
isVertical
|
|
228
|
+
? "translate-x-1 group-hover/thumb:translate-x-0 group-focus/thumb:translate-x-0"
|
|
229
|
+
: "translate-y-1 group-hover/thumb:translate-y-0 group-focus/thumb:translate-y-0"
|
|
230
|
+
)
|
|
231
|
+
)}
|
|
232
|
+
style={{
|
|
233
|
+
transitionDuration: "var(--duration-fast)",
|
|
234
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
235
|
+
}}
|
|
236
|
+
>
|
|
237
|
+
{formatted}
|
|
238
|
+
</span>
|
|
239
|
+
)}
|
|
240
|
+
</SliderPrimitive.Thumb>
|
|
161
241
|
)
|
|
162
242
|
})}
|
|
163
243
|
</SliderPrimitive.Root>
|
|
@@ -165,4 +245,4 @@ function Slider({
|
|
|
165
245
|
}
|
|
166
246
|
|
|
167
247
|
export { Slider }
|
|
168
|
-
export type { SliderMark }
|
|
248
|
+
export type { SliderMark, SliderValueLabelMode }
|