Modal.svelte 1.37 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

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
11
	let mounted = false;

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

Timothy J. Baek's avatar
Timothy J. Baek committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
	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
Jannik Streidl's avatar
Jannik Streidl committed
39
		class=" fixed top-0 right-0 left-0 bottom-0 bg-black/60 w-full min-h-screen h-screen flex justify-center z-50 overflow-hidden overscroll-contain"
40
		in:fade={{ duration: 10 }}
41
42
43
		on:click={() => {
			show = false;
		}}
Timothy J. Baek's avatar
Timothy J. Baek committed
44
45
	>
		<div
46
			class=" m-auto rounded-2xl max-w-full {sizeToWidth(
Timothy J. Baek's avatar
Timothy J. Baek committed
47
48
				size
			)} mx-2 bg-gray-50 dark:bg-gray-900 shadow-3xl"
49
			in:flyAndScale
Timothy J. Baek's avatar
Timothy J. Baek committed
50
51
52
53
54
55
56
57
			on:click={(e) => {
				e.stopPropagation();
			}}
		>
			<slot />
		</div>
	</div>
{/if}
Jannik Streidl's avatar
Jannik Streidl committed
58
59
60
61
62
63
64
65

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

	@keyframes scaleUp {
		from {
Timothy J. Baek's avatar
Timothy J. Baek committed
66
			transform: scale(0.985);
Jannik Streidl's avatar
Jannik Streidl committed
67
68
69
70
71
72
73
74
			opacity: 0;
		}
		to {
			transform: scale(1);
			opacity: 1;
		}
	}
</style>