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

react-piano-component

v0.1.4

Published

A simple and customizable react piano component

Readme

react-piano-component

A simple and customizable react piano component
Demo: https://lillydinhle.github.io/react-piano-component/

Installation

npm install --save react-piano-component

Usage

import Piano from 'react-piano-component';

<Piano
  // (Required) Specify a note range using `startNote` and `endNote`.
  // The following note range creates a piano with:
  // C4, C#4, D4, D#4, E4, F4, F#4, G4, G#4, A4, A#4, B4, C5
  startNote="C4"
  endNote="C5"

  // (Optional) Map keyboard keys to notes using `keyboardMap`.
  // The following `keyboardMap` maps the keyboard key `Q` to the note `C4`,
  // so whenever `Q` is pressed, `C4` plays.
  keyboardMap={{
    Q: 'C4',
  }}

  // (Optional) Customize your audio using `renderAudio`!
  // `renderAudio(props)` is called whenever notes start or stop playing.
  renderAudio={
    ({
      notes // (Array) An array of the currently playing notes
    }) => {
      /* Play the given notes and render the audio (or return null) */
    }
  }

  // (Required) Customize your piano keys using `renderPianoKey`!
  // `renderPianoKey(props)` is called once per note with the following props:
  renderPianoKey={
    ({
      note,               // (String) The note corresponding to the key
      isNoteAccidental,   // (Boolean) Whether the note is accidental (C#, D#, F#, G#, or A#)
      isNotePlaying,      // (Boolean) Whether the note is currently playing

      startPlayingNote,   // (Function) A function that starts playing the note
      stopPlayingNote,    // (Function) A function that stops playing the note

      keyboardShortcuts,  // (Array) Keyboard keys mapped to the note, defined by `keyboardMap`.
    }) => {
      /* Return a styled piano key */
    }
  }
/>

Custom Audio

react-piano-component plays audio out-of-the-box, but it supports custom audio too! To customize your audio, define a component that plays the given notes and pass that to renderAudio.

This is an example using custom audio with ToneJS: (Special thanks to @giacomorebonato!)

import React, { Component } from 'react';
import { isEqual } from 'lodash';
import Tone from 'tone';

class InstrumentAudio extends Component {
  constructor(props) {
    super(props);
    this.synth = new Tone.PolySynth(4, Tone.Synth).toMaster();
  }

  componentDidMount() {
    this.startPlayingNotes(this.props.notes);
  }

  componentDidUpdate(prevProps) {
    const notes = this.props.notes;
    const prevNotes = prevProps.notes;

    if (!isEqual(notes, prevNotes)) {
      const startedNotes = getStartedNotes(notes, prevNotes);
      this.startPlayingNotes(startedNotes);

      const stoppedNotes = getStoppedNotes(notes, prevNotes);
      this.stopPlayingNotes(stoppedNotes);
    }
  }

  startPlayingNotes(startedNotes) {
    this.synth.triggerAttack(startedNotes);
  }

  stopPlayingNotes(stoppedNotes) {
    this.synth.triggerRelease(stoppedNotes);
  }

  render() {
    return null;
  }
}

And passed like this to the Piano component (with all the rest of the props).

<Piano {...props} playAudio={InstrumentAudio} />

image