@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
|
@@ -4,6 +4,7 @@ import * as React from "react"
|
|
|
4
4
|
import * as RechartsPrimitive from "recharts"
|
|
5
5
|
|
|
6
6
|
import { cn } from "../../lib/utils"
|
|
7
|
+
import { useReducedMotion } from "../../hooks/use-reduced-motion"
|
|
7
8
|
|
|
8
9
|
// Format: { THEME_NAME: CSS_SELECTOR }
|
|
9
10
|
const THEMES = { light: "", dark: ".dark" } as const
|
|
@@ -81,19 +82,6 @@ function injectAnimationDefault(
|
|
|
81
82
|
return React.cloneElement(element, nextProps)
|
|
82
83
|
}
|
|
83
84
|
|
|
84
|
-
function usePrefersReducedMotion() {
|
|
85
|
-
const [reduced, setReduced] = React.useState(false)
|
|
86
|
-
React.useEffect(() => {
|
|
87
|
-
if (typeof window === "undefined" || !window.matchMedia) return
|
|
88
|
-
const mql = window.matchMedia("(prefers-reduced-motion: reduce)")
|
|
89
|
-
setReduced(mql.matches)
|
|
90
|
-
const onChange = (e: MediaQueryListEvent) => setReduced(e.matches)
|
|
91
|
-
mql.addEventListener("change", onChange)
|
|
92
|
-
return () => mql.removeEventListener("change", onChange)
|
|
93
|
-
}, [])
|
|
94
|
-
return reduced
|
|
95
|
-
}
|
|
96
|
-
|
|
97
85
|
function ChartContainer({
|
|
98
86
|
id,
|
|
99
87
|
className,
|
|
@@ -120,7 +108,7 @@ function ChartContainer({
|
|
|
120
108
|
}) {
|
|
121
109
|
const uniqueId = React.useId()
|
|
122
110
|
const chartId = `chart-${id || uniqueId.replace(/:/g, "")}`
|
|
123
|
-
const prefersReducedMotion =
|
|
111
|
+
const prefersReducedMotion = useReducedMotion()
|
|
124
112
|
const effectiveAnimate = animate && !prefersReducedMotion
|
|
125
113
|
|
|
126
114
|
// ResponsiveContainer logs "width(-1) and height(-1)" whenever it renders
|
|
@@ -195,6 +183,9 @@ const ChartStyle = ({ id, config }: { id: string; config: ChartConfig }) => {
|
|
|
195
183
|
}
|
|
196
184
|
|
|
197
185
|
return (
|
|
186
|
+
// allow-inline-style: generates N custom-property writes scoped to a
|
|
187
|
+
// runtime [data-chart=${id}] selector — no static class can express
|
|
188
|
+
// per-instance generated CSS, and the values written ARE tokens.
|
|
198
189
|
<style
|
|
199
190
|
dangerouslySetInnerHTML={{
|
|
200
191
|
__html: `
|
|
@@ -10,7 +10,7 @@ const codeVariants = cva("font-mono text-muted-foreground bg-muted", {
|
|
|
10
10
|
variant: {
|
|
11
11
|
inline: "inline rounded px-[0.3em] py-[0.1em] text-xs",
|
|
12
12
|
block:
|
|
13
|
-
"block w-full rounded-md border border-border px-3 py-2.5 text-sm leading-relaxed overflow-x-auto scrollbar-thin scrollbar-
|
|
13
|
+
"block w-full rounded-md border border-border px-3 py-2.5 text-sm leading-relaxed overflow-x-auto [scrollbar-width:thin] [scrollbar-color:theme(colors.border)_transparent] whitespace-pre outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50",
|
|
14
14
|
},
|
|
15
15
|
size: {
|
|
16
16
|
sm: "",
|
|
@@ -10,7 +10,7 @@ import { Button } from "./button";
|
|
|
10
10
|
/* Helpers */
|
|
11
11
|
/* -------------------------------------------------------------------------- */
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
import { clamp } from "../../lib/clamp";
|
|
14
14
|
|
|
15
15
|
function hexToRgb(hex: string): [number, number, number] | null {
|
|
16
16
|
if (!isValidHex(hex)) return null;
|
|
@@ -549,7 +549,7 @@ export function ColorPicker({
|
|
|
549
549
|
type="button"
|
|
550
550
|
onClick={addColor}
|
|
551
551
|
disabled={disabled || !isValidHex(draft)}
|
|
552
|
-
className="w-full"
|
|
552
|
+
className="w-full min-h-11"
|
|
553
553
|
>
|
|
554
554
|
Add color
|
|
555
555
|
</Button>
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
|
|
5
|
+
import { cn } from "../../lib/utils"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Explicit default locale — never reads navigator.language, which differs
|
|
9
|
+
* between server and client and causes hydration mismatches.
|
|
10
|
+
*/
|
|
11
|
+
const FORMAT_RELATIVE_TIME_DEFAULT_LOCALE = "en-US"
|
|
12
|
+
|
|
13
|
+
/** Thresholds for picking the right Intl unit. */
|
|
14
|
+
const UNITS: { unit: Intl.RelativeTimeFormatUnit; ms: number }[] = [
|
|
15
|
+
{ unit: "second", ms: 60_000 },
|
|
16
|
+
{ unit: "minute", ms: 3_600_000 },
|
|
17
|
+
{ unit: "hour", ms: 86_400_000 },
|
|
18
|
+
{ unit: "day", ms: 2_592_000_000 },
|
|
19
|
+
{ unit: "month", ms: 31_536_000_000 },
|
|
20
|
+
{ unit: "year", ms: Infinity },
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
function formatRelative(
|
|
24
|
+
date: Date,
|
|
25
|
+
locale: string,
|
|
26
|
+
numeric: Intl.RelativeTimeFormatNumeric,
|
|
27
|
+
style: Intl.RelativeTimeFormatStyle
|
|
28
|
+
): string {
|
|
29
|
+
const diffMs = date.getTime() - Date.now()
|
|
30
|
+
const absDiff = Math.abs(diffMs)
|
|
31
|
+
|
|
32
|
+
for (let i = 0; i < UNITS.length; i++) {
|
|
33
|
+
const { unit, ms } = UNITS[i]
|
|
34
|
+
if (absDiff < ms) {
|
|
35
|
+
const prevMs = UNITS[i - 1]?.ms ?? 1_000
|
|
36
|
+
const value = Math.round(diffMs / prevMs)
|
|
37
|
+
return new Intl.RelativeTimeFormat(locale, { numeric, style }).format(
|
|
38
|
+
value,
|
|
39
|
+
unit
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// Fallback: years
|
|
44
|
+
const value = Math.round(diffMs / 31_536_000_000)
|
|
45
|
+
return new Intl.RelativeTimeFormat(locale, { numeric, style }).format(
|
|
46
|
+
value,
|
|
47
|
+
"year"
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface FormatRelativeTimeProps
|
|
52
|
+
extends Omit<React.ComponentProps<"time">, "children" | "dateTime"> {
|
|
53
|
+
/**
|
|
54
|
+
* The date to express as relative to now. Accepts a Date, ISO 8601 string,
|
|
55
|
+
* or UNIX timestamp (ms).
|
|
56
|
+
*/
|
|
57
|
+
date: Date | string | number
|
|
58
|
+
/**
|
|
59
|
+
* BCP 47 locale tag. Defaults to "en-US" (explicit, SSR-safe).
|
|
60
|
+
*/
|
|
61
|
+
locale?: string
|
|
62
|
+
/**
|
|
63
|
+
* Whether the displayed string should update automatically.
|
|
64
|
+
* @default true
|
|
65
|
+
*/
|
|
66
|
+
live?: boolean
|
|
67
|
+
/**
|
|
68
|
+
* How often to recalculate the relative string, in milliseconds.
|
|
69
|
+
* Ignored when `live` is false.
|
|
70
|
+
* @default 30000 (30 s)
|
|
71
|
+
*/
|
|
72
|
+
updateInterval?: number
|
|
73
|
+
/**
|
|
74
|
+
* `Intl.RelativeTimeFormat` numeric mode.
|
|
75
|
+
* "auto" produces "yesterday", "today", "tomorrow".
|
|
76
|
+
* "always" produces "-1 day", "0 days", "+1 day".
|
|
77
|
+
* @default "auto"
|
|
78
|
+
*/
|
|
79
|
+
numeric?: Intl.RelativeTimeFormatNumeric
|
|
80
|
+
/**
|
|
81
|
+
* `Intl.RelativeTimeFormat` style.
|
|
82
|
+
* @default "long"
|
|
83
|
+
*/
|
|
84
|
+
formatStyle?: Intl.RelativeTimeFormatStyle
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Locale-aware relative-time display primitive — a thin wrapper around
|
|
89
|
+
* `Intl.RelativeTimeFormat`. Renders a `<time>` element with a machine-
|
|
90
|
+
* readable `dateTime` attribute and a human-readable relative string like
|
|
91
|
+
* "3 minutes ago" or "in 2 days". Optionally live-updates.
|
|
92
|
+
*
|
|
93
|
+
* Display-only. For editable date/time inputs use DatePicker or TimePicker.
|
|
94
|
+
*/
|
|
95
|
+
function FormatRelativeTime({
|
|
96
|
+
date,
|
|
97
|
+
locale = FORMAT_RELATIVE_TIME_DEFAULT_LOCALE,
|
|
98
|
+
live = true,
|
|
99
|
+
updateInterval = 30_000,
|
|
100
|
+
numeric = "auto",
|
|
101
|
+
formatStyle = "long",
|
|
102
|
+
className,
|
|
103
|
+
title,
|
|
104
|
+
...props
|
|
105
|
+
}: FormatRelativeTimeProps) {
|
|
106
|
+
const parsed = React.useMemo(
|
|
107
|
+
() => (date instanceof Date ? date : new Date(date)),
|
|
108
|
+
[date]
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
const [text, setText] = React.useState<string>(() =>
|
|
112
|
+
formatRelative(parsed, locale, numeric, formatStyle)
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
React.useEffect(() => {
|
|
116
|
+
setText(formatRelative(parsed, locale, numeric, formatStyle))
|
|
117
|
+
if (!live) return
|
|
118
|
+
const id = setInterval(() => {
|
|
119
|
+
setText(formatRelative(parsed, locale, numeric, formatStyle))
|
|
120
|
+
}, updateInterval)
|
|
121
|
+
return () => clearInterval(id)
|
|
122
|
+
}, [parsed, locale, live, updateInterval, numeric, formatStyle])
|
|
123
|
+
|
|
124
|
+
const isoString = parsed.toISOString()
|
|
125
|
+
|
|
126
|
+
return (
|
|
127
|
+
<time
|
|
128
|
+
data-slot="format-relative-time"
|
|
129
|
+
dateTime={isoString}
|
|
130
|
+
title={title ?? isoString}
|
|
131
|
+
className={cn(className)}
|
|
132
|
+
{...props}
|
|
133
|
+
>
|
|
134
|
+
{text}
|
|
135
|
+
</time>
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export { FormatRelativeTime, FORMAT_RELATIVE_TIME_DEFAULT_LOCALE }
|
|
@@ -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 }
|