forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTooltip.tsx
320 lines (302 loc) · 10.2 KB
/
Tooltip.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import type { CSSProperties, ExtractPropTypes } from 'vue';
import { computed, watch, defineComponent, ref } from 'vue';
import VcTooltip from '../vc-tooltip';
import classNames from '../_util/classNames';
import PropTypes from '../_util/vue-types';
import warning from '../_util/warning';
import {
getStyle,
filterEmpty,
isValidElement,
initDefaultProps,
isFragment,
} from '../_util/props-util';
import { cloneElement } from '../_util/vnode';
export type { TriggerType, TooltipPlacement } from './abstractTooltipProps';
import abstractTooltipProps from './abstractTooltipProps';
import useConfigInject from '../config-provider/hooks/useConfigInject';
import getPlacements from '../_util/placements';
import firstNotUndefined from '../_util/firstNotUndefined';
import raf from '../_util/raf';
import { parseColor } from './util';
export type { AdjustOverflow, PlacementsConfig } from '../_util/placements';
import useStyle from './style';
import { getTransitionName } from '../_util/transition';
import type { CustomSlotsType } from '../_util/type';
// https://github.com/react-component/tooltip
// https://github.com/yiminghe/dom-align
export interface TooltipAlignConfig {
points?: [string, string];
offset?: [number | string, number | string];
targetOffset?: [number | string, number | string];
overflow?: { adjustX: boolean; adjustY: boolean };
useCssRight?: boolean;
useCssBottom?: boolean;
useCssTransform?: boolean;
}
const splitObject = <T extends CSSProperties>(
obj: T,
keys: (keyof T)[],
): Record<'picked' | 'omitted', T> => {
const picked: T = {} as T;
const omitted: T = { ...obj };
keys.forEach(key => {
if (obj && key in obj) {
picked[key] = obj[key];
delete omitted[key];
}
});
return { picked, omitted };
};
export const tooltipProps = () => ({
...abstractTooltipProps(),
title: PropTypes.any,
});
export const tooltipDefaultProps = () => ({
trigger: 'hover',
align: {},
placement: 'top',
mouseEnterDelay: 0.1,
mouseLeaveDelay: 0.1,
arrowPointAtCenter: false,
autoAdjustOverflow: true,
});
export type TooltipProps = Partial<ExtractPropTypes<ReturnType<typeof tooltipProps>>>;
export default defineComponent({
compatConfig: { MODE: 3 },
name: 'ATooltip',
inheritAttrs: false,
props: initDefaultProps(tooltipProps(), {
trigger: 'hover',
align: {},
placement: 'top',
mouseEnterDelay: 0.1,
mouseLeaveDelay: 0.1,
arrowPointAtCenter: false,
autoAdjustOverflow: true,
}),
slots: Object as CustomSlotsType<{
title?: any;
default?: any;
}>,
// emits: ['update:visible', 'visibleChange'],
setup(props, { slots, emit, attrs, expose }) {
if (process.env.NODE_ENV !== 'production') {
[
['visible', 'open'],
['onVisibleChange', 'onOpenChange'],
].forEach(([deprecatedName, newName]) => {
warning(
props[deprecatedName] === undefined,
'Tooltip',
`\`${deprecatedName}\` is deprecated, please use \`${newName}\` instead.`,
);
});
}
const { prefixCls, getPopupContainer, direction, rootPrefixCls } = useConfigInject(
'tooltip',
props,
);
const mergedOpen = computed(() => props.open ?? props.visible);
const innerOpen = ref(firstNotUndefined([props.open, props.visible]));
const tooltip = ref();
let rafId: any;
watch(mergedOpen, val => {
raf.cancel(rafId);
rafId = raf(() => {
innerOpen.value = !!val;
});
});
const isNoTitle = () => {
const title = props.title ?? slots.title;
return !title && title !== 0;
};
const handleVisibleChange = (val: boolean) => {
const noTitle = isNoTitle();
if (mergedOpen.value === undefined) {
innerOpen.value = noTitle ? false : val;
}
if (!noTitle) {
emit('update:visible', val);
emit('visibleChange', val);
emit('update:open', val);
emit('openChange', val);
}
};
const getPopupDomNode = () => {
return tooltip.value.getPopupDomNode();
};
expose({
getPopupDomNode,
open: innerOpen,
forcePopupAlign: () => tooltip.value?.forcePopupAlign(),
});
const tooltipPlacements = computed(() => {
const { builtinPlacements, autoAdjustOverflow, arrow, arrowPointAtCenter } = props;
let mergedArrowPointAtCenter = arrowPointAtCenter;
if (typeof arrow === 'object') {
mergedArrowPointAtCenter = arrow.pointAtCenter ?? arrowPointAtCenter;
}
return (
builtinPlacements ||
getPlacements({
arrowPointAtCenter: mergedArrowPointAtCenter,
autoAdjustOverflow,
})
);
});
const isTrueProps = (val: boolean | '') => {
return val || val === '';
};
const getDisabledCompatibleChildren = (ele: any) => {
const elementType = ele.type as any;
if (typeof elementType === 'object' && ele.props) {
if (
((elementType.__ANT_BUTTON === true || elementType === 'button') &&
isTrueProps(ele.props.disabled)) ||
(elementType.__ANT_SWITCH === true &&
(isTrueProps(ele.props.disabled) || isTrueProps(ele.props.loading))) ||
(elementType.__ANT_RADIO === true && isTrueProps(ele.props.disabled))
) {
// Pick some layout related style properties up to span
// Prevent layout bugs like https://github.com/ant-design/ant-design/issues/5254
const { picked, omitted } = splitObject(getStyle(ele), [
'position',
'left',
'right',
'top',
'bottom',
'float',
'display',
'zIndex',
]);
const spanStyle: CSSProperties = {
display: 'inline-block', // default inline-block is important
...picked,
cursor: 'not-allowed',
lineHeight: 1, // use the true height of child nodes
width: ele.props && ele.props.block ? '100%' : undefined,
};
const buttonStyle: CSSProperties = {
...omitted,
pointerEvents: 'none',
};
const child = cloneElement(
ele,
{
style: buttonStyle,
},
true,
);
return (
<span style={spanStyle} class={`${prefixCls.value}-disabled-compatible-wrapper`}>
{child}
</span>
);
}
}
return ele;
};
const getOverlay = () => {
return props.title ?? slots.title?.();
};
const onPopupAlign = (domNode: HTMLElement, align: any) => {
const placements = tooltipPlacements.value;
// 当前返回的位置
const placement = Object.keys(placements).find(
key =>
placements[key].points[0] === align.points?.[0] &&
placements[key].points[1] === align.points?.[1],
);
if (placement) {
// 根据当前坐标设置动画点
const rect = domNode.getBoundingClientRect();
const transformOrigin = {
top: '50%',
left: '50%',
};
if (placement.indexOf('top') >= 0 || placement.indexOf('Bottom') >= 0) {
transformOrigin.top = `${rect.height - align.offset[1]}px`;
} else if (placement.indexOf('Top') >= 0 || placement.indexOf('bottom') >= 0) {
transformOrigin.top = `${-align.offset[1]}px`;
}
if (placement.indexOf('left') >= 0 || placement.indexOf('Right') >= 0) {
transformOrigin.left = `${rect.width - align.offset[0]}px`;
} else if (placement.indexOf('right') >= 0 || placement.indexOf('Left') >= 0) {
transformOrigin.left = `${-align.offset[0]}px`;
}
domNode.style.transformOrigin = `${transformOrigin.left} ${transformOrigin.top}`;
}
};
const colorInfo = computed(() => parseColor(prefixCls.value, props.color));
const injectFromPopover = computed(() => (attrs as any)['data-popover-inject']);
const [wrapSSR, hashId] = useStyle(
prefixCls,
computed(() => !injectFromPopover.value),
);
return () => {
const { openClassName, overlayClassName, overlayStyle, overlayInnerStyle } = props;
let children = filterEmpty(slots.default?.()) ?? null;
children = children.length === 1 ? children[0] : children;
let tempVisible = innerOpen.value;
// Hide tooltip when there is no title
if (mergedOpen.value === undefined && isNoTitle()) {
tempVisible = false;
}
if (!children) {
return null;
}
const child = getDisabledCompatibleChildren(
isValidElement(children) && !isFragment(children) ? children : <span>{children}</span>,
);
const childCls = classNames({
[openClassName || `${prefixCls.value}-open`]: true,
[child.props && child.props.class]: child.props && child.props.class,
});
const customOverlayClassName = classNames(
overlayClassName,
{
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
},
colorInfo.value.className,
hashId.value,
);
const formattedOverlayInnerStyle = {
...colorInfo.value.overlayStyle,
...overlayInnerStyle,
};
const arrowContentStyle = colorInfo.value.arrowStyle;
const vcTooltipProps = {
...attrs,
...(props as TooltipProps),
prefixCls: prefixCls.value,
arrow: !!props.arrow,
getPopupContainer: getPopupContainer?.value,
builtinPlacements: tooltipPlacements.value,
visible: tempVisible,
ref: tooltip,
overlayClassName: customOverlayClassName,
overlayStyle: { ...arrowContentStyle, ...overlayStyle },
overlayInnerStyle: formattedOverlayInnerStyle,
onVisibleChange: handleVisibleChange,
onPopupAlign,
transitionName: getTransitionName(
rootPrefixCls.value,
'zoom-big-fast',
props.transitionName,
),
};
return wrapSSR(
<VcTooltip
{...vcTooltipProps}
v-slots={{
arrowContent: () => <span class={`${prefixCls.value}-arrow-content`}></span>,
overlay: getOverlay,
}}
>
{innerOpen.value ? cloneElement(child, { class: childCls }) : child}
</VcTooltip>,
);
};
},
});