mirror of
https://github.com/remvze/moodist.git
synced 2025-12-17 08:54:13 +00:00
Merge pull request #26 from SuperMeepBoy/improve-accessibility-2
Allow using spacebar or enter to trigger buttons
This commit is contained in:
commit
ab9d47befb
3 changed files with 31 additions and 8 deletions
|
|
@ -7,6 +7,8 @@ import { fade } from '@/lib/motion';
|
||||||
|
|
||||||
import styles from './favorite.module.css';
|
import styles from './favorite.module.css';
|
||||||
|
|
||||||
|
import { useKeyboardButton } from '@/hooks/useKeyboardButton';
|
||||||
|
|
||||||
interface FavoriteProps {
|
interface FavoriteProps {
|
||||||
id: string;
|
id: string;
|
||||||
}
|
}
|
||||||
|
|
@ -17,11 +19,16 @@ export function Favorite({ id }: FavoriteProps) {
|
||||||
|
|
||||||
const variants = fade();
|
const variants = fade();
|
||||||
|
|
||||||
|
const handleKeyDown = useKeyboardButton(() => {
|
||||||
|
toggleFavorite(id);
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AnimatePresence initial={false} mode="wait">
|
<AnimatePresence initial={false} mode="wait">
|
||||||
<button
|
<button
|
||||||
aria-label="Add Sound to Favorites"
|
aria-label="Add Sound to Favorites"
|
||||||
className={cn(styles.favoriteButton, isFavorite && styles.isFavorite)}
|
className={cn(styles.favoriteButton, isFavorite && styles.isFavorite)}
|
||||||
|
onKeyDown={handleKeyDown}
|
||||||
onClick={e => {
|
onClick={e => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
toggleFavorite(id);
|
toggleFavorite(id);
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ import styles from './sound.module.css';
|
||||||
|
|
||||||
import type { Sound } from '@/data/types';
|
import type { Sound } from '@/data/types';
|
||||||
|
|
||||||
|
import { useKeyboardButton } from '@/hooks/useKeyboardButton';
|
||||||
|
|
||||||
interface SoundProps extends Sound {
|
interface SoundProps extends Sound {
|
||||||
functional: boolean;
|
functional: boolean;
|
||||||
hidden: boolean;
|
hidden: boolean;
|
||||||
|
|
@ -73,14 +75,9 @@ export function Sound({
|
||||||
toggle();
|
toggle();
|
||||||
}, [toggle]);
|
}, [toggle]);
|
||||||
|
|
||||||
const handleKeyDown = useCallback(
|
const handleKeyDown = useKeyboardButton(() => {
|
||||||
(event: React.KeyboardEvent<HTMLDivElement>) => {
|
toggle();
|
||||||
if (event.key === 'Enter') {
|
});
|
||||||
toggle();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[toggle],
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
|
|
|
||||||
19
src/hooks/useKeyboardButton.ts
Normal file
19
src/hooks/useKeyboardButton.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
import { useCallback } from 'react';
|
||||||
|
import type { KeyboardEvent } from 'react';
|
||||||
|
|
||||||
|
export const useKeyboardButton = (
|
||||||
|
actionCallback: () => void,
|
||||||
|
): ((event: KeyboardEvent<HTMLElement>) => void) => {
|
||||||
|
const handleKeyDown = useCallback(
|
||||||
|
(event: KeyboardEvent<HTMLElement>) => {
|
||||||
|
if (event.key === 'Enter' || event.key === ' ') {
|
||||||
|
event.preventDefault();
|
||||||
|
actionCallback();
|
||||||
|
event.stopPropagation();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[actionCallback],
|
||||||
|
);
|
||||||
|
|
||||||
|
return handleKeyDown;
|
||||||
|
};
|
||||||
Loading…
Add table
Reference in a new issue