@behavioral-agentic-design-system/core 0.40.0 → 0.45.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 +41 -9
- package/scripts/a11y-lint.cjs +66 -3
- package/scripts/check-spec.mjs +73 -0
- package/scripts/deal-directions.mjs +186 -0
- package/scripts/health-report.mjs +23 -3
- package/scripts/lib/counted-lint.mjs +100 -0
- package/scripts/lib/marketing-library.mjs +129 -0
- package/scripts/lib/on-system.mjs +249 -0
- package/scripts/lib/random.mjs +22 -0
- package/scripts/lib/spec-parse.d.mts +25 -0
- package/scripts/lib/spec-parse.mjs +188 -0
- package/scripts/lib/tells.mjs +128 -0
- package/scripts/lint-css-vars.mjs +175 -0
- package/scripts/lint-html-nesting.mjs +116 -0
- package/scripts/lint-live-ctas.mjs +138 -0
- package/scripts/lint-slop.mjs +25 -1
- package/scripts/on-system-score.mjs +95 -0
- package/scripts/slop-rules.mjs +1 -1
- package/scripts/source-images.mjs +180 -0
- package/scripts/ux-tells.mjs +78 -0
- package/scripts/wcag-audit.cjs +72 -5
- package/src/api/brand-colors.ts +3 -0
- package/src/api/brand-fonts.ts +3 -0
- package/src/api/brand-mark.ts +6 -1
- package/src/api/dev-write-guard.ts +53 -0
- package/src/api/layers/file.ts +7 -4
- package/src/api/onboarding-extract.ts +85 -30
- package/src/api/onboarding.ts +3 -0
- package/src/api/save-tokens.ts +3 -0
- package/src/app/layout.tsx +6 -0
- package/src/app/opengraph-image.tsx +5 -0
- package/src/app/preview/canvas/page.tsx +12 -0
- package/src/app/preview/components/page.tsx +2003 -2
- package/src/app/preview/studio/blank-screen.tsx +143 -0
- package/src/app/preview/studio/page.tsx +7 -0
- package/src/app/shell.tsx +168 -49
- package/src/components/Foundations/Motion.stories.tsx +1 -0
- package/src/components/canvas/canvas.tsx +458 -0
- package/src/components/canvas/layout.ts +72 -0
- package/src/components/canvas/parse-canvas.ts +32 -0
- package/src/components/canvas/sample.ts +90 -0
- package/src/components/canvas/spec-frame.tsx +43 -0
- package/src/components/canvas/types.ts +33 -0
- package/src/components/customize/design-controls.tsx +16 -1
- package/src/components/docs-tab.tsx +7 -7
- package/src/components/home-recent.tsx +73 -0
- package/src/components/home-tab.tsx +13 -13
- package/src/components/layers-tab.tsx +2 -2
- package/src/components/markdown-preview.tsx +60 -12
- package/src/components/studio/control-panel.tsx +299 -0
- package/src/components/studio/control-row.tsx +77 -0
- package/src/components/studio/preview-frame.tsx +165 -0
- package/src/components/studio/preview-pane.tsx +247 -0
- package/src/components/studio/studio.tsx +158 -0
- package/src/components/studio/types.ts +218 -0
- package/src/components/studio/use-studio-tweaks.ts +128 -0
- package/src/components/studio/use-studio-view-state.ts +72 -0
- package/src/components/ui/anchor.tsx +37 -3
- package/src/components/ui/attachment.tsx +211 -0
- package/src/components/ui/autocomplete.tsx +1 -1
- package/src/components/ui/chart.tsx +3 -0
- package/src/components/ui/code.tsx +1 -1
- package/src/components/ui/color-picker.tsx +21 -5
- package/src/components/ui/data-table.tsx +64 -3
- package/src/components/ui/date-field.tsx +1 -1
- package/src/components/ui/editable.tsx +1 -0
- package/src/components/ui/faceted-filter.tsx +187 -0
- package/src/components/ui/gauge-chart.tsx +252 -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/inline-edit.tsx +175 -0
- package/src/components/ui/json-input.tsx +351 -0
- package/src/components/ui/month-picker.tsx +1 -1
- package/src/components/ui/navigation-progress.tsx +281 -0
- package/src/components/ui/number-input.tsx +27 -7
- package/src/components/ui/otp-input.tsx +208 -0
- package/src/components/ui/password-input.tsx +2 -1
- package/src/components/ui/password-strength-input.tsx +288 -0
- package/src/components/ui/phone-input.tsx +274 -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/search-input.tsx +1 -1
- 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/tags-input.tsx +1 -1
- package/src/components/ui/timer.tsx +1 -0
- package/src/components/ui/tour.tsx +1 -3
- package/src/components/ui/transfer-list.tsx +13 -26
- package/src/components/ui/week-picker.tsx +1 -1
- package/src/components/ui/year-picker.tsx +1 -1
- package/src/data/component-added.json +958 -0
- package/src/data/docs-index.ts +36 -0
- package/src/hooks/use-global-mono-font.ts +1 -0
- package/src/hooks/use-global-primary-color.ts +1 -0
- package/src/hooks/use-global-sans-font.ts +1 -0
- package/src/hooks/use-global-serif-font.ts +1 -0
- package/src/hooks/use-global-type-scale.ts +17 -2
- package/src/hooks/use-mobile.ts +2 -0
- package/src/hooks/use-sync-type-scale.ts +7 -11
- package/src/lib/clamp.ts +2 -0
- package/src/lib/spec-parse.ts +4 -0
- package/src/lib/svg-sanitize.ts +19 -9
- package/src/stories/Anchor.stories.tsx +54 -0
- package/src/stories/Attachment.stories.tsx +189 -0
- package/src/stories/Autocomplete.stories.tsx +2 -0
- package/src/stories/Canvas.stories.tsx +45 -0
- package/src/stories/Chart.stories.tsx +4 -0
- package/src/stories/Code.stories.tsx +1 -0
- package/src/stories/ColorPicker.stories.tsx +41 -0
- package/src/stories/DataTable.stories.tsx +30 -0
- package/src/stories/DateField.stories.tsx +21 -0
- package/src/stories/Editable.stories.tsx +2 -0
- package/src/stories/FacetedFilter.stories.tsx +197 -0
- package/src/stories/GaugeChart.stories.tsx +169 -0
- package/src/stories/Heatmap.stories.tsx +172 -0
- package/src/stories/InfiniteScroll.stories.tsx +225 -0
- package/src/stories/InlineEdit.stories.tsx +106 -0
- package/src/stories/JsonInput.stories.tsx +212 -0
- package/src/stories/MarkdownPreview.stories.tsx +53 -0
- package/src/stories/MonthPicker.stories.tsx +21 -0
- package/src/stories/NavigationProgress.stories.tsx +159 -0
- package/src/stories/NumberInput.stories.tsx +26 -0
- package/src/stories/OtpInput.stories.tsx +110 -0
- package/src/stories/PasswordInput.stories.tsx +4 -0
- package/src/stories/PasswordStrengthInput.stories.tsx +211 -0
- package/src/stories/PhoneInput.stories.tsx +127 -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/SearchInput.stories.tsx +2 -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/TagsInput.stories.tsx +2 -0
- package/src/stories/Timer.stories.tsx +26 -0
- package/src/stories/Tour.stories.tsx +1 -0
- package/src/stories/TransferList.stories.tsx +4 -0
- package/src/stories/WeekPicker.stories.tsx +21 -0
- package/src/stories/YearPicker.stories.tsx +21 -0
- package/src/stories/story-groups.json +33 -16
- package/src/tokens.json +82 -54
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
|
|
5
|
+
import { cn } from "../../lib/utils"
|
|
6
|
+
import { Button } from "./button"
|
|
7
|
+
import { CloseButton } from "./close-button"
|
|
8
|
+
import { Portal } from "./portal"
|
|
9
|
+
import { Presence } from "./presence"
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Snackbar — ONE transient feedback message at a time, anchored to the bottom
|
|
13
|
+
* of the screen, that auto-dismisses. "Copied to clipboard", "Settings saved",
|
|
14
|
+
* "Moved to Archive · Undo". The next message waits in a queue behind the
|
|
15
|
+
* current one; nothing ever stacks.
|
|
16
|
+
*
|
|
17
|
+
* Peer parity: MUI Snackbar (single message, bottom-center default, optional
|
|
18
|
+
* action, auto-hide), Ant Design `message` API (imperative `message.success()`),
|
|
19
|
+
* Chakra UI `useToast` (imperative hook, pre-v3).
|
|
20
|
+
*
|
|
21
|
+
* How this differs from Sonner (`<Toaster />` + `toast()`)
|
|
22
|
+
* ---------------------------------------------------------
|
|
23
|
+
* Sonner is a *stacked* queue for async events — several toasts visible at
|
|
24
|
+
* once, corner-anchored, each independently dismissable. Reach for it when the
|
|
25
|
+
* user needs to see multiple pending notifications (upload finished, new
|
|
26
|
+
* message, error). Snackbar is for lightweight confirmations of something the
|
|
27
|
+
* user just did, where a second message should *replace* the first, not pile
|
|
28
|
+
* on top of it. Same visual family (popover surface, hairline border,
|
|
29
|
+
* shadow-lg) so the two read as one system.
|
|
30
|
+
*
|
|
31
|
+
* Anatomy
|
|
32
|
+
* -------
|
|
33
|
+
* [icon?] message [action button?] [dismiss ×?]
|
|
34
|
+
*
|
|
35
|
+
* Accessibility
|
|
36
|
+
* -------------
|
|
37
|
+
* - The outlet is a persistently mounted `role="status"` / `aria-live="polite"`
|
|
38
|
+
* / `aria-atomic="true"` region. Mounting the live region BEFORE the first
|
|
39
|
+
* message lands is what makes screen readers reliably announce it; a live
|
|
40
|
+
* region created in the same tick as its content is often skipped.
|
|
41
|
+
* - Non-modal: focus never moves. The action and dismiss buttons are reachable
|
|
42
|
+
* in the normal tab order (they sit at the end of `<body>`).
|
|
43
|
+
* - The auto-hide timer pauses while the pointer is over the snackbar or focus
|
|
44
|
+
* is inside it, and resumes with the remaining time (SC 2.2.1 Timing
|
|
45
|
+
* Adjustable). Escape dismisses only when focus is inside — the snackbar
|
|
46
|
+
* never hijacks a global key.
|
|
47
|
+
* - A sticky message (`duration: Infinity`) is always dismissible: the × shows
|
|
48
|
+
* by default when there is no finite timer, so no message can trap itself.
|
|
49
|
+
* - Dismiss is `CloseButton size="icon-xl"` (44px) and the action is a Button
|
|
50
|
+
* with `min-h-11` — both are flow-changing CTAs and meet the 44×44 floor.
|
|
51
|
+
*
|
|
52
|
+
* Motion
|
|
53
|
+
* ------
|
|
54
|
+
* Enter/exit ride `<Presence transition="slide-up">` — the existing
|
|
55
|
+
* `presence-slide-up-in/out` keyframes (`--duration-base` / `--ease-out`,
|
|
56
|
+
* generated from build-tokens.cjs; spec §4 pattern a). The exit frame is what
|
|
57
|
+
* lets the queue hand off cleanly: the next message mounts only after the
|
|
58
|
+
* previous one has actually left the DOM (see `QueueAdvance` below). Reduced
|
|
59
|
+
* motion collapses both keyframes via the base layer.
|
|
60
|
+
*/
|
|
61
|
+
|
|
62
|
+
const SNACKBAR_ANCHORS = ["bottom-center", "bottom-left", "bottom-right"] as const
|
|
63
|
+
|
|
64
|
+
type SnackbarAnchor = (typeof SNACKBAR_ANCHORS)[number]
|
|
65
|
+
|
|
66
|
+
type SnackbarDismissReason = "timeout" | "action" | "dismiss" | "escape" | "programmatic"
|
|
67
|
+
|
|
68
|
+
interface SnackbarAction {
|
|
69
|
+
/** Visible label — a verb ("Undo", "View", "Retry"), not "OK". */
|
|
70
|
+
label: string
|
|
71
|
+
/** Fires on click; the snackbar dismisses right after. */
|
|
72
|
+
onClick: () => void
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
interface SnackbarOptions {
|
|
76
|
+
/** The message. Keep it to one line of plain text; this is a confirmation, not an alert. */
|
|
77
|
+
message: React.ReactNode
|
|
78
|
+
/**
|
|
79
|
+
* Auto-hide delay in ms. Defaults to the provider's `duration` (4000).
|
|
80
|
+
* Pass `Infinity` for a sticky message — the dismiss × then shows by default.
|
|
81
|
+
*/
|
|
82
|
+
duration?: number
|
|
83
|
+
/** Optional single action, rendered as a ghost Button after the message. */
|
|
84
|
+
action?: SnackbarAction
|
|
85
|
+
/** Optional leading icon (a Lucide icon at strokeWidth 1.5). Decorative — the message carries the meaning. */
|
|
86
|
+
icon?: React.ReactNode
|
|
87
|
+
/**
|
|
88
|
+
* Show the dismiss ×. Defaults to `true` when `duration` is not finite
|
|
89
|
+
* (a sticky message must be dismissible), `false` otherwise.
|
|
90
|
+
*/
|
|
91
|
+
dismissible?: boolean
|
|
92
|
+
/** Per-message anchor override. Defaults to the provider's `anchor`. */
|
|
93
|
+
anchor?: SnackbarAnchor
|
|
94
|
+
/** Called once when the message leaves, with why. */
|
|
95
|
+
onDismiss?: (reason: SnackbarDismissReason) => void
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
interface SnackbarItem extends SnackbarOptions {
|
|
99
|
+
id: number
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
interface SnackbarContextValue {
|
|
103
|
+
/** Enqueue a message. Shows immediately if nothing is visible. Returns its id. */
|
|
104
|
+
show: (options: SnackbarOptions | string) => number
|
|
105
|
+
/**
|
|
106
|
+
* Dismiss the current message (no id), a specific one (its id — removed
|
|
107
|
+
* from the queue if it has not shown yet), or everything (`"all"`).
|
|
108
|
+
*/
|
|
109
|
+
dismiss: (id?: number | "all") => void
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const SnackbarContext = React.createContext<SnackbarContextValue | null>(null)
|
|
113
|
+
|
|
114
|
+
function useSnackbar(): SnackbarContextValue {
|
|
115
|
+
const ctx = React.useContext(SnackbarContext)
|
|
116
|
+
if (!ctx) {
|
|
117
|
+
throw new Error("useSnackbar must be used within a <SnackbarProvider>.")
|
|
118
|
+
}
|
|
119
|
+
return ctx
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
interface SnackbarProviderProps {
|
|
123
|
+
children?: React.ReactNode
|
|
124
|
+
/** Where the outlet sits. Default "bottom-center" (MUI's default). */
|
|
125
|
+
anchor?: SnackbarAnchor
|
|
126
|
+
/** Default auto-hide delay in ms for messages that don't set their own. Default 4000. */
|
|
127
|
+
duration?: number
|
|
128
|
+
/** Portal target. Defaults to the provider's own `document.body`. */
|
|
129
|
+
container?: Element | null
|
|
130
|
+
/** Merged onto the snackbar surface (not the outlet). */
|
|
131
|
+
className?: string
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const ANCHOR_CLASS: Record<SnackbarAnchor, string> = {
|
|
135
|
+
"bottom-center": "justify-center",
|
|
136
|
+
"bottom-left": "justify-start",
|
|
137
|
+
"bottom-right": "justify-end",
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function SnackbarProvider({
|
|
141
|
+
children,
|
|
142
|
+
anchor = "bottom-center",
|
|
143
|
+
duration = 4000,
|
|
144
|
+
container,
|
|
145
|
+
className,
|
|
146
|
+
}: SnackbarProviderProps) {
|
|
147
|
+
const [queue, setQueue] = React.useState<SnackbarItem[]>([])
|
|
148
|
+
const [current, setCurrent] = React.useState<SnackbarItem | null>(null)
|
|
149
|
+
const [open, setOpen] = React.useState(false)
|
|
150
|
+
// "open" → visible; "exiting" → Presence is playing the exit keyframe and
|
|
151
|
+
// QueueAdvance will promote the next item once the node is really gone.
|
|
152
|
+
const phaseRef = React.useRef<"idle" | "open" | "exiting">("idle")
|
|
153
|
+
const idRef = React.useRef(0)
|
|
154
|
+
const currentRef = React.useRef<SnackbarItem | null>(null)
|
|
155
|
+
currentRef.current = current
|
|
156
|
+
|
|
157
|
+
// Promote the head of the queue when nothing is showing.
|
|
158
|
+
React.useEffect(() => {
|
|
159
|
+
if (current || phaseRef.current !== "idle") return
|
|
160
|
+
if (queue.length === 0) return
|
|
161
|
+
const [next, ...rest] = queue
|
|
162
|
+
phaseRef.current = "open"
|
|
163
|
+
setQueue(rest)
|
|
164
|
+
setCurrent(next)
|
|
165
|
+
setOpen(true)
|
|
166
|
+
}, [queue, current])
|
|
167
|
+
|
|
168
|
+
const close = React.useCallback((reason: SnackbarDismissReason) => {
|
|
169
|
+
if (phaseRef.current !== "open") return
|
|
170
|
+
phaseRef.current = "exiting"
|
|
171
|
+
currentRef.current?.onDismiss?.(reason)
|
|
172
|
+
setOpen(false)
|
|
173
|
+
}, [])
|
|
174
|
+
|
|
175
|
+
// Fires from QueueAdvance's cleanup once Presence has unmounted the message.
|
|
176
|
+
const handleExited = React.useCallback(() => {
|
|
177
|
+
if (phaseRef.current !== "exiting") return
|
|
178
|
+
phaseRef.current = "idle"
|
|
179
|
+
setCurrent(null)
|
|
180
|
+
}, [])
|
|
181
|
+
|
|
182
|
+
const show = React.useCallback((options: SnackbarOptions | string) => {
|
|
183
|
+
const id = ++idRef.current
|
|
184
|
+
const item: SnackbarItem =
|
|
185
|
+
typeof options === "string" ? { id, message: options } : { id, ...options }
|
|
186
|
+
setQueue((q) => [...q, item])
|
|
187
|
+
return id
|
|
188
|
+
}, [])
|
|
189
|
+
|
|
190
|
+
const dismiss = React.useCallback(
|
|
191
|
+
(id?: number | "all") => {
|
|
192
|
+
if (id === "all") {
|
|
193
|
+
setQueue([])
|
|
194
|
+
close("programmatic")
|
|
195
|
+
return
|
|
196
|
+
}
|
|
197
|
+
if (id === undefined || currentRef.current?.id === id) {
|
|
198
|
+
close("programmatic")
|
|
199
|
+
return
|
|
200
|
+
}
|
|
201
|
+
setQueue((q) => q.filter((item) => item.id !== id))
|
|
202
|
+
},
|
|
203
|
+
[close]
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
const value = React.useMemo(() => ({ show, dismiss }), [show, dismiss])
|
|
207
|
+
|
|
208
|
+
const resolvedAnchor = current?.anchor ?? anchor
|
|
209
|
+
const resolvedDuration = current?.duration ?? duration
|
|
210
|
+
|
|
211
|
+
return (
|
|
212
|
+
<SnackbarContext.Provider value={value}>
|
|
213
|
+
{children}
|
|
214
|
+
<Portal container={container}>
|
|
215
|
+
{/* Persistently mounted live region — see the a11y note in the file header.
|
|
216
|
+
pointer-events-none so the empty outlet never blocks clicks beneath it;
|
|
217
|
+
the surface re-enables them. Bottom padding respects the safe area. */}
|
|
218
|
+
<div
|
|
219
|
+
data-slot="snackbar-outlet"
|
|
220
|
+
data-anchor={resolvedAnchor}
|
|
221
|
+
role="status"
|
|
222
|
+
aria-live="polite"
|
|
223
|
+
aria-atomic="true"
|
|
224
|
+
className={cn(
|
|
225
|
+
"pointer-events-none fixed inset-x-0 bottom-0 z-50 flex px-4 pt-4",
|
|
226
|
+
"pb-[max(1rem,env(safe-area-inset-bottom))]",
|
|
227
|
+
ANCHOR_CLASS[resolvedAnchor]
|
|
228
|
+
)}
|
|
229
|
+
>
|
|
230
|
+
<Presence present={open} transition="slide-up" className="pointer-events-auto">
|
|
231
|
+
{current ? (
|
|
232
|
+
<>
|
|
233
|
+
<SnackbarSurface
|
|
234
|
+
key={current.id}
|
|
235
|
+
item={current}
|
|
236
|
+
duration={resolvedDuration}
|
|
237
|
+
className={className}
|
|
238
|
+
onClose={close}
|
|
239
|
+
/>
|
|
240
|
+
<QueueAdvance onExited={handleExited} />
|
|
241
|
+
</>
|
|
242
|
+
) : null}
|
|
243
|
+
</Presence>
|
|
244
|
+
</div>
|
|
245
|
+
</Portal>
|
|
246
|
+
</SnackbarContext.Provider>
|
|
247
|
+
)
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Zero-render sentinel whose effect cleanup fires when Presence removes the
|
|
252
|
+
* message from the DOM — i.e. after the exit keyframe has finished (or
|
|
253
|
+
* instantly under reduced motion / a consumer's `animation: none`). The
|
|
254
|
+
* provider only promotes the next queued message from here, so two messages
|
|
255
|
+
* never overlap. `onExited` guards on phase === "exiting", which is what makes
|
|
256
|
+
* React StrictMode's simulated unmount in dev a no-op.
|
|
257
|
+
*/
|
|
258
|
+
function QueueAdvance({ onExited }: { onExited: () => void }) {
|
|
259
|
+
React.useEffect(() => onExited, [onExited])
|
|
260
|
+
return null
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
interface SnackbarSurfaceProps {
|
|
264
|
+
item: SnackbarItem
|
|
265
|
+
duration: number
|
|
266
|
+
className?: string
|
|
267
|
+
onClose: (reason: SnackbarDismissReason) => void
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function SnackbarSurface({ item, duration, className, onClose }: SnackbarSurfaceProps) {
|
|
271
|
+
const finite = Number.isFinite(duration) && duration > 0
|
|
272
|
+
const dismissible = item.dismissible ?? !finite
|
|
273
|
+
const hasControls = Boolean(item.action) || dismissible
|
|
274
|
+
|
|
275
|
+
// Pausable timer: remaining time survives hover / focus pauses.
|
|
276
|
+
const timerRef = React.useRef<number | null>(null)
|
|
277
|
+
const remainingRef = React.useRef(duration)
|
|
278
|
+
const startedAtRef = React.useRef(0)
|
|
279
|
+
const onCloseRef = React.useRef(onClose)
|
|
280
|
+
onCloseRef.current = onClose
|
|
281
|
+
|
|
282
|
+
const stop = React.useCallback(() => {
|
|
283
|
+
if (timerRef.current !== null) {
|
|
284
|
+
window.clearTimeout(timerRef.current)
|
|
285
|
+
timerRef.current = null
|
|
286
|
+
}
|
|
287
|
+
}, [])
|
|
288
|
+
|
|
289
|
+
const start = React.useCallback(() => {
|
|
290
|
+
if (!finite || timerRef.current !== null) return
|
|
291
|
+
startedAtRef.current = Date.now()
|
|
292
|
+
timerRef.current = window.setTimeout(() => {
|
|
293
|
+
timerRef.current = null
|
|
294
|
+
onCloseRef.current("timeout")
|
|
295
|
+
}, remainingRef.current)
|
|
296
|
+
}, [finite])
|
|
297
|
+
|
|
298
|
+
const pause = React.useCallback(() => {
|
|
299
|
+
if (timerRef.current === null) return
|
|
300
|
+
stop()
|
|
301
|
+
remainingRef.current = Math.max(0, remainingRef.current - (Date.now() - startedAtRef.current))
|
|
302
|
+
}, [stop])
|
|
303
|
+
|
|
304
|
+
React.useEffect(() => {
|
|
305
|
+
start()
|
|
306
|
+
return stop
|
|
307
|
+
}, [start, stop])
|
|
308
|
+
|
|
309
|
+
// Hover + focus-within pause; resume on leave / blur-out. A focus-within
|
|
310
|
+
// pause only resumes once focus has left the whole surface, so tabbing from
|
|
311
|
+
// the action to the dismiss × does not restart the clock.
|
|
312
|
+
const handleBlur = (e: React.FocusEvent<HTMLDivElement>) => {
|
|
313
|
+
if (e.currentTarget.contains(e.relatedTarget as Node | null)) return
|
|
314
|
+
start()
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const handleKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
|
|
318
|
+
if (e.key === "Escape") {
|
|
319
|
+
e.stopPropagation()
|
|
320
|
+
onClose("escape")
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return (
|
|
325
|
+
<div
|
|
326
|
+
data-slot="snackbar"
|
|
327
|
+
data-anchor={item.anchor}
|
|
328
|
+
onMouseEnter={pause}
|
|
329
|
+
onMouseLeave={start}
|
|
330
|
+
onFocus={pause}
|
|
331
|
+
onBlur={handleBlur}
|
|
332
|
+
onKeyDown={handleKeyDown}
|
|
333
|
+
className={cn(
|
|
334
|
+
// Popover surface + hairline border + shadow-lg: the Sonner / Alert
|
|
335
|
+
// feedback family, so the two toast shapes read as one system.
|
|
336
|
+
"flex min-h-12 w-full max-w-xl items-center gap-2 overflow-hidden rounded-lg border border-foreground/10 bg-popover py-2 pl-4 text-popover-foreground shadow-lg sm:w-auto sm:min-w-72",
|
|
337
|
+
hasControls ? "pr-2" : "pr-4",
|
|
338
|
+
className
|
|
339
|
+
)}
|
|
340
|
+
>
|
|
341
|
+
{item.icon ? (
|
|
342
|
+
<span
|
|
343
|
+
data-slot="snackbar-icon"
|
|
344
|
+
aria-hidden="true"
|
|
345
|
+
className="flex shrink-0 items-center text-icon-default [&>svg]:size-5"
|
|
346
|
+
>
|
|
347
|
+
{item.icon}
|
|
348
|
+
</span>
|
|
349
|
+
) : null}
|
|
350
|
+
{/* min-w-0 + overflow-wrap:anywhere: long single-word identifiers
|
|
351
|
+
(a token name, a URL) wrap instead of pushing the controls out. */}
|
|
352
|
+
<div
|
|
353
|
+
data-slot="snackbar-message"
|
|
354
|
+
className="min-w-0 flex-1 py-1 text-sm font-medium leading-snug [overflow-wrap:anywhere]"
|
|
355
|
+
>
|
|
356
|
+
{item.message}
|
|
357
|
+
</div>
|
|
358
|
+
{item.action ? (
|
|
359
|
+
<Button
|
|
360
|
+
data-slot="snackbar-action"
|
|
361
|
+
type="button"
|
|
362
|
+
variant="ghost"
|
|
363
|
+
size="default"
|
|
364
|
+
className="min-h-11 shrink-0 px-3 font-semibold text-primary hover:text-primary"
|
|
365
|
+
onClick={() => {
|
|
366
|
+
item.action?.onClick()
|
|
367
|
+
onClose("action")
|
|
368
|
+
}}
|
|
369
|
+
>
|
|
370
|
+
{item.action.label}
|
|
371
|
+
</Button>
|
|
372
|
+
) : null}
|
|
373
|
+
{dismissible ? (
|
|
374
|
+
<CloseButton
|
|
375
|
+
data-slot="snackbar-dismiss"
|
|
376
|
+
size="icon-xl"
|
|
377
|
+
aria-label="Dismiss"
|
|
378
|
+
onClick={() => onClose("dismiss")}
|
|
379
|
+
/>
|
|
380
|
+
) : null}
|
|
381
|
+
</div>
|
|
382
|
+
)
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
export { SnackbarProvider, useSnackbar, SNACKBAR_ANCHORS }
|
|
386
|
+
export type {
|
|
387
|
+
SnackbarAction,
|
|
388
|
+
SnackbarAnchor,
|
|
389
|
+
SnackbarContextValue,
|
|
390
|
+
SnackbarDismissReason,
|
|
391
|
+
SnackbarOptions,
|
|
392
|
+
SnackbarProviderProps,
|
|
393
|
+
}
|