fix(a11y) make ContextMenuItem actionable via keyboard by default

if we pass an onClick event handler but no keyboard event handlers,
default the onKeyPress event to trigger the thing on click when typing
enter or space.

this makes most ContextMenuItem now actually actionable via keyboard,
since most of them dont have any keyboard event handlers attached, only
click event handlers.
This commit is contained in:
Emmanuel Pelletier 2023-02-28 18:19:47 +01:00
parent f727b9295f
commit e071a11f0d
1 changed files with 13 additions and 1 deletions

View File

@ -1,4 +1,4 @@
import React, { ReactNode } from 'react';
import React, { ReactNode, useCallback } from 'react';
import { useSelector } from 'react-redux';
import { makeStyles } from 'tss-react/mui';
@ -179,6 +179,18 @@ const ContextMenuItem = ({
const { classes: styles, cx } = useStyles();
const _overflowDrawer: boolean = useSelector(showOverflowDrawer);
const onKeyPressHandler = useCallback(e => {
// only trigger the fallback behavior (onClick) if we dont have any explicit keyboard event handler
if (onClick && !onKeyPress && !onKeyDown && (e.key === 'Enter' || e.key === ' ')) {
e.preventDefault();
onClick(e);
}
if (onKeyPress) {
onKeyPress(e);
}
}, [ onClick, onKeyPress, onKeyDown ]);
return (
<div
aria-controls = { controls }