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

making piano notes highlightable #12

Merged
merged 1 commit into from
Dec 31, 2024
Merged
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
5 changes: 5 additions & 0 deletions components/ChordBank/ChordDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react";
import { Chord } from "@/types";
import { NOTES } from "@/constants";
import Grid from "@/components/Grid";
import PianoKeyboard from "@/components/PianoKeyboard";

interface ChordDetailProps {
chord: Chord;
Expand All @@ -24,6 +25,10 @@ const ChordDetail: React.FC<ChordDetailProps> = ({ chord }) => {
<div key={index}>
<h2 className="font-mono">{note.name}</h2>
<Grid pattern={chord.pattern} rootNote={parseInt(note.alias)} />
<PianoKeyboard
pattern={chord.pattern}
rootNote={parseInt(note.alias)}
/>
</div>
))}
</div>
Expand Down
36 changes: 32 additions & 4 deletions components/PianoKeyboard.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import React from "react";
import { NOTES } from "@/constants";
import { Note } from "@/types";
import { Note, Interval } from "@/types";
import { getNoteHighlight } from "@/utils/NoteHighlighter";

interface PianoKeyProps {
note: Note;
octave: number;
pattern?: Interval;
rootNote?: number;
}

const PianoKey: React.FC<PianoKeyProps> = ({ note, octave }) => {
const PianoKey: React.FC<PianoKeyProps> = ({
note,
octave,
pattern,
rootNote,
}) => {
const isBlack = !note.natural;

// Get highlight styling if pattern and rootNote are provided
const highlight =
pattern && rootNote !== undefined
? getNoteHighlight(note, pattern, rootNote)
: { opacity: 1 };

return (
<div
className={`
Expand All @@ -22,14 +36,22 @@ const PianoKey: React.FC<PianoKeyProps> = ({ note, octave }) => {
? `linear-gradient(to bottom, black 0%, black 30%, ${note.color} 30%, ${note.color} 100%)`
: note.color,
color: note.textColor,
opacity: highlight.opacity,
filter: highlight.filter,
transition: "opacity 0.2s, filter 0.2s", // Optional: smooth transition for highlights
}}
>
{note.name}
</div>
);
};

const PianoKeyboard: React.FC = () => {
interface PianoKeyboardProps {
pattern?: Interval;
rootNote?: number;
}

const PianoKeyboard: React.FC<PianoKeyboardProps> = ({ pattern, rootNote }) => {
const createKeys = () => {
const keys = [];
const octaves = 2;
Expand All @@ -52,7 +74,13 @@ const PianoKeyboard: React.FC = () => {
<div className="w-full overflow-x-auto p-4">
<div className="flex relative">
{keys.map((key, index) => (
<PianoKey key={index} note={key.note} octave={key.octave} />
<PianoKey
key={index}
note={key.note}
octave={key.octave}
pattern={pattern}
rootNote={rootNote}
/>
))}
</div>
</div>
Expand Down