Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | <script setup lang="ts"> import { computed, onMounted, onUnmounted, ref, watch } from 'vue'; const props = withDefaults( defineProps<{ show?: boolean; maxWidth?: 'sm' | 'md' | 'lg' | 'xl' | '2xl'; closeable?: boolean; }>(), { show: false, maxWidth: '2xl', closeable: true, }, ); const emit = defineEmits(['close']); const dialog = ref(); const showSlot = ref(props.show); watch( () => props.show, () => { if (props.show) { document.body.style.overflow = 'hidden'; showSlot.value = true; dialog.value?.showModal(); } else { document.body.style.overflow = ''; setTimeout(() => { dialog.value?.close(); showSlot.value = false; }, 200); } }, ); const close = () => { if (props.closeable) { emit('close'); } }; const closeOnEscape = (e: KeyboardEvent) => { if (e.key === 'Escape') { e.preventDefault(); if (props.show) { close(); } } }; onMounted(() => document.addEventListener('keydown', closeOnEscape)); onUnmounted(() => { document.removeEventListener('keydown', closeOnEscape); document.body.style.overflow = ''; }); const maxWidthClass = computed(() => { return { sm: 'sm:max-w-sm', md: 'sm:max-w-md', lg: 'sm:max-w-lg', xl: 'sm:max-w-xl', '2xl': 'sm:max-w-2xl', }[props.maxWidth]; }); </script> <template> <dialog class="z-50 m-0 min-h-full min-w-full overflow-y-auto bg-transparent backdrop:bg-transparent" ref="dialog" > <div class="fixed inset-0 z-50 overflow-y-auto px-4 py-6 sm:px-0" scroll-region > <Transition enter-active-class="ease-out duration-300" enter-from-class="opacity-0" enter-to-class="opacity-100" leave-active-class="ease-in duration-200" leave-from-class="opacity-100" leave-to-class="opacity-0" > <div v-show="show" class="fixed inset-0 transform transition-all" @click="close" > <div class="absolute inset-0 bg-gray-500 opacity-75 dark:bg-gray-900" /> </div> </Transition> <Transition enter-active-class="ease-out duration-300" enter-from-class="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95" enter-to-class="opacity-100 translate-y-0 sm:scale-100" leave-active-class="ease-in duration-200" leave-from-class="opacity-100 translate-y-0 sm:scale-100" leave-to-class="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95" > <div v-show="show" class="mb-6 transform overflow-hidden rounded-lg bg-white shadow-xl transition-all sm:mx-auto sm:w-full dark:bg-gray-800" :class="maxWidthClass" > <slot v-if="showSlot" /> </div> </Transition> </div> </dialog> </template> |