@@ -7,20 +7,19 @@ import positionTooltip from "../../helpers/positionTooltip.js";
7
7
/*
8
8
Renders an accessible element that displays a tooltip.
9
9
Element can either be a link or not.
10
-
10
+
11
11
Usage:
12
12
13
13
<Tooltip
14
14
tooltipBody="This is some helper text to display in a tooltip"
15
15
id="forgot-tooltip"
16
16
text="Forgot something?"
17
17
/>
18
-
19
- Props:
20
18
19
+ Props:
21
20
--REQUIRED--
22
- text [String] - The text to display. Hovering over it will reveal the tooltip.
23
- tooltipBody [String] - The text to be displayed in the tooltip
21
+ text [String] - Visible text (also acts as accessible label)
22
+ tooltipBody [String] - Tooltip content
24
23
25
24
--OPTIONAL--
26
25
classes [String] - Additional CSS classes that will be added to the control container div element.
@@ -49,67 +48,59 @@ const Tooltip = (props) => {
49
48
const tooltipRef = useRef ( null ) ;
50
49
const triggerClass = classnames ( "dfn-tooltip-trigger" , classes ) ;
51
50
52
- const handleMouseEnter = ( event ) => {
51
+ const handleMouseEnter = ( ) => {
53
52
const currentTooltip = tooltipRef . current ;
54
- positionTooltip ( currentTooltip , position ) ;
55
- } ;
56
-
57
- const renderTriggerElement = ( ) => {
58
- if ( link ) {
59
- return renderLink ( ) ;
60
- } else if ( url ) {
61
- return renderAnchor ( ) ;
62
- } else {
63
- return renderButton ( ) ;
53
+ if ( currentTooltip ) {
54
+ positionTooltip ( currentTooltip , position ) ;
64
55
}
65
56
} ;
66
57
67
- const renderAnchor = ( ) => {
68
- return (
69
- < a
70
- aria-labelledby = { tooltipElementId }
71
- className = { triggerClass }
72
- href = { url }
73
- onMouseEnter = { handleMouseEnter }
74
- >
75
- { text }
76
- </ a >
77
- ) ;
78
- } ;
58
+ const renderAnchor = ( ) => (
59
+ < a
60
+ aria-describedby = { tooltipElementId }
61
+ className = { triggerClass }
62
+ href = { url }
63
+ onMouseEnter = { handleMouseEnter }
64
+ >
65
+ { text }
66
+ </ a >
67
+ ) ;
79
68
80
- const renderButton = ( ) => {
81
- return (
82
- < button
83
- aria-labelledby = { tooltipElementId }
84
- className = { triggerClass }
85
- onClick = { onClick }
86
- onMouseEnter = { handleMouseEnter }
87
- type = { buttonType }
88
- >
89
- { text }
90
- </ button >
91
- ) ;
92
- } ;
69
+ const renderButton = ( ) => (
70
+ < button
71
+ aria-describedby = { tooltipElementId }
72
+ className = { triggerClass }
73
+ onClick = { onClick }
74
+ onMouseEnter = { handleMouseEnter }
75
+ type = { buttonType }
76
+ >
77
+ { text }
78
+ </ button >
79
+ ) ;
93
80
94
- const renderLink = ( ) => {
95
- return (
96
- < Link
97
- aria-labelledby = { tooltipElementId }
98
- className = { triggerClass }
99
- onMouseEnter = { handleMouseEnter }
100
- to = { link }
101
- >
102
- { text }
103
- </ Link >
104
- ) ;
81
+ const renderLink = ( ) => (
82
+ < Link
83
+ aria-describedby = { tooltipElementId }
84
+ className = { triggerClass }
85
+ onMouseEnter = { handleMouseEnter }
86
+ to = { link }
87
+ >
88
+ { text }
89
+ </ Link >
90
+ ) ;
91
+
92
+ const renderTriggerElement = ( ) => {
93
+ if ( link ) return renderLink ( ) ;
94
+ if ( url ) return renderAnchor ( ) ;
95
+ return renderButton ( ) ;
105
96
} ;
106
97
107
98
return (
108
99
< div className = "dfn-tooltip" >
109
100
{ renderTriggerElement ( ) }
110
101
< div
111
102
className = { classnames ( "dfn-tooltip-bubble" , `tooltip-${ position } ` ) }
112
- id = { ` ${ id } -desc` }
103
+ id = { tooltipElementId }
113
104
ref = { tooltipRef }
114
105
role = "tooltip"
115
106
>
0 commit comments