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

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

Timothy J. Baek's avatar
Timothy J. Baek committed
8
9
	let mounted = false;

Timothy J. Baek's avatar
Timothy J. Baek committed
10
	const sizeToWidth = (size) => {
Timothy J. Baek's avatar
Timothy J. Baek committed
11
12
13
		if (size === 'xs') {
			return 'w-[16rem]';
		} else if (size === 'sm') {
Timothy J. Baek's avatar
Timothy J. Baek committed
14
15
16
17
18
19
			return 'w-[30rem]';
		} else {
			return 'w-[40rem]';
		}
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
	onMount(() => {
		mounted = true;
	});

	$: if (mounted) {
		if (show) {
			document.body.style.overflow = 'hidden';
		} else {
			document.body.style.overflow = 'unset';
		}
	}
</script>

{#if show}
	<!-- svelte-ignore a11y-click-events-have-key-events -->
	<!-- svelte-ignore a11y-no-static-element-interactions -->
	<div
		class="fixed top-0 right-0 left-0 bottom-0 bg-stone-900/50 w-full min-h-screen h-screen flex justify-center z-50 overflow-hidden overscroll-contain"
		on:click={() => {
			show = false;
		}}
	>
		<div
Timothy J. Baek's avatar
Timothy J. Baek committed
43
44
45
			class="m-auto rounded-xl max-w-full {sizeToWidth(
				size
			)} mx-2 bg-gray-50 dark:bg-gray-900 shadow-3xl"
Timothy J. Baek's avatar
Timothy J. Baek committed
46
47
48
49
50
51
52
53
54
			transition:fade={{ delay: 100, duration: 200 }}
			on:click={(e) => {
				e.stopPropagation();
			}}
		>
			<slot />
		</div>
	</div>
{/if}