Repository navigation
Expand file tree
/
Copy pathhelpful-prompt.tsx
More file actions
294 lines (275 loc) · 10 KB
/
Copy pathhelpful-prompt.tsx
File metadata and controls
294 lines (275 loc) · 10 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
289
290
291
292
293
294
"use client";
import { useEffect, useRef, useState } from "react";
import { AnimatePresence, motion } from "motion/react";
import { useReducedMotion } from "@/lib/use-reduced-motion";
import { usePreviewPlay } from "@/lab/preview-play";
import { cn } from "@/lib/cn";
type Step = "ask" | "why" | "done";
const EASE_OUT = [0.23, 1, 0.32, 1] as const;
// The pill reshapes around each step; a touch of bounce so it settles
// rather than stops.
const RESHAPE = { type: "spring", duration: 0.45, bounce: 0.12 } as const;
const SWAP = { duration: 0.22, ease: EASE_OUT } as const;
export function HelpfulPrompt({
question = "Was this helpful?",
onAnswer,
demo,
className,
}: {
question?: string;
// Fires with the answer and, for a "no", what was missing (may be empty).
onAnswer?: (helpful: boolean, note?: string) => void;
// Drives the steps from outside (the card preview) without touching focus.
demo?: { step: Step; note: string; helpful: boolean } | null;
className?: string;
}) {
const reduceMotion = useReducedMotion();
const [step, setStep] = useState<Step>("ask");
const [helpful, setHelpful] = useState(true);
const [note, setNote] = useState("");
const input = useRef<HTMLInputElement>(null);
const undo = useRef<HTMLButtonElement>(null);
const yes = useRef<HTMLButtonElement>(null);
// Focus follows the step, but only after a person acted, never on load.
const moveFocus = useRef<Step | null>(null);
const usingKeys = useRef(false);
const shown = demo ?? { step, note, helpful };
useEffect(() => {
const target = moveFocus.current;
moveFocus.current = null;
if (target === "why") input.current?.focus();
else if (target === "done") undo.current?.focus();
else if (target === "ask") yes.current?.focus();
}, [step]);
const go = (next: Step) => {
// Saying no always drops you into the field, ready to type. Other steps
// take focus only for keyboard users (typing counts), so a mouse click
// never leaves a ring behind.
if (next === "why" || usingKeys.current) {
moveFocus.current = next;
}
setStep(next);
};
const answerYes = () => {
setHelpful(true);
go("done");
onAnswer?.(true);
};
const send = (e?: React.FormEvent) => {
e?.preventDefault();
if (!note.trim()) return;
setHelpful(false);
go("done");
onAnswer?.(false, note.trim());
};
const t = reduceMotion ? { duration: 0 } : RESHAPE;
const swap = reduceMotion ? { duration: 0 } : SWAP;
const enter = {
initial: { opacity: 0, filter: "blur(4px)", scale: 0.98 },
animate: { opacity: 1, filter: "blur(0px)", scale: 1 },
exit: {
opacity: 0,
filter: "blur(4px)",
scale: 0.98,
transition: { duration: 0.12 },
},
transition: swap,
};
return (
<motion.div
layout
transition={t}
role="group"
aria-label="Page feedback"
onPointerDownCapture={() => (usingKeys.current = false)}
onKeyDownCapture={() => (usingKeys.current = true)}
// Radius set here so the reshaping keeps a true pill, never an oval.
style={{ borderRadius: 999 }}
className={cn(
"relative flex h-12 items-center overflow-hidden bg-background shadow-raised",
className,
)}
>
<AnimatePresence mode="popLayout" initial={false}>
{shown.step === "ask" && (
<motion.div
key="ask"
{...enter}
className="flex items-center gap-1 pr-1.5 pl-5"
>
<span className="mr-2 text-[14px] whitespace-nowrap text-foreground">
{question}
</span>
<Choice ref={yes} onClick={answerYes}>
Yes
</Choice>
<Choice onClick={() => go("why")}>No</Choice>
</motion.div>
)}
{shown.step === "why" && (
<motion.form
key="why"
{...enter}
onSubmit={send}
className="flex items-center gap-1 pr-1.5 pl-5"
>
<label htmlFor="helpful-note" className="sr-only">
What was missing?
</label>
<input
ref={input}
id="helpful-note"
value={shown.note}
onChange={(e) => setNote(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Escape") go("ask");
}}
placeholder="What was missing?"
autoComplete="off"
maxLength={200}
// 16px on phones: iOS zooms into any smaller input it focuses.
className="h-9 w-[min(14rem,48vw)] bg-transparent text-base text-foreground outline-hidden placeholder:text-muted sm:text-[14px]"
/>
<button
type="button"
aria-label="Cancel"
onClick={() => go("ask")}
className="grid size-9 shrink-0 place-items-center rounded-full text-muted outline-hidden transition-[color,background-color,scale] duration-150 ease-out hover:bg-surface hover:text-foreground focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-solid focus-visible:outline-foreground active:scale-[0.96]"
>
<svg
viewBox="0 0 16 16"
className="size-3.5"
fill="none"
stroke="currentColor"
strokeWidth={1.7}
strokeLinecap="round"
aria-hidden
>
<path d="M4.5 4.5l7 7M11.5 4.5l-7 7" />
</svg>
</button>
<button
type="submit"
aria-label="Send feedback"
disabled={!shown.note.trim()}
className="grid size-9 shrink-0 place-items-center rounded-full bg-foreground text-background outline-hidden transition-[scale,opacity] duration-150 ease-out focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-solid focus-visible:outline-foreground active:scale-[0.96] disabled:opacity-25"
>
<svg
viewBox="0 0 16 16"
className="size-4"
fill="none"
stroke="currentColor"
strokeWidth={1.7}
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden
>
<path d="M8 12.5v-9M4 7l4-4 4 4" />
</svg>
</button>
</motion.form>
)}
{shown.step === "done" && (
<motion.div
key="done"
{...enter}
className="flex items-center gap-2 pr-1.5 pl-4"
>
<svg
viewBox="0 0 16 16"
className="size-4 shrink-0 text-foreground"
fill="none"
aria-hidden
>
<motion.path
d="M3.5 8.5 6.5 11.5 12.5 4.5"
stroke="currentColor"
strokeWidth={1.8}
strokeLinecap="round"
strokeLinejoin="round"
initial={{ pathLength: reduceMotion ? 1 : 0 }}
animate={{ pathLength: 1 }}
transition={{ duration: 0.35, delay: 0.1, ease: EASE_OUT }}
/>
</svg>
<span className="text-[14px] whitespace-nowrap text-foreground">
{shown.helpful ? "Thanks, glad it helped" : "Thanks, noted"}
</span>
<button
ref={undo}
type="button"
onClick={() => {
setNote("");
go("ask");
}}
className="ml-1 h-9 rounded-full px-3 text-[13px] text-muted outline-hidden transition-[color,background-color,scale] duration-150 ease-out hover:bg-surface hover:text-foreground focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-solid focus-visible:outline-foreground active:scale-[0.96]"
>
Undo
</button>
</motion.div>
)}
</AnimatePresence>
<span className="sr-only" aria-live="polite">
{shown.step === "done" ? "Thanks for your feedback" : ""}
</span>
</motion.div>
);
}
function Choice({
ref,
onClick,
children,
}: {
ref?: React.Ref<HTMLButtonElement>;
onClick: () => void;
children: React.ReactNode;
}) {
return (
<button
ref={ref}
type="button"
onClick={onClick}
className="h-9 rounded-full bg-surface px-4 text-[14px] font-medium text-foreground outline-hidden transition-[background-color,scale] duration-150 ease-out hover:bg-foreground/10 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-solid focus-visible:outline-foreground active:scale-[0.96]"
>
{children}
</button>
);
}
// The card's hover show: a reader says no, types what was missing, sends,
// then another says yes.
const NOTE = "An example with forms";
export default function HelpfulPromptDemo() {
const play = usePreviewPlay();
const [demo, setDemo] = useState<{
step: Step;
note: string;
helpful: boolean;
} | null>(null);
useEffect(() => {
if (play !== true) return;
const timers: ReturnType<typeof setTimeout>[] = [];
const at = (ms: number, fn: () => void) => timers.push(setTimeout(fn, ms));
const run = () => {
at(500, () => setDemo({ step: "why", note: "", helpful: false }));
for (let i = 1; i <= NOTE.length; i++) {
at(1000 + i * 45, () =>
setDemo({ step: "why", note: NOTE.slice(0, i), helpful: false }),
);
}
const sent = 1000 + NOTE.length * 45 + 450;
at(sent, () => setDemo({ step: "done", note: NOTE, helpful: false }));
at(sent + 1700, () => setDemo({ step: "ask", note: "", helpful: true }));
at(sent + 2500, () => setDemo({ step: "done", note: "", helpful: true }));
at(sent + 4300, () => {
setDemo({ step: "ask", note: "", helpful: true });
run();
});
};
run();
return () => {
timers.forEach(clearTimeout);
setDemo(null);
};
}, [play]);
return <HelpfulPrompt demo={play === true ? demo : null} />;
}