@designa8/vibedesign-v6 2.1.2 → 2.2.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.
@@ -0,0 +1,336 @@
1
+ import * as React from "react";
2
+ import { Button } from "../Button";
3
+ import { Icon } from "../Icon";
4
+
5
+ export type ToastTone = "success" | "error" | "info" | "warning";
6
+
7
+ export interface ToastStackItem {
8
+ /** Stable unique id — used for dismissal and enter/exit tracking. */
9
+ id: string;
10
+ tone?: ToastTone;
11
+ title: string;
12
+ description?: string;
13
+ /** Inline text action, e.g. "View details". */
14
+ actionLabel?: string;
15
+ onAction?: () => void;
16
+ /** Auto-dismiss in ms. 0 = persist. Defaults to 6000; error toasts persist. */
17
+ duration?: number;
18
+ }
19
+
20
+ export interface ToastStackProps
21
+ extends Omit<React.ComponentPropsWithoutRef<"div">, "children"> {
22
+ /** Rendered top to bottom. Items past `max` collapse into peeking slabs. */
23
+ toasts: ToastStackItem[];
24
+ onDismiss: (id: string) => void;
25
+ onDismissAll?: () => void;
26
+ /** Legible cards before the rest collapse. Defaults to 3. */
27
+ max?: number;
28
+ dismissAllLabel?: string;
29
+ /** Accessible name for the notification region. */
30
+ label?: string;
31
+ }
32
+
33
+ const TONE: Record<
34
+ ToastTone,
35
+ { icon: string; tile: string; glyph: string; rail: string; border: string }
36
+ > = {
37
+ success: {
38
+ icon: "check-checkmark",
39
+ tile: "bg-semantic-success-soft",
40
+ glyph: "text-semantic-success",
41
+ rail: "bg-semantic-success",
42
+ border: "border-hairline",
43
+ },
44
+ error: {
45
+ icon: "circle-alert",
46
+ tile: "bg-semantic-error-soft",
47
+ glyph: "text-semantic-error",
48
+ rail: "bg-semantic-error",
49
+ border: "border-semantic-error-soft",
50
+ },
51
+ info: {
52
+ icon: "info",
53
+ tile: "bg-semantic-info-soft",
54
+ glyph: "text-semantic-info",
55
+ rail: "bg-semantic-info",
56
+ border: "border-hairline",
57
+ },
58
+ warning: {
59
+ icon: "triangle-alert",
60
+ tile: "bg-semantic-warning-soft",
61
+ glyph: "text-semantic-warning",
62
+ rail: "bg-semantic-warning",
63
+ border: "border-hairline",
64
+ },
65
+ };
66
+
67
+ const CONTROL = "rounded-sm font-weight-body-sm transition duration-150 ease-out";
68
+
69
+ interface ToastCardProps {
70
+ toast: ToastStackItem;
71
+ state: "open" | "closed";
72
+ onDismiss: (id: string) => void;
73
+ }
74
+
75
+ const ToastCard = ({ toast, state, onDismiss }: ToastCardProps) => {
76
+ const tone = toast.tone ?? "info";
77
+ const theme = TONE[tone];
78
+ const duration = toast.duration ?? (tone === "error" ? 0 : 6000);
79
+
80
+ const [paused, setPaused] = React.useState(false);
81
+ const [entered, setEntered] = React.useState(false);
82
+ const remaining = React.useRef(duration);
83
+ const startedAt = React.useRef(0);
84
+ const dismissRef = React.useRef(onDismiss);
85
+ dismissRef.current = onDismiss;
86
+
87
+ React.useEffect(() => {
88
+ const frame = window.requestAnimationFrame(() => setEntered(true));
89
+ return () => window.cancelAnimationFrame(frame);
90
+ }, []);
91
+
92
+ const open = state === "open" && entered;
93
+
94
+ React.useEffect(() => {
95
+ if (duration <= 0 || paused || state !== "open") return;
96
+ startedAt.current = Date.now();
97
+ const timer = window.setTimeout(
98
+ () => dismissRef.current(toast.id),
99
+ Math.max(remaining.current, 0),
100
+ );
101
+ return () => {
102
+ window.clearTimeout(timer);
103
+ remaining.current = Math.max(
104
+ remaining.current - (Date.now() - startedAt.current),
105
+ 0,
106
+ );
107
+ };
108
+ }, [duration, paused, state, toast.id]);
109
+
110
+ return (
111
+ <div
112
+ data-state={open ? "open" : "closed"}
113
+ style={{ zIndex: open ? "var(--z-toast, 1300)" : undefined }}
114
+ className="relative grid w-full transition-[grid-template-rows,opacity,transform] duration-200 ease-out data-[state=open]:grid-rows-[1fr] data-[state=open]:translate-y-0 data-[state=open]:scale-100 data-[state=open]:opacity-100 data-[state=closed]:pointer-events-none data-[state=closed]:grid-rows-[0fr] data-[state=closed]:translate-y-2 data-[state=closed]:scale-95 data-[state=closed]:opacity-0"
115
+ >
116
+ <div
117
+ data-state={open ? "open" : "closed"}
118
+ className="min-h-0 data-[state=closed]:overflow-hidden"
119
+ >
120
+ <div
121
+ role={tone === "error" ? "alert" : "status"}
122
+ aria-live={tone === "error" ? "assertive" : "polite"}
123
+ aria-hidden={!open}
124
+ onMouseEnter={() => setPaused(true)}
125
+ onMouseLeave={() => setPaused(false)}
126
+ onFocus={() => setPaused(true)}
127
+ onBlur={() => setPaused(false)}
128
+ className={[
129
+ "relative flex items-start gap-md rounded-lg border bg-surface pb-lg pl-base pr-md pt-base shadow-card",
130
+ "transition-[box-shadow,border-color] duration-150 ease-out hover:shadow-md",
131
+ theme.border,
132
+ ].join(" ")}
133
+ >
134
+ <span
135
+ className={[
136
+ "mt-xxs flex size-10 shrink-0 items-center justify-center rounded-full",
137
+ theme.tile,
138
+ ].join(" ")}
139
+ >
140
+ <Icon
141
+ name={theme.icon}
142
+ size={20}
143
+ strokeWidth={2.5}
144
+ className={theme.glyph}
145
+ />
146
+ </span>
147
+
148
+ <div className="flex min-w-0 flex-1 flex-col gap-xxs">
149
+ <p className="text-body-md text-ink">{toast.title}</p>
150
+ {toast.description ? (
151
+ <p className="text-body-sm text-muted">{toast.description}</p>
152
+ ) : null}
153
+ </div>
154
+
155
+ <div className="flex shrink-0 items-center gap-sm">
156
+ {toast.actionLabel ? (
157
+ <Button
158
+ type="button"
159
+ variant="bare"
160
+ size="auto"
161
+ onClick={toast.onAction}
162
+ className={[
163
+ "px-sm py-xs text-body-sm text-primary-text hover:bg-primary-soft",
164
+ CONTROL,
165
+ ].join(" ")}
166
+ >
167
+ {toast.actionLabel}
168
+ </Button>
169
+ ) : null}
170
+
171
+ <Button
172
+ type="button"
173
+ variant="bare"
174
+ size="auto"
175
+ aria-label={`Dismiss ${toast.title}`}
176
+ onClick={() => onDismiss(toast.id)}
177
+ className={[
178
+ "flex size-8 items-center justify-center bg-surface-soft text-muted hover:bg-surface-strong hover:text-ink",
179
+ CONTROL,
180
+ ].join(" ")}
181
+ >
182
+ <Icon name="close" size={16} />
183
+ </Button>
184
+ </div>
185
+
186
+ {duration > 0 ? (
187
+ <span
188
+ aria-hidden="true"
189
+ className="absolute bottom-sm left-base right-base h-1 overflow-hidden rounded-full bg-hairline-soft"
190
+ >
191
+ <span
192
+ className={["block h-full w-full origin-left rounded-full", theme.rail].join(" ")}
193
+ style={{
194
+ animation: `toaststack-drain ${duration}ms linear forwards`,
195
+ animationPlayState: paused ? "paused" : "running",
196
+ }}
197
+ />
198
+ </span>
199
+ ) : null}
200
+ </div>
201
+ </div>
202
+ </div>
203
+ );
204
+ };
205
+
206
+ export const ToastStack = React.forwardRef<HTMLDivElement, ToastStackProps>(
207
+ function ToastStack(
208
+ {
209
+ toasts,
210
+ onDismiss,
211
+ onDismissAll,
212
+ max = 3,
213
+ dismissAllLabel = "Dismiss all",
214
+ label = "Notifications",
215
+ className,
216
+ ...rest
217
+ },
218
+ ref,
219
+ ) {
220
+ const [exiting, setExiting] = React.useState<ToastStackItem[]>([]);
221
+ const seen = React.useRef<ToastStackItem[]>(toasts);
222
+
223
+ // Toast cards stay mounted through their exit animation: a toast that
224
+ // leaves `toasts` is re-rendered closed for one motion beat, then dropped.
225
+ React.useEffect(() => {
226
+ const live = new Set(toasts.map((toast) => toast.id));
227
+ const gone = seen.current.filter((toast) => !live.has(toast.id));
228
+ seen.current = toasts;
229
+ if (gone.length > 0) setExiting((prev) => [...prev, ...gone]);
230
+ }, [toasts]);
231
+
232
+ React.useEffect(() => {
233
+ if (exiting.length === 0) return;
234
+ const timer = window.setTimeout(() => setExiting([]), 240);
235
+ return () => window.clearTimeout(timer);
236
+ }, [exiting]);
237
+
238
+ const dismiss = React.useCallback(
239
+ (id: string) => {
240
+ onDismiss(id);
241
+ },
242
+ [onDismiss],
243
+ );
244
+
245
+ React.useEffect(() => {
246
+ const onKeyDown = (event: KeyboardEvent) => {
247
+ if (event.key !== "Escape" || toasts.length === 0) return;
248
+ onDismiss(toasts[0].id);
249
+ };
250
+ document.addEventListener("keydown", onKeyDown);
251
+ return () => document.removeEventListener("keydown", onKeyDown);
252
+ }, [onDismiss, toasts]);
253
+
254
+ const legible = toasts.slice(0, max);
255
+ const overflow = Math.max(toasts.length - legible.length, 0);
256
+
257
+ return (
258
+ <div
259
+ ref={ref}
260
+ role="region"
261
+ aria-label={label}
262
+ className={["flex w-full max-w-3xl flex-col", className]
263
+ .filter(Boolean)
264
+ .join(" ")}
265
+ {...rest}
266
+ >
267
+ <style>{`@keyframes toaststack-drain{from{transform:scaleX(1)}to{transform:scaleX(0)}}`}</style>
268
+
269
+ <div className="ml-auto flex w-full max-w-md flex-col gap-md">
270
+ {onDismissAll && toasts.length > 1 ? (
271
+ <div className="flex justify-end">
272
+ <Button
273
+ type="button"
274
+ variant="bare"
275
+ size="auto"
276
+ onClick={onDismissAll}
277
+ className="rounded-sm px-xs py-xxs text-body-sm font-weight-body-sm text-muted transition duration-150 ease-out hover:text-ink"
278
+ >
279
+ {dismissAllLabel}
280
+ </Button>
281
+ </div>
282
+ ) : null}
283
+
284
+ {legible.length > 0 ? (
285
+ <div className="flex flex-col gap-md">
286
+ {legible.map((toast) => (
287
+ <ToastCard
288
+ key={toast.id}
289
+ toast={toast}
290
+ state="open"
291
+ onDismiss={dismiss}
292
+ />
293
+ ))}
294
+ </div>
295
+ ) : null}
296
+
297
+ {exiting.length > 0 ? (
298
+ <div className="flex flex-col gap-md">
299
+ {exiting.map((toast) => (
300
+ <ToastCard
301
+ key={toast.id}
302
+ toast={toast}
303
+ state="closed"
304
+ onDismiss={dismiss}
305
+ />
306
+ ))}
307
+ </div>
308
+ ) : null}
309
+
310
+ {overflow > 0 ? (
311
+ <>
312
+ <div
313
+ aria-hidden="true"
314
+ className="-mt-sm flex flex-col items-center gap-xs"
315
+ >
316
+ <div className="flex h-4 w-11/12 items-center rounded-lg border border-hairline-soft bg-surface px-sm">
317
+ <span className="h-1 w-full rounded-full bg-hairline" />
318
+ </div>
319
+ {overflow > 1 ? (
320
+ <div className="flex h-3 w-10/12 items-center rounded-lg border border-hairline-soft bg-surface-soft px-sm">
321
+ <span className="h-1 w-2/3 rounded-full bg-hairline-soft" />
322
+ </div>
323
+ ) : null}
324
+ </div>
325
+ <span className="sr-only">
326
+ {overflow} more notification{overflow > 1 ? "s" : ""} queued
327
+ </span>
328
+ </>
329
+ ) : null}
330
+ </div>
331
+ </div>
332
+ );
333
+ },
334
+ );
335
+
336
+ ToastStack.displayName = "ToastStack";
@@ -0,0 +1,2 @@
1
+ export { ToastStack } from "./ToastStack";
2
+ export type { ToastStackProps, ToastStackItem, ToastTone } from "./ToastStack";
@@ -0,0 +1,113 @@
1
+ import * as React from "react";
2
+ import { Button } from "../Button";
3
+ import { ToastStack, type ToastStackItem } from "./ToastStack";
4
+
5
+ const seed: ToastStackItem[] = [
6
+ {
7
+ id: "published",
8
+ tone: "success",
9
+ title: "Project published",
10
+ description: "Your changes are now live at plasma.app",
11
+ duration: 14000,
12
+ },
13
+ {
14
+ id: "upload",
15
+ tone: "error",
16
+ title: "Failed to upload file",
17
+ description: "The file is too large. Please try a smaller file (max 10 MB).",
18
+ },
19
+ {
20
+ id: "template",
21
+ tone: "info",
22
+ title: "Template available",
23
+ description: "A new dashboard template is ready to use.",
24
+ actionLabel: "View details",
25
+ duration: 14000,
26
+ },
27
+ ];
28
+
29
+ const overflowSeed: ToastStackItem[] = [
30
+ { id: "o1", tone: "success", title: "Invite sent", description: "We emailed the invite.", duration: 0 },
31
+ { id: "o2", tone: "warning", title: "Usage at 82%", description: "You are close to your monthly limit." },
32
+ { id: "o3", tone: "info", title: "New comment on Home", actionLabel: "Open" },
33
+ { id: "o4", tone: "success", title: "Autosave on", description: "Drafts save every few seconds." },
34
+ { id: "o5", tone: "info", title: "Synced 3 files" },
35
+ ];
36
+
37
+ let counter = 0;
38
+
39
+ export default function Preview() {
40
+ const [toasts, setToasts] = React.useState<ToastStackItem[]>(seed);
41
+ const [queued, setQueued] = React.useState<ToastStackItem[]>(overflowSeed);
42
+
43
+ const dismiss = (id: string) =>
44
+ setToasts((prev) => prev.filter((toast) => toast.id !== id));
45
+
46
+ const dismissQueued = (id: string) =>
47
+ setQueued((prev) => prev.filter((toast) => toast.id !== id));
48
+
49
+ const addToast = () => {
50
+ counter += 1;
51
+ setToasts((prev) => [
52
+ {
53
+ id: `added-${counter}`,
54
+ tone: counter % 3 === 0 ? "success" : "info",
55
+ title: `Toast number ${prev.length + 1}`,
56
+ description: "Added to the front of the stack.",
57
+ duration: 8000,
58
+ },
59
+ ...prev,
60
+ ]);
61
+ };
62
+
63
+ return (
64
+ <div className="w-full max-w-3xl mx-auto p-lg space-y-lg">
65
+ <div className="space-y-xs">
66
+ <h2 className="text-heading-sm text-ink">Live stack</h2>
67
+ <p className="text-body-sm text-muted">
68
+ Hover any card to pause its countdown — the rail freezes in place.
69
+ </p>
70
+ <div className="flex flex-wrap items-center gap-sm pt-xs">
71
+ <Button variant="secondary" size="sm" onClick={addToast}>
72
+ Add a toast
73
+ </Button>
74
+ <Button variant="ghost" size="sm" onClick={() => setToasts(seed)}>
75
+ Reset stack
76
+ </Button>
77
+ </div>
78
+ </div>
79
+
80
+ <div className="rounded-xl border border-hairline-soft bg-canvas p-base">
81
+ <ToastStack
82
+ toasts={toasts}
83
+ onDismiss={dismiss}
84
+ onDismissAll={() => setToasts([])}
85
+ />
86
+ {toasts.length === 0 ? (
87
+ <p className="py-xxl text-center text-body-sm text-muted">
88
+ No notifications — the region renders nothing at all.
89
+ </p>
90
+ ) : null}
91
+ </div>
92
+
93
+ <div className="space-y-xs">
94
+ <h2 className="text-heading-sm text-ink">Four or more queued</h2>
95
+ <p className="text-body-sm text-muted">
96
+ Past three cards the oldest collapse into peeking slabs behind the stack.
97
+ </p>
98
+ <div className="rounded-xl border border-hairline-soft bg-canvas p-base">
99
+ <ToastStack
100
+ toasts={queued}
101
+ onDismiss={dismissQueued}
102
+ onDismissAll={() => setQueued([])}
103
+ />
104
+ {queued.length === 0 ? (
105
+ <p className="py-xxl text-center text-body-sm text-muted">
106
+ Queue is clear.
107
+ </p>
108
+ ) : null}
109
+ </div>
110
+ </div>
111
+ </div>
112
+ );
113
+ }
@@ -17,7 +17,7 @@ export interface UpgradeCardProps
17
17
  /** Muted supporting text beneath the title */
18
18
  subtitle?: string;
19
19
  /** Icon rendered inside the tinted badge */
20
- iconName?: React.ComponentProps<typeof Icon>["name"];
20
+ iconName?: string;
21
21
  /** When provided, renders the credits-left block under the header row */
22
22
  credits?: UpgradeCardCredits;
23
23
  /** Fraction of total at or below which the meter switches to the warning recipe. Default 0.1 */
package/src/index.ts CHANGED
@@ -51,6 +51,7 @@ export * from "./atoms/TemplateHeader";
51
51
  export * from "./atoms/TemplateHero";
52
52
  export * from "./atoms/TemplateSummary";
53
53
  export * from "./atoms/Toast";
54
+ export * from "./atoms/ToastStack";
54
55
  export * from "./atoms/TopNavbar";
55
56
  export * from "./atoms/TopupModal";
56
57
  export * from "./atoms/Typography";