From 60cc2e9369aff3a374458cf1c3234eec8cd0530e Mon Sep 17 00:00:00 2001 From: Jef Roelandt Date: Tue, 23 Apr 2024 13:24:41 +0200 Subject: [PATCH] feat: allow using spacebar or enter to trigger buttons It is a good practice for accessibility. Cf https://webaim.org/techniques/keyboard/ or any other resource on the Internet talking about buttons accessibiltiy. --- src/components/sound/favorite/favorite.tsx | 7 +++++++ src/components/sound/sound.tsx | 13 +++++-------- src/hooks/useKeyboardButton.ts | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 src/hooks/useKeyboardButton.ts diff --git a/src/components/sound/favorite/favorite.tsx b/src/components/sound/favorite/favorite.tsx index 64f122b..80c364f 100644 --- a/src/components/sound/favorite/favorite.tsx +++ b/src/components/sound/favorite/favorite.tsx @@ -7,6 +7,8 @@ import { fade } from '@/lib/motion'; import styles from './favorite.module.css'; +import { useKeyboardButton } from '@/hooks/useKeyboardButton'; + interface FavoriteProps { id: string; } @@ -17,11 +19,16 @@ export function Favorite({ id }: FavoriteProps) { const variants = fade(); + const handleKeyDown = useKeyboardButton(() => { + toggleFavorite(id); + }); + return (