Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(src/index.js): add accessibility support for tabbing #695

Merged
merged 2 commits into from
Apr 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ class ReactTooltip extends React.Component {
}

target.addEventListener('mouseenter', this.showTooltip, isCaptureMode);
target.addEventListener('focus', this.showTooltip, isCaptureMode);
if (effect === 'float') {
target.addEventListener(
'mousemove',
Expand All @@ -296,6 +297,7 @@ class ReactTooltip extends React.Component {
);
}
target.addEventListener('mouseleave', this.hideTooltip, isCaptureMode);
target.addEventListener('blur', this.showTooltip, isCaptureMode);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't on blur triggers hideTooltip instead? I believe this caused tooltips to be always displayed when we updated to the latest release

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, my apologies, let me fix this

});
}

Expand Down Expand Up @@ -401,6 +403,11 @@ class ReactTooltip extends React.Component {
scrollHide = this.props.scrollHide;
}

// adding aria-describedby to target to make tooltips read by screen readers
if (e && e.currentTarget && e.currentTarget.setAttribute) {
e.currentTarget.setAttribute('aria-describedby', this.state.uuid);
}

// Make sure the correct place is set
const desiredPlace =
e.currentTarget.getAttribute('data-place') || this.props.place || 'top';
Expand Down Expand Up @@ -621,6 +628,11 @@ class ReactTooltip extends React.Component {
if (!isMyElement || !this.state.show) return;
}

// clean up aria-describedby when hiding tooltip
if (e && e.currentTarget && e.currentTarget.removeAttribute) {
e.currentTarget.removeAttribute('aria-describedby');
}

const resetState = () => {
const isVisible = this.state.show;
// Check if the mouse is actually over the tooltip, if so don't hide the tooltip
Expand Down Expand Up @@ -737,7 +749,7 @@ class ReactTooltip extends React.Component {
}

render() {
const { extraClass, html, ariaProps, disable } = this.state;
const { extraClass, html, ariaProps, disable, uuid } = this.state;
const content = this.getTooltipContent();
const isEmptyTip = this.isEmptyTip(content);
const style = generateTooltipStyle(
Expand Down Expand Up @@ -773,7 +785,7 @@ class ReactTooltip extends React.Component {
return (
<Wrapper
className={`${wrapperClassName}`}
id={this.props.id}
id={this.props.id || uuid}
ref={ref => (this.tooltipRef = ref)}
{...ariaProps}
data-id="tooltip"
Expand All @@ -784,7 +796,7 @@ class ReactTooltip extends React.Component {
return (
<Wrapper
className={`${wrapperClassName}`}
id={this.props.id}
id={this.props.id || uuid}
{...ariaProps}
ref={ref => (this.tooltipRef = ref)}
data-id="tooltip"
Expand Down