forked from element-plus/element-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseDialog.ts
181 lines (157 loc) Β· 4.03 KB
/
useDialog.ts
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { computed, ref, watch, nextTick, onMounted } from 'vue'
import isServer from '@element-plus/utils/isServer'
import { UPDATE_MODEL_EVENT } from '@element-plus/utils/constants'
import PopupManager from '@element-plus/utils/popup-manager'
import { clearTimer, isNumber } from '@element-plus/utils/util'
import { useLockScreen, useRestoreActive, useModal } from '@element-plus/hooks'
import type { Ref, CSSProperties } from 'vue'
import type { SetupContext } from '@vue/runtime-core'
import type { UseDialogProps } from './dialog'
export const CLOSE_EVENT = 'close'
export const OPEN_EVENT = 'open'
export const CLOSED_EVENT = 'closed'
export const OPENED_EVENT = 'opened'
export { UPDATE_MODEL_EVENT }
export default function(props: UseDialogProps, ctx: SetupContext, targetRef: Ref<HTMLElement>) {
const visible = ref(false)
const closed = ref(false)
const dialogRef = ref(null)
const openTimer = ref<TimeoutHandle>(null)
const closeTimer = ref<TimeoutHandle>(null)
const rendered = ref(false) // when desctroyOnClose is true, we initialize it as false vise versa
const zIndex = ref(props.zIndex || PopupManager.nextZIndex())
const modalRef = ref<HTMLElement>(null)
const normalizeWidth = () => {
if(isNumber(props.width))
return `${props.width}px`
else
return props.width
}
const style = computed(() => {
const style = {} as CSSProperties
if (!props.fullscreen) {
style.marginTop = props.top
if (props.width) {
style.width = normalizeWidth()
}
}
return style
})
function afterEnter() {
ctx.emit(OPENED_EVENT)
}
function afterLeave() {
ctx.emit(CLOSED_EVENT)
ctx.emit(UPDATE_MODEL_EVENT, false)
if (props.destroyOnClose) {
rendered.value = false
}
}
function beforeLeave() {
ctx.emit(CLOSE_EVENT)
}
function open() {
clearTimer(closeTimer)
clearTimer(openTimer)
if (props.openDelay && props.openDelay > 0) {
openTimer.value = window.setTimeout(() => {
openTimer.value = null
doOpen()
}, props.openDelay)
} else {
doOpen()
}
}
function close() {
// if (this.willClose && !this.willClose()) return;
clearTimer(openTimer)
clearTimer(closeTimer)
if (props.closeDelay && props.closeDelay > 0) {
closeTimer.value = window.setTimeout(() => {
closeTimer.value = null
doClose()
}, props.closeDelay)
} else {
doClose()
}
}
function hide(shouldCancel: boolean) {
if (shouldCancel) return
closed.value = true
visible.value = false
}
function handleClose() {
if (props.beforeClose) {
props.beforeClose(hide)
} else {
close()
}
}
function onModalClick() {
if (props.closeOnClickModal) {
handleClose()
}
}
function doOpen() {
if (isServer) {
return
}
// if (props.willOpen?.()) {
// return
// }
visible.value = true
}
function doClose() {
visible.value = false
}
if (props.lockScroll) {
useLockScreen(visible)
}
if (props.closeOnPressEscape) {
useModal({
handleClose,
}, visible)
}
useRestoreActive(visible)
watch(() => props.modelValue, val => {
if (val) {
closed.value = false
open()
rendered.value = true // enables lazy rendering
ctx.emit(OPEN_EVENT)
zIndex.value = props.zIndex ? zIndex.value++ : PopupManager.nextZIndex()
// this.$el.addEventListener('scroll', this.updatePopper)
nextTick(() => {
if (targetRef.value) {
targetRef.value.scrollTop = 0
}
})
} else {
// this.$el.removeEventListener('scroll', this.updatePopper
if (visible.value) {
close()
}
}
})
onMounted(() => {
if (props.modelValue) {
visible.value = true
rendered.value = true // enables lazy rendering
open()
}
})
return {
afterEnter,
afterLeave,
beforeLeave,
handleClose,
onModalClick,
closed,
dialogRef,
style,
rendered,
modalRef,
visible,
zIndex,
}
}