forked from mantinedev/mantine
/
Tooltip.tsx
202 lines (177 loc) · 5.31 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
import React, { cloneElement, forwardRef, useRef } from 'react';
import { isElement, ForwardRefWithStaticComponents } from '@mantine/utils';
import { useMergedRef } from '@mantine/hooks';
import { getDefaultZIndex, useComponentDefaultProps } from '@mantine/styles';
import { TooltipGroup } from './TooltipGroup/TooltipGroup';
import { TooltipFloating } from './TooltipFloating/TooltipFloating';
import { useTooltip } from './use-tooltip';
import { FloatingArrow, getFloatingPosition, FloatingPosition } from '../Floating';
import { MantineTransition, Transition } from '../Transition';
import { OptionalPortal } from '../Portal';
import { Box } from '../Box';
import { TOOLTIP_ERRORS } from './Tooltip.errors';
import { TooltipBaseProps } from './Tooltip.types';
import useStyles from './Tooltip.styles';
export interface TooltipProps extends TooltipBaseProps {
/** Called when tooltip position changes */
onPositionChange?(position: FloatingPosition): void;
/** Open delay in ms */
openDelay?: number;
/** Close delay in ms */
closeDelay?: number;
/** Controls opened state */
opened?: boolean;
/** Space between target element and tooltip in px */
offset?: number;
/** Determines whether component should have an arrow */
withArrow?: boolean;
/** Arrow size in px */
arrowSize?: number;
/** Arrow offset in px */
arrowOffset?: number;
/** One of premade transitions ot transition object */
transition?: MantineTransition;
/** Transition duration in ms */
transitionDuration?: number;
/** Determines which events will be used to show tooltip */
events?: { hover: boolean; focus: boolean; touch: boolean };
/** useEffect dependencies to force update tooltip position */
positionDependencies?: any[];
/** Set if tooltip is attached to an inline element */
inline?: boolean;
}
const defaultProps: Partial<TooltipProps> = {
position: 'top',
refProp: 'ref',
withinPortal: false,
inline: false,
arrowSize: 4,
arrowOffset: 5,
offset: 5,
transition: 'fade',
transitionDuration: 100,
width: 'auto',
events: { hover: true, focus: false, touch: false },
zIndex: getDefaultZIndex('popover'),
positionDependencies: [],
};
const _Tooltip = forwardRef<HTMLElement, TooltipProps>((props, ref) => {
const arrowRef = useRef<HTMLDivElement | null>(null);
const {
children,
position,
refProp,
label,
openDelay,
closeDelay,
onPositionChange,
opened,
withinPortal,
radius,
color,
classNames,
styles,
unstyled,
style,
className,
withArrow,
arrowSize,
arrowOffset,
offset,
transition,
transitionDuration,
multiline,
width,
events,
zIndex,
disabled,
positionDependencies,
onClick,
onMouseEnter,
onMouseLeave,
inline,
...others
} = useComponentDefaultProps('Tooltip', defaultProps, props);
const { classes, cx, theme } = useStyles(
{ radius, color, width, multiline },
{ name: 'Tooltip', classNames, styles, unstyled }
);
const tooltip = useTooltip({
position: getFloatingPosition(theme.dir, position),
closeDelay,
openDelay,
onPositionChange,
opened,
events,
arrowRef,
offset: offset + (withArrow ? arrowSize / 2 : 0),
positionDependencies: [...positionDependencies, children],
inline,
});
if (!isElement(children)) {
throw new Error(TOOLTIP_ERRORS.children);
}
const targetRef = useMergedRef(tooltip.reference, (children as any).ref, ref);
return (
<>
<OptionalPortal withinPortal={withinPortal}>
<Transition
mounted={!disabled && tooltip.opened}
transition={transition}
duration={tooltip.isGroupPhase ? 10 : transitionDuration}
>
{(transitionStyles) => (
<Box
{...others}
{...tooltip.getFloatingProps({
ref: tooltip.floating,
className: classes.tooltip,
style: {
...style,
...transitionStyles,
zIndex,
top: tooltip.y ?? 0,
left: tooltip.x ?? 0,
},
})}
>
{label}
<FloatingArrow
ref={arrowRef}
arrowX={tooltip.arrowX}
arrowY={tooltip.arrowY}
visible={withArrow}
withBorder={false}
position={tooltip.placement}
arrowSize={arrowSize}
arrowOffset={arrowOffset}
className={classes.arrow}
/>
</Box>
)}
</Transition>
</OptionalPortal>
{cloneElement(
children,
tooltip.getReferenceProps({
onClick,
onMouseEnter,
onMouseLeave,
onMouseMove: props.onMouseMove,
onPointerDown: props.onPointerDown,
onPointerEnter: props.onPointerEnter,
[refProp]: targetRef,
className: cx(className, children.props.className),
...children.props,
})
)}
</>
);
}) as any;
_Tooltip.Group = TooltipGroup;
_Tooltip.Floating = TooltipFloating;
_Tooltip.displayName = '@mantine/core/Tooltip';
export const Tooltip: ForwardRefWithStaticComponents<
TooltipProps,
{ Group: typeof TooltipGroup; Floating: typeof TooltipFloating }
> = _Tooltip;