From cb9c10c71b631fecfa11148d933b01b4ae7b143a Mon Sep 17 00:00:00 2001 From: Nick Taylor Date: Sat, 14 Sep 2024 13:54:00 -0400 Subject: [PATCH] cleaned up piano events --- src/components/PianoKeyboard.tsx | 63 ++++++++++++++------------------ src/components/PianoKeys.tsx | 27 +------------- 2 files changed, 30 insertions(+), 60 deletions(-) diff --git a/src/components/PianoKeyboard.tsx b/src/components/PianoKeyboard.tsx index 867695b..af1f0e6 100644 --- a/src/components/PianoKeyboard.tsx +++ b/src/components/PianoKeyboard.tsx @@ -5,45 +5,38 @@ interface PianoKeyboardProps { } export const PianoKeyboard = ({ playNote }: PianoKeyboardProps) => { + const pressPianoKey = (event: React.KeyboardEvent | React.MouseEvent) => { + const { note } = (event.target as HTMLElement).dataset; + + if (!note) { + throw new Error("No note found on key"); + } + + playNote(note as Note); + }; + return ( - <> + // Disable eslint rule for static element interactions here because the div is just handling + // event delegation for the piano keys. The piano keys themselves are buttons. + // eslint-disable-next-line jsx-a11y/click-events-have-key-events,jsx-a11y/no-static-element-interactions +
- - - - - - - - + + + + + + + +
- - - - - + + + + +
- +
); }; diff --git a/src/components/PianoKeys.tsx b/src/components/PianoKeys.tsx index 437da5a..c187512 100644 --- a/src/components/PianoKeys.tsx +++ b/src/components/PianoKeys.tsx @@ -19,7 +19,6 @@ export type NoteMapKey = T extends `${infer L}#${infer R}` interface PianoKeyProps { note: string; - playNote: (note: Note) => void; } type LeftPosition = `left-[${number}px] md:left-[${number}px]`; @@ -28,20 +27,11 @@ interface BlackPianoKeyProps extends PianoKeyProps { leftPosition: LeftPosition; } -export const WhitePianoKey = ({ note, playNote }: PianoKeyProps) => { +export const WhitePianoKey = ({ note }: PianoKeyProps) => { return (