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

[Tooltip] Meet dismissable WCAG criterion #22376

Merged
merged 1 commit into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions packages/material-ui/src/Tooltip/Tooltip.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ export interface TooltipProps extends StandardProps<React.HTMLAttributes<HTMLDiv
*
* @param {object} event The event source of the callback.
*/
onClose?: (event: React.ChangeEvent<{}>) => void;
onClose?: (event: React.SyntheticEvent | Event) => void;
/**
* Callback fired when the component requests to be open.
*
* @param {object} event The event source of the callback.
*/
onOpen?: (event: React.ChangeEvent<{}>) => void;
onOpen?: (event: React.SyntheticEvent) => void;
/**
* If `true`, the tooltip is shown.
*/
Expand Down
58 changes: 43 additions & 15 deletions packages/material-ui/src/Tooltip/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import withStyles from '../styles/withStyles';
import capitalize from '../utils/capitalize';
import Grow from '../Grow';
import Popper from '../Popper';
import useEventCallback from '../utils/useEventCallback';
import useForkRef from '../utils/useForkRef';
import useId from '../utils/useId';
import useIsFocusVisible from '../utils/useIsFocusVisible';
Expand Down Expand Up @@ -326,22 +327,27 @@ const Tooltip = React.forwardRef(function Tooltip(props, ref) {
}
};

const handleClose = (event) => {
clearTimeout(hystersisTimer);
hystersisTimer = setTimeout(() => {
hystersisOpen = false;
}, 800 + leaveDelay);
setOpenState(false);

if (onClose) {
onClose(event);
}
const handleClose = useEventCallback(
/**
* @param {React.SyntheticEvent | Event} event
*/
(event) => {
clearTimeout(hystersisTimer);
hystersisTimer = setTimeout(() => {
hystersisOpen = false;
}, 800 + leaveDelay);
setOpenState(false);

if (onClose) {
onClose(event);
}

clearTimeout(closeTimer.current);
closeTimer.current = setTimeout(() => {
ignoreNonTouchEvents.current = false;
}, theme.transitions.duration.shortest);
};
clearTimeout(closeTimer.current);
closeTimer.current = setTimeout(() => {
ignoreNonTouchEvents.current = false;
}, theme.transitions.duration.shortest);
},
);

const handleLeave = (forward = true) => (event) => {
const childrenProps = children.props;
Expand Down Expand Up @@ -402,6 +408,28 @@ const Tooltip = React.forwardRef(function Tooltip(props, ref) {
}, leaveTouchDelay);
};

React.useEffect(() => {
if (!open) {
return undefined;
}

/**
* @param {KeyboardEvent} nativeEvent
*/
function handleKeyDown(nativeEvent) {
Copy link
Member

@oliviertassinari oliviertassinari Aug 27, 2020

Choose a reason for hiding this comment

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

arrow function as in the other places (for nested scopes)?

Copy link
Member Author

Choose a reason for hiding this comment

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

(for nested scopes)

What does this mean?

Copy link
Member

Choose a reason for hiding this comment

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

I thought we were following named function for top-level scope and arrow function for nested levels, following pros and cons of https://stackoverflow.com/questions/34361379/are-arrow-functions-and-functions-equivalent-exchangeable

Suggested change
function handleKeyDown(nativeEvent) {
const handleKeyDown = (nativeEvent) =>

Copy link
Member Author

Choose a reason for hiding this comment

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

Does it matter? They end up as function anyway.

// IE 11, Edge (prior to using Bink?) use 'Esc'
if (nativeEvent.key === 'Escape' || nativeEvent.key === 'Esc') {
oliviertassinari marked this conversation as resolved.
Show resolved Hide resolved
handleClose(nativeEvent);
}
}

document.addEventListener('keydown', handleKeyDown);

return () => {
document.removeEventListener('keydown', handleKeyDown);
};
}, [handleClose, open]);

const handleUseRef = useForkRef(setChildNode, ref);
const handleFocusRef = useForkRef(focusVisibleRef, handleUseRef);
const handleRef = useForkRef(children.ref, handleFocusRef);
Expand Down
26 changes: 26 additions & 0 deletions packages/material-ui/src/Tooltip/Tooltip.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
act,
createClientRender,
fireEvent,
screen,
} from 'test/utils';
import { camelCase } from 'lodash/string';
import Tooltip, { testReset } from './Tooltip';
Expand Down Expand Up @@ -235,6 +236,31 @@ describe('<Tooltip />', () => {
clock.runAll();
});

it('is dismissable by pressing Escape', () => {
const transitionTimeout = 0;
render(
<Tooltip enterDelay={0} TransitionProps={{ timeout: transitionTimeout }} title="Movie quote">
<button autoFocus>Hello, Dave!</button>
</Tooltip>,
);

expect(screen.getByRole('tooltip')).not.toBeInaccessible();

act(() => {
fireEvent.keyDown(
// We don't care about the target. Any Escape should dismiss the tooltip
// eslint-disable-next-line material-ui/disallow-active-element-as-key-event-target
document.activeElement,
{ key: 'Escape' },
);
});
act(() => {
clock.tick(transitionTimeout);
});

expect(screen.queryByRole('tooltip')).to.equal(null);
});

describe('touch screen', () => {
it('should not respond to quick events', () => {
const { getByRole, queryByRole } = render(
Expand Down