import { useState, type ReactNode } from 'react'; import { motion, AnimatePresence } from 'motion/react'; import { useRouterState } from '@tanstack/react-router'; import { Minimize2 } from 'lucide-react'; import { Sheet, SheetContent, SheetDescription, SheetTitle } from '@/components/ui/sheet'; import { ScrollArea } from '@/components/ui/scroll-area'; import { Sidebar } from '@/components/layout/sidebar'; import { TopBar } from '@/components/layout/top-bar'; import { useMediaQuery } from '@/hooks/use-media-query'; import { useTheme } from '@/contexts/ThemeContext'; import { useLayout } from '@/contexts/LayoutContext'; import { useKiosk } from '@/contexts/KioskContext'; import { cn } from '@/lib/utils'; interface MainLayoutProps { status: string; onLogout: () => void; children: ReactNode; } export function MainLayout({ status, onLogout, children }: MainLayoutProps) { const isMobile = !useMediaQuery('(min-width: 768px)'); const { appearance } = useTheme(); const customBg = appearance.background.type !== 'none'; // Framer's reducedMotion only suppresses transforms, so the always-present // width + page-transition animations need an explicit opt-out to make the // “减少动效” setting actually felt. const reduce = appearance.reduceMotion || appearance.disableMotion; const { editing } = useLayout(); const { kiosk, exit: exitKiosk } = useKiosk(); // The sidebar rests as a slim icon rail and auto-expands on hover/focus (the // peek logic below) — there's no collapse button. Two cases force it fully // expanded: layout-edit mode (the drag-to-reorder list needs room) and the // 「钉住侧栏展开」 appearance pref (operator opted out of the rail entirely). const effectiveCollapsed = !editing && !appearance.sidebarPinned; // Hover/focus peek: while collapsed, hovering (or keyboard-focusing) the rail // temporarily expands it. The rail lives in the flex flow, so the page // content is pushed right rather than overlaid. The pinned `collapsed` pref // (and the TopBar toggle that reflects it) is untouched — this is transient. const [hovered, setHovered] = useState(false); const [focused, setFocused] = useState(false); const peeking = hovered || focused; const showCollapsed = effectiveCollapsed && !peeking; const [mobileOpen, setMobileOpen] = useState(false); const pathname = useRouterState({ select: (s) => s.location.pathname }); return (
{/* Desktop sidebar (hidden in kiosk) */} {!isMobile && !kiosk && ( setHovered(true)} onMouseLeave={() => setHovered(false)} onFocusCapture={(e) => { // Only *keyboard* focus (focus-visible) peeks the rail open. A mouse // click on a nav link also focuses it, but must NOT peek — that // click-focus lingers and never blurs when the pointer moves to the // content, so the rail would stay stuck open after navigating. if ((e.target as HTMLElement).matches?.(':focus-visible')) setFocused(true); }} onBlur={(e) => { if (!e.currentTarget.contains(e.relatedTarget as Node)) setFocused(false); }} className="relative h-full shrink-0 overflow-hidden" > {/* Fixed-width inner: the rail's content is ALWAYS laid out at the expanded width and the aside simply clips it as `width` animates. Nothing reflows during the transition, so icons never shift and labels never wrap — they're revealed/clipped, not re-laid-out. */}
)} {/* Mobile sidebar in sheet (hidden in kiosk) */} {isMobile && !kiosk && ( {/* Radix Dialog (the Sheet primitive) requires an accessible name + description; the nav itself is the visible content, so these are screen-reader-only. */} 导航菜单 切换 SnowLuma 控制台的页面。 setMobileOpen(false)} /> )} {/* Kiosk: chrome-free, with a corner button (+ Esc) to exit. */} {kiosk && ( )} {/* Main column — a content "canvas" nested into the chrome with a rounded top-left corner at the sidebar/topbar junction. It's a flex sibling of the rail, so as the rail expands/collapses the whole canvas (and its rounded corner) is pushed along with it. Depth comes from a soft shadow + the canvas tone sitting on the (sidebar-toned) chrome, not a border. */}
{!kiosk && ( setMobileOpen(true)} onLogout={onLogout} isMobile={isMobile} /> )}
{children}
); }