@behavioral-agentic-design-system/core 0.39.0 → 0.40.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 +8 -3
- package/scripts/a11y-lint.cjs +93 -39
- package/scripts/a11y-ratchet.mjs +10 -23
- package/scripts/bads-components.mjs +118 -0
- package/scripts/build-tokens.cjs +175 -17
- package/scripts/check-stories.mjs +1 -1
- package/scripts/health-report.mjs +52 -10
- package/scripts/lib/scan-roots.cjs +143 -0
- package/scripts/lint-component-source.mjs +100 -0
- package/scripts/lint-microanimation.mjs +267 -0
- package/scripts/lint-slop.mjs +21 -28
- package/scripts/lint-token-intent.mjs +10 -16
- package/scripts/size-audit.cjs +94 -27
- package/scripts/source-of-truth.cjs +58 -0
- package/scripts/ux-smoke.mjs +438 -0
- package/scripts/wcag-audit.cjs +49 -1
- package/src/api/layers/file.ts +2 -0
- package/src/app/preview/components/page.tsx +972 -0
- package/src/app/preview/health/page.tsx +8 -5
- package/src/app/preview/history/page.tsx +11 -0
- package/src/app/shell.tsx +261 -87
- package/src/components/customize/brand-mark-section.tsx +4 -2
- package/src/components/customize/design-controls.tsx +1 -1
- package/src/components/customize/discipline-section.tsx +91 -0
- 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 +9 -4
- package/src/components/home-tab.tsx +3 -0
- package/src/components/ui/accordion.tsx +7 -0
- package/src/components/ui/alert.tsx +1 -0
- package/src/components/ui/announcement-bar.tsx +256 -0
- package/src/components/ui/badge.tsx +1 -0
- package/src/components/ui/callout.tsx +218 -0
- package/src/components/ui/chart.tsx +2 -14
- package/src/components/ui/color-picker.tsx +1 -1
- package/src/components/ui/format-relative-time.tsx +139 -0
- package/src/components/ui/grid.tsx +255 -0
- package/src/components/ui/icon.tsx +108 -0
- package/src/components/ui/index.ts +8 -0
- package/src/components/ui/page-header.tsx +63 -0
- 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/select.tsx +89 -4
- package/src/components/ui/separator.tsx +1 -0
- package/src/components/ui/slider.tsx +83 -3
- package/src/components/ui/sparkline.tsx +15 -15
- package/src/components/ui/textarea.tsx +28 -1
- package/src/data/docs-index.ts +5 -0
- package/src/data/history.json +269 -0
- package/src/hooks/use-reduced-motion.ts +6 -0
- package/src/stories/Accordion.stories.tsx +59 -0
- package/src/stories/Alert.stories.tsx +3 -1
- package/src/stories/AnnouncementBar.stories.tsx +261 -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 +4 -0
- package/src/stories/ColorPicker.stories.tsx +23 -0
- package/src/stories/FormatRelativeTime.stories.tsx +116 -0
- package/src/stories/Grid.stories.tsx +197 -0
- package/src/stories/Icon.stories.tsx +175 -0
- package/src/stories/MultiSelect.stories.tsx +1 -1
- package/src/stories/PageHeader.stories.tsx +102 -0
- package/src/stories/Portal.stories.tsx +189 -0
- package/src/stories/Presence.stories.tsx +228 -0
- package/src/stories/ProgressStack.stories.tsx +287 -0
- package/src/stories/Select.stories.tsx +25 -0
- package/src/stories/Separator.stories.tsx +3 -0
- package/src/stories/Sheet.stories.tsx +9 -3
- package/src/stories/Slider.stories.tsx +54 -5
- package/src/stories/Sparkline.stories.tsx +39 -0
- package/src/stories/Textarea.stories.tsx +18 -0
- package/src/stories/story-groups.json +14 -5
- package/src/styles.css +133 -12
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { createPortal } from "react-dom"
|
|
5
|
+
|
|
6
|
+
import { cn } from "../../lib/utils"
|
|
7
|
+
import { usePortalThemeClass } from "../../hooks/use-portal-theme-class"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Portal — render children into a different DOM container (by default the
|
|
11
|
+
* `<body>` of the component's own document) while keeping them in the React
|
|
12
|
+
* tree, so context, events, and state all still flow from the parent.
|
|
13
|
+
*
|
|
14
|
+
* Peer parity: Radix Portal, Chakra UI Portal, Mantine Portal, Ark UI Portal,
|
|
15
|
+
* MUI Portal. Zero dependencies beyond react-dom's `createPortal`.
|
|
16
|
+
*
|
|
17
|
+
* Why not just call createPortal?
|
|
18
|
+
* --------------------------------
|
|
19
|
+
* A raw portal lands OUTSIDE any theme/brand class a consumer applied to a
|
|
20
|
+
* page wrapper, so theme-scoped custom properties (`--shell-chrome-bg-rgb`,
|
|
21
|
+
* `--brand-*`) go invalid at computed-value time inside it and the surface
|
|
22
|
+
* renders unthemed — the failure documented in hooks/use-portal-theme-class
|
|
23
|
+
* and CLAUDE.md → "Portal components and CSS scope". Every BADS-shipped Radix
|
|
24
|
+
* portal merges `usePortalThemeClass()` onto its content for exactly this
|
|
25
|
+
* reason; this primitive does the same for arbitrary content, so a fork's
|
|
26
|
+
* own floating bar or toast gets the scope for free.
|
|
27
|
+
*
|
|
28
|
+
* The portal root is a `<div class="contents">`: it stays in the DOM tree
|
|
29
|
+
* (so the scope class's custom properties inherit into the children) but
|
|
30
|
+
* generates no box, so it never affects the layout of what it wraps. A plain
|
|
31
|
+
* div carries no semantics, so the historical `display: contents` a11y bug
|
|
32
|
+
* (dropped semantics on buttons/lists) does not apply.
|
|
33
|
+
*
|
|
34
|
+
* Cross-window safety
|
|
35
|
+
* -------------------
|
|
36
|
+
* The default container is read from an inline hidden anchor's
|
|
37
|
+
* `ownerDocument`, never the bare `document` global — a component inside an
|
|
38
|
+
* iframe (the BADS preview tabs) or another window portals into ITS body,
|
|
39
|
+
* not the top-level one (spec §2). Pass `container` to target any element.
|
|
40
|
+
*
|
|
41
|
+
* SSR: the anchor renders on the server; the portal mounts after hydration
|
|
42
|
+
* (same mount gate Radix uses).
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
const useIsoLayoutEffect =
|
|
46
|
+
typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect
|
|
47
|
+
|
|
48
|
+
interface PortalProps extends React.ComponentPropsWithoutRef<"div"> {
|
|
49
|
+
/**
|
|
50
|
+
* Element to render into. Defaults to the `<body>` of the component's own
|
|
51
|
+
* document. `null` is treated as "not ready" and renders nothing until a
|
|
52
|
+
* container resolves — handy with a ref that populates after mount.
|
|
53
|
+
*/
|
|
54
|
+
container?: Element | null
|
|
55
|
+
/** When true, children render in place (no portal). Useful for tests,
|
|
56
|
+
* print stylesheets, or when an ancestor already portals. */
|
|
57
|
+
disabled?: boolean
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function Portal({
|
|
61
|
+
container,
|
|
62
|
+
disabled = false,
|
|
63
|
+
className,
|
|
64
|
+
children,
|
|
65
|
+
...props
|
|
66
|
+
}: PortalProps) {
|
|
67
|
+
const anchorRef = React.useRef<HTMLSpanElement | null>(null)
|
|
68
|
+
const [ownBody, setOwnBody] = React.useState<Element | null>(null)
|
|
69
|
+
const portalThemeClass = usePortalThemeClass()
|
|
70
|
+
|
|
71
|
+
useIsoLayoutEffect(() => {
|
|
72
|
+
setOwnBody(anchorRef.current?.ownerDocument.body ?? null)
|
|
73
|
+
}, [])
|
|
74
|
+
|
|
75
|
+
if (disabled) {
|
|
76
|
+
return (
|
|
77
|
+
<div
|
|
78
|
+
data-slot="portal"
|
|
79
|
+
data-disabled="true"
|
|
80
|
+
className={cn("contents", className)}
|
|
81
|
+
{...props}
|
|
82
|
+
>
|
|
83
|
+
{children}
|
|
84
|
+
</div>
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// `container === undefined` means "use my own body"; `null` means "wait".
|
|
89
|
+
const target = container === undefined ? ownBody : container
|
|
90
|
+
|
|
91
|
+
return (
|
|
92
|
+
<>
|
|
93
|
+
<span ref={anchorRef} data-slot="portal-anchor" hidden aria-hidden="true" />
|
|
94
|
+
{target
|
|
95
|
+
? createPortal(
|
|
96
|
+
<div
|
|
97
|
+
data-slot="portal"
|
|
98
|
+
className={cn("contents", portalThemeClass, className)}
|
|
99
|
+
{...props}
|
|
100
|
+
>
|
|
101
|
+
{children}
|
|
102
|
+
</div>,
|
|
103
|
+
target
|
|
104
|
+
)
|
|
105
|
+
: null}
|
|
106
|
+
</>
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export { Portal }
|
|
111
|
+
export type { PortalProps }
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { Slot } from "@radix-ui/react-slot"
|
|
5
|
+
|
|
6
|
+
import { cn } from "../../lib/utils"
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Presence — mount/unmount wrapper with a token-driven enter *and exit*
|
|
10
|
+
* animation. `present={true}` mounts the child and plays the enter keyframe;
|
|
11
|
+
* `present={false}` plays the exit keyframe and only THEN unmounts (or hides)
|
|
12
|
+
* the child. Conditional rendering (`{open && <X />}`) can never animate out
|
|
13
|
+
* because the node is gone before the browser paints a frame — that exit
|
|
14
|
+
* frame is the whole reason this primitive exists.
|
|
15
|
+
*
|
|
16
|
+
* Peer parity: Ark UI Presence, Chakra UI v3 Presence, Mantine Transition,
|
|
17
|
+
* MUI Fade / Grow / Slide / Zoom, Radix's internal Presence.
|
|
18
|
+
*
|
|
19
|
+
* How the exit wait works
|
|
20
|
+
* -----------------------
|
|
21
|
+
* On `present=false` the wrapper flips to `data-state="closed"`, which the
|
|
22
|
+
* design-system CSS (keyed off `[data-slot="presence"][data-transition=…]
|
|
23
|
+
* [data-state=…]` in build-tokens.cjs — spec §4 pattern b) maps to the
|
|
24
|
+
* matching `presence-<transition>-out` keyframe. The effect then reads
|
|
25
|
+
* `element.getAnimations()`: zero running animations → unmount immediately
|
|
26
|
+
* (a consumer overrode `animation: none`, or the browser lacks the API);
|
|
27
|
+
* otherwise wait for every `finished` promise. A monotonically increasing
|
|
28
|
+
* token guards the wait, so a `present=true` that arrives mid-exit cancels
|
|
29
|
+
* the pending unmount and the enter keyframe simply replays.
|
|
30
|
+
*
|
|
31
|
+
* `animation-fill-mode: both` on the CSS side is load-bearing — without it
|
|
32
|
+
* the exit keyframe snaps back to its start frame (fully visible) for the
|
|
33
|
+
* one frame between `animationend` and React's commit.
|
|
34
|
+
*
|
|
35
|
+
* Reduced motion: the base layer forces `animation-duration: 0.01ms`, the
|
|
36
|
+
* `finished` promise still resolves, and the child unmounts effectively
|
|
37
|
+
* immediately. No per-component guard needed.
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
const PRESENCE_TRANSITIONS = [
|
|
41
|
+
"fade",
|
|
42
|
+
"scale",
|
|
43
|
+
"slide-up",
|
|
44
|
+
"slide-down",
|
|
45
|
+
"slide-left",
|
|
46
|
+
"slide-right",
|
|
47
|
+
] as const
|
|
48
|
+
|
|
49
|
+
type PresenceTransition = (typeof PRESENCE_TRANSITIONS)[number]
|
|
50
|
+
|
|
51
|
+
type Phase = "mounted" | "exiting" | "unmounted"
|
|
52
|
+
|
|
53
|
+
interface PresenceProps extends React.ComponentPropsWithoutRef<"div"> {
|
|
54
|
+
/** Whether the child should be shown. Flipping to false plays the exit
|
|
55
|
+
* animation before the child leaves the DOM. */
|
|
56
|
+
present: boolean
|
|
57
|
+
/**
|
|
58
|
+
* Which enter/exit keyframe pair to play. Written to `data-transition`;
|
|
59
|
+
* the keyframes live in the generated styles.css. Default: fade.
|
|
60
|
+
*/
|
|
61
|
+
transition?: PresenceTransition
|
|
62
|
+
/**
|
|
63
|
+
* When true (default) the child is removed from the DOM after the exit
|
|
64
|
+
* animation. When false it stays mounted with the `hidden` attribute —
|
|
65
|
+
* useful for expensive subtrees (iframes, charts) that should keep their
|
|
66
|
+
* state between shows.
|
|
67
|
+
*/
|
|
68
|
+
unmountOnExit?: boolean
|
|
69
|
+
/**
|
|
70
|
+
* When true, renders as the single child element instead of a wrapper div —
|
|
71
|
+
* `data-slot`, `data-state`, `data-transition`, and `hidden` are merged
|
|
72
|
+
* onto the child (via Radix Slot). The child must forward its ref and must
|
|
73
|
+
* not set its own `animation` shorthand, which would override the keyframe.
|
|
74
|
+
*/
|
|
75
|
+
asChild?: boolean
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const useIsoLayoutEffect =
|
|
79
|
+
typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect
|
|
80
|
+
|
|
81
|
+
const Presence = React.forwardRef<HTMLDivElement, PresenceProps>(
|
|
82
|
+
(
|
|
83
|
+
{
|
|
84
|
+
present,
|
|
85
|
+
transition = "fade",
|
|
86
|
+
unmountOnExit = true,
|
|
87
|
+
asChild = false,
|
|
88
|
+
className,
|
|
89
|
+
children,
|
|
90
|
+
...props
|
|
91
|
+
},
|
|
92
|
+
forwardedRef
|
|
93
|
+
) => {
|
|
94
|
+
const nodeRef = React.useRef<HTMLDivElement | null>(null)
|
|
95
|
+
const [phase, setPhase] = React.useState<Phase>(
|
|
96
|
+
present ? "mounted" : "unmounted"
|
|
97
|
+
)
|
|
98
|
+
// Bumped on every present flip; a pending exit wait only commits its
|
|
99
|
+
// unmount if the token it captured is still current.
|
|
100
|
+
const tokenRef = React.useRef(0)
|
|
101
|
+
|
|
102
|
+
const setRefs = React.useCallback(
|
|
103
|
+
(node: HTMLDivElement | null) => {
|
|
104
|
+
nodeRef.current = node
|
|
105
|
+
if (typeof forwardedRef === "function") forwardedRef(node)
|
|
106
|
+
else if (forwardedRef) forwardedRef.current = node
|
|
107
|
+
},
|
|
108
|
+
[forwardedRef]
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
// Layout effect so the data-state flip lands before the browser paints
|
|
112
|
+
// the frame in which the exit keyframe must already be running.
|
|
113
|
+
useIsoLayoutEffect(() => {
|
|
114
|
+
tokenRef.current += 1
|
|
115
|
+
if (present) {
|
|
116
|
+
setPhase("mounted")
|
|
117
|
+
} else {
|
|
118
|
+
setPhase((p) => (p === "unmounted" ? p : "exiting"))
|
|
119
|
+
}
|
|
120
|
+
}, [present])
|
|
121
|
+
|
|
122
|
+
React.useEffect(() => {
|
|
123
|
+
if (phase !== "exiting") return
|
|
124
|
+
const token = tokenRef.current
|
|
125
|
+
const finish = () => {
|
|
126
|
+
if (tokenRef.current === token) setPhase("unmounted")
|
|
127
|
+
}
|
|
128
|
+
const el = nodeRef.current
|
|
129
|
+
if (!el || typeof el.getAnimations !== "function") {
|
|
130
|
+
finish()
|
|
131
|
+
return
|
|
132
|
+
}
|
|
133
|
+
const animations = el.getAnimations()
|
|
134
|
+
if (animations.length === 0) {
|
|
135
|
+
finish()
|
|
136
|
+
return
|
|
137
|
+
}
|
|
138
|
+
let cancelled = false
|
|
139
|
+
Promise.allSettled(animations.map((a) => a.finished)).then(() => {
|
|
140
|
+
if (!cancelled) finish()
|
|
141
|
+
})
|
|
142
|
+
return () => {
|
|
143
|
+
cancelled = true
|
|
144
|
+
}
|
|
145
|
+
}, [phase])
|
|
146
|
+
|
|
147
|
+
const isOpen = phase === "mounted"
|
|
148
|
+
const isGone = phase === "unmounted"
|
|
149
|
+
|
|
150
|
+
if (isGone && unmountOnExit) return null
|
|
151
|
+
|
|
152
|
+
const Comp = asChild ? Slot : "div"
|
|
153
|
+
|
|
154
|
+
return (
|
|
155
|
+
<Comp
|
|
156
|
+
ref={setRefs}
|
|
157
|
+
data-slot="presence"
|
|
158
|
+
data-state={isOpen ? "open" : "closed"}
|
|
159
|
+
data-transition={transition}
|
|
160
|
+
hidden={isGone || undefined}
|
|
161
|
+
className={cn(className)}
|
|
162
|
+
{...props}
|
|
163
|
+
>
|
|
164
|
+
{children}
|
|
165
|
+
</Comp>
|
|
166
|
+
)
|
|
167
|
+
}
|
|
168
|
+
)
|
|
169
|
+
Presence.displayName = "Presence"
|
|
170
|
+
|
|
171
|
+
export { Presence, PRESENCE_TRANSITIONS }
|
|
172
|
+
export type { PresenceProps, PresenceTransition }
|
|
@@ -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 }
|
|
@@ -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
|