Repository navigation
Expand file tree
/
Copy pathpromise-toast.tsx
More file actions
288 lines (268 loc) · 9.98 KB
/
Copy pathpromise-toast.tsx
File metadata and controls
288 lines (268 loc) · 9.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"use client";
import { useEffect, useLayoutEffect, useRef, useState } from "react";
import { AnimatePresence, animate, motion, useMotionValue } from "motion/react";
import { useReducedMotion } from "@/lib/use-reduced-motion";
import { cn } from "@/lib/cn";
type Status = "idle" | "loading" | "success" | "error";
type Messages = { loading: string; success: string; error: string };
const EASE_OUT = [0.23, 1, 0.32, 1] as const;
const ICON_SWAP = { type: "spring", duration: 0.3, bounce: 0 } as const;
// The width follows the text with no bounce: a pill that overshoots would
// look like it measured wrong.
const MORPH = { type: "spring", duration: 0.3, bounce: 0 } as const;
const INSTANT = { duration: 0 };
export function PromiseToast({
action,
children,
messages = { loading: "Saving…", success: "Saved", error: "Couldn't save" },
// Enough to read one word. Errors wait longer because they ask for a
// decision, and hovering or focusing the toast holds either one open.
successDuration = 2000,
errorDuration = 6000,
className,
}: {
action: (signal: AbortSignal) => Promise<unknown>;
children: React.ReactNode;
messages?: Messages;
successDuration?: number;
errorDuration?: number;
className?: string;
}) {
const [status, setStatus] = useState<Status>("idle");
const triggerRef = useRef<HTMLButtonElement>(null);
const controller = useRef<AbortController>(undefined);
const dismiss = useRef<ReturnType<typeof setTimeout>>(undefined);
const held = useRef(false);
useEffect(
() => () => {
controller.current?.abort();
clearTimeout(dismiss.current);
},
[],
);
const schedule = (next: Status) => {
clearTimeout(dismiss.current);
if (held.current || (next !== "success" && next !== "error")) return;
dismiss.current = setTimeout(
() => setStatus("idle"),
next === "success" ? successDuration : errorDuration,
);
};
// Replace, never stack: every click saves the same thing, so a newer save
// makes the older result meaningless. The one toast morphs back to loading
// in place instead of piling up stale outcomes.
const run = async () => {
controller.current?.abort();
const current = new AbortController();
controller.current = current;
clearTimeout(dismiss.current);
setStatus("loading");
let next: Status;
try {
await action(current.signal);
next = "success";
} catch {
next = "error";
}
if (current.signal.aborted) return;
setStatus(next);
schedule(next);
};
const hold = (on: boolean) => {
held.current = on;
if (on) clearTimeout(dismiss.current);
else schedule(status);
};
const loading = status === "loading";
return (
<div className={cn("flex flex-col items-center gap-4", className)}>
<button
ref={triggerRef}
type="button"
// aria-disabled keeps focus on the button while a save is in flight;
// a second press there would only race the first.
aria-disabled={loading}
onClick={() => !loading && run()}
className={cn(
"h-10 touch-manipulation rounded-full bg-foreground px-4 text-sm font-medium text-background outline-hidden transition-[scale,opacity] duration-150 ease-out select-none focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-foreground active:scale-[0.96] motion-reduce:transition-[opacity]",
loading && "cursor-progress opacity-60 active:scale-100",
)}
>
{children}
</button>
{/* A fixed 44px slot, so the toast coming and going never moves the
button above it. */}
<div className="flex h-11 items-start justify-center">
<AnimatePresence initial={false}>
{status !== "idle" && (
<Toast
status={status}
messages={messages}
onRetry={() => {
// Retry unmounts itself, so hand focus to the trigger first
// instead of dropping it on the page.
triggerRef.current?.focus();
held.current = false;
run();
}}
onHold={hold}
/>
)}
</AnimatePresence>
</div>
<span className="sr-only" role="status">
{status === "idle" ? "" : messages[status]}
</span>
</div>
);
}
function Toast({
status,
messages,
onRetry,
onHold,
}: {
status: Exclude<Status, "idle">;
messages: Messages;
onRetry: () => void;
onHold: (on: boolean) => void;
}) {
const reduceMotion = useReducedMotion();
const contentRef = useRef<HTMLDivElement>(null);
const width = useMotionValue<number | "auto">("auto");
// The pill animates its real width to whatever the incoming content
// measures, rather than scaling, so the text inside never stretches.
useLayoutEffect(() => {
const el = contentRef.current;
if (!el) return;
let first = true;
const observer = new ResizeObserver(() => {
const next = el.offsetWidth;
if (first || reduceMotion) width.jump(next);
else animate(width, next, MORPH);
first = false;
});
observer.observe(el);
return () => observer.disconnect();
}, [width, reduceMotion]);
return (
<motion.div
// Drops out of the button above it and returns there on the way out.
initial={{ opacity: 0, y: reduceMotion ? 0 : -6, scale: reduceMotion ? 1 : 0.97 }}
animate={{ opacity: 1, y: 0, scale: 1 }}
exit={{
opacity: 0,
y: reduceMotion ? 0 : -4,
scale: reduceMotion ? 1 : 0.98,
transition: { duration: 0.15, ease: EASE_OUT },
}}
transition={{ duration: 0.25, ease: EASE_OUT }}
style={{ width }}
className="relative h-11 origin-top overflow-hidden rounded-full bg-background text-sm text-foreground shadow-raised"
onPointerEnter={(e) => e.pointerType !== "touch" && onHold(true)}
onPointerLeave={(e) => e.pointerType !== "touch" && onHold(false)}
onFocus={() => onHold(true)}
onBlur={(e) => {
if (!e.currentTarget.contains(e.relatedTarget)) onHold(false);
}}
>
{/* w-max lets this report the incoming content's natural width; the
outgoing copy is popped out of flow so it never counts. */}
<div ref={contentRef} className="relative flex h-full w-max">
<AnimatePresence mode="popLayout" initial={false}>
<motion.div
key={status}
initial={{ opacity: 0, filter: "blur(4px)", y: reduceMotion ? 0 : 4 }}
animate={{ opacity: 1, filter: "blur(0px)", y: 0 }}
exit={{
opacity: 0,
filter: "blur(4px)",
y: reduceMotion ? 0 : -4,
transition: { duration: 0.12, ease: EASE_OUT },
}}
transition={{ duration: 0.25, ease: EASE_OUT }}
className={cn(
"flex h-full items-center gap-2 pl-3.5 whitespace-nowrap",
// 6px around the 32px Retry keeps the radii concentric: 22 = 16 + 6.
status === "error" ? "pr-1.5" : "pr-4",
)}
>
<motion.span
aria-hidden
className={cn("grid size-4 place-items-center", status === "error" && "text-danger")}
initial={reduceMotion ? { opacity: 0 } : { opacity: 0, scale: 0.25, filter: "blur(4px)" }}
animate={{ opacity: 1, scale: 1, filter: "blur(0px)" }}
transition={reduceMotion ? INSTANT : ICON_SWAP}
>
<StatusIcon status={status} />
</motion.span>
<span aria-hidden className="font-medium">
{messages[status]}
</span>
{status === "error" && (
<button
type="button"
onClick={onRetry}
className="ml-2 h-8 touch-manipulation rounded-full bg-surface px-3 text-sm font-medium text-foreground outline-hidden transition-[scale,background-color] duration-150 ease-out select-none hover:bg-border focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-foreground active:scale-[0.96] motion-reduce:transition-[background-color]"
>
Retry
</button>
)}
</motion.div>
</AnimatePresence>
</div>
</motion.div>
);
}
function StatusIcon({ status }: { status: Exclude<Status, "idle"> }) {
const props = {
viewBox: "0 0 16 16",
className: "size-4",
fill: "none",
stroke: "currentColor",
strokeWidth: 1.5,
strokeLinecap: "round" as const,
strokeLinejoin: "round" as const,
};
if (status === "loading")
return (
// Spins fast on purpose: a quicker spinner makes the same wait feel
// shorter. Reduced motion slows it rather than freezing the only
// sign that work is happening.
<svg
{...props}
className="size-4 animate-[spin_700ms_linear_infinite] motion-reduce:animate-[spin_1.6s_linear_infinite]"
>
<circle cx="8" cy="8" r="6" opacity={0.2} />
<path d="M8 2a6 6 0 0 1 6 6" />
</svg>
);
if (status === "success")
return (
<svg {...props}>
<path d="m3.5 8.5 3 3 6-7" />
</svg>
);
return (
<svg {...props}>
<circle cx="8" cy="8" r="6.25" />
<path d="M8 5v3.25" />
<path d="M8 10.75h.01" strokeWidth={2} />
</svg>
);
}
// How long the fake request takes: long enough to see the loading state
// settle, short enough not to feel broken.
const FAKE_LATENCY = 1400;
export default function PromiseToastDemo() {
const attempt = useRef(0);
// Alternates so both outcomes show up, success first. A Retry after a
// failure is the next attempt, so it succeeds.
const save = (signal: AbortSignal) =>
new Promise<void>((resolve, reject) => {
const fail = attempt.current++ % 2 === 1;
const timer = setTimeout(() => (fail ? reject() : resolve()), FAKE_LATENCY);
signal.addEventListener("abort", () => clearTimeout(timer), { once: true });
});
return <PromiseToast action={save}>Save changes</PromiseToast>;
}