Skip to content

Commit a79cd54

Browse files
committed
refactor: code cleanup
1 parent 62e758c commit a79cd54

File tree

3 files changed

+42
-56
lines changed

3 files changed

+42
-56
lines changed

packages/vue-final-modal/src/components/VueFinalModal/VueFinalModal.vue

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -53,40 +53,15 @@ const { disablePageScroll, enablePageScroll } = useScrollLock(props, {
5353
5454
const {
5555
visible,
56-
57-
contentVisible,
58-
contentListeners,
59-
contentTransition,
60-
61-
overlayVisible,
62-
overlayListeners,
63-
overlayTransition,
64-
65-
enterTransition,
66-
leaveTransition,
56+
contentVisible, contentListeners, contentTransition,
57+
overlayVisible, overlayListeners, overlayTransition,
58+
enterTransition, leaveTransition,
6759
} = useTransition(props, {
6860
modelValueLocal,
69-
onEntering() {
70-
nextTick(() => {
71-
disablePageScroll()
72-
focus()
73-
})
74-
},
75-
onEnter() {
76-
emit('opened')
77-
// eslint-disable-next-line vue/custom-event-name-casing
78-
emit('_opened')
79-
resolveToggle('opened')
80-
},
81-
onLeave() {
82-
arrayRemoveItem(openedModals, modalExposed)
83-
resetZIndex()
84-
enablePageScroll()
85-
emit('closed')
86-
// eslint-disable-next-line vue/custom-event-name-casing
87-
emit('_closed')
88-
resolveToggle('closed')
89-
},
61+
onEntering,
62+
onEnter,
63+
onLeave,
64+
9065
})
9166
9267
const { modalExposed, resolveToggle } = useInternalExposed(props, { modelValueLocal, overlayVisible })
@@ -109,6 +84,29 @@ onBeforeUnmount(() => {
10984
openLastOverlay()
11085
})
11186
87+
function onEntering() {
88+
nextTick(() => {
89+
disablePageScroll()
90+
focus()
91+
})
92+
}
93+
94+
function onEnter() {
95+
emit('opened')
96+
// eslint-disable-next-line vue/custom-event-name-casing
97+
emit('_opened')
98+
resolveToggle('opened')
99+
}
100+
function onLeave() {
101+
arrayRemoveItem(openedModals, modalExposed)
102+
resetZIndex()
103+
enablePageScroll()
104+
emit('closed')
105+
// eslint-disable-next-line vue/custom-event-name-casing
106+
emit('_closed')
107+
resolveToggle('closed')
108+
}
109+
112110
function open(): boolean {
113111
let shouldStop = false
114112
emit('beforeOpen', { stop: () => shouldStop = true })

packages/vue-final-modal/src/components/VueFinalModal/VueFinalModalProps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { ModalId, StyleValue } from '~/Modal'
66
* @see [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729)
77
*/
88
type LiteralUnion<T extends U, U = string> = T | (U & Record<never, never>)
9-
type VfmTransition = LiteralUnion<'vfm-fade' | 'vfm-slide-down' | 'vfm-slide-up' | 'vfm-slide-right' | 'vfm-slide-left'>
9+
export type VfmTransition = LiteralUnion<'vfm-fade' | 'vfm-slide-down' | 'vfm-slide-up' | 'vfm-slide-right' | 'vfm-slide-left'>
1010

1111
export const vueFinalModalProps = {
1212
/**

packages/vue-final-modal/src/components/VueFinalModal/useTransition.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import type { ComputedRef, Ref, TransitionProps } from 'vue'
1+
import type { Ref, TransitionProps } from 'vue'
22
import { computed, nextTick, ref, watch } from 'vue'
33
import type VueFinalModal from './VueFinalModal.vue'
4+
import type { VfmTransition } from './VueFinalModalProps'
45
import type { ComponentProps } from '~/Component'
56

67
export enum TransitionState {
@@ -40,34 +41,15 @@ export function useTransition(
4041
onLeaving?: () => void
4142
onLeave?: () => void
4243
},
43-
): {
44-
visible: Ref<boolean>
45-
contentVisible: Ref<boolean>
46-
contentListeners: TransitionListeners
47-
contentTransition: ComputedRef<TransitionProps>
48-
overlayVisible: Ref<boolean>
49-
overlayListeners: TransitionListeners
50-
overlayTransition: ComputedRef<TransitionProps>
51-
enterTransition: () => void
52-
leaveTransition: () => void
53-
} {
44+
) {
5445
const { modelValueLocal, onEntering, onEnter, onLeaving, onLeave } = options
5546
const visible = ref(modelValueLocal.value)
5647

5748
const [contentVisible, contentState, contentListeners] = useTransitionState(visible.value)
5849
const [overlayVisible, overlayState, overlayListeners] = useTransitionState(visible.value)
5950

60-
const contentTransition = computed<TransitionProps>(() => {
61-
if (typeof props.contentTransition === 'string')
62-
return { name: props.contentTransition, appear: true }
63-
return { appear: true, ...props.contentTransition }
64-
})
65-
66-
const overlayTransition = computed<TransitionProps>(() => {
67-
if (typeof props.overlayTransition === 'string')
68-
return { name: props.overlayTransition, appear: true }
69-
return { appear: true, ...props.overlayTransition }
70-
})
51+
const contentTransition = computed<TransitionProps>(() => mergeTransition(props.contentTransition))
52+
const overlayTransition = computed<TransitionProps>(() => mergeTransition(props.overlayTransition))
7153

7254
const isReadyToBeDestroyed = computed(() =>
7355
(props.hideOverlay || overlayState.value === TransitionState.Leave)
@@ -127,3 +109,9 @@ export function useTransition(
127109
leaveTransition,
128110
}
129111
}
112+
113+
function mergeTransition(transition?: VfmTransition | TransitionProps) {
114+
if (typeof transition === 'string')
115+
return { name: transition, appear: true }
116+
return { appear: true, ...transition }
117+
}

0 commit comments

Comments
 (0)