Update modal to support different events for hiding and cancelling, and support entry transitions for fixed modals

This commit is contained in:
Andrew Kingston 2021-06-21 09:57:17 +01:00
parent 1879fbeee3
commit 2003c69698
2 changed files with 19 additions and 9 deletions

View File

@ -27,9 +27,17 @@
visible = false visible = false
} }
export function cancel() {
if (!visible) {
return
}
dispatch("cancel")
hide()
}
function handleKey(e) { function handleKey(e) {
if (visible && e.key === "Escape") { if (visible && e.key === "Escape") {
hide() cancel()
} }
} }
@ -41,7 +49,7 @@
} }
} }
setContext(Context.Modal, { show, hide }) setContext(Context.Modal, { show, hide, cancel })
</script> </script>
<svelte:window on:keydown={handleKey} /> <svelte:window on:keydown={handleKey} />
@ -56,15 +64,17 @@
<Portal target=".modal-container"> <Portal target=".modal-container">
<div <div
class="spectrum-Underlay is-open" class="spectrum-Underlay is-open"
transition:fade|local={{ duration: 200 }} in:fade={{ duration: 200 }}
on:mousedown|self={hide} out:fade|local={{ duration: 200 }}
on:mousedown|self={cancel}
> >
<div class="modal-wrapper" on:mousedown|self={hide}> <div class="modal-wrapper" on:mousedown|self={cancel}>
<div class="modal-inner-wrapper" on:mousedown|self={hide}> <div class="modal-inner-wrapper" on:mousedown|self={cancel}>
<div <div
use:focusFirstInput use:focusFirstInput
class="spectrum-Modal is-open" class="spectrum-Modal is-open"
transition:fly|local={{ y: 30, duration: 200 }} in:fly={{ y: 30, duration: 200 }}
out:fly|local={{ y: 30, duration: 200 }}
> >
<slot /> <slot />
</div> </div>

View File

@ -16,7 +16,7 @@
export let onConfirm = undefined export let onConfirm = undefined
export let disabled = false export let disabled = false
const { hide } = getContext(Context.Modal) const { hide, cancel } = getContext(Context.Modal)
let loading = false let loading = false
$: confirmDisabled = disabled || loading $: confirmDisabled = disabled || loading
@ -56,7 +56,7 @@
> >
<slot name="footer" /> <slot name="footer" />
{#if showCancelButton} {#if showCancelButton}
<Button group secondary on:click={hide}>{cancelText}</Button> <Button group secondary on:click={cancel}>{cancelText}</Button>
{/if} {/if}
{#if showConfirmButton} {#if showConfirmButton}
<Button <Button