Skip to content

Commit c0e19ca

Browse files
authored
[4.x] Change Inertia Modal.vue component to use a native <dialog> (#1431)
* Change Modal to use native <dialog> instead of Vue Teleport and div * changed overflow to happen earlier, to more closely match existing behavior and seem more responsive * updated livewire modal to use <dialog> * added preventDefault to allow the regular close animation to fire nicely * reverted <dialog> implementation * added second ref for showing the slot to stop content from appearing and disappearing, preventing modal size shifting
1 parent e8335d0 commit c0e19ca

File tree

1 file changed

+38
-31
lines changed
  • stubs/inertia/resources/js/Components

1 file changed

+38
-31
lines changed
Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup>
2-
import { computed, onMounted, onUnmounted, watch } from 'vue';
2+
import { computed, onMounted, onUnmounted, ref, watch } from 'vue';
33
44
const props = defineProps({
55
show: {
@@ -17,18 +17,27 @@ const props = defineProps({
1717
});
1818
1919
const emit = defineEmits(['close']);
20+
const dialog = ref();
21+
const showSlot = ref(props.show);
2022
2123
watch(() => props.show, () => {
2224
if (props.show) {
2325
document.body.style.overflow = 'hidden';
26+
showSlot.value = true;
27+
dialog.value?.showModal();
2428
} else {
2529
document.body.style.overflow = null;
30+
setTimeout(() => {
31+
dialog.value?.close();
32+
showSlot.value = false;
33+
}, 200);
2634
}
2735
});
2836
2937
const close = () => {
3038
if (props.closeable) {
3139
emit('close');
40+
e.preventDefault();
3241
}
3342
};
3443
@@ -57,35 +66,33 @@ const maxWidthClass = computed(() => {
5766
</script>
5867
5968
<template>
60-
<teleport to="body">
61-
<transition leave-active-class="duration-200">
62-
<div v-show="show" class="fixed inset-0 overflow-y-auto px-4 py-6 sm:px-0 z-50" scroll-region>
63-
<transition
64-
enter-active-class="ease-out duration-300"
65-
enter-from-class="opacity-0"
66-
enter-to-class="opacity-100"
67-
leave-active-class="ease-in duration-200"
68-
leave-from-class="opacity-100"
69-
leave-to-class="opacity-0"
70-
>
71-
<div v-show="show" class="fixed inset-0 transform transition-all" @click="close">
72-
<div class="absolute inset-0 bg-gray-500 dark:bg-gray-900 opacity-75" />
73-
</div>
74-
</transition>
69+
<dialog class="z-50 m-0 min-h-full min-w-full overflow-y-auto bg-transparent backdrop:bg-transparent" ref="dialog">
70+
<div class="fixed inset-0 overflow-y-auto px-4 py-6 sm:px-0 z-50" scroll-region>
71+
<transition
72+
enter-active-class="ease-out duration-300"
73+
enter-from-class="opacity-0"
74+
enter-to-class="opacity-100"
75+
leave-active-class="ease-in duration-200"
76+
leave-from-class="opacity-100"
77+
leave-to-class="opacity-0"
78+
>
79+
<div v-show="show" class="fixed inset-0 transform transition-all" @click="close">
80+
<div class="absolute inset-0 bg-gray-500 dark:bg-gray-900 opacity-75" />
81+
</div>
82+
</transition>
7583
76-
<transition
77-
enter-active-class="ease-out duration-300"
78-
enter-from-class="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
79-
enter-to-class="opacity-100 translate-y-0 sm:scale-100"
80-
leave-active-class="ease-in duration-200"
81-
leave-from-class="opacity-100 translate-y-0 sm:scale-100"
82-
leave-to-class="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
83-
>
84-
<div v-show="show" class="mb-6 bg-white dark:bg-gray-800 rounded-lg overflow-hidden shadow-xl transform transition-all sm:w-full sm:mx-auto" :class="maxWidthClass">
85-
<slot v-if="show" />
86-
</div>
87-
</transition>
88-
</div>
89-
</transition>
90-
</teleport>
84+
<transition
85+
enter-active-class="ease-out duration-300"
86+
enter-from-class="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
87+
enter-to-class="opacity-100 translate-y-0 sm:scale-100"
88+
leave-active-class="ease-in duration-200"
89+
leave-from-class="opacity-100 translate-y-0 sm:scale-100"
90+
leave-to-class="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
91+
>
92+
<div v-show="show" class="mb-6 bg-white dark:bg-gray-800 rounded-lg overflow-hidden shadow-xl transform transition-all sm:w-full sm:mx-auto" :class="maxWidthClass">
93+
<slot v-if="showSlot"/>
94+
</div>
95+
</transition>
96+
</div>
97+
</dialog>
9198
</template>

0 commit comments

Comments
 (0)