@@ -8,6 +8,10 @@ import cn from "classnames";
8
8
import CSSTransition , {
9
9
CSSTransitionClassNames ,
10
10
} from "react-transition-group/CSSTransition" ;
11
+ import {
12
+ ConditionalPortal ,
13
+ RenderConditionalPortalProps ,
14
+ } from "@react-md/portal" ;
11
15
import {
12
16
OverridableTransitionProps ,
13
17
TransitionTimeout ,
@@ -16,26 +20,33 @@ import { bem, SimplePosition } from "@react-md/utils";
16
20
17
21
import {
18
22
DEFAULT_TOOLTIP_CLASSNAMES ,
23
+ DEFAULT_TOOLTIP_POSITION ,
19
24
DEFAULT_TOOLTIP_TIMEOUT ,
20
25
} from "./constants" ;
21
26
27
+ /** @remarks \@since 2.8.0 */
28
+ export type TooltipTransitionProps = Pick <
29
+ OverridableTransitionProps ,
30
+ | "onEnter"
31
+ | "onEntering"
32
+ | "onEntered"
33
+ | "onExit"
34
+ | "onExiting"
35
+ | "onExited"
36
+ | "mountOnEnter"
37
+ | "unmountOnExit"
38
+ > ;
39
+
22
40
/**
23
41
* The base props for the `Tooltip` component. This can be extended when
24
42
* creating custom tooltip implementations.
43
+ *
44
+ * @remarks \@since 2.8.0 Supports the {@link RenderConditionalPortalProps}
25
45
*/
26
46
export interface TooltipProps
27
- extends Pick <
28
- OverridableTransitionProps ,
29
- | "onEnter"
30
- | "onEntering"
31
- | "onEntered"
32
- | "onExit"
33
- | "onExiting"
34
- | "onExited"
35
- | "mountOnEnter"
36
- | "unmountOnExit"
37
- > ,
38
- HTMLAttributes < HTMLSpanElement > {
47
+ extends TooltipTransitionProps ,
48
+ HTMLAttributes < HTMLSpanElement > ,
49
+ RenderConditionalPortalProps {
39
50
/**
40
51
* An id for the tooltip. This is required for accessibility and finding an
41
52
* element to attach event listeners to show and hide the tooltip.
@@ -114,6 +125,28 @@ const block = bem("rmd-tooltip");
114
125
* with an animation when the visibility changes. If this component is used, you
115
126
* will need to manually add all the event listeners and triggers to change the
116
127
* `visible` prop.
128
+ *
129
+ * @example
130
+ * Simple Usage
131
+ * ```tsx
132
+ * import { Button } from "@react-md/button";
133
+ * import { useTooltip, Tooltip } from "@react-md/tooltip";
134
+ *
135
+ * function Example() {
136
+ * const { tooltipProps, elementProps } = useTooltip({
137
+ * baseId: 'my-element',
138
+ * });
139
+ *
140
+ * return (
141
+ * <>
142
+ * <Button {...elementProps}>Button</Button>
143
+ * <Tooltip {...tooltipProps}>
144
+ * Tooltip Content
145
+ * </Tooltip>
146
+ * </>
147
+ * );
148
+ * }
149
+ * ```
117
150
*/
118
151
export const Tooltip = forwardRef < HTMLSpanElement , TooltipProps > (
119
152
function Tooltip (
@@ -124,51 +157,60 @@ export const Tooltip = forwardRef<HTMLSpanElement, TooltipProps>(
124
157
timeout = DEFAULT_TOOLTIP_TIMEOUT ,
125
158
dense = false ,
126
159
lineWrap = true ,
127
- position = "below" ,
160
+ position = DEFAULT_TOOLTIP_POSITION ,
128
161
children,
129
162
onEnter,
130
163
onEntering,
131
164
onEntered,
132
165
onExit,
133
166
onExiting,
134
167
onExited,
168
+ portal = true ,
169
+ portalInto,
170
+ portalIntoId,
135
171
mountOnEnter = true ,
136
172
unmountOnExit = true ,
137
173
...props
138
174
} ,
139
175
ref
140
176
) {
141
177
return (
142
- < CSSTransition
143
- classNames = { classNames }
144
- in = { visible }
145
- timeout = { timeout }
146
- onEnter = { onEnter }
147
- onEntering = { onEntering }
148
- onEntered = { onEntered }
149
- onExit = { onExit }
150
- onExiting = { onExiting }
151
- onExited = { onExited }
152
- mountOnEnter = { mountOnEnter }
153
- unmountOnExit = { unmountOnExit }
178
+ < ConditionalPortal
179
+ portal = { portal }
180
+ portalInto = { portalInto }
181
+ portalIntoId = { portalIntoId }
154
182
>
155
- < span
156
- { ...props }
157
- ref = { ref }
158
- role = "tooltip"
159
- className = { cn (
160
- block ( {
161
- dense,
162
- "line-wrap" : lineWrap ,
163
- "dense-line-wrap" : dense && lineWrap ,
164
- [ position ] : true ,
165
- } ) ,
166
- className
167
- ) }
183
+ < CSSTransition
184
+ classNames = { classNames }
185
+ in = { visible }
186
+ timeout = { timeout }
187
+ onEnter = { onEnter }
188
+ onEntering = { onEntering }
189
+ onEntered = { onEntered }
190
+ onExit = { onExit }
191
+ onExiting = { onExiting }
192
+ onExited = { onExited }
193
+ mountOnEnter = { mountOnEnter }
194
+ unmountOnExit = { unmountOnExit }
168
195
>
169
- { children }
170
- </ span >
171
- </ CSSTransition >
196
+ < span
197
+ { ...props }
198
+ ref = { ref }
199
+ role = "tooltip"
200
+ className = { cn (
201
+ block ( {
202
+ dense,
203
+ "line-wrap" : lineWrap ,
204
+ "dense-line-wrap" : dense && lineWrap ,
205
+ [ position ] : true ,
206
+ } ) ,
207
+ className
208
+ ) }
209
+ >
210
+ { children }
211
+ </ span >
212
+ </ CSSTransition >
213
+ </ ConditionalPortal >
172
214
) ;
173
215
}
174
216
) ;
0 commit comments