|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useRef } from "react"; |
| 4 | +import { X } from "lucide-react"; |
| 5 | +import { cn } from "@/lib/utils"; |
| 6 | + |
| 7 | +type Props = { |
| 8 | + open: boolean; |
| 9 | + onClose: () => void; |
| 10 | + ariaLabel: string; |
| 11 | + /** Optional title rendered in the drawer header. */ |
| 12 | + title?: string; |
| 13 | + children: React.ReactNode; |
| 14 | +}; |
| 15 | + |
| 16 | +/** |
| 17 | + * Left-anchored mobile drawer. Renders nothing at >= lg — desktop hosts the |
| 18 | + * same content in a sticky <aside> directly. Used by /analyze (ModelList) |
| 19 | + * and /ask (ThreadList) so phone bankers can pick a model / thread. |
| 20 | + * |
| 21 | + * Closes on: backdrop tap, Esc, programmatic onClose. Locks body scroll |
| 22 | + * while open and returns focus to the trigger element on close. |
| 23 | + */ |
| 24 | +export function MobileDrawer({ |
| 25 | + open, |
| 26 | + onClose, |
| 27 | + ariaLabel, |
| 28 | + title, |
| 29 | + children, |
| 30 | +}: Props) { |
| 31 | + const panelRef = useRef<HTMLDivElement>(null); |
| 32 | + const triggerRef = useRef<HTMLElement | null>(null); |
| 33 | + |
| 34 | + // Capture the element that triggered the open so we can restore focus |
| 35 | + // when the drawer closes — important for keyboard users. |
| 36 | + useEffect(() => { |
| 37 | + if (open) { |
| 38 | + triggerRef.current = document.activeElement as HTMLElement | null; |
| 39 | + // Defer focus to next tick so the slide-in animation has the panel |
| 40 | + // mounted before we move focus into it. |
| 41 | + requestAnimationFrame(() => { |
| 42 | + panelRef.current?.focus(); |
| 43 | + }); |
| 44 | + } else if (triggerRef.current) { |
| 45 | + triggerRef.current.focus?.(); |
| 46 | + triggerRef.current = null; |
| 47 | + } |
| 48 | + }, [open]); |
| 49 | + |
| 50 | + // Esc closes; body scroll lock while open. |
| 51 | + useEffect(() => { |
| 52 | + if (!open) return; |
| 53 | + function onKey(e: KeyboardEvent) { |
| 54 | + if (e.key === "Escape") { |
| 55 | + e.preventDefault(); |
| 56 | + onClose(); |
| 57 | + } |
| 58 | + } |
| 59 | + const prevOverflow = document.body.style.overflow; |
| 60 | + document.body.style.overflow = "hidden"; |
| 61 | + window.addEventListener("keydown", onKey); |
| 62 | + return () => { |
| 63 | + document.body.style.overflow = prevOverflow; |
| 64 | + window.removeEventListener("keydown", onKey); |
| 65 | + }; |
| 66 | + }, [open, onClose]); |
| 67 | + |
| 68 | + if (!open) return null; |
| 69 | + |
| 70 | + return ( |
| 71 | + <div |
| 72 | + className="fixed inset-0 z-[70] flex items-stretch justify-start bg-black/60 backdrop-blur-[4px] animate-fade-in lg:hidden" |
| 73 | + onClick={onClose} |
| 74 | + role="presentation" |
| 75 | + > |
| 76 | + <div |
| 77 | + ref={panelRef} |
| 78 | + role="dialog" |
| 79 | + aria-modal="true" |
| 80 | + aria-label={ariaLabel} |
| 81 | + tabIndex={-1} |
| 82 | + onClick={(e) => e.stopPropagation()} |
| 83 | + className={cn( |
| 84 | + "relative flex h-full w-[min(86vw,360px)] flex-col border-r border-border-soft bg-surface shadow-[0_28px_60px_-30px_rgba(0,0,0,0.75)] outline-none animate-slide-in-left", |
| 85 | + "pl-[env(safe-area-inset-left,0px)] pt-[env(safe-area-inset-top,0px)] pb-[env(safe-area-inset-bottom,0px)]" |
| 86 | + )} |
| 87 | + > |
| 88 | + <div className="flex items-center justify-between gap-3 border-b border-border-soft/60 px-4 py-3"> |
| 89 | + <span className="text-[11px] font-medium uppercase tracking-[0.2em] text-text-muted"> |
| 90 | + {title ?? ariaLabel} |
| 91 | + </span> |
| 92 | + <button |
| 93 | + type="button" |
| 94 | + onClick={onClose} |
| 95 | + aria-label="Close" |
| 96 | + className="flex min-h-[44px] min-w-[44px] items-center justify-center rounded-md text-text-muted transition hover:bg-surface2 hover:text-text" |
| 97 | + > |
| 98 | + <X size={16} /> |
| 99 | + </button> |
| 100 | + </div> |
| 101 | + <div className="min-h-0 flex-1 overflow-y-auto">{children}</div> |
| 102 | + </div> |
| 103 | + </div> |
| 104 | + ); |
| 105 | +} |
0 commit comments