Modal.svelte 1.99 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
<script lang="ts">
	import { onMount } from 'svelte';
Jannik Streidl's avatar
Jannik Streidl committed
3
	import { fade } from 'svelte/transition';
Timothy J. Baek's avatar
Timothy J. Baek committed
4

Timothy J. Baek's avatar
Timothy J. Baek committed
5
6
	import { flyAndScale } from '$lib/utils/transitions';

Timothy J. Baek's avatar
Timothy J. Baek committed
7
	export let show = true;
Timothy J. Baek's avatar
Timothy J. Baek committed
8
9
	export let size = 'md';

Timothy J. Baek's avatar
Timothy J. Baek committed
10
	let modalElement = null;
Timothy J. Baek's avatar
Timothy J. Baek committed
11
12
	let mounted = false;

Timothy J. Baek's avatar
Timothy J. Baek committed
13
	const sizeToWidth = (size) => {
Timothy J. Baek's avatar
Timothy J. Baek committed
14
15
16
		if (size === 'xs') {
			return 'w-[16rem]';
		} else if (size === 'sm') {
Timothy J. Baek's avatar
Timothy J. Baek committed
17
			return 'w-[30rem]';
18
19
		} else if (size === 'md') {
			return 'w-[48rem]';
Timothy J. Baek's avatar
Timothy J. Baek committed
20
		} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
21
			return 'w-[56rem]';
Timothy J. Baek's avatar
Timothy J. Baek committed
22
23
24
		}
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
25
	const handleKeyDown = (event: KeyboardEvent) => {
Timothy J. Baek's avatar
Timothy J. Baek committed
26
		if (event.key === 'Escape' && isTopModal()) {
Timothy J. Baek's avatar
Timothy J. Baek committed
27
28
29
30
31
			console.log('Escape');
			show = false;
		}
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
32
33
34
35
36
	const isTopModal = () => {
		const modals = document.getElementsByClassName('modal');
		return modals.length && modals[modals.length - 1] === modalElement;
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
37
38
39
40
	onMount(() => {
		mounted = true;
	});

Timothy J. Baek's avatar
Timothy J. Baek committed
41
42
43
44
45
46
	$: if (show && modalElement) {
		document.body.appendChild(modalElement);
		window.addEventListener('keydown', handleKeyDown);
		document.body.style.overflow = 'hidden';
	} else if (modalElement) {
		window.removeEventListener('keydown', handleKeyDown);
Timothy J. Baek's avatar
Timothy J. Baek committed
47
		document.body.removeChild(modalElement);
Timothy J. Baek's avatar
Timothy J. Baek committed
48
		document.body.style.overflow = 'unset';
Timothy J. Baek's avatar
Timothy J. Baek committed
49
50
51
52
53
54
55
	}
</script>

{#if show}
	<!-- svelte-ignore a11y-click-events-have-key-events -->
	<!-- svelte-ignore a11y-no-static-element-interactions -->
	<div
Timothy J. Baek's avatar
Timothy J. Baek committed
56
		bind:this={modalElement}
Timothy J. Baek's avatar
Timothy J. Baek committed
57
		class="modal fixed top-0 right-0 left-0 bottom-0 bg-black/60 w-full min-h-screen h-screen flex justify-center z-[9999] overflow-hidden overscroll-contain"
58
		in:fade={{ duration: 10 }}
59
		on:mousedown={() => {
60
61
			show = false;
		}}
Timothy J. Baek's avatar
Timothy J. Baek committed
62
63
	>
		<div
Timothy J. Baek's avatar
Timothy J. Baek committed
64
			class=" m-auto rounded-2xl max-w-full {sizeToWidth(
Timothy J. Baek's avatar
Timothy J. Baek committed
65
66
				size
			)} mx-2 bg-gray-50 dark:bg-gray-900 shadow-3xl"
Timothy J. Baek's avatar
Timothy J. Baek committed
67
			in:flyAndScale
68
			on:mousedown={(e) => {
Timothy J. Baek's avatar
Timothy J. Baek committed
69
70
71
72
73
74
75
				e.stopPropagation();
			}}
		>
			<slot />
		</div>
	</div>
{/if}
Jannik Streidl's avatar
Jannik Streidl committed
76
77
78
79
80
81
82
83

<style>
	.modal-content {
		animation: scaleUp 0.1s ease-out forwards;
	}

	@keyframes scaleUp {
		from {
Timothy J. Baek's avatar
Timothy J. Baek committed
84
			transform: scale(0.985);
Jannik Streidl's avatar
Jannik Streidl committed
85
86
87
88
89
90
91
92
			opacity: 0;
		}
		to {
			transform: scale(1);
			opacity: 1;
		}
	}
</style>