"use client";
import { Angry, Check, Frown, Laugh, Meh, MessageSquarePlus, Smile } from "lucide-react";
import { AnimatePresence, motion } from "motion/react";
import { useEffect, useId, useRef, useState, type ReactNode, type RefObject } from "react";
import { cn } from "@/lib/utils";
const FOCUSABLE = 'a[href],button:not([disabled]),input:not([disabled]),select,textarea,[tabindex]:not([tabindex="-1"])';
/** Moves focus into `ref` while open, keeps Tab inside it, closes on Escape and restores focus afterwards. */
function useModalFocus(ref: RefObject<HTMLElement | null>, open: boolean, onClose: () => void) {
const close = useRef(onClose);
useEffect(() => {
close.current = onClose;
});
useEffect(() => {
const node = ref.current;
if (!open || !node) return;
const opener = document.activeElement as HTMLElement | null;
(node.querySelector<HTMLElement>("[data-autofocus]") ?? node).focus({ preventScroll: true });
const onKeyDown = (e: KeyboardEvent) => {
if (e.key === "Escape") return close.current();
if (e.key !== "Tab") return;
const items = [...node.querySelectorAll<HTMLElement>(FOCUSABLE)];
const first = items[0];
const last = items[items.length - 1];
const at = document.activeElement;
if (!first || (e.shiftKey ? at === first || at === node : at === last)) {
e.preventDefault();
(e.shiftKey ? last : first)?.focus();
}
};
node.addEventListener("keydown", onKeyDown);
return () => {
node.removeEventListener("keydown", onKeyDown);
opener?.focus({ preventScroll: true });
};
}, [open, ref]);
}
type MorphDialogProps = {
/** Shown on the button and, after the morph, as the dialog title. */
label: string;
icon?: ReactNode;
open: boolean;
onOpenChange: (open: boolean) => void;
children: ReactNode;
/** Class for the overlay. Covers the nearest positioned ancestor; pass "fixed" for the viewport. */
className?: string;
};
const morph = { type: "spring", stiffness: 380, damping: 34 } as const;
export function MorphDialog({ label, icon, open, onOpenChange, children, className }: MorphDialogProps) {
const id = useId();
const panel = useRef<HTMLDivElement>(null);
useModalFocus(panel, open, () => onOpenChange(false));
return (
<>
{/* The trigger stays mounted; Motion cross-fades it with the dialog that shares its layoutId. */}
<motion.button
layoutId={`dialog-morph-${id}`}
transition={morph}
onClick={() => onOpenChange(true)}
aria-haspopup="dialog"
aria-expanded={open}
style={{ borderRadius: 999 }}
className="relative inline-flex h-11 items-center gap-2 border border-border bg-card px-5 text-sm font-semibold text-card-foreground shadow-lg shadow-black/5"
>
{icon}
<motion.span layoutId={`dialog-morph-label-${id}`} transition={morph} className="inline-block">
{label}
</motion.span>
</motion.button>
<AnimatePresence>
{open && (
<motion.div key="overlay" className={cn("absolute inset-0 z-50 grid place-items-center p-4", className)}>
<motion.div
aria-hidden
onClick={() => onOpenChange(false)}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
className="absolute inset-0 bg-black/35 backdrop-blur-[3px]"
/>
<motion.div
ref={panel}
layoutId={`dialog-morph-${id}`}
transition={morph}
role="dialog"
aria-modal="true"
aria-labelledby={`${id}-title`}
tabIndex={-1}
style={{ borderRadius: 24 }}
className="relative w-full max-w-sm overflow-hidden border border-border bg-card p-5 text-card-foreground shadow-2xl shadow-black/20 outline-none"
>
<h2 id={`${id}-title`} className="text-sm font-semibold">
<motion.span layoutId={`dialog-morph-label-${id}`} transition={morph} className="inline-block">
{label}
</motion.span>
</h2>
<motion.div
initial={{ opacity: 0, y: 8 }}
animate={{ opacity: 1, y: 0, transition: { delay: 0.12, duration: 0.25 } }}
exit={{ opacity: 0, transition: { duration: 0.08 } }}
>
{children}
</motion.div>
</motion.div>
</motion.div>
)}
</AnimatePresence>
</>
);
}
const moods = [
{ icon: Angry, label: "Terrible" },
{ icon: Frown, label: "Bad" },
{ icon: Meh, label: "Okay" },
{ icon: Smile, label: "Good" },
{ icon: Laugh, label: "Great" },
];
export default function DialogMorphFromButtonDemo() {
const [open, setOpen] = useState(true);
const [mood, setMood] = useState(3);
const [sent, setSent] = useState(false);
useEffect(() => {
if (!sent) return;
const t = setTimeout(() => setOpen(false), 1300);
return () => clearTimeout(t);
}, [sent]);
return (
<div className="relative isolate flex h-[420px] w-full max-w-[680px] items-center justify-center overflow-hidden rounded-3xl border border-border bg-muted/40 [background-image:radial-gradient(var(--border)_1px,transparent_1px)] [background-size:18px_18px]">
<MorphDialog
label="Send feedback"
icon={<MessageSquarePlus className="size-4 text-primary" />}
open={open}
onOpenChange={(next) => {
setOpen(next);
if (next) setSent(false);
}}
>
<AnimatePresence mode="wait" initial={false}>
{sent ? (
<motion.div key="done" initial={{ opacity: 0, scale: 0.9 }} animate={{ opacity: 1, scale: 1 }} className="flex h-[212px] flex-col items-center justify-center text-center">
<motion.span
initial={{ scale: 0 }}
animate={{ scale: 1 }}
transition={{ type: "spring", stiffness: 420, damping: 16 }}
className="flex size-12 items-center justify-center rounded-full bg-emerald-500/15 text-emerald-600 dark:text-emerald-400"
>
<Check className="size-6" strokeWidth={3} />
</motion.span>
<p className="mt-3 font-medium">Thanks, that helps.</p>
<p className="text-sm text-muted-foreground">The product team reads every note.</p>
</motion.div>
) : (
<motion.form
key="form"
exit={{ opacity: 0, scale: 0.97, transition: { duration: 0.12 } }}
onSubmit={(e) => {
e.preventDefault();
setSent(true);
}}
className="h-[212px]"
>
<p className="mt-1 text-sm text-muted-foreground">How is the new editor treating you?</p>
<div role="radiogroup" aria-label="Mood" className="mt-3 flex gap-1.5">
{moods.map((m, i) => (
<button
key={m.label}
type="button"
role="radio"
aria-checked={mood === i}
aria-label={m.label}
onClick={() => setMood(i)}
className={cn("relative flex h-10 flex-1 items-center justify-center rounded-xl transition-colors", mood === i ? "text-primary" : "text-muted-foreground hover:text-foreground")}
>
{mood === i && (
<motion.span layoutId="dialog-morph-from-button-mood" className="absolute inset-0 rounded-xl bg-primary/12 ring-1 ring-primary/30" transition={{ type: "spring", stiffness: 460, damping: 30 }} />
)}
<m.icon className="relative size-5" />
</button>
))}
</div>
<textarea
aria-label="Your feedback"
rows={3}
defaultValue="Slash commands are great. I'd love one for tables."
className="mt-3 block w-full resize-none rounded-xl border border-input bg-background px-3 py-2 text-sm text-foreground outline-none focus-visible:ring-2 focus-visible:ring-ring/40"
/>
<div className="mt-3 flex justify-end gap-2">
<button type="button" onClick={() => setOpen(false)} className="h-9 rounded-lg px-3.5 text-sm font-medium text-muted-foreground transition-colors hover:bg-muted hover:text-foreground">
Cancel
</button>
<button type="submit" className="h-9 rounded-lg bg-primary px-4 text-sm font-medium text-primary-foreground shadow-sm shadow-primary/30 transition-transform active:scale-95">
Send
</button>
</div>
</motion.form>
)}
</AnimatePresence>
</MorphDialog>
</div>
);
}