Door Morph
zosasounds
"use client";
import { type CSSProperties, type ReactNode, useId, useRef, useState } from "react";
import { flushSync } from "react-dom";
/**
* Door Morph: a grid of cards ("doors") where opening one turns its title
* into the heading of the page behind it, flying across the screen, and Back
* flies it home. The light follows the pointer and the door leans toward it.
* This is how the home of zosasounds.com opens its sections.
*
* <DoorMorph doors={[
* { id: "journey", title: "Journey", color: "#1d1d1f", body: <p>…</p> },
* { id: "ugc", title: "UGC", color: "#0071e3", body: <p>…</p> },
* ]} />
*
* Built on the View Transitions API. Browsers without it (and reduced motion)
* swap instantly, which still works, it just does not fly.
*/
export type Door = { id: string; title: string; color: string; ink?: string; body: ReactNode };
export type DoorMorphProps = { doors: Door[]; back?: string; className?: string; style?: CSSProperties };
const CSS = `
.zs-doors{display:grid;gap:12px;grid-template-columns:repeat(auto-fill,minmax(180px,1fr))}
.zs-door{position:relative;display:flex;align-items:flex-end;min-height:180px;padding:20px;border:0;border-radius:22px;
font:inherit;text-align:left;cursor:pointer;overflow:hidden;
transform:perspective(800px) rotateX(var(--rx,0deg)) rotateY(var(--ry,0deg));
transition:transform .5s cubic-bezier(.16,1,.3,1)}
.zs-door::before{content:"";position:absolute;inset:0;pointer-events:none;opacity:0;transition:opacity .3s;
background:radial-gradient(260px circle at var(--mx,50%) var(--my,50%),rgba(255,255,255,.28),transparent 60%)}
.zs-door:hover::before{opacity:1}
.zs-door:active{transform:scale(.98)}
.zs-door:focus-visible{outline:3px solid currentColor;outline-offset:3px}
.zs-door__t{position:relative;font-size:clamp(1.4rem,3vw,1.9rem);font-weight:700;letter-spacing:-.03em;line-height:1}
.zs-page{min-height:100%;padding:28px;border-radius:22px}
.zs-page__h{margin:18px 0 12px;font-size:clamp(2.2rem,6vw,3.6rem);font-weight:700;letter-spacing:-.04em;line-height:1;width:fit-content}
.zs-page__back{appearance:none;border:0;background:rgba(127,127,127,.18);color:inherit;font:inherit;font-size:.9rem;
padding:6px 14px;border-radius:999px;cursor:pointer}
@media (prefers-reduced-motion:no-preference){
::view-transition-group(*){animation-duration:.6s;animation-timing-function:cubic-bezier(.16,1,.3,1)}
}
`;
export function DoorMorph({ doors, back = "← Back", className, style }: DoorMorphProps) {
const [open, setOpen] = useState<string | null>(null);
const uid = useId().replace(/[^a-zA-Z0-9]/g, "");
const name = `zs-door-${uid}`;
const titles = useRef(new Map<string, HTMLElement>());
const heading = useRef<HTMLHeadingElement>(null);
function swap(next: string | null) {
const reduce = matchMedia("(prefers-reduced-motion: reduce)").matches;
if (!document.startViewTransition || reduce) {
setOpen(next);
return;
}
// Old state: the element that is about to leave carries the name…
const from = next ? titles.current.get(next) : heading.current;
if (from) from.style.viewTransitionName = name;
const t = document.startViewTransition(() => {
if (from) from.style.viewTransitionName = "";
flushSync(() => setOpen(next));
// …new state: the element that just arrived takes it over.
const to = next ? heading.current : titles.current.get(open ?? "");
if (to) to.style.viewTransitionName = name;
});
t.finished.finally(() => {
heading.current?.style.removeProperty("view-transition-name");
titles.current.forEach((n) => n.style.removeProperty("view-transition-name"));
});
}
function lean(e: React.PointerEvent<HTMLButtonElement>) {
if (e.pointerType !== "mouse") return;
const d = e.currentTarget;
const r = d.getBoundingClientRect();
const x = (e.clientX - r.left) / r.width;
const y = (e.clientY - r.top) / r.height;
d.style.setProperty("--mx", `${x * 100}%`);
d.style.setProperty("--my", `${y * 100}%`);
d.style.setProperty("--rx", `${(0.5 - y) * 5}deg`);
d.style.setProperty("--ry", `${(x - 0.5) * 6}deg`);
}
const door = doors.find((d) => d.id === open);
return (
<div className={className} style={style}>
<style>{CSS}</style>
{door ? (
<section className="zs-page" style={{ background: door.color, color: door.ink ?? "#fff" }}>
<button type="button" className="zs-page__back" onClick={() => swap(null)}>
{back}
</button>
<h2 ref={heading} className="zs-page__h">
{door.title}
</h2>
{door.body}
</section>
) : (
<div className="zs-doors">
{doors.map((d) => (
<button
key={d.id}
type="button"
className="zs-door"
style={{ background: d.color, color: d.ink ?? "#fff" }}
onClick={() => swap(d.id)}
onPointerMove={lean}
onPointerLeave={(e) => {
e.currentTarget.style.setProperty("--rx", "0deg");
e.currentTarget.style.setProperty("--ry", "0deg");
}}
>
<b
className="zs-door__t"
ref={(n) => {
if (n) titles.current.set(d.id, n);
else titles.current.delete(d.id);
}}
>
{d.title}
</b>
</button>
))}
</div>
)}
</div>
);
}