ConfirmDialog.svelte 3.27 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
<script lang="ts">
2
	import { onMount, getContext, createEventDispatcher } from 'svelte';
Timothy J. Baek's avatar
Timothy J. Baek committed
3
	import { fade } from 'svelte/transition';
4
	const i18n = getContext('i18n');
Timothy J. Baek's avatar
Timothy J. Baek committed
5
6
7
8
9

	import { flyAndScale } from '$lib/utils/transitions';

	const dispatch = createEventDispatcher();

Timothy J. Baek's avatar
Timothy J. Baek committed
10
11
	export let title = '';
	export let message = '';
Timothy J. Baek's avatar
Timothy J. Baek committed
12

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
13
14
	export let cancelLabel = $i18n.t('Cancel');
	export let confirmLabel = $i18n.t('Confirm');
Timothy J. Baek's avatar
Timothy J. Baek committed
15

Timothy J. Baek's avatar
Timothy J. Baek committed
16
17
	export let input = false;
	export let inputPlaceholder = '';
18
	export let inputValue = '';
Timothy J. Baek's avatar
Timothy J. Baek committed
19

Timothy J. Baek's avatar
Timothy J. Baek committed
20
	export let show = false;
Timothy J. Baek's avatar
Timothy J. Baek committed
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
	let modalElement = null;
	let mounted = false;

	const handleKeyDown = (event: KeyboardEvent) => {
		if (event.key === 'Escape') {
			console.log('Escape');
			show = false;
		}
	};

	onMount(() => {
		mounted = true;
	});

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

{#if show}
	<!-- svelte-ignore a11y-click-events-have-key-events -->
	<!-- svelte-ignore a11y-no-static-element-interactions -->
	<div
		bind:this={modalElement}
		class=" 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"
		in:fade={{ duration: 10 }}
		on:mousedown={() => {
			show = false;
		}}
	>
		<div
			class=" m-auto rounded-2xl max-w-full w-[32rem] mx-2 bg-gray-50 dark:bg-gray-950 shadow-3xl border border-gray-850"
			in:flyAndScale
			on:mousedown={(e) => {
				e.stopPropagation();
			}}
		>
			<div class="px-[1.75rem] py-6">
Timothy J. Baek's avatar
Timothy J. Baek committed
66
67
68
69
70
71
72
				<div class=" text-lg font-semibold dark:text-gray-200 mb-2.5">
					{#if title !== ''}
						{title}
					{:else}
						{$i18n.t('Confirm your action')}
					{/if}
				</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
73
74
75

				<slot>
					<div class=" text-sm text-gray-500">
Timothy J. Baek's avatar
Timothy J. Baek committed
76
77
78
79
80
						{#if message !== ''}
							{message}
						{:else}
							{$i18n.t('This action cannot be undone. Do you wish to continue?')}
						{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
81
82
83
84
85
86
87
88
89
90

						{#if input}
							<textarea
								bind:value={inputValue}
								placeholder={inputPlaceholder ? inputPlaceholder : $i18n.t('Enter your message')}
								class="w-full mt-2 rounded-lg px-4 py-2 text-sm dark:text-gray-300 dark:bg-gray-900 outline-none resize-none"
								rows="3"
								required
							/>
						{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
91
92
93
94
95
96
97
98
					</div>
				</slot>

				<div class="mt-6 flex justify-between gap-1.5">
					<button
						class="bg-gray-100 hover:bg-gray-200 text-gray-800 dark:bg-gray-850 dark:hover:bg-gray-800 dark:text-white font-medium w-full py-2.5 rounded-lg transition"
						on:click={() => {
							show = false;
Timothy J. Baek's avatar
Timothy J. Baek committed
99
							dispatch('cancel');
Timothy J. Baek's avatar
Timothy J. Baek committed
100
101
102
						}}
						type="button"
					>
Timothy J. Baek's avatar
Timothy J. Baek committed
103
						{cancelLabel}
Timothy J. Baek's avatar
Timothy J. Baek committed
104
105
106
107
108
					</button>
					<button
						class="bg-gray-900 hover:bg-gray-850 text-gray-100 dark:bg-gray-100 dark:hover:bg-white dark:text-gray-800 font-medium w-full py-2.5 rounded-lg transition"
						on:click={() => {
							show = false;
Timothy J. Baek's avatar
Timothy J. Baek committed
109
							dispatch('confirm', inputValue);
Timothy J. Baek's avatar
Timothy J. Baek committed
110
111
112
						}}
						type="button"
					>
Timothy J. Baek's avatar
Timothy J. Baek committed
113
						{confirmLabel}
Timothy J. Baek's avatar
Timothy J. Baek committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
					</button>
				</div>
			</div>
		</div>
	</div>
{/if}

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

	@keyframes scaleUp {
		from {
			transform: scale(0.985);
			opacity: 0;
		}
		to {
			transform: scale(1);
			opacity: 1;
		}
	}
</style>