123 lines
3.5 KiB
TypeScript
123 lines
3.5 KiB
TypeScript
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
|
//
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
// copyright notice and this permission notice appear in all copies.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
import { clsx } from "clsx";
|
|
import {
|
|
createContext,
|
|
type PropsWithChildren,
|
|
type ReactNode,
|
|
useContext,
|
|
useEffect,
|
|
useMemo,
|
|
useState,
|
|
} from "react";
|
|
import { createPortal } from "react-dom";
|
|
import { Link } from "react-router";
|
|
|
|
import { Logo } from "../Atoms/Logo/Logo";
|
|
import { Sidebar } from "../Atoms/Sidebar/Sidebar";
|
|
import { Toasts } from "../Atoms/Toasts/Toasts";
|
|
import { ConfirmDialog } from "../Molecules/Dialog/ConfirmDialog";
|
|
|
|
type Props = PropsWithChildren<{
|
|
headerLeading?: ReactNode;
|
|
headerTrailing: ReactNode;
|
|
sidebar?: ReactNode;
|
|
}>;
|
|
|
|
const LayoutContext = createContext({
|
|
setDrawer: (() => {}) as (v: boolean) => void,
|
|
});
|
|
|
|
export function Layout({
|
|
headerLeading,
|
|
headerTrailing,
|
|
sidebar,
|
|
children,
|
|
}: Props) {
|
|
const [hasDrawer, setDrawer] = useState(false);
|
|
const layoutContext = useMemo(
|
|
() => ({
|
|
setDrawer,
|
|
}),
|
|
[],
|
|
);
|
|
return (
|
|
<LayoutContext value={layoutContext}>
|
|
<div className="text-txt-primary bg-level-0 min-h-screen">
|
|
<header className="fixed top-0 z-2 left-0 right-0 px-4 flex items-center border-b border-border-solid h-12 bg-level-0">
|
|
<Link to="/">
|
|
<Logo className="w-12 h-5" />
|
|
</Link>
|
|
{headerLeading && (
|
|
<>
|
|
<svg
|
|
className="mx-3 text-txt-tertiary"
|
|
width="8"
|
|
height="18"
|
|
viewBox="0 0 8 18"
|
|
fill="none"
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
>
|
|
<path d="M1 17L7 1" stroke="currentColor" />
|
|
</svg>
|
|
{headerLeading}
|
|
</>
|
|
)}
|
|
<div className="ml-auto">{headerTrailing}</div>
|
|
</header>
|
|
<div className="flex min-h-screen" id="main">
|
|
{sidebar && <Sidebar>{sidebar}</Sidebar>}
|
|
<main
|
|
className={clsx(
|
|
"w-full mt-12 transition-all duration-300",
|
|
hasDrawer && "pr-105",
|
|
)}
|
|
>
|
|
<div className="py-12 px-8 max-w-[1200px] w-full mx-auto min-h-[calc(100vh-48px)]">
|
|
{children}
|
|
</div>
|
|
</main>
|
|
</div>
|
|
<Toasts />
|
|
<ConfirmDialog />
|
|
</div>
|
|
</LayoutContext>
|
|
);
|
|
}
|
|
|
|
export function Drawer({
|
|
children,
|
|
className,
|
|
}: PropsWithChildren<{ className?: string }>) {
|
|
const { setDrawer } = useContext(LayoutContext);
|
|
useEffect(() => {
|
|
setDrawer(true);
|
|
return () => {
|
|
setDrawer(false);
|
|
};
|
|
}, [setDrawer]);
|
|
return createPortal(
|
|
<aside
|
|
className={clsx(
|
|
"fixed pt-20 top-0 right-0 w-105 px-6 pb-8 border-border-solid border-l h-screen bg-level-0",
|
|
className,
|
|
)}
|
|
>
|
|
{children}
|
|
</aside>,
|
|
document.body,
|
|
);
|
|
}
|