ConfirmDialog.svelte 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <script lang="ts">
  2. import { onMount, getContext, createEventDispatcher } from 'svelte';
  3. const i18n = getContext('i18n');
  4. const dispatch = createEventDispatcher();
  5. import { fade } from 'svelte/transition';
  6. import { flyAndScale } from '$lib/utils/transitions';
  7. export let title = '';
  8. export let message = '';
  9. export let cancelLabel = $i18n.t('Cancel');
  10. export let confirmLabel = $i18n.t('Confirm');
  11. export let input = false;
  12. export let inputPlaceholder = '';
  13. export let inputValue = '';
  14. export let show = false;
  15. let modalElement = null;
  16. let mounted = false;
  17. const handleKeyDown = (event: KeyboardEvent) => {
  18. if (event.key === 'Escape') {
  19. console.log('Escape');
  20. show = false;
  21. }
  22. if (event.key === 'Enter') {
  23. console.log('Enter');
  24. show = false;
  25. dispatch('confirm', inputValue);
  26. }
  27. };
  28. onMount(() => {
  29. mounted = true;
  30. });
  31. $: if (mounted) {
  32. if (show) {
  33. window.addEventListener('keydown', handleKeyDown);
  34. document.body.style.overflow = 'hidden';
  35. } else {
  36. window.removeEventListener('keydown', handleKeyDown);
  37. document.body.style.overflow = 'unset';
  38. }
  39. }
  40. </script>
  41. {#if show}
  42. <!-- svelte-ignore a11y-click-events-have-key-events -->
  43. <!-- svelte-ignore a11y-no-static-element-interactions -->
  44. <div
  45. bind:this={modalElement}
  46. class=" fixed top-0 right-0 left-0 bottom-0 bg-black/60 w-full h-screen max-h-[100dvh] flex justify-center z-[9999] overflow-hidden overscroll-contain"
  47. in:fade={{ duration: 10 }}
  48. on:mousedown={() => {
  49. show = false;
  50. }}
  51. >
  52. <div
  53. class=" m-auto rounded-2xl max-w-full w-[32rem] mx-2 bg-gray-50 dark:bg-gray-950 max-h-[100dvh] shadow-3xl"
  54. in:flyAndScale
  55. on:mousedown={(e) => {
  56. e.stopPropagation();
  57. }}
  58. >
  59. <div class="px-[1.75rem] py-6 flex flex-col">
  60. <div class=" text-lg font-semibold dark:text-gray-200 mb-2.5">
  61. {#if title !== ''}
  62. {title}
  63. {:else}
  64. {$i18n.t('Confirm your action')}
  65. {/if}
  66. </div>
  67. <slot>
  68. <div class=" text-sm text-gray-500 flex-1">
  69. {#if message !== ''}
  70. {message}
  71. {:else}
  72. {$i18n.t('This action cannot be undone. Do you wish to continue?')}
  73. {/if}
  74. {#if input}
  75. <textarea
  76. bind:value={inputValue}
  77. placeholder={inputPlaceholder ? inputPlaceholder : $i18n.t('Enter your message')}
  78. 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"
  79. rows="3"
  80. required
  81. />
  82. {/if}
  83. </div>
  84. </slot>
  85. <div class="mt-6 flex justify-between gap-1.5">
  86. <button
  87. 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"
  88. on:click={() => {
  89. show = false;
  90. dispatch('cancel');
  91. }}
  92. type="button"
  93. >
  94. {cancelLabel}
  95. </button>
  96. <button
  97. 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"
  98. on:click={() => {
  99. show = false;
  100. dispatch('confirm', inputValue);
  101. }}
  102. type="button"
  103. >
  104. {confirmLabel}
  105. </button>
  106. </div>
  107. </div>
  108. </div>
  109. </div>
  110. {/if}
  111. <style>
  112. .modal-content {
  113. animation: scaleUp 0.1s ease-out forwards;
  114. }
  115. @keyframes scaleUp {
  116. from {
  117. transform: scale(0.985);
  118. opacity: 0;
  119. }
  120. to {
  121. transform: scale(1);
  122. opacity: 1;
  123. }
  124. }
  125. </style>