@@ -21,13 +21,7 @@ import {
21
21
UnwrapRef ,
22
22
watch ,
23
23
unref ,
24
- getCurrentInstance ,
25
- Transition ,
26
- ref ,
27
- nextTick ,
28
- onMounted ,
29
24
withDirectives ,
30
- WatchStopHandle ,
31
25
} from 'vue'
32
26
import {
33
27
chakra ,
@@ -36,23 +30,16 @@ import {
36
30
useMultiStyleConfig ,
37
31
useStyles ,
38
32
} from '@chakra-ui/vue-system'
39
- import {
40
- createContext ,
41
- getSelector ,
42
- TemplateRef ,
43
- useRef ,
44
- } from '@chakra-ui/vue-utils'
33
+ import { createContext , TemplateRef , useRef } from '@chakra-ui/vue-utils'
45
34
import { CPortal } from '@chakra-ui/c-portal'
46
35
import { FocusLockProps } from '@chakra-ui/c-focus-lock'
47
36
import { CMotion } from '@chakra-ui/c-motion'
48
37
import { CCloseButton } from '@chakra-ui/c-close-button'
49
- import { MotionDirective , useMotion } from '@vueuse/motion'
50
-
38
+ import { MotionDirective , useMotions } from '@vueuse/motion'
51
39
import { useModal , UseModalOptions , UseModalReturn } from './use-modal'
52
- import { useDialogTransition , useDialogTransitions } from './modal-transitions'
40
+ import { DialogMotionPreset , dialogMotionPresets } from './modal-transitions'
53
41
54
42
type ScrollBehavior = 'inside' | 'outside'
55
- type MotionPreset = 'slideInBottom' | 'slideInRight' | 'scale' | 'none'
56
43
57
44
export interface ModalOptions
58
45
extends Omit <
@@ -126,7 +113,7 @@ export interface CModalProps extends UnwrapRef<UseModalOptions>, ModalOptions {
126
113
/**
127
114
* The transition that should be used for the modal
128
115
*/
129
- motionPreset ?: MotionPreset
116
+ motionPreset : DialogMotionPreset
130
117
}
131
118
132
119
type IUseModalOptions = ToRefs <
@@ -136,15 +123,12 @@ type IUseModalOptions = ToRefs<
136
123
| 'handleEscape'
137
124
| 'preserveScrollBarGap'
138
125
| 'allowPinchZoom'
139
- | 'motionPreset'
140
126
| 'trapFocus'
141
127
| 'autoFocus'
142
128
>
143
129
>
144
130
145
131
interface CModalContext extends IUseModalOptions , UseModalReturn {
146
- // /** The transition to be used for the CModal */
147
- // motionPreset?: MotionPreset
148
132
dialogRef : ( el : TemplateRef ) => void
149
133
overlayRef : ( el : TemplateRef ) => void
150
134
closeModal : ( ) => void
@@ -216,24 +200,13 @@ export const CModal = defineComponent({
216
200
} ,
217
201
motionPreset : {
218
202
type : String as PropType < CModalProps [ 'motionPreset' ] > ,
219
- default : 'scale ' ,
203
+ default : 'slideInBottom ' ,
220
204
} ,
221
205
} ,
222
206
emits : [ 'update:is-open' , 'escape' , 'close' ] ,
223
207
setup ( props , { slots, attrs, emit } ) {
224
- const isOpen = computed ( ( ) => props . isOpen )
225
- const { localIsOpen, transitionsExited } = useDialogTransitions ( isOpen , {
226
- onChildrenEntered : ( ) => {
227
- console . log ( '========= TRANSITIONS ENTER ================' )
228
- } ,
229
- onChildrenLeft : ( ) => {
230
- console . log ( '========= TRANSITIONS LEFT ================' )
231
- emit ( 'update:is-open' , false )
232
- } ,
233
- } )
234
-
235
208
const closeModal = ( ) => {
236
- localIsOpen . value = false
209
+ emit ( 'update:is-open' , false )
237
210
}
238
211
239
212
const handleEscape = ( event : KeyboardEvent ) => {
@@ -276,13 +249,26 @@ export const CModalContent = defineComponent({
276
249
inheritAttrs : false ,
277
250
emits : [ 'click' , 'mousedown' , 'keydown' ] ,
278
251
setup ( _ , { attrs, slots, emit } ) {
279
- const { dialogContainerProps, dialogProps, dialogRefEl } = unref (
252
+ const { dialogContainerProps, dialogProps, isOpen , motionPreset } = unref (
280
253
useModalContext ( )
281
254
)
282
255
const styles = useStyles ( )
256
+ const transitionId = 'modal-content'
257
+
258
+ /** Handles exit transition */
259
+ const leave = ( done : VoidFunction ) => {
260
+ const motions = useMotions ( )
261
+ const instance = motions [ transitionId ]
262
+ instance ?. leave ( ( ) => {
263
+ done ( )
264
+ } )
265
+ }
283
266
284
- const { localIsOpen, register } = unref ( useDialogTransition ( ) )
285
- const modalContentTransition = register ( 'modal-content' )
267
+ watch ( isOpen , ( newVal ) => {
268
+ if ( ! newVal ) {
269
+ leave ( ( ) => null )
270
+ }
271
+ } )
286
272
287
273
const dialogContainerStyles = computed < SystemStyleObject > ( ( ) => ( {
288
274
display : 'flex' ,
@@ -303,17 +289,6 @@ export const CModalContent = defineComponent({
303
289
...styles . value . dialog ,
304
290
} ) )
305
291
306
- watch (
307
- modalContentTransition ,
308
- ( content ) => {
309
- console . log ( 'modalContentTransition state changed to:' , content . status )
310
- } ,
311
- {
312
- immediate : true ,
313
- deep : true ,
314
- }
315
- )
316
-
317
292
return ( ) => {
318
293
return h (
319
294
chakra ( 'div' , {
@@ -322,66 +297,26 @@ export const CModalContent = defineComponent({
322
297
} ) ,
323
298
dialogContainerProps . value ( { emit } ) ,
324
299
( ) => [
325
- h (
326
- Transition ,
327
- {
328
- css : false ,
329
- onBeforeEnter : ( ) => {
330
- modalContentTransition . value . status = 'active'
331
- console . log ( 'onBeforeEnter' , 'active' )
332
- } ,
333
- onAfterEnter : ( ) => {
334
- modalContentTransition . value . status = 'entered'
335
- console . log ( 'onAfterEnter' , 'entered' )
336
- } ,
337
- onBeforeLeave : ( ) => {
338
- modalContentTransition . value . status = 'active'
339
- console . log ( 'onAfterLeave' , 'active' )
340
- } ,
341
- onLeave : ( el , done ) => {
342
- modalContentTransition . value . status = 'exited'
343
- console . log ( 'onAfterLeave' , 'exited' )
344
- done ( )
345
- } ,
346
- } ,
347
- ( ) => [
348
- localIsOpen . value &&
349
- withDirectives (
350
- h (
351
- chakra ( 'section' , {
352
- __css : dialogStyles . value ,
353
- label : 'modal__content' ,
354
- } ) ,
355
- {
356
- ...attrs ,
357
- ...dialogProps . value ( { emit } ) ,
358
- } ,
359
- slots
360
- ) ,
361
- [
362
- [
363
- MotionDirective ( {
364
- initial : {
365
- scale : 0.8 ,
366
- opacity : 0 ,
367
- translateY : 10 ,
368
- } ,
369
- enter : {
370
- scale : 1 ,
371
- opacity : 1 ,
372
- translateY : 0 ,
373
- } ,
374
- leave : {
375
- scale : 0.8 ,
376
- opacity : 0 ,
377
- translateY : 10 ,
378
- } ,
379
- } ) ,
380
- ] ,
381
- ]
382
- ) ,
383
- ]
384
- ) ,
300
+ isOpen . value &&
301
+ withDirectives (
302
+ h (
303
+ chakra ( 'section' , {
304
+ __css : dialogStyles . value ,
305
+ label : 'modal__content' ,
306
+ } ) ,
307
+ {
308
+ ...attrs ,
309
+ ...dialogProps . value ( { emit } ) ,
310
+ } ,
311
+ slots
312
+ ) ,
313
+ [
314
+ [
315
+ MotionDirective ( dialogMotionPresets [ motionPreset ?. value ] ) ,
316
+ transitionId ,
317
+ ] ,
318
+ ]
319
+ ) ,
385
320
]
386
321
)
387
322
}
0 commit comments