From cd2ce96a334da6fa94b91d877552380245721470 Mon Sep 17 00:00:00 2001 From: Elijah Duffy Date: Mon, 11 Aug 2025 17:12:31 -0700 Subject: [PATCH] dialog: extend api with disabled controls --- src/lib/Dialog.svelte | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/lib/Dialog.svelte b/src/lib/Dialog.svelte index d1fbcdc..6d51278 100644 --- a/src/lib/Dialog.svelte +++ b/src/lib/Dialog.svelte @@ -17,12 +17,23 @@ loaded: () => void; /** if default controls are used, returns true if the submit button is in loading state */ isLoading: () => boolean; + /** if the default controls are used, disabled submission */ + disable: () => void; + /** if the default controls are used, enables submission */ + enable: () => void; + /** if the default controls are used, returns true if the dialog is disabled */ + isDisabled: () => boolean; /** if default controls are used, disables submission and exiting */ freeze: () => void; /** if default controls are used, enables submission and exiting */ unfreeze: () => void; /** if the default controls are used, returns true if the dialog is frozen */ isFrozen: () => boolean; + /** + * if the default controls are used, returns true if the continue button is available. + * checks if the dialog is in loading, disabled, or frozen state. + */ + canContinue: () => boolean; /** * title sets the dialog title. WARNING: Title is NOT bindable and * changes made via this API will NOT propagate to consuming components. @@ -90,6 +101,7 @@ let error = $state(null); let loading = $state(false); let frozen = $state(false); + let disabled = $state(false); // disable window scroll when dialog is open $effect(() => { @@ -110,9 +122,13 @@ loading: () => (loading = true), loaded: () => (loading = false), isLoading: () => loading, + disable: () => (disabled = true), + enable: () => (disabled = false), + isDisabled: () => disabled, freeze: () => (frozen = true), unfreeze: () => (frozen = false), isFrozen: () => frozen, + canContinue: () => !loading && !disabled && !frozen, title: (newTitle) => (title = newTitle) }; @@ -191,11 +207,11 @@ onclick={() => { if (controls?.ok?.action) { controls.ok.action(dialogAPI); - } else if (!frozen && !loading) { + } else if (!frozen && !loading && !disabled) { open = false; } }} - disabled={frozen} + disabled={frozen || loading || disabled} {loading} > {controls?.ok?.label || 'OK'}