npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@diatonic/piano

v0.4.0

Published

Interactive SVG piano for React

Readme

Diatonic Piano

Lint, Test, Type Check, Build npm

Interactive SVG piano as a React component.

Reference

Installation

npm install @diatonic/piano

Then import the component:

import { Piano } from "@diatonic/piano";

function App() {
  return <Piano />;
}

Static Piano

By default, the piano will expand to fill its container. If you place the <Piano> tag inside a div with fixed width, you'll get a simple, static piano:

<Piano />

Simple piano

The following props can be set to modify the SVG representation static piano:

| Prop | Description | Default | | ------------------- | ------------------------------------------------------------------------------------------------------------------------- | ----------------- | | octaves | Number of octaves | 2 | | width | Width of the <svg> element | '100%' | | height | Height of the <svg> element | '100%' | | preserveAspectRatio | Attribute for <svg> element (details) | 'xMinYMin meet' |

Pressed and Highlighted keys

Two props can be used to set a key as pressed or highlighted. There are no real differences between the two, but it can be used to separate permanently pressed keys from the key currently being hovered, or to mark a chord and a scale at the same time. Highlighted keys are lighter in the default style. The two props are:

| Prop | Description | Default | | ----------- | ------------------------- | ------- | | pressed | Keys will be pressed | [] | | highlighted | Keys will be highlighted | [] |

As an example, you can press F4, A4, and C#5 to highlight an F augmented chord:

<Piano pressed={["F4", "A4", "C#5"]} />

Pressed keys

Internally, the component uses kamasi to figure out which notes to highlight. The component also accepts kamasi objects as input. So if your cool friends want to improvise over a D minor blues scale, you're just a single line from knowing which keys to use:

import { scale } from "kamasi";

<Piano highlighted={scale("D blues minor")} />;

Highlighted scale

Styling

Default

The piano includes built-in default colors via SVG presentation attributes, so it works out-of-the-box with sensible default colors:

import { Piano } from "@diatonic/piano";

<Piano />

CSS Variables

Import the default stylesheet to override variables:

import "@diatonic/piano/styles.css";
:root {
  /* White keys */
  --piano-key-diatonic-fill: #f0f0f0;
  --piano-key-diatonic-pressed-fill: #4caf50;
  --piano-key-diatonic-highlighted-fill: #81c784;
  --piano-key-diatonic-stroke: #333;

  /* Black keys */
  --piano-key-chromatic-fill: #333;
  --piano-key-chromatic-pressed-fill: #4caf50;
  --piano-key-chromatic-highlighted-fill: #81c784;
  --piano-key-chromatic-stroke: #000;

  /* Global */
  --piano-key-stroke-width: 4;
}

All available CSS variables:

| Variable | Description | Default | | ------------------------------------------ | --------------------------------------- | ------------------ | | --piano-key-diatonic-fill | White key default color | #f7f5f0 | | --piano-key-diatonic-pressed-fill | White key pressed color | #e07858 | | --piano-key-diatonic-highlighted-fill | White key highlighted color | #eca088 | | --piano-key-chromatic-fill | Black key default color | #4a423c | | --piano-key-chromatic-pressed-fill | Black key pressed color | #c04838 | | --piano-key-chromatic-highlighted-fill | Black key highlighted color | #b86850 | | --piano-key-diatonic-stroke | White key border color | #c0b8b0 | | --piano-key-diatonic-pressed-stroke | White key pressed border | #b8624c | | --piano-key-diatonic-highlighted-stroke | White key highlighted border | #b8624c | | --piano-key-chromatic-stroke | Black key border color | #4a423c | | --piano-key-chromatic-pressed-stroke | Black key pressed border | #4a423c | | --piano-key-chromatic-highlighted-stroke | Black key highlighted border | #4a423c | | --piano-key-stroke-width | Border width for all keys | 2 |

Advanced CSS Targeting

For maximum control, target CSS classes and data attributes directly:

Classes:

| Class | Description | | ---------------------------------- | ------------------------------------------------- | | .diatonic-piano | Top <svg> component | | .diatonic-piano-octave-<n> | The <g> tag grouping all keys within one octave | | .diatonic-piano-key | The <path> tag for all keys | | .diatonic-piano-key-<pitchclass> | All keys of this pitch class (e.g., C, Cs) | | .diatonic-piano-key-<pitch> | Specific key (e.g., C4, Cs4) |

