ConfirmDialog.svelte 3.3 KB

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