forked from react-component/tooltip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTooltip.tsx
More file actions
143 lines (133 loc) · 3.91 KB
/
Tooltip.tsx
File metadata and controls
143 lines (133 loc) · 3.91 KB
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
import type { ArrowType, TriggerProps, TriggerRef } from '@rc-component/trigger';
import Trigger from '@rc-component/trigger';
import type { ActionType, AlignType, AnimationType } from '@rc-component/trigger/lib/interface';
import * as React from 'react';
import { forwardRef, useImperativeHandle, useRef } from 'react';
import { placements } from './placements';
import Popup from './Popup';
import classNames from 'classnames';
export interface TooltipProps
extends Pick<
TriggerProps,
| 'onPopupAlign'
| 'builtinPlacements'
| 'fresh'
| 'children'
| 'mouseLeaveDelay'
| 'mouseEnterDelay'
| 'prefixCls'
| 'forceRender'
| 'popupVisible'
> {
trigger?: ActionType | ActionType[];
defaultVisible?: boolean;
visible?: boolean;
placement?: string;
/** @deprecated Use `motion` instead */
transitionName?: string;
/** @deprecated Use `motion` instead */
animation?: AnimationType;
/** Config popup motion */
motion?: TriggerProps['popupMotion'];
onVisibleChange?: (visible: boolean) => void;
afterVisibleChange?: (visible: boolean) => void;
overlay: (() => React.ReactNode) | React.ReactNode;
/** @deprecated Please use `styles={{ root: {} }}` */
overlayStyle?: React.CSSProperties;
/** @deprecated Please use `classNames={{ root: {} }}` */
overlayClassName?: string;
getTooltipContainer?: (node: HTMLElement) => HTMLElement;
destroyTooltipOnHide?: boolean;
align?: AlignType;
showArrow?: boolean | ArrowType;
arrowContent?: React.ReactNode;
id?: string;
/** @deprecated Please use `styles={{ body: {} }}` */
overlayInnerStyle?: React.CSSProperties;
zIndex?: number;
styles?: TooltipStyles;
classNames?: TooltipClassNames;
}
export interface TooltipStyles {
root?: React.CSSProperties;
body?: React.CSSProperties;
}
export interface TooltipClassNames {
root?: string;
body?: string;
}
export interface TooltipRef extends TriggerRef {}
const Tooltip = (props: TooltipProps, ref: React.Ref<TooltipRef>) => {
const {
overlayClassName,
trigger = ['hover'],
mouseEnterDelay = 0,
mouseLeaveDelay = 0.1,
overlayStyle,
prefixCls = 'rc-tooltip',
children,
onVisibleChange,
afterVisibleChange,
transitionName,
animation,
motion,
placement = 'right',
align = {},
destroyTooltipOnHide = false,
defaultVisible,
getTooltipContainer,
overlayInnerStyle,
arrowContent,
overlay,
id,
showArrow = true,
classNames: tooltipClassNames,
styles: tooltipStyles,
...restProps
} = props;
const triggerRef = useRef<TriggerRef>(null);
useImperativeHandle(ref, () => triggerRef.current);
const extraProps: Partial<TooltipProps & TriggerProps> = { ...restProps };
if ('visible' in props) {
extraProps.popupVisible = props.visible;
}
const getPopupElement = () => (
<Popup
key="content"
prefixCls={prefixCls}
id={id}
bodyClassName={tooltipClassNames?.body}
overlayInnerStyle={{ ...overlayInnerStyle, ...tooltipStyles?.body }}
>
{overlay}
</Popup>
);
return (
<Trigger
popupClassName={classNames(overlayClassName, tooltipClassNames?.root)}
prefixCls={prefixCls}
popup={getPopupElement}
action={trigger}
builtinPlacements={placements}
popupPlacement={placement}
ref={triggerRef}
popupAlign={align}
getPopupContainer={getTooltipContainer}
onPopupVisibleChange={onVisibleChange}
afterPopupVisibleChange={afterVisibleChange}
popupTransitionName={transitionName}
popupAnimation={animation}
popupMotion={motion}
defaultPopupVisible={defaultVisible}
autoDestroy={destroyTooltipOnHide}
mouseLeaveDelay={mouseLeaveDelay}
popupStyle={{ ...overlayStyle, ...tooltipStyles?.root }}
mouseEnterDelay={mouseEnterDelay}
arrow={showArrow}
{...extraProps}
>
{children}
</Trigger>
);
};
export default forwardRef(Tooltip);