Modal.svelte 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <script lang="ts">
  2. import { onMount } from 'svelte';
  3. import { fade } from 'svelte/transition';
  4. export let show = true;
  5. export let size = 'md';
  6. let mounted = false;
  7. const sizeToWidth = (size) => {
  8. if (size === 'xs') {
  9. return 'w-[16rem]';
  10. } else if (size === 'sm') {
  11. return 'w-[30rem]';
  12. } else {
  13. return 'w-[44rem]';
  14. }
  15. };
  16. onMount(() => {
  17. mounted = true;
  18. });
  19. $: if (mounted) {
  20. if (show) {
  21. document.body.style.overflow = 'hidden';
  22. } else {
  23. document.body.style.overflow = 'unset';
  24. }
  25. }
  26. </script>
  27. {#if show}
  28. <!-- svelte-ignore a11y-click-events-have-key-events -->
  29. <!-- svelte-ignore a11y-no-static-element-interactions -->
  30. <div
  31. 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"
  32. in:fade={{ duration: 10 }}
  33. on:click={() => {
  34. show = false;
  35. }}
  36. >
  37. <div
  38. class=" modal-content m-auto rounded-2xl max-w-full {sizeToWidth(
  39. size
  40. )} mx-2 bg-gray-50 dark:bg-gray-900 shadow-3xl"
  41. in:fade={{ duration: 10 }}
  42. on:click={(e) => {
  43. e.stopPropagation();
  44. }}
  45. >
  46. <slot />
  47. </div>
  48. </div>
  49. {/if}
  50. <style>
  51. .modal-content {
  52. animation: scaleUp 0.1s ease-out forwards;
  53. }
  54. @keyframes scaleUp {
  55. from {
  56. transform: scale(0.985);
  57. opacity: 0;
  58. }
  59. to {
  60. transform: scale(1);
  61. opacity: 1;
  62. }
  63. }
  64. </style>