-
-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathTooltip.tsx
More file actions
164 lines (144 loc) · 4.49 KB
/
Tooltip.tsx
File metadata and controls
164 lines (144 loc) · 4.49 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
import type { ArrowType, TriggerProps, TriggerRef } from '@rc-component/trigger';
import Trigger from '@rc-component/trigger';
import type { ActionType, AlignType } from '@rc-component/trigger/lib/interface';
import useId from '@rc-component/util/lib/hooks/useId';
import { clsx } from 'clsx';
import * as React from 'react';
import { useImperativeHandle, useRef } from 'react';
import { placements } from './placements';
import Popup from './Popup';
export type SemanticName = 'root' | 'arrow' | 'container' | 'uniqueContainer';
export interface TooltipProps
extends Pick<
TriggerProps,
| 'onPopupAlign'
| 'builtinPlacements'
| 'fresh'
| 'mouseLeaveDelay'
| 'mouseEnterDelay'
| 'prefixCls'
| 'forceRender'
| 'popupVisible'
> {
children: React.ReactElement;
// Style
classNames?: Partial<Record<SemanticName, string>>;
styles?: Partial<Record<SemanticName, React.CSSProperties>>;
/** Config popup motion */
motion?: TriggerProps['popupMotion'];
// Rest
trigger?: ActionType | ActionType[];
defaultVisible?: boolean;
visible?: boolean;
placement?: string;
onVisibleChange?: (visible: boolean) => void;
afterVisibleChange?: (visible: boolean) => void;
overlay: (() => React.ReactNode) | React.ReactNode;
getTooltipContainer?: (node: HTMLElement) => HTMLElement;
destroyOnHidden?: boolean;
align?: AlignType;
showArrow?: boolean | ArrowType;
arrowContent?: React.ReactNode;
id?: string;
zIndex?: number;
/**
* Configures Tooltip to reuse the background for transition usage.
* This is an experimental API and may not be stable.
*/
unique?: TriggerProps['unique'];
}
export interface TooltipRef extends TriggerRef {}
const Tooltip = React.forwardRef<TooltipRef, TooltipProps>((props, ref) => {
const {
trigger = ['hover'],
mouseEnterDelay = 0,
mouseLeaveDelay = 0.1,
prefixCls = 'rc-tooltip',
children,
onVisibleChange,
afterVisibleChange,
motion,
placement = 'right',
align = {},
destroyOnHidden = false,
defaultVisible,
getTooltipContainer,
arrowContent,
overlay,
id,
showArrow = true,
classNames,
styles,
...restProps
} = props;
const mergedId = useId(id);
const triggerRef = useRef<TriggerRef>(null);
useImperativeHandle(ref, () => triggerRef.current);
const extraProps: Partial<TooltipProps & TriggerProps> = { ...restProps };
if ('visible' in props) {
extraProps.popupVisible = props.visible;
}
// ========================= Arrow ==========================
// Process arrow configuration
const mergedArrow = React.useMemo(() => {
if (!showArrow) {
return false;
}
// Convert true to object for unified processing
const arrowConfig = showArrow === true ? {} : showArrow;
// Apply semantic styles with unified logic
return {
...arrowConfig,
className: clsx(arrowConfig.className, classNames?.arrow),
style: { ...arrowConfig.style, ...styles?.arrow },
content: arrowConfig.content ?? arrowContent,
};
}, [showArrow, classNames?.arrow, styles?.arrow, arrowContent]);
// ======================== Children ========================
const getChildren: TriggerProps['children'] = ({ open }) => {
const child = React.Children.only(children);
const ariaProps: React.AriaAttributes = {
'aria-describedby': overlay && open ? mergedId : undefined,
};
return React.cloneElement(child, ariaProps);
};
// ========================= Render =========================
return (
<Trigger
popupClassName={classNames?.root}
prefixCls={prefixCls}
popup={
<Popup
key="content"
prefixCls={prefixCls}
id={mergedId}
classNames={classNames}
styles={styles}
>
{overlay}
</Popup>
}
action={trigger}
builtinPlacements={placements}
popupPlacement={placement}
ref={triggerRef}
popupAlign={align}
getPopupContainer={getTooltipContainer}
onOpenChange={onVisibleChange}
afterOpenChange={afterVisibleChange}
popupMotion={motion}
defaultPopupVisible={defaultVisible}
autoDestroy={destroyOnHidden}
mouseLeaveDelay={mouseLeaveDelay}
popupStyle={styles?.root}
mouseEnterDelay={mouseEnterDelay}
arrow={mergedArrow}
uniqueContainerClassName={classNames?.uniqueContainer}
uniqueContainerStyle={styles?.uniqueContainer}
{...extraProps}
>
{getChildren}
</Trigger>
);
});
export default Tooltip;