From ae936a5275ee4a2cf32a84dbf8dc004e20b748e1 Mon Sep 17 00:00:00 2001 From: Justin Shaw Date: Sat, 24 Apr 2021 17:50:33 -0700 Subject: [PATCH] fix(src/index.js): add accessibility support for tabbing (#695) --- src/index.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index b951563ad..6d0d07962 100755 --- a/src/index.js +++ b/src/index.js @@ -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', @@ -296,6 +297,7 @@ class ReactTooltip extends React.Component { ); } target.addEventListener('mouseleave', this.hideTooltip, isCaptureMode); + target.addEventListener('blur', this.showTooltip, isCaptureMode); }); } @@ -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'; @@ -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 @@ -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( @@ -773,7 +785,7 @@ class ReactTooltip extends React.Component { return ( (this.tooltipRef = ref)} {...ariaProps} data-id="tooltip" @@ -784,7 +796,7 @@ class ReactTooltip extends React.Component { return ( (this.tooltipRef = ref)} data-id="tooltip"