Created
September 30, 2020 19:32
-
-
Save aeaston/1058b47bf813a2b391d94943beb91c8e to your computer and use it in GitHub Desktop.
Accessible Div Blog - ExpandedClickableArea
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 ExpandedClickableArea(props) { | |
const clickableElemTypes = ['a', 'button', 'input']; | |
const refExpandedArea = useRef(); | |
function handleClick(e) { | |
const clickableElems = [ | |
...refExpandedArea.current.querySelectorAll('[data-expand-click-area]') | |
]; | |
if (clickableElems.length !== 1) { | |
throw new Error( | |
`Expected one clickable element but found ${clickableElems.length}` | |
); | |
} | |
const clickableElem = clickableElems[0]; | |
const targetIsClickable = clickableElemTypes.includes(e.target.tagName.toLowerCase()); | |
if (clickableElem !== e.target && !targetIsClickable) { | |
clickableElem.click(); | |
} | |
} | |
return ( | |
<div | |
ref={refExpandedArea} | |
className={`${props.className} ${styles.expandedClickableArea}`} | |
tabIndex=”0” | |
onClick={handleClick} | |
> | |
{props.children} | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment