Modal.svelte 1.4 KB

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