Created
October 2, 2020 20:41
-
-
Save dzucconi/1f13f32273b9b240229391fa21b646c1 to your computer and use it in GitHub Desktop.
onA11yClick
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
type Handler<T> = ( | |
event: React.KeyboardEvent<T> | React.MouseEvent<T, MouseEvent> | |
) => void | |
export const onA11yClick = <T>(handler: Handler<T>) => { | |
return { | |
tabIndex: 0, | |
role: "button", | |
onClick: (event: React.MouseEvent<T, MouseEvent>) => handler(event), | |
onKeyPress: (event: React.KeyboardEvent<T>) => { | |
if (event.key === "Enter" || event.key === " ") { | |
handler(event) | |
} | |
}, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment