Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: highlight keys when they're played via keyboard #133

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 90 additions & 8 deletions src/components/PianoKeyboard.tsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,129 @@
import React, { useEffect } from "react";
import { BlackPianoKey, WhitePianoKey, type Note } from "./PianoKeys";

interface PianoKeyboardProps {
playNote: (note: Note) => void;
}

const keyNoteMap: { [key: string]: Note } = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SURAJ-SHARMA27, there already is a keynote map. See

<KeyboardMap
keyMap={keyboardMap}
onKeyPress={(event: KeyboardEvent) => {
const note = keyboardMap[event.key];
if (note) {
playNote(note);
}
}}
/>

This is where the change in styling should happen. Also, for the colours, use the tailwind classes, e.g. bg-gray-300 from hover:bg-gray-300.

className="text-sm flex flex-col border border-black rounded-sm bg-color-white active:scale-95 hover:bg-gray-300 w-10 md:w-12 h-32 md:h-56"

a: "C4",
s: "D4",
d: "E4",
f: "F4",
g: "G4",
h: "A4",
j: "B4",
k: "C5",
w: "C#4",
e: "D#4",
t: "F#4",
y: "G#4",
u: "A#4",
};

export const PianoKeyboard = ({ playNote }: PianoKeyboardProps) => {
const handleKeyDown = (event: KeyboardEvent) => {
const note = keyNoteMap[event.key.toLowerCase()];
if (note) {
playNote(note);
highlightKey(note);
}
};

const highlightKey = (note: Note) => {
const keyElement = document.querySelector(`[data-note="${note}"]`);
if (keyElement) {
const isBlackKey = note.includes("#");
const highlightColor = isBlackKey ? "#A8A9AD" : "#D1D4DC";
keyElement.setAttribute("style", `background-color: ${highlightColor};`);
setTimeout(() => {
keyElement.removeAttribute("style");
}, 200);
}
};

useEffect(() => {
window.addEventListener("keydown", handleKeyDown);

return () => {
window.removeEventListener("keydown", handleKeyDown);
};
}, []);

return (
<>
<div className="flex gap-1 relative">
<WhitePianoKey note="C4" playNote={playNote} />
<WhitePianoKey note="D4" playNote={playNote} />
<WhitePianoKey note="E4" playNote={playNote} />
<WhitePianoKey note="F4" playNote={playNote} />
<WhitePianoKey note="G4" playNote={playNote} />
<WhitePianoKey note="A4" playNote={playNote} />
<WhitePianoKey note="B4" playNote={playNote} />
<WhitePianoKey note="C5" playNote={playNote} />
<WhitePianoKey
note="C4"
playNote={playNote}
highlightKey={highlightKey}
/>
<WhitePianoKey
note="D4"
playNote={playNote}
highlightKey={highlightKey}
/>
<WhitePianoKey
note="E4"
playNote={playNote}
highlightKey={highlightKey}
/>
<WhitePianoKey
note="F4"
playNote={playNote}
highlightKey={highlightKey}
/>
<WhitePianoKey
note="G4"
playNote={playNote}
highlightKey={highlightKey}
/>
<WhitePianoKey
note="A4"
playNote={playNote}
highlightKey={highlightKey}
/>
<WhitePianoKey
note="B4"
playNote={playNote}
highlightKey={highlightKey}
/>
<WhitePianoKey
note="C5"
playNote={playNote}
highlightKey={highlightKey}
/>
</div>
<div className="absolute top-4">
<BlackPianoKey
note="C#4"
playNote={playNote}
leftPosition="left-[25px] md:left-[29px]"
highlightKey={highlightKey}
/>
<BlackPianoKey
note="D#4"
playNote={playNote}
leftPosition="left-[70px] md:left-[81px]"
highlightKey={highlightKey}
/>
<BlackPianoKey
note="F#4"
playNote={playNote}
leftPosition="left-[157px] md:left-[185px]"
highlightKey={highlightKey}
/>
<BlackPianoKey
note="G#4"
playNote={playNote}
leftPosition="left-[202px] md:left-[237px]"
highlightKey={highlightKey}
/>
<BlackPianoKey
note="A#4"
playNote={playNote}
leftPosition="left-[246px] md:left-[290px]"
highlightKey={highlightKey}
/>
</div>
</>
Expand Down
26 changes: 15 additions & 11 deletions src/components/PianoKeys.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,34 @@ export type Note =
export type NoteMapKey<T extends Note> = T extends `${infer L}#${infer R}`
? `${Lowercase<L>}-sharp-${Lowercase<R>}`
: Lowercase<T>;

interface PianoKeyProps {
note: string;
playNote: (note: Note) => void;
}

type LeftPosition = `left-[${number}px] md:left-[${number}px]`;

interface BlackPianoKeyProps extends PianoKeyProps {
leftPosition: LeftPosition;
}
interface PianoKeyProps {
note: string;
playNote: (note: Note) => void;
highlightKey: (note: Note) => void;
}

export const WhitePianoKey = ({ note, playNote }: PianoKeyProps) => {
export const WhitePianoKey = ({
note,
playNote,
highlightKey,
}: PianoKeyProps) => {
return (
<button
className="text-sm flex flex-col border border-black rounded-sm bg-color-white active:scale-95 hover:bg-gray-300 w-10 md:w-12 h-32 md:h-56"
className="text-sm flex flex-col border border-black rounded-sm bg-white active:scale-95 hover:bg-gray-300 w-10 md:w-12 h-32 md:h-56 focus:outline-none focus:ring-2 focus:ring-blue-500"
data-note={note}
onClick={(event) => {
const { note } = event.currentTarget.dataset;

if (!note) {
throw new Error("No note found on key");
}

playNote(note as Note);
highlightKey(note as Note);
}}
>
<span className="sr-only">{`piano key ${note}`}</span>
Expand All @@ -53,19 +56,20 @@ export const BlackPianoKey = ({
note,
leftPosition,
playNote,
}: BlackPianoKeyProps) => {
highlightKey,
}: BlackPianoKeyProps & { highlightKey: (note: Note) => void }) => {
return (
<button
className={`text-xs md:text-sm flex flex-col border border-black rounded-sm bg-black active:scale-95 hover:bg-gray-300 w-8 md:w-10 h-20 md:p-1 md:h-36 absolute ${leftPosition}`}
data-note={note}
onClick={(event) => {
const { note } = event.currentTarget.dataset;

if (!note) {
throw new Error("No note found on key");
}

playNote(note as Note);
highlightKey(note as Note);
}}
>
<span className="sr-only">{`piano key ${note}`}</span>
Expand Down