You just need to add the "onclick" handler on the root div:
<div class="modal" tabindex="-1" on:click={ () => send('CLOSE') }>
I use XState to send the 'CLOSE' event to the state machine, on another examples where people just use a flag variable you can do like this:
<script>
export let open = false;
export let onClosed;
const modalClose = () => {
open = false;
if (onClosed) {
onClosed();
}
}
</script>
{#if open}
<div class="modal" on:click={modalClose} >
...
I didn't have to add the stopPropagation
flag on the internal buttons as I think Svelte manage automatically, but just in case you need it, inside your modal on:click
logic you can add the flag: on:click|stopPropagation
<div class="modal-footer">
<button type="button" class="btn btn-secondary" on:click={modalClose}>
Close
</button>
<button type="button" class="btn btn-primary" on:click|stopPropagation={yourSaveLogic()}>
Save changes
</button>
</div>
on:click|stopPropagation
for a shorter syntax – Infrared