-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Anton <[email protected]>
- Loading branch information
Showing
6 changed files
with
91 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<script lang="ts"> | ||
let dialog: HTMLDialogElement | undefined; | ||
/** | ||
* Allows the user to close the modal by clicking outside it. | ||
*/ | ||
export let closeOnBackdrop: boolean = false; | ||
/** | ||
* Whether or not the modal is open or closed. | ||
*/ | ||
export let show: boolean = false; | ||
/** | ||
* Ensures that the real DOM element is opened or closed when the svelte value changes. | ||
*/ | ||
$: if (dialog && show) { | ||
dialog.showModal(); | ||
} else { | ||
dialog?.close(); | ||
} | ||
/** | ||
* Closes the modal if the user clicks outside it | ||
* Only works if closeOnBackdrop is true | ||
*/ | ||
function closeOnBackdropClick(event: MouseEvent) { | ||
if (!closeOnBackdrop) return; | ||
if (!dialog) { | ||
throw new TypeError("Dialog should always be defined on mount"); | ||
} | ||
/** | ||
* TODO: Is there really no better way to check if the user clicked outside? | ||
*/ | ||
const rect = dialog.getBoundingClientRect(); | ||
const clickIsOnDialog = | ||
rect.top <= event.clientY && | ||
event.clientY <= rect.top + rect.height && | ||
rect.left <= event.clientX && | ||
event.clientX <= rect.left + rect.width; | ||
if (!clickIsOnDialog) { | ||
show = false; | ||
} | ||
} | ||
</script> | ||
|
||
<!--TODO: Remove this when supported by Svelte--> | ||
<!-- svelte-ignore a11y-click-events-have-key-events a11y-no-noninteractive-element-interactions --> | ||
<dialog | ||
bind:this={dialog} | ||
on:click|self={(event) => { | ||
closeOnBackdropClick(event); | ||
}} | ||
> | ||
<slot /> | ||
</dialog> | ||
|
||
<style> | ||
dialog { | ||
color: var(--text-color); | ||
background-color: var(--background-color); | ||
} | ||
dialog::backdrop { | ||
background-color: var(--modal-background-color); | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { writable } from "svelte/store"; | ||
|
||
export const showSettings = writable(false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters