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(popover): clear popover delayCheckHover timeout #622

Merged
merged 4 commits into from
Jul 29, 2020
Merged
Changes from 1 commit
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
49 changes: 26 additions & 23 deletions react/src/lib/Popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ class Popover extends React.Component {
// focus on the first button in the EventOverlay
const { isOpen } = this.state;
const { autoFocusOnFirstElt } = this.props;

if (autoFocusOnFirstElt
&& isOpen
&& !prevState.isOpen
&& this.overlay
) {

if (autoFocusOnFirstElt && isOpen && !prevState.isOpen && this.overlay) {
const eventOverlay = ReactDOM.findDOMNode(this.overlay);

if (eventOverlay) {
Expand All @@ -38,6 +34,7 @@ class Popover extends React.Component {
componentWillUnmount() {
this.hideTimerId && clearTimeout(this.hideTimerId);
this.showTimerId && clearTimeout(this.showTimerId);
this.delayCheckHoverTimerId && clearTimeout(this.delayCheckHoverTimerId);
}

delayedHide = e => {
Expand Down Expand Up @@ -86,7 +83,7 @@ class Popover extends React.Component {

handleHide = e => {
this.setState({ isHovering: false }, () => this.delayedHide(e));
}
};

handleMouseEnter = e => {
const { children } = this.props;
Expand All @@ -100,7 +97,10 @@ class Popover extends React.Component {
const delay = popoverTrigger === 'MouseEnter' ? hoverDelay : 0;
e.persist();

this.setState({ isHovering: false }, () => setTimeout(() => this.delayedHide(e), delay));
this.setState({ isHovering: false }, () => {
this.delayCheckHoverTimerId && clearTimeout(this.delayCheckHoverTimerId);
this.delayCheckHoverTimerId = setTimeout(() => this.delayedHide(e), delay);
});
};

handleMouseLeave = e => {
Expand Down Expand Up @@ -159,12 +159,9 @@ class Popover extends React.Component {
e.preventDefault();
e.stopPropagation();
e.persist();

// Open Popover
this.setState(
{ isHovering: true },
() => this.delayedShow(e)
);
this.setState({ isHovering: true }, () => this.delayedShow(e));
break;
case 27: // ESC
e.persist();
Expand All @@ -179,7 +176,7 @@ class Popover extends React.Component {
}
break;
}
}
};

handleKeyDownEventOverlay = e => {
if (this.state.isOpen && this.overlay && this.anchorRef) {
Expand All @@ -190,13 +187,16 @@ class Popover extends React.Component {
switch (e.which) {
case 9:
if (tabbableElements.length) {
if (this.props.closeOnFocusLeavesContent) { // if closeOnFocusLeavesContent = true
if (e.shiftKey) { // SHIFT + TAB
if (this.props.closeOnFocusLeavesContent) {
// if closeOnFocusLeavesContent = true
Copy link
Collaborator

Choose a reason for hiding this comment

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

don't think we need this comment. seems to repeat the code exactly :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

i didn't write the comment. formatting the file moved it to the next line :P

if (e.shiftKey) {
// SHIFT + TAB
// If first interactable element in EventOverlay, hide the popover
if (document.activeElement === tabbableElements[0]) {
this.handleHide(e);
}
} else { // TAB
} else {
// TAB
// If last interactable element in EventOverlay, hide the popover
if (document.activeElement === tabbableElements[tabbableElements.length - 1]) {
e.preventDefault();
Expand All @@ -205,16 +205,19 @@ class Popover extends React.Component {
trigger.focus();
}
}
} else { // if closeOnFocusLeavesContent = false
} else {
// if closeOnFocusLeavesContent = false
// Intent is for TAB and SHIFT+TAB to trap the user inside the dialog (AKA popover)
if (e.shiftKey) { // SHIFT + TAB
if (e.shiftKey) {
// SHIFT + TAB
// If first interactable element in EventOverlay, go to the last interatable element
if (document.activeElement === tabbableElements[0]) {
e.preventDefault();
e.stopPropagation();
tabbableElements[tabbableElements.length - 1].focus();
}
} else { // TAB
} else {
// TAB
// If last interactable element in EventOverlay, go to the first interatable element
if (document.activeElement === tabbableElements[tabbableElements.length - 1]) {
e.preventDefault();
Expand All @@ -232,7 +235,7 @@ class Popover extends React.Component {
break;
}
}
}
};

render() {
const { isOpen } = this.state;
Expand Down Expand Up @@ -307,7 +310,6 @@ class Popover extends React.Component {

const anchorWithTriggers = () => children && React.cloneElement(children, getTriggers());


return (
<React.Fragment>
{anchorWithTriggers()}
Expand All @@ -328,7 +330,8 @@ class Popover extends React.Component {
e.persist();
this.handleHide(e);
},
onKeyDown: !includeFocusOnHover ? this.handleKeyDownEventOverlay : undefined})}
onKeyDown: !includeFocusOnHover ? this.handleKeyDownEventOverlay : undefined,
})}
{...(popoverTrigger === 'Focus' && { allowClickAway: false })}
{...otherProps}
>
Expand Down