Repository navigation
Expand file tree
/
Copy pathcall-widget.tsx
More file actions
443 lines (418 loc) · 16.2 KB
/
Copy pathcall-widget.tsx
File metadata and controls
443 lines (418 loc) · 16.2 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
"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";
export type CallPhase = "incoming" | "active" | "declined" | "ended";
// Accept is green the world over, so it's the one colour here that isn't a
// theme token: a convention, not decoration.
const ANSWER = "light-dark(oklch(0.62 0.16 150), oklch(0.66 0.16 150))";
const MORPH = { type: "spring", duration: 0.45, bounce: 0.1 } as const;
const FADE = { duration: 0.2, ease: [0.23, 1, 0.32, 1] } as const;
const ICON_SWAP = { type: "spring", duration: 0.3, bounce: 0 } as const;
// One button's width plus the gap, so a control can start tucked exactly
// behind the one to its right.
const STEP = 52;
// The ring spreads and fades like sound leaving a phone; the wiggle is a
// short buzz with a long rest, so it nudges without nagging.
const CALL_CSS = `
@keyframes call-ring { from { transform: scale(1); opacity: .6 } to { transform: scale(1.7); opacity: 0 } }
@keyframes call-wiggle { 0%, 70%, 100% { transform: rotate(0) } 74% { transform: rotate(-14deg) } 78% { transform: rotate(12deg) } 82% { transform: rotate(-9deg) } 86% { transform: rotate(6deg) } 90% { transform: rotate(0) } }
`;
const clock = (s: number) =>
`${String(Math.floor(s / 60)).padStart(2, "0")}:${String(s % 60).padStart(2, "0")}`;
export function CallWidget({
name,
avatar,
kind = "video",
phase,
onPhaseChange,
muted,
onMutedChange,
cameraOff,
onCameraOffChange,
sharing,
onSharingChange,
seconds,
className,
}: {
name: string;
avatar: string;
kind?: "video" | "voice";
phase: CallPhase;
onPhaseChange: (phase: CallPhase) => void;
muted: boolean;
onMutedChange: (muted: boolean) => void;
cameraOff: boolean;
onCameraOffChange: (off: boolean) => void;
sharing: boolean;
onSharingChange: (sharing: boolean) => void;
// Time in the call, owned by whoever runs the call.
seconds: number;
className?: string;
}) {
const reduceMotion = useReducedMotion();
const ringing = phase === "incoming";
const inCall = phase === "active";
const over = !ringing && !inCall;
const transition = reduceMotion ? { duration: 0 } : MORPH;
const subtitle = ringing
? `Incoming ${kind} call`
: inCall
? clock(seconds)
: phase === "declined"
? "Call declined"
: `Call ended · ${clock(seconds)}`;
const toggles = [
{ key: "mic", label: "Mute", on: muted, set: onMutedChange, icon: <MicIcon off={muted} /> },
...(kind === "video"
? [{ key: "cam", label: "Turn camera off", on: cameraOff, set: onCameraOffChange, icon: <VideoIcon off={cameraOff} /> }]
: []),
{ key: "share", label: "Share screen", on: sharing, set: onSharingChange, icon: <ShareIcon /> },
];
return (
<motion.div
layout
transition={transition}
style={{ borderRadius: 28 }}
className={cn(
"flex w-fit max-w-full items-center gap-3 bg-background p-2 shadow-raised",
className,
)}
>
<style>{CALL_CSS}</style>
{/* The caller stays in place through every phase; only what's around
them changes. */}
<motion.span
layout="position"
transition={transition}
className="relative size-11 shrink-0"
>
{ringing && !reduceMotion && (
<>
<span className="absolute inset-0 animate-[call-ring_2s_ease-out_infinite] rounded-full border border-foreground/25" />
<span className="absolute inset-0 animate-[call-ring_2s_ease-out_1s_infinite] rounded-full border border-foreground/25" />
</>
)}
{/* eslint-disable-next-line @next/next/no-img-element -- a tiny local SVG */}
<img
src={avatar}
alt=""
className={cn(
"relative size-11 rounded-full bg-[oklch(0.97_0_0)] transition-[opacity,filter] duration-200 ease-out",
over && "opacity-50 grayscale",
)}
/>
</motion.span>
<motion.span
layout="position"
transition={transition}
className="flex min-w-0 flex-1 flex-col pr-1"
>
<span className="truncate text-[15px] font-medium text-foreground">
{name}
</span>
<span className="grid text-[13px] text-muted tabular-nums">
<AnimatePresence initial={false} mode="popLayout">
<motion.span
key={ringing ? "ring" : inCall ? "call" : phase}
initial={{ opacity: 0, y: 3, filter: "blur(3px)" }}
animate={{ opacity: 1, y: 0, filter: "blur(0px)" }}
exit={{ opacity: 0, filter: "blur(2px)", transition: { duration: 0.1 } }}
transition={FADE}
className="col-start-1 row-start-1 truncate whitespace-nowrap"
>
{subtitle}
</motion.span>
</AnimatePresence>
</span>
</motion.span>
<span aria-live="polite" className="sr-only">
{ringing ? `${name} is calling` : subtitle}
</span>
<div className="ml-1 flex shrink-0 items-center gap-2">
<AnimatePresence initial={false} mode="popLayout">
{/* Decline bows out where it stands: it isn't the one you chose. */}
{ringing && (
<motion.button
key="decline"
type="button"
aria-label="Decline"
onClick={() => onPhaseChange("declined")}
initial={{ opacity: 0, scale: 0.6, filter: "blur(4px)" }}
animate={{ opacity: 1, scale: 1, filter: "blur(0px)" }}
exit={{ opacity: 0, scale: 0.6, filter: "blur(4px)", transition: { duration: 0.15 } }}
transition={FADE}
className="grid size-11 touch-manipulation place-items-center rounded-full bg-danger text-white outline-hidden transition-[scale,background-color] duration-150 ease-out select-none hover:bg-[color-mix(in_oklab,var(--danger)_88%,white)] focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-foreground active:scale-[0.96]"
>
<PhoneIcon down />
</motion.button>
)}
{/* The controls unfold out of the End button: each starts tucked
behind it and slides out to its place, the nearest first. */}
{inCall &&
toggles.map((t, i) => {
const behind = (toggles.length - i) * STEP;
return (
<motion.button
key={t.key}
type="button"
aria-label={t.label}
aria-pressed={t.on}
onClick={() => t.set(!t.on)}
initial={reduceMotion ? { opacity: 0 } : { x: behind, opacity: 0, scale: 0.8 }}
animate={{ x: 0, opacity: 1, scale: 1 }}
// Leaving, they fade where they stand: the pill is
// shrinking at the same time, so sliding back would carry
// them out past its edge as ghosts.
exit={{
opacity: 0,
scale: reduceMotion ? 1 : 0.8,
filter: "blur(3px)",
transition: { duration: 0.12 },
}}
transition={{
...MORPH,
delay: reduceMotion ? 0 : (toggles.length - 1 - i) * 0.05 + 0.08,
}}
// Switched on means something is held back (muted, camera
// off), so it inverts: the odd one out is the one to notice.
className={cn(
"grid size-11 touch-manipulation place-items-center rounded-full outline-hidden transition-[scale,background-color,color] 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]",
t.on
? "bg-foreground text-background"
: "bg-surface text-foreground hover:bg-foreground/10",
)}
>
{t.icon}
</motion.button>
);
})}
</AnimatePresence>
{/* One button throughout: Accept becomes End becomes Call again. It
never leaves the spot you clicked, it only changes what it is:
green to red as the handset tips over to hang up, then quiet. */}
<motion.button
layout
transition={transition}
type="button"
aria-label={ringing ? "Accept" : inCall ? "End call" : undefined}
onClick={() => onPhaseChange(ringing ? "active" : inCall ? "ended" : "incoming")}
style={{ borderRadius: 999 }}
className={cn(
"group/call relative z-10 grid h-11 touch-manipulation place-items-center overflow-hidden bg-surface outline-hidden transition-[scale,color] duration-200 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]",
over ? "px-4 text-foreground" : "w-11 text-white",
inCall && "w-14",
)}
>
{/* Colours never cross-fade: half-transparent red over green reads
as brown, and over grey as pink. The red wipes in instead, a
circle filling out from the centre, and drains back into it on
hang-up. The green underneath waits until it's covered. */}
<span
aria-hidden
className={cn(
"absolute inset-0 transition-opacity ease-out",
ringing ? "opacity-100 duration-200" : "opacity-0 delay-300 duration-0",
)}
style={{ background: ANSWER }}
/>
<span
aria-hidden
// Fills generously, drains fast: the red is gone before the next
// label arrives, so the two never share the button.
className={cn(
"absolute inset-0 bg-danger transition-[clip-path] motion-reduce:transition-none",
inCall
? "duration-300 ease-[cubic-bezier(0.23,1,0.32,1)]"
: "duration-150 ease-[cubic-bezier(0.4,0,1,1)]",
)}
style={{
clipPath: inCall
? "circle(75% at 50% 50%)"
: "circle(0% at 50% 50%)",
}}
/>
{/* Hover lightens with a veil rather than a filter, which can
leave square corners on a button that is changing size. */}
<span
aria-hidden
className="absolute inset-0 bg-white/0 transition-[background-color] duration-150 ease-out group-hover/call:bg-white/12"
/>
<AnimatePresence initial={false} mode="popLayout">
<motion.span
key={over ? "again" : "phone"}
layout="position"
initial={{ opacity: 0, scale: 0.25, filter: "blur(4px)" }}
animate={{ opacity: 1, scale: 1, filter: "blur(0px)" }}
exit={{ opacity: 0, scale: 0.25, filter: "blur(4px)", transition: { duration: 0.12 } }}
// "Call again" waits for the red to drain first.
transition={{ ...ICON_SWAP, delay: over && !reduceMotion ? 0.14 : 0 }}
className="relative grid place-items-center"
>
{over ? (
<span className="text-sm font-medium whitespace-nowrap">Call again</span>
) : (
<motion.span
className="grid"
initial={false}
// The handset rotates over rather than swapping icons, so
// accepting reads as picking up and hanging up as putting
// it down.
animate={{ rotate: inCall ? 135 : 0 }}
transition={reduceMotion ? { duration: 0 } : MORPH}
>
<span
className={cn(
"grid",
ringing && !reduceMotion && "animate-[call-wiggle_2.4s_ease-in-out_infinite]",
)}
>
<PhoneIcon />
</span>
</motion.span>
)}
</motion.span>
</AnimatePresence>
</motion.button>
</div>
</motion.div>
);
}
const STROKE = {
viewBox: "0 0 24 24",
fill: "none",
stroke: "currentColor",
strokeWidth: 1.75,
strokeLinecap: "round",
strokeLinejoin: "round",
className: "size-5",
"aria-hidden": true,
} as const;
// A slash that draws across the icon as it switches off, and back.
function Slash({ on }: { on: boolean }) {
const reduceMotion = useReducedMotion();
return (
<motion.path
d="M4 4l16 16"
initial={false}
animate={{ pathLength: on ? 1 : 0, opacity: on ? 1 : 0 }}
transition={reduceMotion ? { duration: 0 } : ICON_SWAP}
/>
);
}
function MicIcon({ off = false }: { off?: boolean }) {
return (
<svg {...STROKE}>
<rect x="9" y="3" width="6" height="11" rx="3" />
<path d="M5.5 11a6.5 6.5 0 0 0 13 0M12 17.5V21" />
<Slash on={off} />
</svg>
);
}
function VideoIcon({ off = false }: { off?: boolean }) {
return (
<svg {...STROKE}>
<rect x="3" y="6.5" width="12.5" height="11" rx="2.5" />
<path d="m15.5 10.5 5-3v9l-5-3" />
<Slash on={off} />
</svg>
);
}
function ShareIcon() {
return (
<svg {...STROKE}>
<rect x="3" y="4.5" width="18" height="12.5" rx="2.5" />
<path d="M8.5 21h7M12 13.5v-5M9.5 10.5 12 8l2.5 2.5" />
</svg>
);
}
function PhoneIcon({ down = false }: { down?: boolean }) {
return (
<svg
{...STROKE}
// The same handset, tipped over to hang up.
style={down ? { transform: "rotate(135deg)" } : undefined}
>
<path d="M6.6 3.5h2.6l1.3 4.1-2 1.3a11 11 0 0 0 6.6 6.6l1.3-2 4.1 1.3v2.6a2 2 0 0 1-2.2 2A16.5 16.5 0 0 1 4.6 5.7a2 2 0 0 1 2-2.2Z" />
</svg>
);
}
export default function CallWidgetDemo() {
const play = usePreviewPlay();
const [phase, setPhase] = useState<CallPhase>("incoming");
const [muted, setMuted] = useState(false);
const [cameraOff, setCameraOff] = useState(false);
const [sharing, setSharing] = useState(false);
const [seconds, setSeconds] = useState(0);
const started = useRef(0);
// The call clock runs only while a call is live, and only a tick a second.
useEffect(() => {
if (phase !== "active") return;
const id = setInterval(
() => setSeconds(Math.floor((performance.now() - started.current) / 1000)),
1000,
);
return () => clearInterval(id);
}, [phase]);
const change = (next: CallPhase) => {
if (next === "active") {
started.current = performance.now();
setSeconds(0);
setMuted(false);
setCameraOff(false);
setSharing(false);
}
setPhase(next);
};
// In an index card, hovering answers, mutes for a moment and hangs up.
const changeRef = useRef(change);
useEffect(() => {
changeRef.current = change;
});
useEffect(() => {
if (play !== true) return;
const steps: [() => void, number][] = [
[() => changeRef.current("active"), 700],
[() => setMuted(true), 1500],
[() => setMuted(false), 900],
[() => changeRef.current("ended"), 1300],
[() => changeRef.current("incoming"), 1400],
];
let i = 0;
let timer: ReturnType<typeof setTimeout>;
const next = () => {
const [run, wait] = steps[i % steps.length];
i++;
timer = setTimeout(() => {
run();
next();
}, wait);
};
next();
return () => {
clearTimeout(timer);
changeRef.current("incoming");
};
}, [play]);
return (
// Centred in every phase; the widget grows evenly both ways as the
// controls unfold, so the button you pressed only drifts half the width.
<div className="flex w-[460px] max-w-full justify-center">
<CallWidget
name="Maya Chen"
avatar="/avatars/cara.svg"
phase={phase}
onPhaseChange={change}
muted={muted}
onMutedChange={setMuted}
cameraOff={cameraOff}
onCameraOffChange={setCameraOff}
sharing={sharing}
onSharingChange={setSharing}
seconds={seconds}
/>
</div>
);
}