LabDAW

Pad

A grid of drum pads struck by pointer, touch or keyboard, with velocity and choke groups.

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) and onPadOff(value, eventDetails) report the pad's value, ready for an audio engine. eventDetails.velocity says how hard it was struck, from 0 to 1, and eventDetails.cancel() in onPadOn keeps 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 chokeGroup stop each other, like a closed hihat cutting an open one. Striking one fires onPadOff with reason: "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: activePads lights pads from outside, such as the steps of a sequencer, without calling onPadOn or onPadOff.
  • 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:

pnpm dlx shadcn@latest add @audio-ui/pad

Or the primitive alone, to style yourself:

pnpm 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:

pnpm dlx shadcn@latest add @audio-ui/primitives-css

Usage

import { Pad, PadItem } from "@/components/audio-ui/pad";

Pad events:

<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):

<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.

import { 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

  • onPadOn gets velocity from 0 to 1, a reason ("press" or "keyboard"), a source ("mouse", "touch", "pen" or "keyboard") and the event.
  • onPadOff gets a reason: "press", "keyboard", or "choke" when another pad of its group cut it.

Interaction

Pointer

Behavior · 3

Press a pad
Fires onPadOn with a velocity; the pad stays active while held.
Release
Fires onPadOff.
Multiple pointers
Each pointer (finger) holds its own pad, so several play at once on touch devices.

Keyboard

The pads share one tab stop. On the focused pad:

Keys · 4

ArrowLeft / ArrowRight
Move focus to the previous / next pad in reading order
ArrowUp / ArrowDown
Move focus a row up / down
Space / Enter
Hold the focused pad: onPadOn on press, onPadOff on release
Home / End
Move focus to the first / last pad

API 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).

OptionTypeDefault
columnsnumber4How many pads a row holds: what the up and down arrows move by. Lay the grid out to match.
activePadsstring[]Pads lit from elsewhere, such as a sequencer or a MIDI input, on top of the ones held here.
disabledbooleanfalseStrikes nothing.
onPadOn(value, details) => voidA pad is struck. details.cancel() keeps it silent.
onPadOff(value, details) => voidA 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-active on a pad while it sounds, held here or listed in activePads, and data-disabled on the Root and the pads.
  • --pad-velocity on a pad, from 0 to 1: how hard it was last struck, for a flash that follows the force.

Parts

Parts · 2

Events

Events · 2

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.

Built by Ouest Labs, the source code is available on GitHub.
RSS