Skip to content

Commit 04f35e9

Browse files
authored
fix(tooltip): do not use native title when tooltip is off (#1196)
* fix(tooltip): do not use native title when tooltip is off * Update src/components/tooltip/tooltip.spec.js Co-Authored-By: Nicola Molinari <[email protected]> * chore: pr comment
1 parent 68acc88 commit 04f35e9

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/components/tooltip/tooltip.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,25 @@ const Tooltip = props => {
140140
}, [props.off, closeAfter, handleClose, toggle, isOpen]);
141141

142142
const childrenProps = {
143-
'aria-describedby': tooltipIsOpen ? id : null,
144-
// for seo and accessibility, we add the tooltip's title
145-
// as a native title when the title is hidden
146-
title:
147-
!tooltipIsOpen && typeof props.title === 'string' ? props.title : null,
148-
...props.children.props,
149143
// don't pass event listeners to children
150144
onFocus: null,
151145
onMouseOver: null,
152146
onMouseLeave: null,
153147
onBlur: null,
154148
};
155149

150+
const tooltipProps = !props.off
151+
? {
152+
'aria-describedby': tooltipIsOpen ? id : null,
153+
// for seo and accessibility, we add the tooltip's title
154+
// as a native title when the title is hidden
155+
title:
156+
!tooltipIsOpen && typeof props.title === 'string'
157+
? props.title
158+
: null,
159+
}
160+
: {};
161+
156162
const eventListeners = !props.off
157163
? {
158164
onMouseOver: handleEnter,
@@ -170,7 +176,10 @@ const Tooltip = props => {
170176
return (
171177
<React.Fragment>
172178
<WrapperComponent {...eventListeners} ref={reference.ref}>
173-
{React.cloneElement(props.children, { ...childrenProps })}
179+
{React.cloneElement(props.children, {
180+
...childrenProps,
181+
...tooltipProps,
182+
})}
174183
</WrapperComponent>
175184
{tooltipIsOpen && (
176185
<TooltipWrapperComponent>

src/components/tooltip/tooltip.spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,8 @@ describe('when off is true', () => {
430430

431431
// should not be visible
432432
expect(queryByText('What kind of bear is best?')).not.toBeInTheDocument();
433-
// should not remove title
434-
expect(button).toHaveProperty('title', 'What kind of bear is best?');
433+
// should not have title
434+
expect(button).not.toHaveAttribute('title');
435435

436436
fireEvent.blur(button);
437437

@@ -447,8 +447,8 @@ describe('when off is true', () => {
447447

448448
// should not be visible
449449
expect(queryByText('What kind of bear is best?')).not.toBeInTheDocument();
450-
// should not remove title
451-
expect(button).toHaveProperty('title', 'What kind of bear is best?');
450+
// should not have title
451+
expect(button).not.toHaveAttribute('title');
452452

453453
fireEvent.mouseLeave(button);
454454

0 commit comments

Comments
 (0)