"use client";
import { AnimatePresence, type HTMLMotionProps, motion, type Transition, useReducedMotion } from "motion/react";
import { useEffect, useState } from "react";
import { TextMorph } from "@/components/motion/text-morph";
import { EASE_IN, EASE_IN_OUT, EASE_OUT, SPRING_PRESS, SPRING_SWAP } from "@/lib/ease";
import { useMessages } from "@/lib/i18n";
import { useSound } from "@/lib/sound";
import { navigateSound } from "@/lib/sounds/navigate";
import { successSound } from "@/lib/sounds/success";
import { cn } from "@/lib/utils";
export const sendButtonMessages = {
send: "Send",
sending: "Sending",
sent: "Sent",
};
export type SendButtonMessages = typeof sendButtonMessages;
type Phase = "idle" | "sending" | "sent";
export interface SendButtonProps
extends Omit, "children" | "onChange"> {
/** Runs the send. Return a promise to keep Sending up until it settles. */
onSend?: () => unknown;
/** Override any built-in string, e.g. for translation. */
labels?: Partial;
/** Play a swish as the plane leaves and a success chime once sent. Off by default; a `SoundProvider` can mute it app-wide. */
sound?: boolean;
}
// Sending shows at least this long, so the flight always reads.
const SENDING_MS = 1000;
const SENT_MS = 1800;
const EXIT = { duration: 0.14, ease: EASE_OUT } as const;
const FADE = { duration: 0.16, ease: EASE_OUT } as const;
const DRAW = { duration: 0.28, ease: EASE_OUT, delay: 0.08 } as const;
// A short wind-up back and down, then away, accelerating like a throw.
const LAUNCH: Transition = { duration: 0.55, times: [0, 0.22, 1], ease: [EASE_OUT, EASE_IN] };
// The contrail starts as the plane leaves the wind-up and accelerates with
// it, then erases from its tail.
const TRAIL: Transition = { duration: 0.8, times: [0, 0.5, 1], ease: [EASE_IN, EASE_OUT], delay: 0.14 };
// The dots bounce in a wave while it sends.
const BOUNCE: Transition = { duration: 0.6, ease: EASE_IN_OUT, repeat: Number.POSITIVE_INFINITY, repeatDelay: 0.1 };
const DOTS = [0, 1, 2];
/**
* A send that you can watch go. The paper plane winds up and takes off
* through the top right of the pill, drawing a contrail that erases from its
* tail; three dots bounce while it sends; a check pops in for Sent; then the
* plane flies back in from the bottom left, as if it had looped around.
*/
export function SendButton({
onSend,
labels,
sound = false,
className,
onClick,
...rest
}: SendButtonProps) {
const t = useMessages("SendButton", sendButtonMessages, labels);
const playSend = useSound(navigateSound, { enabled: sound });
const playSent = useSound(successSound, { enabled: sound });
const reduce = Boolean(useReducedMotion());
const [phase, setPhase] = useState("idle");
// Counts sends, so each one replays the contrail.
const [flights, setFlights] = useState(0);
useEffect(() => {
if (phase !== "sent") return;
const id = setTimeout(() => setPhase("idle"), SENT_MS);
return () => clearTimeout(id);
}, [phase]);
const send = async () => {
setPhase("sending");
setFlights((count) => count + 1);
const started = Date.now();
try {
await onSend?.();
} catch {
setPhase("idle");
return;
}
const wait = Math.max(0, SENDING_MS - (Date.now() - started));
setTimeout(() => {
playSent();
setPhase("sent");
}, wait);
};
const instant = { duration: 0 };
const fadeOnly = { initial: { opacity: 0 }, animate: { opacity: 1 }, exit: { opacity: 0, transition: EXIT } };
return (
{
onClick?.(event);
if (event.defaultPrevented || phase !== "idle") return;
playSend();
void send();
}}
className={cn(
// The pill clips the plane, so it leaves and returns through the edge.
"relative inline-flex h-9 items-center overflow-hidden rounded-full border pr-2.5 pl-3 text-sm font-medium transition-colors duration-200",
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/50",
"disabled:pointer-events-none disabled:opacity-50",
phase === "sent"
? "border-primary/30 bg-primary/10 text-primary"
: "border-primary bg-primary text-primary-foreground hover:bg-primary/90",
className,
)}
{...rest}
>
{/* The contrail, from the plane's tail up and out of the pill. */}
{flights > 0 && !reduce ? (
) : null}
{phase === "idle" ? (
) : phase === "sending" ? (
{DOTS.map((dot) => (
))}
) : (
)}
{t(phase === "idle" ? "send" : phase)}
);
}