feat: add keyboard shortcut for play button

This commit is contained in:
MAZE 2024-04-26 14:41:57 +03:30
parent ebb35deaf9
commit d3a2a12e1f
2 changed files with 17 additions and 4 deletions

View file

@ -1,4 +1,4 @@
import { useEffect } from 'react'; import { useCallback, useEffect } from 'react';
import { BiPause, BiPlay } from 'react-icons/bi/index'; import { BiPause, BiPlay } from 'react-icons/bi/index';
import { useSoundStore } from '@/store'; import { useSoundStore } from '@/store';
@ -15,21 +15,33 @@ export function PlayButton() {
const showSnackbar = useSnackbar(); const showSnackbar = useSnackbar();
const handleClick = () => { const handleToggle = useCallback(() => {
if (noSelected) return showSnackbar('Please first select a sound to play.'); if (noSelected) return showSnackbar('Please first select a sound to play.');
toggle(); toggle();
}; }, [showSnackbar, toggle, noSelected]);
useEffect(() => { useEffect(() => {
if (isPlaying && noSelected) pause(); if (isPlaying && noSelected) pause();
}, [isPlaying, pause, noSelected]); }, [isPlaying, pause, noSelected]);
useEffect(() => {
const listener = (e: KeyboardEvent) => {
if (e.shiftKey && e.key === ' ') {
handleToggle();
}
};
document.addEventListener('keydown', listener);
return () => document.removeEventListener('keydown', listener);
}, [handleToggle]);
return ( return (
<button <button
aria-disabled={noSelected} aria-disabled={noSelected}
className={cn(styles.playButton, noSelected && styles.disabled)} className={cn(styles.playButton, noSelected && styles.disabled)}
onClick={handleClick} onClick={handleToggle}
> >
{isPlaying ? ( {isPlaying ? (
<> <>

View file

@ -71,6 +71,7 @@ export function Notepad({ onClose, show }: NotepadProps) {
ref={textareaRef} ref={textareaRef}
value={note} value={note}
onChange={e => write(e.target.value)} onChange={e => write(e.target.value)}
onKeyDown={e => e.stopPropagation()}
/> />
<p className={styles.counter}> <p className={styles.counter}>