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 🙏

© 2024 – Pkg Stats / Ryan Hefner

klavier

v2.0.1

Published

A lightweight, customizable, interactive piano library built with React.

Downloads

407

Readme

Features

Getting started

npm install klavier
import React from 'React'
import { Klavier } from 'klavier';

const App = () => {
  return <Klavier /> // render a default 88-key keyboard
}

Props

| Prop | Default value | Description | |-----------------------|------------------|-------------------------------------------------------------------------------------------------------------------------| | keyRange | [21, 108] | The lowest and the highest notes of the piano in MIDI numbers (0-127). | | defaultActiveKeys | [] | Keys that are pressed by default. Subsequent updates are ignored. Cleared when the user begins playing. | | activeKeys | | Currently pressed keys. Puts component into controlled mode; keys notes must be managed externally via callbacks. | | onKeyPress | | Fired when a key is pressed. | | onKeyRelease | | Fired when a key is released. | | onChange | | Fired when active keys are changed via user input. | | interactive | true | Enable interaction with the piano via computer keyboard, mouse, or touch. | | keymap | DEFAULT_KEYMAP | Mapping of computer keys to MIDI note numbers, e.g. [{ key: 'q', midiNumber: 60 }, ..., { key: 'i', midiNumber: 72 }] | | width | "auto" | Width of the piano. Accepts any valid CSS value. When unspecified, the piano fills it's container and is responsive. | | height | "auto" | Height of the piano. Accepts any valid CSS value. | | blackKeyHeight | "67.5%" | Height of the black key. Allows tweaking the appearance of black keys in relation to white keys. | | components | | Allows replacing default components for black and white keys and adding a custom label to each key. |

Overview

Klavier is primarily a visual keyboard library. While it can be easily connected to a sound engine for playback, its primary purpose is displaying the current state of the keyboard at a given moment, and notifying the consumer about changes to the states when keys are played or released.

Interactivity

Klavier is interactive by default. It supports user input via computer keyboard, mouse, and touch. Interactivity can be completely or partially disabled.

Disabling interactivity

<Klavier interactive={false} />

Selective interactivity

interactive prop can be provided with an object to selectively enable/disable input, e.g. only allow input from mouse:

<Klavier interactive={{ mouse: true }} />

Note: When using object notation, omitted properties are set to false.

Customizing the size

By default, Klavier is responsive takes up the full width of its parent container. This can be changed by using a combination of width and height props. Those attributes accept any valid CSS values.

Fixed width:

<Klavier width="50vw" />

Fixed width and height:

<Klavier width="800px" height="80px" />

Reducing the height in responsive mode

Because height should be relative to the width of the keyboard, specifying a fixed height without a fixed width is not recommended, as it will the instrument to widen/shrink based on the viewport size. Instead, you can utilize the CSS aspect-ratio on the parent:

<div style={{ aspectRatio: '8 / 1' }}>
  <Klavier width="100%" height="100%"/>
</div>

Customizing black key height

The default height of the black keys is based on standard piano actions. To customize its size, use the blackKeyHeight prop. For example, to set the black keys at half the height of the white keys:

<Klavier blackKeyHeight="50%"/>

Appearance

Styling keys

Klavier intentionally ships with absolutely minimal styling. The appearance of the keyboard can be customized by specifying custom components for the black and white keys. This enables changing the look of the keyboard with the approach of your choice.

const CustomBlackKey = ({ active, note }) => { return <div /> }
const CustomWhiteKey = ({ active, note }) => { return <div /> }

<Klavier components={{ blackKey: CustomBlackKey, whiteKey: CustomWhiteKey }} />

Important: avoid defining components directly in the prop object, as it can cause performance issues.

Custom labels

components can also be used to render custom labels on keys, whether it's the octave number, pitch name, or a keyboard shortcut. As with keys, it requires a custom React component. The component will be injected with necessary note information.

const CustomLabel = ({ active, note, midiC0, keyboardShortcut }) => {
  return // Your custom label
}

<Klavier components={{ label: CustomLabel }}