@behavioral-agentic-design-system/core 0.40.0 → 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 +19 -3
- package/scripts/a11y-lint.cjs +41 -2
- package/scripts/health-report.mjs +16 -2
- package/scripts/lint-slop.mjs +25 -1
- package/scripts/wcag-audit.cjs +72 -5
- package/src/app/opengraph-image.tsx +5 -0
- package/src/app/preview/components/page.tsx +1269 -2
- package/src/app/shell.tsx +8 -5
- package/src/components/Foundations/Motion.stories.tsx +1 -0
- package/src/components/customize/design-controls.tsx +16 -1
- package/src/components/studio/studio.tsx +153 -0
- package/src/components/ui/anchor.tsx +37 -3
- package/src/components/ui/attachment.tsx +211 -0
- package/src/components/ui/chart.tsx +3 -0
- package/src/components/ui/code.tsx +1 -1
- package/src/components/ui/color-picker.tsx +1 -1
- package/src/components/ui/gauge-chart.tsx +238 -0
- package/src/components/ui/heatmap.tsx +256 -0
- package/src/components/ui/index.ts +9 -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/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/scroll-progress.tsx +100 -0
- package/src/components/ui/snackbar.tsx +393 -0
- package/src/components/ui/sortable-list.tsx +342 -0
- package/src/components/ui/squircle.tsx +1 -1
- package/src/components/ui/table.tsx +1 -1
- package/src/components/ui/tour.tsx +1 -3
- package/src/data/docs-index.ts +24 -0
- package/src/lib/clamp.ts +2 -0
- package/src/stories/Anchor.stories.tsx +54 -0
- package/src/stories/Attachment.stories.tsx +189 -0
- package/src/stories/Chart.stories.tsx +4 -0
- package/src/stories/Code.stories.tsx +1 -0
- package/src/stories/ColorPicker.stories.tsx +1 -0
- package/src/stories/GaugeChart.stories.tsx +163 -0
- package/src/stories/Heatmap.stories.tsx +172 -0
- package/src/stories/InfiniteScroll.stories.tsx +225 -0
- package/src/stories/JsonInput.stories.tsx +212 -0
- package/src/stories/NavigationProgress.stories.tsx +159 -0
- package/src/stories/NumberInput.stories.tsx +1 -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 +3 -1
- package/src/stories/ScrollProgress.stories.tsx +117 -0
- package/src/stories/Snackbar.stories.tsx +315 -0
- package/src/stories/SortableList.stories.tsx +263 -0
- package/src/stories/Squircle.stories.tsx +1 -0
- package/src/stories/Table.stories.tsx +1 -0
- package/src/stories/Tour.stories.tsx +1 -0
- package/src/stories/story-groups.json +17 -6
- package/LICENSE +0 -21
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { ResponsiveContainer, PieChart, Pie } from "recharts"
|
|
5
|
+
|
|
6
|
+
import { cn } from "../../lib/utils"
|
|
7
|
+
import { useReducedMotion } from "../../hooks/use-reduced-motion"
|
|
8
|
+
|
|
9
|
+
// Arc geometry. Recharts measures angles counter-clockwise from 3 o'clock, so
|
|
10
|
+
// starting at 225° (lower-left) and sweeping down to -45° (lower-right) draws
|
|
11
|
+
// a 270° dial with a 90° opening at the bottom — the classic speedometer
|
|
12
|
+
// shape. Both the background track and the value fill share the same start
|
|
13
|
+
// angle so they stay concentric and aligned.
|
|
14
|
+
const START_ANGLE = 225
|
|
15
|
+
const SWEEP = 270
|
|
16
|
+
const END_ANGLE = START_ANGLE - SWEEP
|
|
17
|
+
|
|
18
|
+
// A 270° arc leaves the bottom quarter of the bounding circle empty. Nudging
|
|
19
|
+
// the center down and sizing the radius to fit keeps the visible dial
|
|
20
|
+
// optically centered in its box instead of floating high.
|
|
21
|
+
const CENTER_Y = "55%"
|
|
22
|
+
const OUTER_RADIUS = "90%"
|
|
23
|
+
const INNER_RADIUS = "72%"
|
|
24
|
+
|
|
25
|
+
// Pixel heights per size. The chart is a square dial, so height is the
|
|
26
|
+
// controlling dimension; width follows the container.
|
|
27
|
+
const SIZE_PX = {
|
|
28
|
+
sm: 120,
|
|
29
|
+
md: 160,
|
|
30
|
+
lg: 200,
|
|
31
|
+
xl: 240,
|
|
32
|
+
} as const
|
|
33
|
+
|
|
34
|
+
// Center value type size per dial size. Chosen so the number sits inside the
|
|
35
|
+
// inner hole with breathing room: text-lg (25px) in a ~75px hole, up to
|
|
36
|
+
// text-3xl (49px) in a ~150px hole. text-3xl is the top of BADS' body tier;
|
|
37
|
+
// the marketing tier (text-4xl = 36px) is smaller and never belongs here.
|
|
38
|
+
const VALUE_TEXT_CLASS = {
|
|
39
|
+
sm: "text-lg",
|
|
40
|
+
md: "text-xl",
|
|
41
|
+
lg: "text-2xl",
|
|
42
|
+
xl: "text-3xl",
|
|
43
|
+
} as const
|
|
44
|
+
|
|
45
|
+
// Single-datum series: a Pie with one entry draws one sector spanning its
|
|
46
|
+
// full startAngle→endAngle range. Both layers reuse this, so the track is the
|
|
47
|
+
// full sweep and the value layer just shortens its endAngle.
|
|
48
|
+
const ARC_DATA = [{ value: 1 }]
|
|
49
|
+
|
|
50
|
+
export type GaugeChartSize = keyof typeof SIZE_PX
|
|
51
|
+
|
|
52
|
+
export interface GaugeChartProps
|
|
53
|
+
extends Omit<React.ComponentProps<"div">, "children"> {
|
|
54
|
+
/** The reading to display. Clamped to the `[min, max]` range for the arc. */
|
|
55
|
+
value: number
|
|
56
|
+
/** Lower bound of the range. Defaults to 0. */
|
|
57
|
+
min?: number
|
|
58
|
+
/** Upper bound of the range. Defaults to 100. */
|
|
59
|
+
max?: number
|
|
60
|
+
/**
|
|
61
|
+
* Short caption rendered under the value in the center of the dial
|
|
62
|
+
* ("CPU usage", "Story coverage"). Also becomes the default accessible
|
|
63
|
+
* name when no `aria-label` is supplied.
|
|
64
|
+
*/
|
|
65
|
+
label?: string
|
|
66
|
+
/**
|
|
67
|
+
* Dial size. Controls pixel height (sm 120, md 160, lg 200, xl 240) and the
|
|
68
|
+
* center value's type size. Defaults to `"md"`.
|
|
69
|
+
*/
|
|
70
|
+
size?: GaugeChartSize
|
|
71
|
+
/**
|
|
72
|
+
* Fill color for the value arc. Accepts any CSS color or `var()` reference.
|
|
73
|
+
* Defaults to `var(--chart-1)` — pass `var(--chart-2)`…`var(--chart-5)` to
|
|
74
|
+
* distinguish several gauges in the same view. The background track always
|
|
75
|
+
* uses `var(--muted)` so the data is the only saturated mark on the canvas.
|
|
76
|
+
*/
|
|
77
|
+
color?: string
|
|
78
|
+
/**
|
|
79
|
+
* Formats the center value and the announced `aria-valuetext`. Defaults to
|
|
80
|
+
* an integer via `Intl.NumberFormat` so a fork's locale is respected.
|
|
81
|
+
*/
|
|
82
|
+
formatValue?: (value: number) => string
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const defaultFormatValue = (value: number) =>
|
|
86
|
+
new Intl.NumberFormat(undefined, { maximumFractionDigits: 0 }).format(value)
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Radial arc dial for a single reading within a known range — CPU load,
|
|
90
|
+
* storage used, a health score, a completion percentage. A quiet
|
|
91
|
+
* `--muted` track sweeps 270°; the value arc in a `--chart-*` color fills the
|
|
92
|
+
* proportion of that sweep the reading represents, and the formatted value
|
|
93
|
+
* (plus an optional label) sits in the center hole.
|
|
94
|
+
*
|
|
95
|
+
* Semantically this is a `role="meter"` (ARIA 1.2): a determinate measurement
|
|
96
|
+
* of where a value sits between min and max, the same contract the linear
|
|
97
|
+
* `Meter` primitive uses. `aria-valuenow` / `aria-valuemin` / `aria-valuemax`
|
|
98
|
+
* are only valid on range roles, so the wrapper is not `role="img"` — an
|
|
99
|
+
* `img` carrying value attributes fails axe's `aria-allowed-attr`.
|
|
100
|
+
*
|
|
101
|
+
* Built on Recharts 3.x (already a BADS peer dependency) with the same
|
|
102
|
+
* ResizeObserver mount-gate as `ChartContainer` and `Sparkline`, so the chart
|
|
103
|
+
* never mounts at -1×-1 during SSR and paints invisible.
|
|
104
|
+
*/
|
|
105
|
+
function GaugeChart({
|
|
106
|
+
value,
|
|
107
|
+
min = 0,
|
|
108
|
+
max = 100,
|
|
109
|
+
label,
|
|
110
|
+
size = "md",
|
|
111
|
+
color = "var(--chart-1)",
|
|
112
|
+
formatValue = defaultFormatValue,
|
|
113
|
+
className,
|
|
114
|
+
style,
|
|
115
|
+
"aria-label": ariaLabel,
|
|
116
|
+
...props
|
|
117
|
+
}: GaugeChartProps) {
|
|
118
|
+
const prefersReducedMotion = useReducedMotion()
|
|
119
|
+
const animate = !prefersReducedMotion
|
|
120
|
+
|
|
121
|
+
// ResizeObserver gate — same pattern as ChartContainer and Sparkline.
|
|
122
|
+
const containerRef = React.useRef<HTMLDivElement | null>(null)
|
|
123
|
+
const [hasSize, setHasSize] = React.useState(false)
|
|
124
|
+
|
|
125
|
+
React.useEffect(() => {
|
|
126
|
+
const el = containerRef.current
|
|
127
|
+
if (!el) return
|
|
128
|
+
const observer = new ResizeObserver((entries) => {
|
|
129
|
+
for (const entry of entries) {
|
|
130
|
+
const { width, height } = entry.contentRect
|
|
131
|
+
setHasSize(width > 0 && height > 0)
|
|
132
|
+
}
|
|
133
|
+
})
|
|
134
|
+
observer.observe(el)
|
|
135
|
+
return () => observer.disconnect()
|
|
136
|
+
}, [])
|
|
137
|
+
|
|
138
|
+
// Guard a degenerate range (min === max) so the division below can't
|
|
139
|
+
// produce NaN, then clamp so an out-of-range reading pins to an end of the
|
|
140
|
+
// dial instead of wrapping around it.
|
|
141
|
+
const range = max - min
|
|
142
|
+
const fraction =
|
|
143
|
+
range > 0 ? Math.min(1, Math.max(0, (value - min) / range)) : 0
|
|
144
|
+
const valueEndAngle = START_ANGLE - fraction * SWEEP
|
|
145
|
+
|
|
146
|
+
const height = SIZE_PX[size]
|
|
147
|
+
const formatted = formatValue(value)
|
|
148
|
+
const valueText = `${formatted} out of ${formatValue(max)}`
|
|
149
|
+
|
|
150
|
+
return (
|
|
151
|
+
<div
|
|
152
|
+
ref={containerRef}
|
|
153
|
+
data-slot="gauge-chart"
|
|
154
|
+
data-size={size}
|
|
155
|
+
role="meter"
|
|
156
|
+
aria-label={ariaLabel ?? label ?? "Gauge"}
|
|
157
|
+
aria-valuenow={value}
|
|
158
|
+
aria-valuemin={min}
|
|
159
|
+
aria-valuemax={max}
|
|
160
|
+
aria-valuetext={valueText}
|
|
161
|
+
className={cn("relative block w-full", className)}
|
|
162
|
+
style={{ height, ...style }}
|
|
163
|
+
{...props}
|
|
164
|
+
>
|
|
165
|
+
{hasSize ? (
|
|
166
|
+
<ResponsiveContainer width="100%" height="100%">
|
|
167
|
+
<PieChart margin={{ top: 0, right: 0, bottom: 0, left: 0 }}>
|
|
168
|
+
{/* Background track — the quiet chrome. Static, never animated. */}
|
|
169
|
+
<Pie
|
|
170
|
+
data={ARC_DATA}
|
|
171
|
+
dataKey="value"
|
|
172
|
+
cx="50%"
|
|
173
|
+
cy={CENTER_Y}
|
|
174
|
+
innerRadius={INNER_RADIUS}
|
|
175
|
+
outerRadius={OUTER_RADIUS}
|
|
176
|
+
startAngle={START_ANGLE}
|
|
177
|
+
endAngle={END_ANGLE}
|
|
178
|
+
cornerRadius="50%"
|
|
179
|
+
fill="var(--muted)"
|
|
180
|
+
stroke="none"
|
|
181
|
+
isAnimationActive={false}
|
|
182
|
+
/>
|
|
183
|
+
{/* Value arc — the only saturated mark on the canvas. Skipped at
|
|
184
|
+
zero so Recharts never draws a zero-angle sector. */}
|
|
185
|
+
{fraction > 0 ? (
|
|
186
|
+
<Pie
|
|
187
|
+
data={ARC_DATA}
|
|
188
|
+
dataKey="value"
|
|
189
|
+
cx="50%"
|
|
190
|
+
cy={CENTER_Y}
|
|
191
|
+
innerRadius={INNER_RADIUS}
|
|
192
|
+
outerRadius={OUTER_RADIUS}
|
|
193
|
+
startAngle={START_ANGLE}
|
|
194
|
+
endAngle={valueEndAngle}
|
|
195
|
+
cornerRadius="50%"
|
|
196
|
+
fill={color}
|
|
197
|
+
stroke="none"
|
|
198
|
+
isAnimationActive={animate}
|
|
199
|
+
animationDuration={700}
|
|
200
|
+
animationEasing="ease-out"
|
|
201
|
+
/>
|
|
202
|
+
) : null}
|
|
203
|
+
</PieChart>
|
|
204
|
+
</ResponsiveContainer>
|
|
205
|
+
) : null}
|
|
206
|
+
|
|
207
|
+
{/* Center readout. Absolutely positioned over the dial's center (which
|
|
208
|
+
sits at CENTER_Y, not 50%). aria-hidden because the wrapper already
|
|
209
|
+
announces the value via aria-valuetext — otherwise AT reads it twice.
|
|
210
|
+
min-w-0 + truncate on the label is the long-label overflow contract:
|
|
211
|
+
a localized caption can be 2× longer than the English one. */}
|
|
212
|
+
<div
|
|
213
|
+
aria-hidden="true"
|
|
214
|
+
className="pointer-events-none absolute left-1/2 flex w-[60%] min-w-0 -translate-x-1/2 -translate-y-1/2 flex-col items-center text-center"
|
|
215
|
+
style={{ top: CENTER_Y }}
|
|
216
|
+
>
|
|
217
|
+
<span
|
|
218
|
+
className={cn(
|
|
219
|
+
"font-semibold leading-none tabular-nums text-foreground",
|
|
220
|
+
VALUE_TEXT_CLASS[size]
|
|
221
|
+
)}
|
|
222
|
+
>
|
|
223
|
+
{formatted}
|
|
224
|
+
</span>
|
|
225
|
+
{label ? (
|
|
226
|
+
<span
|
|
227
|
+
className="mt-1 w-full truncate text-xs text-muted-foreground"
|
|
228
|
+
title={label}
|
|
229
|
+
>
|
|
230
|
+
{label}
|
|
231
|
+
</span>
|
|
232
|
+
) : null}
|
|
233
|
+
</div>
|
|
234
|
+
</div>
|
|
235
|
+
)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export { GaugeChart }
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
|
|
5
|
+
import { cn } from "../../lib/utils"
|
|
6
|
+
|
|
7
|
+
// Heatmap — a matrix of cells whose color intensity encodes a value.
|
|
8
|
+
//
|
|
9
|
+
// The fifth chart primitive, and the first one that is not a Recharts wrapper:
|
|
10
|
+
// Recharts has no heatmap series, and a value matrix does not want an SVG
|
|
11
|
+
// anyway. It wants a <table>. Row and column headers are the axes, each cell
|
|
12
|
+
// is a <td> with a tinted swatch, and the formatted value rides along as
|
|
13
|
+
// screen-reader-only text, so the chart is its own text alternative — a
|
|
14
|
+
// screen reader walks it as "Mon, Week 3, 12 commits" with no separate data
|
|
15
|
+
// table needed (docs/chart-design.md → Accessibility recap).
|
|
16
|
+
//
|
|
17
|
+
// Peer parity: Mantine `@mantine/charts` Heatmap (5 levels, Less→More legend,
|
|
18
|
+
// per-cell tooltip), MUI X Charts Heatmap (x/y axes + continuous color map),
|
|
19
|
+
// AntV Heatmap (xField/yField/colorField + legend ramp). BADS keeps the five
|
|
20
|
+
// levels and the legend, uses a native `title` for the per-cell tooltip, and
|
|
21
|
+
// adds the table semantics none of the three ship.
|
|
22
|
+
//
|
|
23
|
+
// Color (docs/chart-color-palette.md): ONE series color — `--chart-1` by
|
|
24
|
+
// default — and an opacity ramp for intensity. The data is the only saturated
|
|
25
|
+
// thing on the canvas; labels and the legend chrome sit in muted-foreground.
|
|
26
|
+
// Level 0 (the lowest bucket) and "no data" are `--muted`, so a quiet cell
|
|
27
|
+
// reads as surface, not as a faint data mark. Zero new tokens.
|
|
28
|
+
//
|
|
29
|
+
// Motion is pattern (c): the swatch transitions opacity + box-shadow on
|
|
30
|
+
// var(--duration-fast) / var(--ease-out) for the hover ring. Static otherwise.
|
|
31
|
+
|
|
32
|
+
const LEVELS = 5
|
|
33
|
+
/** Opacity for levels 1..4 (level 0 is the muted "lowest" bucket). */
|
|
34
|
+
const LEVEL_OPACITY = [0, 0.3, 0.5, 0.75, 1] as const
|
|
35
|
+
|
|
36
|
+
const sizeClass = {
|
|
37
|
+
sm: "size-3",
|
|
38
|
+
md: "size-4",
|
|
39
|
+
lg: "size-6",
|
|
40
|
+
} as const
|
|
41
|
+
|
|
42
|
+
// Explicit default locale, same reasoning as FormatNumber: an implicit
|
|
43
|
+
// navigator locale formats differently on the server and the client. Pass
|
|
44
|
+
// `formatValue` from your i18n layer to localize.
|
|
45
|
+
const defaultFormatter = new Intl.NumberFormat("en-US")
|
|
46
|
+
|
|
47
|
+
export interface HeatmapProps
|
|
48
|
+
extends Omit<React.ComponentProps<"div">, "children"> {
|
|
49
|
+
/** Row (y-axis) labels, top to bottom. One per row of `data`. */
|
|
50
|
+
rows: string[]
|
|
51
|
+
/** Column (x-axis) labels, left to right. One per entry of each row. */
|
|
52
|
+
columns: string[]
|
|
53
|
+
/**
|
|
54
|
+
* `rows.length` × `columns.length` matrix. `null` / `undefined` is "no data"
|
|
55
|
+
* and renders as a quiet muted cell, announced as "No data".
|
|
56
|
+
*/
|
|
57
|
+
data: ReadonlyArray<ReadonlyArray<number | null | undefined>>
|
|
58
|
+
/**
|
|
59
|
+
* Series color — any CSS color, usually a chart token
|
|
60
|
+
* (`var(--chart-2)`). Intensity is an opacity ramp of this one color.
|
|
61
|
+
* Defaults to `var(--chart-1)`.
|
|
62
|
+
*/
|
|
63
|
+
color?: string
|
|
64
|
+
/** Cell size: sm 12px, md 16px (default), lg 24px. */
|
|
65
|
+
size?: keyof typeof sizeClass
|
|
66
|
+
/**
|
|
67
|
+
* Fixed `[min, max]` for the intensity scale. Defaults to the data's own
|
|
68
|
+
* range. Pass it when several heatmaps must share one scale (a row of
|
|
69
|
+
* months, one per card) so the same value reads the same shade everywhere.
|
|
70
|
+
*/
|
|
71
|
+
domain?: [number, number]
|
|
72
|
+
/**
|
|
73
|
+
* Formats a value for the cell tooltip, the screen-reader text, and the
|
|
74
|
+
* legend. Defaults to `Intl.NumberFormat("en-US")`.
|
|
75
|
+
*/
|
|
76
|
+
formatValue?: (value: number) => string
|
|
77
|
+
/** Render the Less → More ramp below the grid. Default `true`. */
|
|
78
|
+
showLegend?: boolean
|
|
79
|
+
/**
|
|
80
|
+
* Accessible name — becomes the scroll region's `aria-label` and the
|
|
81
|
+
* table's caption. Say what the matrix measures: "Commits by weekday,
|
|
82
|
+
* last 12 weeks". Default "Heatmap".
|
|
83
|
+
*/
|
|
84
|
+
label?: string
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function quantize(
|
|
88
|
+
value: number | null | undefined,
|
|
89
|
+
min: number,
|
|
90
|
+
max: number
|
|
91
|
+
): number | null {
|
|
92
|
+
if (value == null || Number.isNaN(value)) return null
|
|
93
|
+
if (max === min) return value > 0 ? LEVELS - 1 : 0
|
|
94
|
+
if (value <= min) return 0
|
|
95
|
+
const t = (value - min) / (max - min)
|
|
96
|
+
return Math.min(LEVELS - 1, 1 + Math.floor(t * (LEVELS - 1)))
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function Heatmap({
|
|
100
|
+
rows,
|
|
101
|
+
columns,
|
|
102
|
+
data,
|
|
103
|
+
color = "var(--chart-1)",
|
|
104
|
+
size = "md",
|
|
105
|
+
domain,
|
|
106
|
+
formatValue,
|
|
107
|
+
showLegend = true,
|
|
108
|
+
label = "Heatmap",
|
|
109
|
+
className,
|
|
110
|
+
...props
|
|
111
|
+
}: HeatmapProps) {
|
|
112
|
+
const format = React.useCallback(
|
|
113
|
+
(v: number) => (formatValue ? formatValue(v) : defaultFormatter.format(v)),
|
|
114
|
+
[formatValue]
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
const [min, max] = React.useMemo<[number, number]>(() => {
|
|
118
|
+
if (domain) return domain
|
|
119
|
+
let lo = Number.POSITIVE_INFINITY
|
|
120
|
+
let hi = Number.NEGATIVE_INFINITY
|
|
121
|
+
for (const row of data) {
|
|
122
|
+
for (const v of row) {
|
|
123
|
+
if (v == null || Number.isNaN(v)) continue
|
|
124
|
+
if (v < lo) lo = v
|
|
125
|
+
if (v > hi) hi = v
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (lo === Number.POSITIVE_INFINITY) return [0, 0]
|
|
129
|
+
return [lo, hi]
|
|
130
|
+
}, [data, domain])
|
|
131
|
+
|
|
132
|
+
const swatchSize = sizeClass[size]
|
|
133
|
+
const motion: React.CSSProperties = {
|
|
134
|
+
transitionDuration: "var(--duration-fast)",
|
|
135
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return (
|
|
139
|
+
<div
|
|
140
|
+
data-slot="heatmap"
|
|
141
|
+
data-size={size}
|
|
142
|
+
// The matrix can be wider than its container (52 weeks, 24 hours), so
|
|
143
|
+
// the root is a scroll region. A scroll container no keyboard user can
|
|
144
|
+
// reach fails axe (scrollable-region-focusable) — hence tabIndex + a
|
|
145
|
+
// landmark role with the chart's name (corrections #019).
|
|
146
|
+
role="region"
|
|
147
|
+
aria-label={label}
|
|
148
|
+
tabIndex={0}
|
|
149
|
+
className={cn(
|
|
150
|
+
"inline-flex max-w-full flex-col gap-3 overflow-x-auto rounded-md outline-none",
|
|
151
|
+
"focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
|
|
152
|
+
className
|
|
153
|
+
)}
|
|
154
|
+
{...props}
|
|
155
|
+
>
|
|
156
|
+
<table
|
|
157
|
+
data-slot="heatmap-table"
|
|
158
|
+
className="w-max border-separate border-spacing-0.5 text-xs text-muted-foreground"
|
|
159
|
+
>
|
|
160
|
+
<caption className="sr-only">{label}</caption>
|
|
161
|
+
<thead>
|
|
162
|
+
<tr>
|
|
163
|
+
{/* Empty corner cell above the row labels. */}
|
|
164
|
+
<td aria-hidden="true" />
|
|
165
|
+
{columns.map((column, ci) => (
|
|
166
|
+
<th
|
|
167
|
+
key={`${column}-${ci}`}
|
|
168
|
+
scope="col"
|
|
169
|
+
data-slot="heatmap-column-label"
|
|
170
|
+
className="px-0 pb-1 text-center align-bottom font-normal whitespace-nowrap"
|
|
171
|
+
>
|
|
172
|
+
{column}
|
|
173
|
+
</th>
|
|
174
|
+
))}
|
|
175
|
+
</tr>
|
|
176
|
+
</thead>
|
|
177
|
+
<tbody>
|
|
178
|
+
{rows.map((row, ri) => (
|
|
179
|
+
<tr key={`${row}-${ri}`}>
|
|
180
|
+
<th
|
|
181
|
+
scope="row"
|
|
182
|
+
data-slot="heatmap-row-label"
|
|
183
|
+
className="pr-2 text-left align-middle font-normal whitespace-nowrap"
|
|
184
|
+
>
|
|
185
|
+
{row}
|
|
186
|
+
</th>
|
|
187
|
+
{columns.map((column, ci) => {
|
|
188
|
+
const value = data[ri]?.[ci]
|
|
189
|
+
const level = quantize(value, min, max)
|
|
190
|
+
const text =
|
|
191
|
+
level == null ? "No data" : format(value as number)
|
|
192
|
+
return (
|
|
193
|
+
<td
|
|
194
|
+
key={`${column}-${ci}`}
|
|
195
|
+
data-slot="heatmap-cell"
|
|
196
|
+
data-level={level ?? undefined}
|
|
197
|
+
title={`${row} · ${column}: ${text}`}
|
|
198
|
+
className="group/cell p-0 align-middle"
|
|
199
|
+
>
|
|
200
|
+
<span
|
|
201
|
+
data-slot="heatmap-swatch"
|
|
202
|
+
aria-hidden="true"
|
|
203
|
+
className={cn(
|
|
204
|
+
"block rounded-sm transition-[opacity,box-shadow]",
|
|
205
|
+
swatchSize,
|
|
206
|
+
level == null && "bg-muted/50",
|
|
207
|
+
level === 0 && "bg-muted",
|
|
208
|
+
"group-hover/cell:ring-2 group-hover/cell:ring-ring group-hover/cell:ring-offset-1 group-hover/cell:ring-offset-background"
|
|
209
|
+
)}
|
|
210
|
+
style={{
|
|
211
|
+
...motion,
|
|
212
|
+
...(level != null && level > 0
|
|
213
|
+
? { backgroundColor: color, opacity: LEVEL_OPACITY[level] }
|
|
214
|
+
: null),
|
|
215
|
+
}}
|
|
216
|
+
/>
|
|
217
|
+
<span className="sr-only">{text}</span>
|
|
218
|
+
</td>
|
|
219
|
+
)
|
|
220
|
+
})}
|
|
221
|
+
</tr>
|
|
222
|
+
))}
|
|
223
|
+
</tbody>
|
|
224
|
+
</table>
|
|
225
|
+
{showLegend && (
|
|
226
|
+
<div
|
|
227
|
+
data-slot="heatmap-legend"
|
|
228
|
+
className="flex items-center justify-end gap-1 text-xs text-muted-foreground"
|
|
229
|
+
>
|
|
230
|
+
<span>Less</span>
|
|
231
|
+
<span className="sr-only">
|
|
232
|
+
{` (${format(min)}) to `}
|
|
233
|
+
</span>
|
|
234
|
+
<span aria-hidden="true" className="flex items-center gap-0.5">
|
|
235
|
+
{Array.from({ length: LEVELS }, (_, level) => (
|
|
236
|
+
<span
|
|
237
|
+
key={level}
|
|
238
|
+
data-level={level}
|
|
239
|
+
className={cn("block rounded-sm", swatchSize, level === 0 && "bg-muted")}
|
|
240
|
+
style={
|
|
241
|
+
level > 0
|
|
242
|
+
? { backgroundColor: color, opacity: LEVEL_OPACITY[level] }
|
|
243
|
+
: undefined
|
|
244
|
+
}
|
|
245
|
+
/>
|
|
246
|
+
))}
|
|
247
|
+
</span>
|
|
248
|
+
<span>More</span>
|
|
249
|
+
<span className="sr-only">{` (${format(max)})`}</span>
|
|
250
|
+
</div>
|
|
251
|
+
)}
|
|
252
|
+
</div>
|
|
253
|
+
)
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export { Heatmap }
|
|
@@ -152,3 +152,12 @@ export * from "./icon";
|
|
|
152
152
|
export * from "./progress-stack";
|
|
153
153
|
export * from "./callout";
|
|
154
154
|
export * from "./announcement-bar";
|
|
155
|
+
export * from "./gauge-chart";
|
|
156
|
+
export * from "./password-strength-input";
|
|
157
|
+
export * from "./pill";
|
|
158
|
+
export * from "./snackbar";
|
|
159
|
+
export * from "./json-input";
|
|
160
|
+
export * from "./sortable-list";
|
|
161
|
+
export * from "./heatmap";
|
|
162
|
+
export * from "./navigation-progress";
|
|
163
|
+
export * from "./attachment";
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { cn } from "../../lib/utils"
|
|
5
|
+
import { Spinner } from "./spinner"
|
|
6
|
+
|
|
7
|
+
export interface InfiniteScrollProps {
|
|
8
|
+
/** Fired when the sentinel enters the viewport and `hasMore` is true. */
|
|
9
|
+
onLoadMore: () => void
|
|
10
|
+
/** Whether more pages are available. When false the sentinel is removed. */
|
|
11
|
+
hasMore: boolean
|
|
12
|
+
/** Override the default spinner. Pass `null` to suppress the loader entirely. */
|
|
13
|
+
loader?: React.ReactNode
|
|
14
|
+
/** Shown below the list when `hasMore` is false. */
|
|
15
|
+
endMessage?: React.ReactNode
|
|
16
|
+
/**
|
|
17
|
+
* IntersectionObserver threshold (0–1). Default 0.
|
|
18
|
+
* Higher values trigger earlier (before the sentinel fully enters view).
|
|
19
|
+
*/
|
|
20
|
+
threshold?: number
|
|
21
|
+
/**
|
|
22
|
+
* IntersectionObserver rootMargin. Default "0px 0px 200px 0px" (triggers
|
|
23
|
+
* 200px before the sentinel reaches the viewport bottom).
|
|
24
|
+
*/
|
|
25
|
+
rootMargin?: string
|
|
26
|
+
children: React.ReactNode
|
|
27
|
+
className?: string
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Wraps a list with an IntersectionObserver-based load-more trigger.
|
|
32
|
+
* The sentinel sits below `children`; when it enters the viewport and
|
|
33
|
+
* `hasMore` is true, `onLoadMore` fires once per intersection.
|
|
34
|
+
*
|
|
35
|
+
* Sources: Mantine InfiniteScroll (Embla fork), TanStack Query infinite
|
|
36
|
+
* scroll recipe, react-infinite-scroll-component (npm), shadcn community
|
|
37
|
+
* infinite-scroll recipe (2026-Q3).
|
|
38
|
+
*/
|
|
39
|
+
function InfiniteScroll({
|
|
40
|
+
onLoadMore,
|
|
41
|
+
hasMore,
|
|
42
|
+
loader,
|
|
43
|
+
endMessage,
|
|
44
|
+
threshold = 0,
|
|
45
|
+
rootMargin = "0px 0px 200px 0px",
|
|
46
|
+
children,
|
|
47
|
+
className,
|
|
48
|
+
}: InfiniteScrollProps) {
|
|
49
|
+
const sentinelRef = React.useRef<HTMLDivElement>(null)
|
|
50
|
+
const loadingRef = React.useRef(false)
|
|
51
|
+
|
|
52
|
+
React.useEffect(() => {
|
|
53
|
+
const sentinel = sentinelRef.current
|
|
54
|
+
if (!sentinel || !hasMore) return
|
|
55
|
+
|
|
56
|
+
const observer = new IntersectionObserver(
|
|
57
|
+
(entries) => {
|
|
58
|
+
const entry = entries[0]
|
|
59
|
+
if (!entry.isIntersecting) {
|
|
60
|
+
// The sentinel left the viewport: re-arm for the next entry. Before
|
|
61
|
+
// this, the guard only reset on a `hasMore` transition, so a list
|
|
62
|
+
// whose `hasMore` stayed true loaded exactly one page and went inert.
|
|
63
|
+
loadingRef.current = false
|
|
64
|
+
return
|
|
65
|
+
}
|
|
66
|
+
if (!loadingRef.current) {
|
|
67
|
+
loadingRef.current = true
|
|
68
|
+
onLoadMore()
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
{ threshold, rootMargin }
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
observer.observe(sentinel)
|
|
75
|
+
return () => observer.disconnect()
|
|
76
|
+
}, [hasMore, onLoadMore, threshold, rootMargin])
|
|
77
|
+
|
|
78
|
+
// Reset loading guard when hasMore changes (i.e. a new page loaded).
|
|
79
|
+
React.useEffect(() => {
|
|
80
|
+
loadingRef.current = false
|
|
81
|
+
}, [hasMore])
|
|
82
|
+
|
|
83
|
+
const defaultLoader = (
|
|
84
|
+
<div className="flex justify-center py-4" aria-label="Loading more items">
|
|
85
|
+
<Spinner size="sm" />
|
|
86
|
+
</div>
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
return (
|
|
90
|
+
<div data-slot="infinite-scroll" className={cn("w-full", className)}>
|
|
91
|
+
{children}
|
|
92
|
+
{hasMore ? (
|
|
93
|
+
<div ref={sentinelRef} aria-hidden="true">
|
|
94
|
+
{loader === null ? null : loader ?? defaultLoader}
|
|
95
|
+
</div>
|
|
96
|
+
) : (
|
|
97
|
+
endMessage
|
|
98
|
+
)}
|
|
99
|
+
</div>
|
|
100
|
+
)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export { InfiniteScroll }
|