dialog: pass state to snippet overrides

This commit is contained in:
Elijah Duffy
2026-03-15 12:58:28 -07:00
parent ade904d0c9
commit bb27d0c9d7
2 changed files with 21 additions and 15 deletions

View File

@@ -112,9 +112,9 @@
/** Bindable open state of the dialog */
open?: boolean;
/** Title of the dialog */
title: string | Snippet;
title: string | Snippet<[state: DialogState]>;
/** Description of the dialog, optionally rendered below the title */
description?: string | Snippet;
description?: string | Snippet<[state: DialogState]>;
/** Size of the dialog (default: 'sm') */
size?: 'sm' | 'md' | 'lg' | 'max';
/** Additional classes for the dialog */
@@ -122,7 +122,7 @@
/** Content of the dialog */
children?: Snippet;
/** Bottom controls for the dialog */
controls?: Snippet | DialogControls;
controls?: Snippet<[state: DialogState]> | DialogControls;
/** Sets bottom alignment of controls (default: end) */
controlsAlign?: 'start' | 'center' | 'end';
/** Top-right close control */
@@ -342,7 +342,9 @@
controlsAlign === 'end' && 'justify-end'
]}
>
{#if controls && typeof controls === 'function'}{@render controls()}{:else if controls?.flip}
{#if controls && typeof controls === 'function'}{@render controls(
getState()
)}{:else if controls?.flip}
{#if controls.ok !== null}
{@render dialogOkButton(getState(), controls.ok)}
{/if}
@@ -417,10 +419,10 @@
</button>
{/snippet}
{#snippet stringOrSnippet(val: string | Snippet)}
{#snippet stringOrSnippet(val: string | Snippet<[state: DialogState]>)}
{#if typeof val === 'string'}
{val}
{:else}
{@render val()}
{@render val(getState())}
{/if}
{/snippet}

View File

@@ -537,15 +537,6 @@
bind:open={dialogOpen}
title="Dialog Title"
size="sm"
controls={{
ok: {
action: (dialog) => {
dialog.close();
alert('Dialog submitted!');
}
},
cancel: null
}}
onopen={(dialog) => {
dialog.error('Example error message!');
dialog.loading();
@@ -554,6 +545,19 @@
}, 2000);
}}
>
{#snippet controls(state)}
<Button onclick={() => state.api.close()}>Cancel</Button>
<Button
onclick={() => {
state.api.close();
alert('Dialog submitted!');
}}
loading={state.loading}
>
Submit
</Button>
{/snippet}
<p>This is a dialog content area.</p>
<Button onclick={() => (nestedDialogOpen = true)}>Open Nested Dialog</Button>