Created
September 30, 2020 19:27
-
-
Save aeaston/0374710529bb4db194b15c9f60e3164e to your computer and use it in GitHub Desktop.
Accessible Div Blog - Clickable Container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ClickableContainer(props) { | |
// ideally these are stored in a separate key mapping utility file | |
const enterKey = 13; | |
const spaceKey = 32; | |
const { children, ariaLabel, ...otherProps } = props; | |
function handleKeyDown(e) { | |
if (e.keyCode === enterKey || e.keyCode === spaceKey) { | |
e.preventDefault(); | |
props.onClick(e); | |
} | |
} | |
return ( | |
<div | |
onKeyDown={handleKeyDown} | |
role="button" | |
tabIndex="0" | |
aria-label={ariaLabel} | |
{...otherProps} | |
> | |
{children} | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment