forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContent.tsx
154 lines (149 loc) · 4.77 KB
/
Content.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
import type { CSSProperties, PropType } from 'vue';
import { computed, ref, defineComponent, nextTick } from 'vue';
import type { MouseEventHandler } from '../_util/EventInterface';
import Transition, { getTransitionProps } from '../_util/transition';
import dialogPropTypes from './IDialogPropTypes';
import { offset } from './util';
const sentinelStyle = { width: 0, height: 0, overflow: 'hidden', outline: 'none' };
export type ContentRef = {
focus: () => void;
changeActive: (next: boolean) => void;
};
export default defineComponent({
name: 'Content',
inheritAttrs: false,
props: {
...dialogPropTypes(),
motionName: String,
ariaId: String,
onVisibleChanged: Function as PropType<(visible: boolean) => void>,
onMousedown: Function as PropType<MouseEventHandler>,
onMouseup: Function as PropType<MouseEventHandler>,
},
setup(props, { expose, slots, attrs }) {
const sentinelStartRef = ref<HTMLDivElement>();
const sentinelEndRef = ref<HTMLDivElement>();
const dialogRef = ref<HTMLDivElement>();
expose({
focus: () => {
sentinelStartRef.value?.focus();
},
changeActive: next => {
const { activeElement } = document;
if (next && activeElement === sentinelEndRef.value) {
sentinelStartRef.value.focus();
} else if (!next && activeElement === sentinelStartRef.value) {
sentinelEndRef.value.focus();
}
},
});
const transformOrigin = ref<string>();
const contentStyleRef = computed(() => {
const { width, height } = props;
const contentStyle: CSSProperties = {};
if (width !== undefined) {
contentStyle.width = typeof width === 'number' ? `${width}px` : width;
}
if (height !== undefined) {
contentStyle.height = typeof height === 'number' ? `${height}px` : height;
}
if (transformOrigin.value) {
contentStyle.transformOrigin = transformOrigin.value;
}
return contentStyle;
});
const onPrepare = () => {
nextTick(() => {
if (dialogRef.value) {
const elementOffset = offset(dialogRef.value);
transformOrigin.value = props.mousePosition
? `${props.mousePosition.x - elementOffset.left}px ${
props.mousePosition.y - elementOffset.top
}px`
: '';
}
});
};
const onVisibleChanged = (visible: boolean) => {
props.onVisibleChanged(visible);
};
return () => {
const {
prefixCls,
footer = slots.footer?.(),
title = slots.title?.(),
ariaId,
closable,
closeIcon = slots.closeIcon?.(),
onClose,
bodyStyle,
bodyProps,
onMousedown,
onMouseup,
visible,
modalRender = slots.modalRender,
destroyOnClose,
motionName,
} = props;
let footerNode: any;
if (footer) {
footerNode = <div class={`${prefixCls}-footer`}>{footer}</div>;
}
let headerNode: any;
if (title) {
headerNode = (
<div class={`${prefixCls}-header`}>
<div class={`${prefixCls}-title`} id={ariaId}>
{title}
</div>
</div>
);
}
let closer: any;
if (closable) {
closer = (
<button type="button" onClick={onClose} aria-label="Close" class={`${prefixCls}-close`}>
{closeIcon || <span class={`${prefixCls}-close-x`} />}
</button>
);
}
const content = (
<div class={`${prefixCls}-content`}>
{closer}
{headerNode}
<div class={`${prefixCls}-body`} style={bodyStyle} {...bodyProps}>
{slots.default?.()}
</div>
{footerNode}
</div>
);
const transitionProps = getTransitionProps(motionName);
return (
<Transition
{...transitionProps}
onBeforeEnter={onPrepare}
onAfterEnter={() => onVisibleChanged(true)}
onAfterLeave={() => onVisibleChanged(false)}
>
{visible || !destroyOnClose ? (
<div
{...attrs}
ref={dialogRef}
v-show={visible}
key="dialog-element"
role="document"
style={[contentStyleRef.value, attrs.style]}
class={[prefixCls, attrs.class]}
onMousedown={onMousedown}
onMouseup={onMouseup}
>
<div tabindex={0} ref={sentinelStartRef} style={sentinelStyle} aria-hidden="true" />
{modalRender ? modalRender({ originVNode: content }) : content}
<div tabindex={0} ref={sentinelEndRef} style={sentinelStyle} aria-hidden="true" />
</div>
) : null}
</Transition>
);
};
},
});