hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { Dialog as DialogPrimitive } from "@kobalte/core/dialog"; |
| 2 | import type { ComponentProps, ParentProps, ValidComponent } from "solid-js"; |
| 3 | import { Show, splitProps } from "solid-js"; |
| 4 | import { IconX } from "@/components/ui/icons"; |
| 5 | import { cn } from "@/lib/cn"; |
| 6 | import { useT } from "@/stores/preferences-context"; |
| 7 | |
| 8 | export const Dialog = DialogPrimitive; |
| 9 | export const DialogTrigger = DialogPrimitive.Trigger; |
| 10 | export const DialogClose = DialogPrimitive.CloseButton; |
| 11 | |
| 12 | type DialogContentExtras = { |
| 13 | dismissable?: boolean; |
| 14 | closeButton?: boolean; |
| 15 | /** "top" pins the dialog high on the screen, so a panel whose height |
| 16 | * follows its content (the command palette's results) grows downward |
| 17 | * instead of jumping up and down around the centre while you type. */ |
| 18 | position?: "center" | "top"; |
| 19 | }; |
| 20 | |
| 21 | export function DialogContent<T extends ValidComponent = "div">( |
| 22 | props: ComponentProps<typeof DialogPrimitive.Content<T>> & DialogContentExtras, |
| 23 | ) { |
| 24 | const [local, rest] = splitProps( |
| 25 | props as ComponentProps<typeof DialogPrimitive.Content> & DialogContentExtras, |
| 26 | ["class", "children", "dismissable", "closeButton", "position"], |
| 27 | ); |
| 28 | // The close button speaks the app language; a bare render (unit tests, |
| 29 | // no PreferencesProvider) falls back to English instead of throwing. |
| 30 | let t: ReturnType<typeof useT> | undefined; |
| 31 | try { |
| 32 | t = useT(); |
| 33 | } catch { |
| 34 | t = undefined; |
| 35 | } |
| 36 | const closeLabel = () => t?.("common.close") ?? "Close"; |
| 37 | return ( |
| 38 | <DialogPrimitive.Portal> |
| 39 | {/* z-[70]: above the mobile nav sheet (z-60) and tab bar (z-40). */} |
| 40 | <DialogPrimitive.Overlay class="fixed inset-0 z-[70] bg-[rgba(13,15,23,0.55)] transition-opacity duration-200" /> |
| 41 | {/* flex center — animate-fade-up must not own transform positioning */} |
| 42 | {/* The centring box is inset by the system bars, so a tall dialog is |
| 43 | never clipped by the status bar or the gesture bar. */} |
| 44 | <div |
| 45 | class={cn( |
| 46 | "pointer-events-none fixed inset-0 z-[70] flex justify-center p-3 pb-[calc(0.75rem+max(env(safe-area-inset-bottom),var(--android-nav-inset,0px)))] pt-[calc(0.75rem+env(safe-area-inset-top))] sm:p-4", |
| 47 | local.position === "top" ? "items-start sm:pt-[12vh]" : "items-center", |
| 48 | )} |
| 49 | > |
| 50 | <DialogPrimitive.Content |
| 51 | // Explicit: with any AlertDialog in the tree, Kobalte hands plain |
| 52 | // dialogs its "alertdialog" role too. |
| 53 | role="dialog" |
| 54 | class={cn( |
| 55 | "pointer-events-auto relative flex max-h-[min(100%,48rem)] w-full max-w-[480px] flex-col overflow-hidden border border-border-line bg-surface-base text-foreground shadow-[0_16px_40px_rgba(0,0,0,0.16)] outline-hidden animate-fade-up rounded-xl sm:rounded-2xl", |
| 56 | local.class, |
| 57 | )} |
| 58 | // Standard dialogs dismiss outside/ESC; pass false only when losing |
| 59 | // unsaved state would be unsafe. |
| 60 | onPointerDownOutside={local.dismissable === false ? (e) => e.preventDefault() : undefined} |
| 61 | onInteractOutside={local.dismissable === false ? (e) => e.preventDefault() : undefined} |
| 62 | onEscapeKeyDown={local.dismissable === false ? (e) => e.preventDefault() : undefined} |
| 63 | {...rest} |
| 64 | > |
| 65 | {/* `closeButton={false}` lets a dialog draw its own close control |
| 66 | (the command palette labels it, beside its clear-search ×). */} |
| 67 | <Show when={local.closeButton !== false}> |
| 68 | <DialogPrimitive.CloseButton |
| 69 | type="button" |
| 70 | class="absolute right-4 top-4 z-10 inline-flex h-8 w-8 items-center justify-center rounded-md text-muted-foreground transition-all hover:bg-accent hover:text-foreground focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2" |
| 71 | aria-label={closeLabel()} |
| 72 | title={closeLabel()} |
| 73 | > |
| 74 | <IconX class="h-4 w-4" /> |
| 75 | </DialogPrimitive.CloseButton> |
| 76 | </Show> |
| 77 | {local.children} |
| 78 | </DialogPrimitive.Content> |
| 79 | </div> |
| 80 | </DialogPrimitive.Portal> |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | export function DialogHeader(props: ParentProps<{ class?: string }>) { |
| 85 | return <div class={cn("flex flex-col space-y-1.5 border-b border-border-hairline px-5 py-5 pr-14 text-center sm:px-7 sm:text-left", props.class)}>{props.children}</div>; |
| 86 | } |
| 87 | |
| 88 | export function DialogBody(props: ParentProps<{ class?: string }>) { |
| 89 | return <div class={cn("overflow-y-auto overscroll-contain px-5 py-5 sm:px-7", props.class)}>{props.children}</div>; |
| 90 | } |
| 91 | |
| 92 | export function DialogTitle<T extends ValidComponent = "h2">( |
| 93 | props: ComponentProps<typeof DialogPrimitive.Title<T>>, |
| 94 | ) { |
| 95 | const [local, rest] = splitProps(props as ComponentProps<typeof DialogPrimitive.Title>, ["class"]); |
| 96 | return ( |
| 97 | <DialogPrimitive.Title |
| 98 | class={cn("text-lg font-semibold leading-none tracking-tight", local.class)} |
| 99 | {...rest} |
| 100 | /> |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | export function DialogDescription<T extends ValidComponent = "p">( |
| 105 | props: ComponentProps<typeof DialogPrimitive.Description<T>>, |
| 106 | ) { |
| 107 | const [local, rest] = splitProps(props as ComponentProps<typeof DialogPrimitive.Description>, ["class"]); |
| 108 | return ( |
| 109 | <DialogPrimitive.Description |
| 110 | class={cn("mt-1.5 text-sm text-muted-foreground", local.class)} |
| 111 | {...rest} |
| 112 | /> |
| 113 | ); |
| 114 | } |