ui: move dialog close button to the sticky header

Assisted-by: pi
This commit is contained in:
Aleksander Grygier
2026-08-22 13:12:05 +02:00
committed by GitHub
parent c53a39b669
commit d3e02783b0
3 changed files with 22 additions and 2 deletions
@@ -14,6 +14,7 @@
<Dialog.Root bind:open {onOpenChange}>
<Dialog.Content
class="z-999999 grid max-h-full max-w-full! grid-rows-[1fr_auto] overflow-hidden p-0 md:h-[90vh] md:max-w-[90vw]!"
showCloseButton
>
<MermaidPreview {svgHtml} />
</Dialog.Content>
@@ -10,7 +10,7 @@
class: className,
portalProps,
ref = $bindable(null),
showCloseButton = true,
showCloseButton = false,
...restProps
}: WithoutChildrenOrChild<DialogPrimitive.ContentProps> & {
portalProps?: DialogPrimitive.PortalProps;
@@ -1,4 +1,6 @@
<script lang="ts">
import { Dialog as DialogPrimitive } from 'bits-ui';
import XIcon from '@lucide/svelte/icons/x';
import { cn, type WithElementRef } from '$lib/components/ui/utils';
import type { HTMLAttributes } from 'svelte/elements';
@@ -6,10 +8,18 @@
children,
class: className,
ref = $bindable(null),
showCloseButton = true,
...restProps
}: WithElementRef<HTMLAttributes<HTMLDivElement>> = $props();
}: WithElementRef<HTMLAttributes<HTMLDivElement>> & {
showCloseButton?: boolean;
} = $props();
</script>
<!--
Header is `sticky`, so it stays at the top while the dialog body scrolls. The close
button lives here (not in the body) so it sticks together with the title. `sticky`
makes it the containing block, so the close can be absolutely placed at its corner.
-->
<div
bind:this={ref}
data-slot="dialog-header"
@@ -20,4 +30,13 @@
{...restProps}
>
{@render children?.()}
{#if showCloseButton}
<DialogPrimitive.Close
class="absolute top-0 right-0 rounded-xs opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:ring-2 focus:ring-ring focus:ring-offset-2 focus:outline-hidden disabled:pointer-events-none [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4"
>
<XIcon />
<span class="sr-only">Close</span>
</DialogPrimitive.Close>
{/if}
</div>