1
- import { computed , readonly , ref , markRaw } from 'vue'
1
+ import { computed , readonly , ref , markRaw , nextTick } from 'vue'
2
2
import { default as Axios } from 'axios'
3
3
import { except , only } from './helpers'
4
4
import { router , usePage } from '@inertiajs/vue3'
@@ -10,7 +10,7 @@ const localModals = ref({})
10
10
class Modal {
11
11
constructor ( component , response , modalProps , onClose , afterLeave ) {
12
12
this . id = Modal . generateId ( )
13
- this . open = true
13
+ this . open = false
14
14
this . listeners = { }
15
15
16
16
this . component = component
@@ -43,9 +43,28 @@ class Modal {
43
43
return stack . value . length < 2 || stack . value [ stack . value . length - 1 ] . id === this . id
44
44
}
45
45
46
+ show = ( ) => {
47
+ const index = this . index . value
48
+
49
+ if ( index > - 1 ) {
50
+ if ( stack . value [ index ] . open ) {
51
+ // Only open if the modal is closed
52
+ return
53
+ }
54
+
55
+ stack . value [ index ] . open = true
56
+ }
57
+ }
58
+
46
59
close = ( ) => {
47
60
const index = this . index . value
61
+
48
62
if ( index > - 1 ) {
63
+ if ( ! stack . value [ index ] . open ) {
64
+ // Only close if the modal is open
65
+ return
66
+ }
67
+
49
68
Object . keys ( this . listeners ) . forEach ( ( event ) => {
50
69
this . off ( event )
51
70
} )
@@ -56,8 +75,17 @@ class Modal {
56
75
}
57
76
58
77
afterLeave = ( ) => {
59
- stack . value = stack . value . filter ( ( m ) => m . id !== this . id )
60
- this . afterLeaveCallback ?. ( )
78
+ const index = this . index . value
79
+
80
+ if ( index > - 1 ) {
81
+ if ( stack . value [ index ] . open ) {
82
+ // Only execute the callback if the modal is closed
83
+ return
84
+ }
85
+
86
+ stack . value = stack . value . filter ( ( m ) => m . id !== this . id )
87
+ this . afterLeaveCallback ?. ( )
88
+ }
61
89
}
62
90
63
91
on = ( event , callback ) => {
@@ -137,6 +165,12 @@ function pushLocalModal(name, modalProps, onClose, afterLeave) {
137
165
return modal
138
166
}
139
167
168
+ function pushFromResponseData ( responseData , modalProps = { } , onClose = null , onAfterLeave = null ) {
169
+ return router
170
+ . resolveComponent ( responseData . component )
171
+ . then ( ( component ) => push ( markRaw ( component ) , responseData , modalProps , onClose , onAfterLeave ) )
172
+ }
173
+
140
174
function visit ( href , method , payload = { } , headers = { } , modalProps = { } , onClose = null , onAfterLeave = null , queryStringArrayFormat = 'brackets' ) {
141
175
return new Promise ( ( resolve , reject ) => {
142
176
if ( href . startsWith ( '#' ) ) {
@@ -159,11 +193,7 @@ function visit(href, method, payload = {}, headers = {}, modalProps = {}, onClos
159
193
'X-InertiaUI-Modal' : true ,
160
194
} ,
161
195
} )
162
- . then ( ( response ) => {
163
- router . resolveComponent ( response . data . component ) . then ( ( component ) => {
164
- resolve ( push ( markRaw ( component ) , response . data , modalProps , onClose , onAfterLeave ) )
165
- } )
166
- } )
196
+ . then ( ( response ) => resolve ( pushFromResponseData ( response . data , modalProps , onClose , onAfterLeave ) ) )
167
197
. catch ( ( error ) => {
168
198
reject ( error )
169
199
} )
@@ -173,6 +203,9 @@ function visit(href, method, payload = {}, headers = {}, modalProps = {}, onClos
173
203
function push ( component , response , modalProps , onClose , afterLeave ) {
174
204
const newModal = new Modal ( component , response , modalProps , onClose , afterLeave )
175
205
stack . value . push ( newModal )
206
+ nextTick ( ( ) => {
207
+ newModal . show ( )
208
+ } )
176
209
return newModal
177
210
}
178
211
@@ -184,6 +217,7 @@ export function useModalStack() {
184
217
return {
185
218
stack : readonly ( stack ) ,
186
219
push,
220
+ pushFromResponseData,
187
221
reset : ( ) => ( stack . value = [ ] ) ,
188
222
visit,
189
223
registerLocalModal,
0 commit comments