Data attributes:

| Attribute | Values | Description | | ------------------ | ----------------------- | ------------------------------ | | data-key-type | diatonic, chromatic | White or black key | | data-pressed | true, false | Whether the key is pressed | | data-highlighted | true, false | Whether the key is highlighted |

Example: Rainbow keys

/* Using a parent class ensures proper specificity */
#rainbow-piano {
  --piano-key-stroke-width: 2;
}
#rainbow-piano .diatonic-piano-key-G4 {
  fill: #f898a4;
}
#rainbow-piano .diatonic-piano-key-A4 {
  fill: #fcda9c;
}
#rainbow-piano .diatonic-piano-key-B4 {
  fill: #f7faa1;
}
#rainbow-piano .diatonic-piano-key-C5 {
  fill: #b4f6a4;
}
#rainbow-piano .diatonic-piano-key-D5 {
  fill: #9be0f1;
}
#rainbow-piano .diatonic-piano-key-E5 {
  fill: #a2aceb;
}
<div id="rainbow-piano">
  <Piano />
</div>

CSS styled piano

Interactivity

Enable direct interaction with piano keys using the interactive prop and event handlers:

| Prop | Description | | ---------------- | -------------------------------------------------------------------------------- | | interactive | Enables clicking, hovering, and keyboard navigation of keys | | onPress | Called with (note, event) when a key is clicked or activated with Enter | | onHighlightStart | Called with (note, event) when pointer enters a key or the key receives focus | | onHighlightEnd | Called with (note, event) when pointer leaves a key or the key loses focus |

Each event handler receives two parameters:

  • note - The note string (e.g., 'C4')
  • event - The original DOM event (MouseEvent, KeyboardEvent, PointerEvent, or FocusEvent)

The component is fully stateless and relies on the parent to update pressed and highlighted props. This gives you complete control over the piano's behavior and enables synchronization with other components.

Using React hooks, you only need a few lines of code. The following is the complete App.js file showing two pianos where the second mirrors the first, transposed a perfect fifth:

import { useState } from "react";
import { Piano } from "@diatonic/piano";
import { NoteList } from "kamasi";

function App() {
  const [pressed, setPressed] = useState(new NoteList());
  const [highlighted, setHighlighted] = useState(new NoteList());

  return (
    <div style={{ width: "300px" }}>
      <Piano
        interactive={true}
        pressed={pressed}
        highlighted={highlighted}
        onPress={(n) => setPressed((note) => note.toggle(n))}
        onHighlightStart={(n) => setHighlighted(new NoteList([n]))}
        onHighlightEnd={() => setHighlighted(new NoteList())}
      />
      <Piano
        pressed={pressed.transpose("P5")}
        highlighted={highlighted.transpose("P5")}
      />
    </div>
  );
}

export default App;

Interactive pianos

See kamasi's documentation for more manipulation you can do, or check the Diatonic web app for inspiration.

Accessibility

The component automatically adapts its ARIA roles and labels based on usage:

  • Display mode (default): role="img" with aria-label="Piano - Pressed keys: ..." or "Piano - No pressed keys"
  • Interactive mode (interactive or keyboardShortcuts): role="group" containing interactive buttons

The aria-label always includes the currently pressed keys for screen reader users, regardless of interactivity mode.

The component offers two complementary modes of keyboard access:

| Prop | Description | Default | | ----------------- | -------------------------------------------------------------- | ------- | | interactive | Enables keyboard navigation with <Tab> and <Enter> | false | | keyboardShortcuts | Enables QWERTY keyboard shortcuts to play notes | false |

Direct key navigation (interactive={true}): Users can traverse through keys in chromatic order (left to right) using Tab, and activate a key with Enter. This works for all visible keys on the piano. The onPress event fires whether the key is clicked or activated with Enter, and onHighlightStart/onHighlightEnd work for both pointer hover and keyboard focus.

QWERTY shortcuts (keyboardShortcuts={true}): Turns the keyboard into a virtual piano. Keys Q-U play octave 3, A-J play octave 4, and Z-M play octave 5 (white keys). Hold Shift to play the black key (sharp) for any key. The onPress event fires with the played note when a keyboard shortcut is used.

Both modes can be enabled simultaneously, and both trigger the same onPress event handler.