-
-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathTooltip.tsx
More file actions
201 lines (174 loc) · 4.46 KB
/
Tooltip.tsx
File metadata and controls
201 lines (174 loc) · 4.46 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
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 React, { useRef, useImperativeHandle, forwardRef } from 'react';
import Trigger, { TriggerProps } from 'rc-trigger';
import { AlignType, AnimationType, ActionType } from 'rc-trigger/lib/interface';
import { placements } from './placements';
import Content from './Content';
/**
* Where to place the tooltop. One of
* 'left' |'right' |'top' |'bottom' | 'topLeft' | 'topRight' | 'bottomLeft' | 'bottomRight';
*/
type Placement =
| 'left'
| 'right'
| 'top'
| 'bottom'
| 'topLeft'
| 'topRight'
| 'bottomLeft'
| 'bottomRight';
export interface TooltipProps extends Pick<TriggerProps, 'onPopupAlign' | 'builtinPlacements'> {
/**
* Call after visible is changed
*/
afterVisibleChange?: () => void;
/**
* value will be merged into placement's config
*/
align?: AlignType;
/**
*
*/
animation?: AnimationType;
/**
* Arrow content, a react component
* @default null
*/
arrowContent?: React.ReactElement;
children?: React.ReactElement;
/**
* Whether tooltip is visible initially
* @efaults false
*/
defaultVisible?: boolean;
/**
* Whether destroy tooltip when tooltip is hidden
* @default false
*/
destroyTooltipOnHide?: boolean;
/**
* Function returning html node which will act as tooltip container.
* By default the tooltip attaches to the body.
* If you want to change the container, simply return a new element.
*/
getTooltipContainer?: (node: HTMLElement) => HTMLElement;
/**
* Id which gets attached to the tooltip content.
* Can be used with aria-describedby to achieve Screenreader-Support.
*/
id?: string;
/**
* Delay time to show when mouse enter.unit: s.
* @default 0
*/
mouseEnterDelay?: number;
/**
* Delay time to hide when mouse leave.unit: s.
* @default 0.1
*/
mouseLeaveDelay?: number;
/**
* Call when visible is changed
*/
onVisibleChange?: (visible: boolean) => void;
/**
* React component to render in the popup
*/
overlay: (() => React.ReactNode) | React.ReactNode;
/**
* Additional className added to popup overlay
*/
overlayClassName?: string;
/**
* Additional style of overlay node
*/
overlayStyle?: React.CSSProperties;
/**
* Where to place the tooltop. One of
* 'left' |'right' |'top' |'bottom' | 'topLeft' | 'topRight' | 'bottomLeft' | 'bottomRight';
*/
placement?: Placement;
/**
*
*/
popupVisible?: boolean;
/**
* prefix class name.
* @default "rc-tooltip"
*/
prefixCls?: string;
/**
* same as https://github.com/react-component/animate
*/
transitionName?: string;
/**
* Which actions cause tooltip shown. enum of 'hover','click','focus'
* @default 'hover'
*/
trigger?: ActionType;
/**
* call when visible is changed
*/
visible?: boolean;
}
const Tooltip = (props: TooltipProps, ref) => {
const {
overlayClassName,
trigger = ['hover'],
mouseEnterDelay = 0,
mouseLeaveDelay = 0.1,
overlayStyle,
prefixCls = 'rc-tooltip',
children,
onVisibleChange,
afterVisibleChange,
transitionName,
animation,
placement = 'right',
align = {},
destroyTooltipOnHide = false,
defaultVisible,
getTooltipContainer,
...restProps
} = props;
const domRef = useRef(null);
useImperativeHandle(ref, () => domRef.current);
const extraProps = { ...restProps };
if ('visible' in props) {
extraProps.popupVisible = props.visible;
}
const getPopupElement = () => {
const { arrowContent = null, overlay, id } = props;
return [
<div className={`${prefixCls}-arrow`} key="arrow">
{arrowContent}
</div>,
<Content key="content" prefixCls={prefixCls} id={id} overlay={overlay} />,
];
};
return (
<Trigger
popupClassName={overlayClassName}
prefixCls={prefixCls}
popup={getPopupElement}
action={trigger}
builtinPlacements={placements}
popupPlacement={placement}
ref={domRef}
popupAlign={align}
getPopupContainer={getTooltipContainer}
onPopupVisibleChange={onVisibleChange}
afterPopupVisibleChange={afterVisibleChange}
popupTransitionName={transitionName}
popupAnimation={animation}
defaultPopupVisible={defaultVisible}
destroyPopupOnHide={destroyTooltipOnHide}
mouseLeaveDelay={mouseLeaveDelay}
popupStyle={overlayStyle}
mouseEnterDelay={mouseEnterDelay}
{...extraProps}
>
{children}
</Trigger>
);
};
export default forwardRef(Tooltip);