forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDialog.tsx
201 lines (189 loc) · 6.01 KB
/
Dialog.tsx
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import type { PropType } from 'vue';
import { defineComponent, onBeforeUnmount, ref, watch, watchEffect } from 'vue';
import contains from '../vc-util/Dom/contains';
import type ScrollLocker from '../vc-util/Dom/scrollLocker';
import classNames from '../_util/classNames';
import type { MouseEventHandler } from '../_util/EventInterface';
import KeyCode from '../_util/KeyCode';
import omit from '../_util/omit';
import pickAttrs from '../_util/pickAttrs';
import { initDefaultProps } from '../_util/props-util';
import type { ContentRef } from './Content';
import Content from './Content';
import dialogPropTypes from './IDialogPropTypes';
import Mask from './Mask';
import { getMotionName, getUUID } from './util';
export default defineComponent({
name: 'Dialog',
inheritAttrs: false,
props: initDefaultProps(
{
...dialogPropTypes(),
getOpenCount: Function as PropType<() => number>,
scrollLocker: Object as PropType<ScrollLocker>,
},
{
mask: true,
visible: false,
keyboard: true,
closable: true,
maskClosable: true,
destroyOnClose: false,
prefixCls: 'rc-dialog',
getOpenCount: () => null,
focusTriggerAfterClose: true,
},
),
setup(props, { attrs, slots }) {
const lastOutSideActiveElementRef = ref<HTMLElement>();
const wrapperRef = ref<HTMLDivElement>();
const contentRef = ref<ContentRef>();
const animatedVisible = ref(props.visible);
const ariaIdRef = ref<string>(`vcDialogTitle${getUUID()}`);
// ========================= Events =========================
const onDialogVisibleChanged = (newVisible: boolean) => {
if (newVisible) {
// Try to focus
if (!contains(wrapperRef.value, document.activeElement as HTMLElement)) {
lastOutSideActiveElementRef.value = document.activeElement as HTMLElement;
contentRef.value?.focus();
}
} else {
const preAnimatedVisible = animatedVisible.value;
// Clean up scroll bar & focus back
animatedVisible.value = false;
if (props.mask && lastOutSideActiveElementRef.value && props.focusTriggerAfterClose) {
try {
lastOutSideActiveElementRef.value.focus({ preventScroll: true });
} catch (e) {
// Do nothing
}
lastOutSideActiveElementRef.value = null;
}
// Trigger afterClose only when change visible from true to false
if (preAnimatedVisible) {
props.afterClose?.();
}
}
};
const onInternalClose = (e: MouseEvent | KeyboardEvent) => {
props.onClose?.(e);
};
// >>> Content
const contentClickRef = ref(false);
const contentTimeoutRef = ref<any>();
// We need record content click incase content popup out of dialog
const onContentMouseDown: MouseEventHandler = () => {
clearTimeout(contentTimeoutRef.value);
contentClickRef.value = true;
};
const onContentMouseUp: MouseEventHandler = () => {
contentTimeoutRef.value = setTimeout(() => {
contentClickRef.value = false;
});
};
const onWrapperClick = (e: MouseEvent) => {
if (!props.maskClosable) return null;
if (contentClickRef.value) {
contentClickRef.value = false;
} else if (wrapperRef.value === e.target) {
onInternalClose(e);
}
};
const onWrapperKeyDown = (e: KeyboardEvent) => {
if (props.keyboard && e.keyCode === KeyCode.ESC) {
e.stopPropagation();
onInternalClose(e);
return;
}
// keep focus inside dialog
if (props.visible) {
if (e.keyCode === KeyCode.TAB) {
contentRef.value.changeActive(!e.shiftKey);
}
}
};
watch(
() => props.visible,
() => {
if (props.visible) {
animatedVisible.value = true;
}
},
{ flush: 'post' },
);
onBeforeUnmount(() => {
clearTimeout(contentTimeoutRef.value);
props.scrollLocker?.unLock();
});
watchEffect(() => {
props.scrollLocker?.unLock();
if (animatedVisible.value) {
props.scrollLocker?.lock();
}
});
return () => {
const {
prefixCls,
mask,
visible,
maskTransitionName,
maskAnimation,
zIndex,
wrapClassName,
rootClassName,
wrapStyle,
closable,
maskProps,
maskStyle,
transitionName,
animation,
wrapProps,
title = slots.title,
} = props;
const { style, class: className } = attrs;
return (
<div class={[`${prefixCls}-root`, rootClassName]} {...pickAttrs(props, { data: true })}>
<Mask
prefixCls={prefixCls}
visible={mask && visible}
motionName={getMotionName(prefixCls, maskTransitionName, maskAnimation)}
style={{
zIndex,
...maskStyle,
}}
maskProps={maskProps}
/>
<div
tabIndex={-1}
onKeydown={onWrapperKeyDown}
class={classNames(`${prefixCls}-wrap`, wrapClassName)}
ref={wrapperRef}
onClick={onWrapperClick}
role="dialog"
aria-labelledby={title ? ariaIdRef.value : null}
style={{ zIndex, ...wrapStyle, display: !animatedVisible.value ? 'none' : null }}
{...wrapProps}
>
<Content
{...omit(props, ['scrollLocker'])}
style={style}
class={className}
v-slots={slots}
onMousedown={onContentMouseDown}
onMouseup={onContentMouseUp}
ref={contentRef}
closable={closable}
ariaId={ariaIdRef.value}
prefixCls={prefixCls}
visible={visible}
onClose={onInternalClose}
onVisibleChanged={onDialogVisibleChanged}
motionName={getMotionName(prefixCls, transitionName, animation)}
/>
</div>
</div>
);
};
},
});