hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { Dialog as DialogPrimitive } from "@kobalte/core/dialog"; |
| 2 | import { createEffect, createSignal, onCleanup, type ParentProps } from "solid-js"; |
| 3 | import { ConfirmDialog } from "@/components/ui/confirm-dialog"; |
| 4 | import { IconX } from "@/components/ui/icons"; |
| 5 | import { cn } from "@/lib/cn"; |
| 6 | import { useT } from "@/stores/preferences-context"; |
| 7 | |
| 8 | let openPanelCount = 0; |
| 9 | |
| 10 | export function SidePanel( |
| 11 | props: ParentProps<{ |
| 12 | open: boolean; |
| 13 | onOpenChange: (open: boolean) => void; |
| 14 | title: string; |
| 15 | description?: string; |
| 16 | class?: string; |
| 17 | /** default ~34rem, wide ~42rem, xl ~48rem — for report tables */ |
| 18 | size?: "default" | "wide" | "xl"; |
| 19 | bodyClass?: string; |
| 20 | /** |
| 21 | * The form inside holds unsaved input. Escape, an outside click or the |
| 22 | * close button then ask before discarding it; a submit or an explicit |
| 23 | * Cancel that sets `open` directly is not asked. |
| 24 | */ |
| 25 | dirty?: boolean; |
| 26 | /** |
| 27 | * Treat any input or change inside the panel as unsaved work, for forms |
| 28 | * that keep their state to themselves. Ignored when `dirty` is given. |
| 29 | * Leave it off for panels whose controls save as you go. |
| 30 | */ |
| 31 | guardUnsaved?: boolean; |
| 32 | }>, |
| 33 | ) { |
| 34 | const t = useT(); |
| 35 | const [confirmDiscard, setConfirmDiscard] = createSignal(false); |
| 36 | const [touched, setTouched] = createSignal(false); |
| 37 | createEffect(() => { |
| 38 | if (props.open) setTouched(false); |
| 39 | }); |
| 40 | const isDirty = () => props.dirty ?? (!!props.guardUnsaved && touched()); |
| 41 | const markTouched = () => { |
| 42 | if (props.guardUnsaved) setTouched(true); |
| 43 | }; |
| 44 | const requestOpenChange = (open: boolean) => { |
| 45 | // A press inside the confirm dialog reads as an outside interaction to |
| 46 | // this non-modal panel; while the dialog is up it owns the decision. |
| 47 | if (!open && confirmDiscard()) return; |
| 48 | if (!open && isDirty()) return setConfirmDiscard(true); |
| 49 | props.onOpenChange(open); |
| 50 | }; |
| 51 | |
| 52 | // Kobalte's modal Dialog hides the app with aria-hidden before its focus |
| 53 | // scope can move focus into this portalled panel. Chromium correctly blocks |
| 54 | // that transition when the opener still owns focus. `inert` makes the app |
| 55 | // unfocusable as well as hidden to assistive tech, while the portal remains |
| 56 | // interactive outside #root. |
| 57 | createEffect(() => { |
| 58 | if (!props.open || typeof document === "undefined") return; |
| 59 | const root = document.getElementById("root"); |
| 60 | openPanelCount += 1; |
| 61 | root?.setAttribute("inert", ""); |
| 62 | onCleanup(() => { |
| 63 | openPanelCount -= 1; |
| 64 | if (openPanelCount === 0) root?.removeAttribute("inert"); |
| 65 | }); |
| 66 | }); |
| 67 | |
| 68 | const width = () => { |
| 69 | if (props.size === "xl") return "max-w-[min(48rem,100vw)]"; |
| 70 | if (props.size === "wide") return "max-w-[min(42rem,100vw)]"; |
| 71 | return "max-w-[min(34rem,100vw)]"; |
| 72 | }; |
| 73 | |
| 74 | return ( |
| 75 | <> |
| 76 | <DialogPrimitive open={props.open} onOpenChange={requestOpenChange} modal={false} preventScroll> |
| 77 | <DialogPrimitive.Portal> |
| 78 | {/* z-[70]: above the mobile nav sheet (z-60) and tab bar (z-40). */} |
| 79 | <DialogPrimitive.Overlay class="fixed inset-0 z-[70] bg-[rgba(13,15,23,0.55)] transition-opacity duration-200 data-closed:opacity-0 data-expanded:opacity-100" /> |
| 80 | <DialogPrimitive.Content |
| 81 | // Explicit: with any AlertDialog in the tree (the discard confirm |
| 82 | // below), Kobalte hands plain dialogs its "alertdialog" role too. |
| 83 | role="dialog" |
| 84 | class={cn( |
| 85 | "fixed inset-y-0 right-0 z-[70] flex h-full w-full flex-col border-l border-border-line bg-surface-base text-foreground shadow-[0_16px_40px_rgba(0,0,0,0.16)] outline-hidden", |
| 86 | // The panel spans the whole display, so it has to keep its own |
| 87 | // header out from under the status bar and its footer off the |
| 88 | // gesture bar. Both insets are 0 in a desktop browser. |
| 89 | "pt-[env(safe-area-inset-top)] pb-[max(env(safe-area-inset-bottom),var(--android-nav-inset,0px))]", |
| 90 | "transition-[transform,opacity] duration-200 ease-out data-closed:translate-x-full data-closed:opacity-0 data-expanded:translate-x-0 data-expanded:opacity-100", |
| 91 | width(), |
| 92 | props.class, |
| 93 | )} |
| 94 | > |
| 95 | <div class="flex shrink-0 items-start justify-between gap-4 border-b border-border-hairline px-5 py-4"> |
| 96 | <div class="min-w-0 flex-1 space-y-1"> |
| 97 | <DialogPrimitive.Title class="truncate text-base font-semibold leading-6 tracking-tight"> |
| 98 | {props.title} |
| 99 | </DialogPrimitive.Title> |
| 100 | {props.description && ( |
| 101 | <DialogPrimitive.Description class="line-clamp-2 text-sm leading-5 text-muted-foreground"> |
| 102 | {props.description} |
| 103 | </DialogPrimitive.Description> |
| 104 | )} |
| 105 | </div> |
| 106 | <DialogPrimitive.CloseButton |
| 107 | type="button" |
| 108 | aria-label={t("common.close")} |
| 109 | class="inline-flex h-9 w-9 shrink-0 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-accent hover:text-foreground focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring" |
| 110 | > |
| 111 | <IconX class="h-4 w-4" /> |
| 112 | </DialogPrimitive.CloseButton> |
| 113 | </div> |
| 114 | <div |
| 115 | class={cn( |
| 116 | "side-panel-body min-h-0 flex-1 overflow-x-hidden overflow-y-auto overscroll-contain px-5 py-5", |
| 117 | // Short forms should still use the full panel height so their |
| 118 | // actions settle at the bottom instead of floating above empty space. |
| 119 | "[&>form]:flex [&>form]:min-h-full [&>form]:flex-col [&>form>div:last-child]:mt-auto", |
| 120 | props.bodyClass, |
| 121 | )} |
| 122 | onInput={markTouched} |
| 123 | onChange={markTouched} |
| 124 | > |
| 125 | {props.children} |
| 126 | </div> |
| 127 | </DialogPrimitive.Content> |
| 128 | </DialogPrimitive.Portal> |
| 129 | </DialogPrimitive> |
| 130 | <ConfirmDialog |
| 131 | open={confirmDiscard()} |
| 132 | onOpenChange={setConfirmDiscard} |
| 133 | title={t("sidePanel.discardTitle")} |
| 134 | summary={t("sidePanel.discardSummary")} |
| 135 | confirmLabel={t("sidePanel.discard")} |
| 136 | cancelLabel={t("sidePanel.keepEditing")} |
| 137 | variant="destructive" |
| 138 | onConfirm={() => props.onOpenChange(false)} |
| 139 | /> |
| 140 | </> |
| 141 | ); |
| 142 | } |