Plays or shows notes, for a virtual instrument, a MIDI visualizer or a chord reference. The registry component is written on the piano primitive, which installs with it, and draws white and black keys for any MIDI range.
Overview
- Range:
minPitchandmaxPitchare MIDI pitches (default48and72, C3 to C5), orpitcheslists the exact keys to show. Key colors and layout follow the chromatic pattern. - Events:
onNoteOn(pitch, eventDetails)andonNoteOff(pitch, eventDetails)report MIDI pitches, ready for an audio engine.eventDetails.reasonsays how the note started ("press","glide"or"keyboard"), andeventDetails.cancel()inonNoteOnkeeps the key silent. - Pointer: press and hold a key to hold its note. Drag across keys for a glissando: each key plays as the pointer crosses it. Each touch holds its own key, so chords work on a touch screen.
- External notes:
activeNoteslights keys from outside, such as a MIDI input or a sequencer's current chord, without callingonNoteOnoronNoteOff. - Accessibility: each key is a button named after its pitch ("C4"), behind one tab stop. The arrow keys move between keys, and Space or Enter holds the focused key until it is released.
- Layout: the primitive can also lay keys out vertically (
orientation="vertical", lowest key at the bottom) or in equal widths (layout="even"). The piano roll's keyboard uses this.
Installation
The primitives are built on the UI SDK kernel, in beta. Nothing of ours comes from npm: the primitive and its adapter are files you copy into your project and own, and the only packages are the kernel's. The commands name its version, @ui-sdk/*@0.1.0-beta.14.
The styled component, with sizes and themed keys, is one command:
The styled component, with size variants and theme styles:
Copy to Clipboardpnpm dlx shadcn@latest add @audio-ui/piano
Or the primitive alone, to style yourself:
Copy to Clipboardpnpm dlx shadcn@latest add @audio-ui/piano-react
It writes the primitive, its adapter and primitive.ts into components/audio-ui/ and installs the kernel's packages and what each file imports. The optional styles are one more command:
Copy to Clipboardpnpm dlx shadcn@latest add @audio-ui/primitives-css
Usage
Copy to Clipboardimport { Piano } from "@/components/audio-ui/piano";
Note events:
Copy to Clipboard<Piano maxPitch={72} minPitch={48} onNoteOff={(pitch) => synth.release(pitch)} onNoteOn={(pitch) => synth.trigger(pitch)} />
External notes (MIDI input, a sequencer's current chord):
Copy to Clipboard<Piano activeNotes={midiInputNotes} maxPitch={72} minPitch={48} />
A MIDI keyboard: useMidiInput listens to the page's MIDI input. Browsers ask for permission, so connect it from a click: with requestAccess, or the ready-made button, npx shadcn@latest add @audio-ui/midi-connect-control.
Copy to Clipboardimport { useMidiInput } from "@/hooks/use-midi-input"; const midi = useMidiInput({ onNoteOff: (pitch) => synth.release(pitch), onNoteOn: (pitch, velocity) => synth.trigger(pitch, velocity), }); <button onClick={midi.requestAccess} type="button">Connect MIDI</button> <Piano activeNotes={midi.activeNotes} maxPitch={72} minPitch={48} />
Everything useMidiInput gives is in Input.
The primitive alone, part by part:
A Root is the keyboard, and a Key is one key, a <button> placed along it for its pitch and named by its note, such as "C4". A Label inside a Key writes that name for you to style. You write a Key for each pitch: the Root's content can read the state, whose pitches lists them lowest first, so the keys follow minPitch, maxPitch and pitches. That is a function in React, the default slot in Vue and a snippet in Svelte.
Give the Root its size. The keys are placed with percentages, so a Root with no height or width has nothing to place them in.
Copy to Clipboardimport { Piano } from "@/components/audio-ui/piano/react"; export function Keys({ on, off }: { on: (pitch: number, velocity: number) => void; off: (pitch: number) => void }) { return ( <Piano.Root className="relative h-32 w-96" maxPitch={72} minPitch={60} onNoteOff={off} onNoteOn={(pitch, { velocity }) => on(pitch, velocity)} > {({ pitches }) => pitches.map((pitch) => ( <Piano.Key className="rounded-b border bg-background data-[color=black]:z-10 data-[color=black]:h-2/3 data-[color=black]:bg-foreground data-active:bg-primary" key={pitch} pitch={pitch} /> )) } </Piano.Root> ); }
In the web adapter, the keyboard makes a key for each pitch, each a wrapper around a <button>. To style or fill them yourself, write the audio-ui-piano-key elements with a pitch and a button inside: the keyboard then leaves them as they are, and an audio-ui-piano-label inside a button gets its note.
What a note reports
onNoteOngetsvelocityfrom 0 to 1, areason("press","glide"or"keyboard"), asourceand theevent.onNoteOffgets areason:"press","glide"or"keyboard".- In the web adapter, they are the
noteonandnoteoffevents, withdetail: { value, reason, source, velocity }, wherevalueis the pitch.
Interaction
Pointer
Behavior · 4
onNoteOn; the key stays pressed while held.onNoteOff.onNoteOff) and the next one sounds (onNoteOn) as the pointer crosses it.Keyboard
The keys share one tab stop. On the focused key:
Keys · 4
onNoteOn on press, onNoteOff on releaseAPI reference
Registry component
Piano accepts style props (size, className) plus the props of the Piano primitive's Root.
Value and state
Props · 9
minPitch…maxPitch.eventDetails.cancel() keeps it silent.Layout and display
Props · 2
Additional valid HTML attributes passed to Piano.Root are forwarded.
Options
Set on the Root, as props (an attribute in kebab case in the web adapter).
| Option | Type | Default | |
|---|---|---|---|
minPitch, maxPitch | number | 48, 72 | The lowest and highest MIDI pitch, inclusive: 60 is middle C. |
pitches | number[] | Only these pitches get keys, instead of minPitch to maxPitch. | |
layout | "keyboard" or "even" | "keyboard" | Like a piano, with the black keys between the white ones, or one equal share for each key, as the rows of a piano roll are. |
orientation | "horizontal" or "vertical" | "horizontal" | Which way the keys run. A vertical keyboard puts the low end at the bottom. |
activeNotes | number[] | Pitches lit from elsewhere, such as a MIDI input or a sequencer, on top of the keys pressed here. | |
disabled | boolean | false | Plays nothing. |
onNoteOn | (pitch, details) => void | A key sounds. details.cancel() keeps it silent. | |
onNoteOff | (pitch, details) => void | A key is released, the pointer glides off it, or it loses focus while held. |
States to style from
data-activeon a key while it sounds, pressed here or listed inactiveNotes.data-coloron a key,"black"or"white",data-disabled,data-layoutanddata-orientation.
Parts
Parts · 3
pitch: key.pitch: [object Object], value: [object Object].Events
Events · 2
@note-off. Web: the noteoff event.@note-on. Web: the noteon event.