@@ -7,20 +7,19 @@ import positionTooltip from "../../helpers/positionTooltip.js";
77/*
88 Renders an accessible element that displays a tooltip.
99 Element can either be a link or not.
10-
10+
1111 Usage:
1212
1313 <Tooltip
1414 tooltipBody="This is some helper text to display in a tooltip"
1515 id="forgot-tooltip"
1616 text="Forgot something?"
1717 />
18-
19- Props:
2018
19+ Props:
2120 --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
2423
2524 --OPTIONAL--
2625 classes [String] - Additional CSS classes that will be added to the control container div element.
@@ -49,67 +48,59 @@ const Tooltip = (props) => {
4948 const tooltipRef = useRef ( null ) ;
5049 const triggerClass = classnames ( "dfn-tooltip-trigger" , classes ) ;
5150
52- const handleMouseEnter = ( event ) => {
51+ const handleMouseEnter = ( ) => {
5352 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 ) ;
6455 }
6556 } ;
6657
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+ ) ;
7968
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+ ) ;
9380
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 ( ) ;
10596 } ;
10697
10798 return (
10899 < div className = "dfn-tooltip" >
109100 { renderTriggerElement ( ) }
110101 < div
111102 className = { classnames ( "dfn-tooltip-bubble" , `tooltip-${ position } ` ) }
112- id = { ` ${ id } -desc` }
103+ id = { tooltipElementId }
113104 ref = { tooltipRef }
114105 role = "tooltip"
115106 >
0 commit comments