"use client"
import {
BatteryFullIcon,
BinocularsIcon,
BreadIcon,
CarProfileIcon,
CellSignalFullIcon,
DotsThreeIcon,
HouseIcon,
MagnifyingGlassIcon,
MapTrifoldIcon,
MicrophoneIcon,
NavigationArrowIcon,
PaintBrushIcon,
WifiHighIcon,
XIcon,
} from "@phosphor-icons/react"
import {
animate,
type MotionStyle,
motion,
useMotionValue,
useReducedMotion,
useTransform,
} from "motion/react"
import { MapArt } from "./MapArt"
import { useMapSheetTuning } from "./tuning"
const STAGE_HEIGHT = 740
const STAGE_RADIUS = 53
const GAP = 9
const MAP_PAN = 60
const PILL_INSET = 22
const PILL_HEIGHT = 64
const ROW_HEIGHT = 72
const TOP = 54
const SHEET = STAGE_HEIGHT - TOP
const LARGE = 0
const MEDIUM = SHEET / 2
const SMALL = SHEET - PILL_INSET - PILL_HEIGHT
const DETENTS = [LARGE, MEDIUM, SMALL]
const GLASS =
"backdrop-blur-[8px] backdrop-saturate-[1.4] backdrop-brightness-[1.06] shadow-[inset_1.5px_1.5px_1px_-1px_oklch(1_0_0/0.95),inset_-1.5px_-1.5px_1px_-1px_oklch(1_0_0/0.6),inset_0_0_0_0.5px_oklch(1_0_0/0.5),inset_0_0_18px_oklch(1_0_0/0.35),0_0_0_0.5px_oklch(0_0_0/0.06),0_10px_36px_oklch(0_0_0/0.16)] dark:backdrop-brightness-[0.9] dark:shadow-[inset_1.5px_1.5px_1px_-1px_oklch(1_0_0/0.5),inset_-1.5px_-1.5px_1px_-1px_oklch(1_0_0/0.22),inset_0_0_0_0.5px_oklch(1_0_0/0.14),inset_0_0_18px_oklch(1_0_0/0.07),0_10px_36px_oklch(0_0_0/0.5)]"
const FILL = "bg-white/45 dark:bg-white/[0.08]"
const CONTROL = `bg-white/25 text-neutral-800 dark:bg-white/[0.08] dark:text-white ${GLASS}`
const RECENTS = [
{ icon: HouseIcon, title: "Home", detail: "Linden Street 4" },
{ icon: PaintBrushIcon, title: "Studio", detail: "River Street 21" },
{ icon: BreadIcon, title: "Bakery on the corner", detail: "Market Lane 7" },
]
function project(velocity: number, deceleration: number) {
return ((velocity / 1000) * deceleration) / (1 - deceleration)
}
export function MapSheet() {
const { deceleration, settle: spring } = useMapSheetTuning()
const reduced = useReducedMotion()
const y = useMotionValue(MEDIUM)
const inset = useTransform(y, [LARGE, MEDIUM, SMALL], [0, GAP, PILL_INSET])
const height = useTransform(() => SHEET - y.get() - inset.get())
const radius = useTransform(
y,
[LARGE, MEDIUM, SMALL],
[STAGE_RADIUS, STAGE_RADIUS - GAP, PILL_HEIGHT / 2],
)
const cornerShape = useTransform(y, [MEDIUM, SMALL], [2, 1])
const contentX = useTransform(inset, (value) => GAP - value)
const rowInset = useTransform(inset, (value) => Math.max(0, value - GAP))
const rowY = useTransform(y, [MEDIUM, SMALL], [0, -(ROW_HEIGHT - PILL_HEIGHT) / 2])
const grabberY = useTransform(y, [MEDIUM, SMALL], [0, -2])
const tint = useTransform(y, [LARGE, MEDIUM], [0.92, 0.12])
const dim = useTransform(y, [LARGE, MEDIUM], [0.3, 0])
const controlsY = useTransform(y, (value) => value - MEDIUM)
const controls = useTransform(y, [MEDIUM * 0.5, MEDIUM], [0, 1])
function settle(target: number, velocity = 0) {
animate(y, target, reduced ? { duration: 0 } : { type: "spring", ...spring, velocity })
}
function nearestDetent(position: number) {
return DETENTS.reduce((best, detent) =>
Math.abs(detent - position) < Math.abs(best - position) ? detent : best,
)
}
function stepDetent() {
const index = DETENTS.indexOf(nearestDetent(y.get()))
settle(DETENTS[(index + DETENTS.length - 1) % DETENTS.length])
}
return (
<>
<div
style={{ height: STAGE_HEIGHT, borderRadius: STAGE_RADIUS }}
className={
"corner-squircle relative w-full max-w-[360px] overflow-clip bg-neutral-100 text-neutral-900 shadow-card dark:bg-neutral-900 dark:text-white"
}
>
{
}
<motion.div
aria-hidden={"true"}
drag
dragConstraints={{ top: -MAP_PAN, right: MAP_PAN, bottom: MAP_PAN, left: -MAP_PAN }}
dragElastic={0.12}
dragMomentum={!reduced}
dragTransition={{
power: 0.2,
timeConstant: 200,
bounceStiffness: 400,
bounceDamping: 40,
}}
style={{ inset: -MAP_PAN, touchAction: "pan-y" }}
className={"absolute cursor-grab active:cursor-grabbing"}
>
<MapArt />
<div className={"absolute top-[356px] left-[262px]"}>
<span
className={
"-translate-1/2 mask-radial-from-10% mask-radial-to-50% absolute top-0 left-0 size-24 -rotate-60 bg-[conic-gradient(from_-28deg,oklch(0.62_0.19_252/0.55)_0deg_56deg,transparent_56deg)]"
}
/>
<span
className={
"-translate-1/2 relative block size-[15px] rounded-full bg-[#0a84ff] shadow-[0_0_0_3.5px_white,0_1px_6px_3.5px_oklch(0_0_0/0.25)]"
}
/>
</div>
</motion.div>
<motion.div
aria-hidden={"true"}
style={{ opacity: dim }}
className={"pointer-events-none absolute inset-0 bg-black"}
/>
<div
aria-hidden={"true"}
className={
"pointer-events-none absolute inset-x-0 top-0 flex h-[50px] items-center justify-between px-[42px] pt-1 font-semibold text-[16px] text-neutral-900 dark:text-white"
}
>
<span className={"tabular-nums"}>9:41</span>
<span className={"flex items-center gap-1.5"}>
<CellSignalFullIcon size={17} weight={"fill"} />
<WifiHighIcon size={17} weight={"bold"} />
<BatteryFullIcon size={24} weight={"fill"} />
</span>
</div>
<motion.div
style={{ y: controlsY, opacity: controls, top: TOP + MEDIUM - 17 - 104 }}
className={"pointer-events-none absolute inset-x-[22px] flex items-end justify-between"}
>
<div className={"pointer-events-auto flex flex-col gap-[10px]"}>
<button
type={"button"}
aria-label={"3D view"}
className={`grid size-[47px] cursor-pointer place-items-center rounded-full font-semibold text-[17px] ${CONTROL}`}
>
3D
</button>
<button
type={"button"}
aria-label={"Look around"}
className={`grid size-[47px] cursor-pointer place-items-center rounded-full ${CONTROL}`}
>
<BinocularsIcon size={22} weight={"fill"} />
</button>
</div>
<div className={`pointer-events-auto flex w-[47px] flex-col rounded-full ${CONTROL}`}>
<button
type={"button"}
aria-label={"Driving directions"}
className={"grid h-[52px] cursor-pointer place-items-center"}
>
<CarProfileIcon size={24} weight={"fill"} />
</button>
<button
type={"button"}
aria-label={"Show my location"}
className={"grid h-[52px] cursor-pointer place-items-center"}
>
<NavigationArrowIcon size={21} className={"-scale-x-100"} />
</button>
</div>
</motion.div>
<motion.section
aria-label={"Search and places"}
drag={"y"}
dragConstraints={{ top: LARGE, bottom: SMALL }}
dragElastic={0.08}
dragMomentum={false}
style={{ y, top: TOP, height: SHEET }}
onDragEnd={(_, { velocity }) => {
const landing = y.get() + project(velocity.y, deceleration)
settle(nearestDetent(landing), velocity.y)
}}
className={"@container absolute inset-x-0 touch-none"}
>
<motion.div
style={
{
left: inset,
right: inset,
height,
borderRadius: radius,
"--corner": cornerShape,
} as MotionStyle
}
className={`absolute top-0 overflow-hidden [corner-shape:superellipse(var(--corner))] ${GLASS}`}
>
<motion.div
aria-hidden={"true"}
style={{ opacity: tint }}
className={"absolute inset-0 bg-white dark:bg-[#1c1c1e]"}
/>
<button
type={"button"}
aria-label={"Resize the sheet"}
onClick={stepDetent}
className={
"absolute inset-x-0 top-0 z-10 flex h-[15px] cursor-grab justify-center pt-[5px] outline-none active:cursor-grabbing"
}
>
<motion.span
style={{ y: grabberY }}
className={"h-1 w-14 rounded-full bg-black/20 dark:bg-white/30"}
/>
</button>
<motion.div
style={{ x: contentX, width: `calc(100cqw - ${GAP * 2}px)` }}
className={"relative px-[15px] pt-[15px] text-neutral-900 dark:text-white"}
>
<motion.div
style={{ paddingLeft: rowInset, paddingRight: rowInset, y: rowY }}
className={"flex items-center gap-2"}
>
<label
className={`flex h-10 min-w-0 flex-1 items-center gap-[7px] rounded-full pr-3.5 pl-[11px] ${FILL}`}
>
<MagnifyingGlassIcon
size={20}
weight={"bold"}
className={"shrink-0 opacity-60"}
/>
<input
type={"search"}
placeholder={"Search Maps"}
aria-label={"Search Maps"}
className={
"min-w-0 flex-1 bg-transparent text-[17px] outline-none placeholder:text-neutral-900/50 dark:placeholder:text-white/55"
}
/>
<MicrophoneIcon size={20} className={"shrink-0 opacity-60"} />
</label>
<span
aria-hidden={"true"}
className={
"grid size-[42px] shrink-0 place-items-center rounded-full bg-gradient-to-br from-sky-400 to-indigo-500 font-semibold text-[17px] text-white"
}
>
P
</span>
</motion.div>
<div className={`corner-squircle mt-[22px] rounded-[22px] ${FILL}`}>
<div className={"flex items-center gap-[19px] py-[17px] pr-4 pl-[17px]"}>
<span
className={
"grid size-7 shrink-0 place-items-center rounded-full bg-[#5ac8fa] text-white"
}
>
<MapTrifoldIcon size={16} weight={"fill"} />
</span>
<div className={"min-w-0 flex-1"}>
<p className={"font-semibold text-[17px] leading-[22px]"}>Weekend in Lisbon</p>
<p className={"text-[15px] leading-5 opacity-60"}>
Save the area around your hotel so directions work without data.
</p>
</div>
<XIcon size={16} weight={"bold"} className={"shrink-0 self-start opacity-50"} />
</div>
<p
className={
"ml-16 border-black/10 border-t py-[14px] text-[#0a84ff] text-[17px] dark:border-white/10 dark:text-[#5ac8fa]"
}
>
Save offline map
</p>
</div>
<p className={"mt-[26px] mb-2.5 pl-1 font-bold text-[22px] leading-7"}>Suggestions</p>
<div
className={`corner-squircle flex h-[65px] items-center gap-3 rounded-[20px] px-[15px] ${FILL}`}
>
<span
className={
"grid size-[31px] shrink-0 place-items-center rounded-full bg-gradient-to-b from-[#3d9bff] to-[#0a6cff] text-white"
}
>
<CarProfileIcon size={17} weight={"fill"} />
</span>
<div className={"min-w-0 flex-1"}>
<p className={"font-semibold text-[17px] leading-[22px]"}>Parked car</p>
<p className={"truncate text-[15px] leading-5 opacity-60"}>
Near the bakery, 3 min walk
</p>
</div>
<DotsThreeIcon size={22} weight={"bold"} className={"shrink-0 opacity-70"} />
</div>
<p className={"mt-[26px] mb-2.5 pl-1 font-bold text-[22px] leading-7"}>Recents</p>
<ul className={`corner-squircle overflow-hidden rounded-[20px] ${FILL}`}>
{RECENTS.map(({ icon: Icon, title, detail }) => (
<li
key={title}
className={"group/row flex h-[62px] items-center gap-3 pl-[15px]"}
>
<span
className={
"grid size-[31px] shrink-0 place-items-center rounded-full bg-black/[0.07] dark:bg-white/15"
}
>
<Icon size={16} weight={"fill"} />
</span>
<div
className={
"flex h-full min-w-0 flex-1 flex-col justify-center border-black/10 border-b pr-[15px] group-last/row:border-b-0 dark:border-white/10"
}
>
<p className={"font-semibold text-[17px] leading-[22px]"}>{title}</p>
<p className={"truncate text-[15px] leading-5 opacity-60"}>{detail}</p>
</div>
</li>
))}
</ul>
</motion.div>
</motion.div>
</motion.section>
</div>
</>
)
}