Skip to content

Commit

Permalink
STCOM-1391 Tooltip - check for hover on tooltip content before closin…
Browse files Browse the repository at this point in the history
…g the tooltip. (#2411)

According to
[WCAG](https://www.w3.org/TR/WCAG21/#content-on-hover-or-focus), content
that's available/visible on hover should remain visible when the content
itself is hovered... mouse-readers everywhere unite!

### Approach
Tooltip already uses timeouts to provide a brief gap between event
listeners and state updates. This implementation added a timeout to
hiding the tooltip where we only hide the tooltip if neither the trigger
nor the tooltip element itself are hovered by the mouse (using
`matches(':hover')`). The CSS of the tooltip class had to be affected to
observe mouse events since they are all placed in the
`#OverlayContainer` by default.
  • Loading branch information
JohnC-80 authored Jan 7, 2025
1 parent 945b706 commit f7e2775
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* Clear filter value after an action chosen from `MultiSelection` menu. Refs STCOM-1385.
* ExportCSV - fix usage within `<Modal>`s by rendering the download link to the `div#OverlayContainer`. Refs STCOM-1387.
* `<MenuSection>` should default its heading/label tag to `H3` instead of `H1`. Refs STCOM-1392.
* `<Tooltip>` should allow for tooltip content to be hovered without closing the tooltip. Refs STCOM-1391.
* `<AdvancedSearchRow>` - change `aria-label` for the input box to enter a search query and the Boolean operator dropdown. Refs STCOM-1195.

## [12.2.0](https://github.com/folio-org/stripes-components/tree/v12.2.0) (2024-10-11)
Expand Down
1 change: 1 addition & 0 deletions lib/Tooltip/Tooltip.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
animation-timing-function: ease-in;
animation-duration: 0.1s;
font-size: var(--font-size-small);
pointer-events: all;
}

.text {
Expand Down
25 changes: 19 additions & 6 deletions lib/Tooltip/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default class Tooltip extends Component {
constructor(props) {
super(props);
this.triggerRef = props.triggerRef || createRef(null);
this.overlayRef = createRef(null);
}

state = {
Expand Down Expand Up @@ -99,10 +100,22 @@ export default class Tooltip extends Component {
const disable = hideOnTouch && isTouch;
clearTimeout(this.timeout);

if (bool !== open && !disable) {
this.setState({
open: bool,
});
// Accessibility - When hiding the tooltip, ensure that the mouse is not hovered over the tooltip
// - for mouse readers (WCAG 2.1 - 1.4.13 )
if (!bool) {
if (!this.overlayRef?.current?.matches(':hover') &&
!this.triggerRef?.current?.matches(':hover')
) {
this.setState({
open: bool,
});
}
} else {
if (bool !== open && !disable) {
this.setState({
open: bool,
});
}
}
}

Expand All @@ -114,7 +127,7 @@ export default class Tooltip extends Component {

hide = () => {
clearTimeout(this.timeout);
this.toggle(false);
this.timeout = setTimeout(() => this.toggle(false), 150);
}

/**
Expand Down Expand Up @@ -213,7 +226,7 @@ export default class Tooltip extends Component {
placement={placement}
modifiers={modifiers}
>
<div className={css.tooltip} aria-hidden data-test-tooltip>
<div className={css.tooltip} ref={this.overlayRef} onMouseLeave={()=>this.hide()} aria-hidden data-test-tooltip>
{text && (
<div className={css.text} data-test-tooltip-text>
{text}
Expand Down

0 comments on commit f7e2775

Please sign in to comment.