Triggers sounds, for a drum machine, a sampler or a clip launcher. The registry component is written on the pad primitive, which installs with it, and lays the pads out in a grid of columns.
Overview
- Events:
onPadOn(value, eventDetails)andonPadOff(value, eventDetails)report the pad'svalue, ready for an audio engine.eventDetails.velocitysays how hard it was struck, from 0 to 1, andeventDetails.cancel()inonPadOnkeeps the pad silent. - Velocity: a pen or a pressure-sensitive screen strikes as hard as it presses. Other pointers strike harder toward the top of the pad. The keyboard strikes at
0.8. Each pad exposes its last velocity as--pad-velocity, so the flash can follow it. - Choke groups: pads sharing a
chokeGroupstop each other, like a closed hihat cutting an open one. Striking one firesonPadOffwithreason: "choke"for the last pad struck in its group, held or still ringing. - Pointer: each pointer holds its own pad, so several play at once on a touch screen. Sliding off a pad does not strike the next one.
- External pads:
activePadslights pads from outside, such as the steps of a sequencer, without callingonPadOnoronPadOff. - Accessibility: each pad is a button named by its content, behind one tab stop. The arrow keys move between pads, and Space or Enter holds the focused pad until it is released.
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, laid out in a grid of columns, is one command:
The styled component, with size variants and theme styles:
Copy to Clipboardpnpm dlx shadcn@latest add @audio-ui/pad
Or the primitive alone, to style yourself:
Copy to Clipboardpnpm dlx shadcn@latest add @audio-ui/pad-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 { Pad, PadItem } from "@/components/audio-ui/pad";
Pad events:
Copy to Clipboard<Pad aria-label="Drum pads" columns={2} onPadOn={(value, { velocity }) => sampler.trigger(value, velocity)} > <PadItem value="kick">Kick</PadItem> <PadItem value="snare">Snare</PadItem> </Pad>
Choke groups (a closed hat cuts the open one):
Copy to Clipboard<Pad aria-label="Hats" columns={2} onPadOff={(value, { reason }) => { if (reason === "choke") { sampler.stop(value); } }} onPadOn={(value, { velocity }) => sampler.trigger(value, velocity)} > <PadItem chokeGroup="hat" value="closed-hat"> Closed hat </PadItem> <PadItem chokeGroup="hat" value="open-hat"> Open hat </PadItem> </Pad>
The primitive alone, part by part:
A Root groups the pads, and an Item is one pad, a <button> identified by its value. Write an Item for each pad, in reading order: the order they have in the document is the order the arrow keys walk.
Copy to Clipboardimport { Pad } from "@/components/audio-ui/pad/react"; const KIT = ["Kick", "Snare", "Hat", "Clap"]; export function Drums({ play }: { play: (name: string, velocity: number) => void }) { return ( <Pad.Root className="grid grid-cols-2 gap-2" columns={2} onPadOn={(value, { velocity }) => play(value, velocity)}> {KIT.map((name) => ( <Pad.Item className="size-20 rounded-md bg-muted data-active:bg-primary" key={name} value={name}> {name} </Pad.Item> ))} </Pad.Root> ); }
In the web adapter, an Item wraps the button: the button inside it is the pad. The two callbacks are the padon and padoff events.
What a strike reports
onPadOngetsvelocityfrom 0 to 1, areason("press"or"keyboard"), asource("mouse","touch","pen"or"keyboard") and theevent.onPadOffgets areason:"press","keyboard", or"choke"when another pad of its group cut it.
Interaction
Pointer
Behavior · 3
onPadOn with a velocity; the pad stays active while held.onPadOff.Keyboard
The pads share one tab stop. On the focused pad:
Keys · 4
onPadOn on press, onPadOff on releaseAPI reference
Registry component
Pad accepts className and style plus the props of the Pad primitive's Root, and sets the grid's columns from columns. PadItem accepts the props of the primitive's Pad.Item.
Additional valid HTML attributes passed to Pad.Root and Pad.Item are forwarded.
Options
Set on the Root, as props (an attribute in kebab case in the web adapter).
| Option | Type | Default | |
|---|---|---|---|
columns | number | 4 | How many pads a row holds: what the up and down arrows move by. Lay the grid out to match. |
activePads | string[] | Pads lit from elsewhere, such as a sequencer or a MIDI input, on top of the ones held here. | |
disabled | boolean | false | Strikes nothing. |
onPadOn | (value, details) => void | A pad is struck. details.cancel() keeps it silent. | |
onPadOff | (value, details) => void | A pad is released, loses focus while held, or is choked. |
An Item takes its value, a disabled, and a chokeGroup: pads that share one stop each other, so striking one stops the last one struck, held or still ringing, as an open and a closed hi-hat do.
States to style from
data-activeon a pad while it sounds, held here or listed inactivePads, anddata-disabledon the Root and the pads.--pad-velocityon a pad, from 0 to 1: how hard it was last struck, for a flash that follows the force.
Parts
Parts · 2
chokeGroup: string, disabled: boolean, value: key.Events
Events · 2
@pad-off. Web: the padoff event.@pad-on. Web: the padon event.Examples
Drum rack
Sixteen pads, each its own sound: strike a pad to play and select it, then shape it with the knobs. The three hihats share a choke group.