"use client"; import { useCallback, useEffect, useRef, useSyncExternalStore } from "react"; import { cn } from "@/lib/utils"; // --------------------------------------------------------------------------- // THE BOTTOM SHEET -- the phone's one surface for "more than fits": the page's // ⋯ actions, the dock's ≡ More, Work's filters, a row's ⋯ menu. On a phone the // actions live where the thumb is, so a menu that drops from the top of the // screen becomes a sheet that rises from the bottom. // // A sheet is at most 80% of the viewport, its body scrolls on its own (the one // inner scroll a phone page has: a sheet's list is its own thing), and it // closes four ways, all of them visible or obvious: the Close button, a tap on // the dimmed page, a drag down on its handle, and Escape. While one is open the // page under it does not scroll, and the place bar does not hide. // // No motion: the lit line's glide is the app's one movement. A drag follows the // finger (that is the finger moving it, not an animation) and lets go. // // Two shapes share the look (app/globals.css, "phone: the shell"): // mounted while open (a menu's panel, More, a row's ⋯). // useSheetFrame() for a container that is ALWAYS rendered and is only a // sheet on a phone (the page's actions, Work's filters): on // the desk the same element sits inline, as it always did. // Which sheet is open is a tiny shared store, so a trigger in one place (the // place bar's ⋯) can open a container rendered in another (the page head). // --------------------------------------------------------------------------- let current: string | null = null; const listeners = new Set<() => void>(); const emit = () => listeners.forEach((l) => l()); export function openSheet(id: string) { current = id; emit(); } export function closeSheet(id?: string) { if (id && current !== id) return; current = null; emit(); } /** Whether the named sheet is the open one. */ export function useSheetOpen(id: string): boolean { return useSyncExternalStore( (on) => { listeners.add(on); return () => listeners.delete(on); }, () => current === id, () => false, ); } /** How many sheets are open, on as `data-sheet-open` (the page holds still, the place bar stays). */ let opened = 0; function hold(on: boolean) { opened = Math.max(0, opened + (on ? 1 : -1)); if (opened > 0) document.documentElement.setAttribute("data-sheet-open", ""); else document.documentElement.removeAttribute("data-sheet-open"); } /** * The behaviour every open sheet has: Escape closes it, the focus moves into * it and goes back to what opened it, and the page under it holds still. */ export function useSheetFrame(open: boolean, onClose: () => void, panel: React.RefObject) { const close = useRef(onClose); close.current = onClose; useEffect(() => { if (!open) return; const opener = document.activeElement as HTMLElement | null; hold(true); panel.current?.focus({ preventScroll: true }); const key = (e: KeyboardEvent) => { if (e.key === "Escape") close.current(); }; document.addEventListener("keydown", key); return () => { document.removeEventListener("keydown", key); hold(false); if (opener && opener.isConnected) opener.focus({ preventScroll: true }); }; }, [open, panel]); } /** * The sheet's head: the grab handle (drag it down to close), what this sheet * is, and Close. Rendered only while the sheet is open. */ export function SheetHead({ label, onClose, panel }: { label: React.ReactNode; onClose: () => void; panel: React.RefObject }) { const drag = useRef<{ y: number; id: number } | null>(null); const end = useCallback( (e: React.PointerEvent, cancel = false) => { const d = drag.current; if (!d || d.id !== e.pointerId) return; drag.current = null; const dy = e.clientY - d.y; if (panel.current) panel.current.style.translate = ""; if (!cancel && dy > 56) onClose(); }, [onClose, panel], ); return (
{ if ((e.target as HTMLElement).closest("button")) return; drag.current = { y: e.clientY, id: e.pointerId }; (e.currentTarget as HTMLElement).setPointerCapture(e.pointerId); }} onPointerMove={(e) => { const d = drag.current; if (!d || d.id !== e.pointerId || !panel.current) return; panel.current.style.translate = `0 ${Math.max(0, e.clientY - d.y)}px`; }} onPointerUp={(e) => end(e)} onPointerCancel={(e) => end(e, true)} >