Skip to content

Instantly share code, notes, and snippets.

@dzucconi
Created October 2, 2020 20:41
Show Gist options
  • Save dzucconi/1f13f32273b9b240229391fa21b646c1 to your computer and use it in GitHub Desktop.
Save dzucconi/1f13f32273b9b240229391fa21b646c1 to your computer and use it in GitHub Desktop.
onA11yClick
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