import { Dialog as DialogPrimitive } from "@kobalte/core/dialog"; import { createEffect, createSignal, onCleanup, type ParentProps } from "solid-js"; import { ConfirmDialog } from "@/components/ui/confirm-dialog"; import { IconX } from "@/components/ui/icons"; import { cn } from "@/lib/cn"; import { useT } from "@/stores/preferences-context"; let openPanelCount = 0; export function SidePanel( props: ParentProps<{ open: boolean; onOpenChange: (open: boolean) => void; title: string; description?: string; class?: string; /** default ~34rem, wide ~42rem, xl ~48rem — for report tables */ size?: "default" | "wide" | "xl"; bodyClass?: string; /** * The form inside holds unsaved input. Escape, an outside click or the * close button then ask before discarding it; a submit or an explicit * Cancel that sets `open` directly is not asked. */ dirty?: boolean; /** * Treat any input or change inside the panel as unsaved work, for forms * that keep their state to themselves. Ignored when `dirty` is given. * Leave it off for panels whose controls save as you go. */ guardUnsaved?: boolean; }>, ) { const t = useT(); const [confirmDiscard, setConfirmDiscard] = createSignal(false); const [touched, setTouched] = createSignal(false); createEffect(() => { if (props.open) setTouched(false); }); const isDirty = () => props.dirty ?? (!!props.guardUnsaved && touched()); const markTouched = () => { if (props.guardUnsaved) setTouched(true); }; const requestOpenChange = (open: boolean) => { // A press inside the confirm dialog reads as an outside interaction to // this non-modal panel; while the dialog is up it owns the decision. if (!open && confirmDiscard()) return; if (!open && isDirty()) return setConfirmDiscard(true); props.onOpenChange(open); }; // Kobalte's modal Dialog hides the app with aria-hidden before its focus // scope can move focus into this portalled panel. Chromium correctly blocks // that transition when the opener still owns focus. `inert` makes the app // unfocusable as well as hidden to assistive tech, while the portal remains // interactive outside #root. createEffect(() => { if (!props.open || typeof document === "undefined") return; const root = document.getElementById("root"); openPanelCount += 1; root?.setAttribute("inert", ""); onCleanup(() => { openPanelCount -= 1; if (openPanelCount === 0) root?.removeAttribute("inert"); }); }); const width = () => { if (props.size === "xl") return "max-w-[min(48rem,100vw)]"; if (props.size === "wide") return "max-w-[min(42rem,100vw)]"; return "max-w-[min(34rem,100vw)]"; }; return ( <> {/* z-[70]: above the mobile nav sheet (z-60) and tab bar (z-40). */}
{props.title} {props.description && ( {props.description} )}
form]:flex [&>form]:min-h-full [&>form]:flex-col [&>form>div:last-child]:mt-auto", props.bodyClass, )} onInput={markTouched} onChange={markTouched} > {props.children}
props.onOpenChange(false)} /> ); }