medkit/src/kit/app-shell.tsx

59 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import * as React from "react"
import { useMediaQuery } from "../hooks/use-media-query"
import { cn } from "../lib/utils"
import { AppSidebar, type AppSidebarProps } from "./app-sidebar"
import { useShell } from "./shell-provider"
/**
* Каркас окна: тёмное меню слева, справа — лист с текущим разделом (`children`, обычно `<Outlet />`).
* Окно целиком не прокручивается — только тело раздела. Свёрнутость меню помнится
* (`<storageKey>.sidebar-collapsed`), на узком экране меню всегда свёрнуто. При печати меню скрыто.
* Нужен ShellProvider выше.
*/
export function AppShell({
sidebar,
className,
children,
}: {
sidebar: Omit<AppSidebarProps, "collapsed" | "onToggle">
className?: string
children: React.ReactNode
}) {
const { storageKey } = useShell()
const collapsedKey = `${storageKey}.sidebar-collapsed`
const [collapsed, setCollapsed] = React.useState(() => {
try {
return localStorage.getItem(collapsedKey) === "1"
} catch {
return false
}
})
// На узких экранах меню — всегда полоса иконок, иначе контенту не остаётся места.
const narrow = useMediaQuery("(max-width: 767px)")
const toggle = () =>
setCollapsed((v) => {
try {
localStorage.setItem(collapsedKey, v ? "0" : "1")
} catch {
// не запомнили — не страшно
}
return !v
})
return (
<div
className={cn(
"flex h-svh overflow-clip bg-sidebar print:block print:h-auto print:overflow-visible print:bg-transparent",
className,
)}
>
<AppSidebar {...sidebar} collapsed={collapsed || narrow} onToggle={toggle} />
<main className="flex min-h-0 min-w-0 flex-1 flex-col overflow-clip bg-background lg:my-2 lg:mr-2 lg:rounded-2xl dark:lg:ring-1 dark:lg:ring-white/6 print:m-0 print:block print:overflow-visible print:rounded-none print:bg-transparent">
{children}
</main>
</div>
)
}