# Sequencer

> A step grid: press a step, or drag across several, to draw a pattern.

**In Vue**

```tsx
<script lang="ts" setup>
import { onBeforeUnmount, ref } from "vue";
import Sequencer from "@/components/audio-ui/sequencer/Sequencer.vue";
import {
  triggerHihat,
  triggerKick,
  triggerSnare,
} from "@/lib/drum-synthesis";
import { createStepPlayer } from "@/lib/step-player";
import { webAudio } from "@/lib/web-audio";

const STEPS = 16;
const BPM = 112;
const VOLUME = 0.7;
const ROWS = ["Kick", "Snare", "Hihat"];
const TRIGGERS = [triggerKick, triggerSnare, triggerHihat];
const VELOCITIES = [
  [1, 0.5, 0.7, 0.5, 0.9, 0.5, 0.7, 0.5, 1, 0.5, 0.7, 0.5, 0.9, 0.5, 0.7, 0.6],
  [1, 1, 1, 1, 0.8, 1, 1, 1, 1, 1, 1, 1, 0.85, 1, 1, 0.4],
  [
    0.55, 0.6, 0.9, 0.6, 0.55, 0.6, 0.9, 0.6, 0.55, 0.6, 0.9, 0.6, 0.55, 0.6,
    0.9, 0.6,
  ],
];
const on = (steps: number[]) =>
  Array.from({ length: STEPS }, (_, step) => steps.includes(step));
const BEAT = [on([0, 4, 8, 12]), on([4, 12]), on([2, 6, 10, 14])];
const EMPTY = () => ROWS.map(() => Array.from({ length: STEPS }, () => false));

/** Plays what the pattern holds on a step: each hit goes through a gain of its own, the step's velocity. */
function playStep(rows: boolean[][], levels: number[][], step: number) {
  const ctx = webAudio.resume();
  if (!ctx) {
    return;
  }
  rows.forEach((row, at) => {
    if (!row[step]) {
      return;
    }
    const hit = ctx.createGain();
    hit.gain.value = (levels[at]?.[step] ?? 1) * VOLUME;
    hit.connect(ctx.destination);
    TRIGGERS[at]?.(ctx, hit, ctx.currentTime);
  });
}

const pattern = ref(BEAT);
const velocities = ref(VELOCITIES);
const playing = ref(false);
const player = createStepPlayer({
  bpm: BPM,
  onStep: (step) => playStep(pattern.value, velocities.value, step),
  steps: STEPS,
});

function toggle() {
  if (player.isPlaying()) {
    player.stop();
  } else {
    player.start();
  }
  playing.value = player.isPlaying();
}

onBeforeUnmount(() => player.stop());
</script>

<template>
  <div style="display: flex; flex-direction: column; gap: 0.75rem; width: 100%">
    <Sequencer
      aria-label="Drum pattern"
      show-velocity
      :row-labels="ROWS"
      :source="player.source"
      :value="pattern"
      :velocities="velocities"
      @value-change="(next) => { pattern = next; }"
      @velocity-change="(next) => { velocities = next; }"
    />
    <div
      style="display: flex; align-items: center; justify-content: space-between; gap: 0.5rem"
    >
      <button
        style="height: 2rem; padding: 0 0.75rem; border: 1px solid var(--border); border-radius: var(--radius, 0.5rem); background: var(--background); color: var(--foreground); font: inherit; font-size: 0.875rem; cursor: pointer"
        type="button"
        @click="toggle"
      >
        {{ playing ? "Stop" : "Play" }}
      </button>
      <button
        data-agent-confirm="Clears every step of the pattern"
        style="height: 2rem; padding: 0 0.75rem; border: 0; border-radius: var(--radius, 0.5rem); background: transparent; color: var(--foreground); font: inherit; font-size: 0.875rem; cursor: pointer"
        type="button"
        @click="pattern = EMPTY()"
      >
        Clear
      </button>
    </div>
  </div>
</template>
```

**In Svelte**

```tsx
<script lang="ts">
import { onDestroy } from "svelte";
import Sequencer from "$lib/components/audio-ui/sequencer/Sequencer.svelte";
import {
  triggerHihat,
  triggerKick,
  triggerSnare,
} from "@/lib/drum-synthesis";
import { createStepPlayer } from "@/lib/step-player";
import { webAudio } from "@/lib/web-audio";

const STEPS = 16;
const BPM = 112;
const VOLUME = 0.7;
const ROWS = ["Kick", "Snare", "Hihat"];
const TRIGGERS = [triggerKick, triggerSnare, triggerHihat];
const VELOCITIES = [
  [1, 0.5, 0.7, 0.5, 0.9, 0.5, 0.7, 0.5, 1, 0.5, 0.7, 0.5, 0.9, 0.5, 0.7, 0.6],
  [1, 1, 1, 1, 0.8, 1, 1, 1, 1, 1, 1, 1, 0.85, 1, 1, 0.4],
  [
    0.55, 0.6, 0.9, 0.6, 0.55, 0.6, 0.9, 0.6, 0.55, 0.6, 0.9, 0.6, 0.55, 0.6,
    0.9, 0.6,
  ],
];
const on = (steps: number[]) =>
  Array.from({ length: STEPS }, (_, step) => steps.includes(step));
const BEAT = [on([0, 4, 8, 12]), on([4, 12]), on([2, 6, 10, 14])];
const EMPTY = () => ROWS.map(() => Array.from({ length: STEPS }, () => false));

/** Plays what the pattern holds on a step: each hit goes through a gain of its own, the step's velocity. */
function playStep(rows: boolean[][], levels: number[][], step: number) {
  const ctx = webAudio.resume();
  if (!ctx) {
    return;
  }
  rows.forEach((row, at) => {
    if (!row[step]) {
      return;
    }
    const hit = ctx.createGain();
    hit.gain.value = (levels[at]?.[step] ?? 1) * VOLUME;
    hit.connect(ctx.destination);
    TRIGGERS[at]?.(ctx, hit, ctx.currentTime);
  });
}

let pattern = $state(BEAT);
let velocities = $state(VELOCITIES);
let playing = $state(false);
const player = createStepPlayer({
  bpm: BPM,
  onStep: (step) => playStep(pattern, velocities, step),
  steps: STEPS,
});

function toggle() {
  if (player.isPlaying()) {
    player.stop();
  } else {
    player.start();
  }
  playing = player.isPlaying();
}

function clear() {
  pattern = EMPTY();
}

onDestroy(() => player.stop());
</script>

<div style="display: flex; flex-direction: column; gap: 0.75rem; width: 100%">
  <Sequencer
    aria-label="Drum pattern"
    onValueChange={(next) => {
      pattern = next;
    }}
    onVelocityChange={(next) => {
      velocities = next;
    }}
    rowLabels={ROWS}
    showVelocity
    source={player.source}
    value={pattern}
    {velocities}
  />
  <div
    style="display: flex; align-items: center; justify-content: space-between; gap: 0.5rem"
  >
    <button
      onclick={toggle}
      style="height: 2rem; padding: 0 0.75rem; border: 1px solid var(--border); border-radius: var(--radius, 0.5rem); background: var(--background); color: var(--foreground); font: inherit; font-size: 0.875rem; cursor: pointer"
      type="button"
    >
      {playing ? "Stop" : "Play"}
    </button>
    <button
      data-agent-confirm="Clears every step of the pattern"
      onclick={clear}
      style="height: 2rem; padding: 0 0.75rem; border: 0; border-radius: var(--radius, 0.5rem); background: transparent; color: var(--foreground); font: inherit; font-size: 0.875rem; cursor: pointer"
      type="button"
    >
      Clear
    </button>
  </div>
</div>
```

**In Web**

```tsx
import {
  triggerHihat,
  triggerKick,
  triggerSnare,
} from "@/lib/drum-synthesis";
import { createStepPlayer } from "@/lib/step-player";
import { webAudio } from "@/lib/web-audio";
import "@/components/audio-ui/sequencer/web";

const STEPS = 16;
const BPM = 112;
const VOLUME = 0.7;
const ROWS = ["Kick", "Snare", "Hihat"];
const TRIGGERS = [triggerKick, triggerSnare, triggerHihat];
const VELOCITIES = [
  [1, 0.5, 0.7, 0.5, 0.9, 0.5, 0.7, 0.5, 1, 0.5, 0.7, 0.5, 0.9, 0.5, 0.7, 0.6],
  [1, 1, 1, 1, 0.8, 1, 1, 1, 1, 1, 1, 1, 0.85, 1, 1, 0.4],
  [
    0.55, 0.6, 0.9, 0.6, 0.55, 0.6, 0.9, 0.6, 0.55, 0.6, 0.9, 0.6, 0.55, 0.6,
    0.9, 0.6,
  ],
];
const on = (steps) =>
  Array.from({ length: STEPS }, (_, step) => steps.includes(step));
const BEAT = [on([0, 4, 8, 12]), on([4, 12]), on([2, 6, 10, 14])];
const EMPTY = () => ROWS.map(() => Array.from({ length: STEPS }, () => false));

/** Plays what the pattern holds on a step: each hit goes through a gain of its own, the step's velocity. */
function playStep(rows, levels, step) {
  const ctx = webAudio.resume();
  if (!ctx) {
    return;
  }
  rows.forEach((row, at) => {
    if (!row[step]) {
      return;
    }
    const hit = ctx.createGain();
    hit.gain.value = (levels[at]?.[step] ?? 1) * VOLUME;
    hit.connect(ctx.destination);
    TRIGGERS[at]?.(ctx, hit, ctx.currentTime);
  });
}

/** Draws the sequencer into `element`, and returns what takes it away. The rows, the steps and the velocity bars are made for you: styled here from their tags. */
export function sequencerWebDemo(element) {
  let pattern = BEAT;
  let velocities = VELOCITIES;
  const player = createStepPlayer({
    bpm: BPM,
    onStep: (step) => playStep(pattern, velocities, step),
    steps: STEPS,
  });

  element.innerHTML = `
    <style>
      /* The rows the custom element makes hold their cells and velocity bars side by side: a grid of the pattern's steps lays them out. The sheet draws each cell and bar. */
      audio-ui-sequencer[data-slot] [data-slot=sequencer-row] { display: grid; grid-template-columns: repeat(16, minmax(0, 1fr)); gap: 0.25rem; }
      audio-ui-sequencer[data-slot] [data-slot=sequencer-cell] { height: 2.25rem; }
      audio-ui-sequencer[data-slot] audio-ui-sequencer-velocity { position: relative; height: 2rem; cursor: ns-resize; background: linear-gradient(to top, var(--muted-foreground) calc(var(--sequencer-velocity) * 100%), transparent 0); }
    </style>
    <div style="display: flex; flex-direction: column; gap: 0.75rem; width: 100%">
      <audio-ui-sequencer aria-label="Drum pattern" velocity-bars velocities='${JSON.stringify(velocities)}' value='${JSON.stringify(pattern)}'></audio-ui-sequencer>
      <div style="display: flex; align-items: center; justify-content: space-between; gap: 0.5rem">
        <button data-action="toggle" style="height: 2rem; padding: 0 0.75rem; border: 1px solid var(--border); border-radius: var(--radius, 0.5rem); background: var(--background); color: var(--foreground); font: inherit; font-size: 0.875rem; cursor: pointer" type="button">Play</button>
        <button data-action="clear" style="height: 2rem; padding: 0 0.75rem; border: 0; border-radius: var(--radius, 0.5rem); background: transparent; color: var(--foreground); font: inherit; font-size: 0.875rem; cursor: pointer" type="button">Clear</button>
      </div>
    </div>`;
  const grid = element.querySelector("audio-ui-sequencer");
  if (grid) {
    grid.source = player.source;
  }
  // The sequencer is controlled by its attributes: each change is written back, or it returns to the last pattern written.
  const onInput = (event) => {
    pattern = event.detail.value;
    grid?.setAttribute("value", JSON.stringify(pattern));
  };
  const onClick = (event) => {
    const action = event.target
      .closest("[data-action]")
      ?.getAttribute("data-action");
    if (action === "toggle") {
      if (player.isPlaying()) {
        player.stop();
      } else {
        player.start();
      }
      const button = element.querySelector('[data-action="toggle"]');
      if (button) {
        button.textContent = player.isPlaying() ? "Stop" : "Play";
      }
    } else if (action === "clear") {
      pattern = EMPTY();
      if (grid) {
        grid.setAttribute("value", JSON.stringify(pattern));
      }
    }
  };
  const onVelocity = (event) => {
    velocities = event.detail.value;
    grid?.setAttribute("velocities", JSON.stringify(velocities));
  };
  grid?.addEventListener("input", onInput);
  grid?.addEventListener("velocitychange", onVelocity);
  element.addEventListener("click", onClick);
  return () => {
    player.stop();
    grid?.removeEventListener("input", onInput);
    grid?.removeEventListener("velocitychange", onVelocity);
    element.removeEventListener("click", onClick);
    element.innerHTML = "";
  };
}
```

**In React**

```tsx
import { PauseIcon, PlayIcon } from "@audio-ui/icons/react";
import { useCallback, useEffect, useRef, useState } from "react";
import { Button } from "@/components/ui/button";
import { Sequencer } from "@/components/audio-ui/sequencer";
import { useMasterBus } from "@/hooks/use-master-bus";
import { useTransportClock } from "@/hooks/use-transport-clock";
import {
  triggerHihat,
  triggerKick,
  triggerSnare,
} from "@/lib/drum-synthesis";
import { webAudio } from "@/lib/web-audio";
import { createSource } from "@/components/audio-ui/primitive";

const STEPS = 16;
const STEPS_PER_BEAT = 4;
const BPM = 112;
const VOLUME = 0.7;
const ROWS = ["Kick", "Snare", "Hihat"];
const TRIGGERS = [triggerKick, triggerSnare, triggerHihat];

const VELOCITIES = [
  [1, 0.5, 0.7, 0.5, 0.9, 0.5, 0.7, 0.5, 1, 0.5, 0.7, 0.5, 0.9, 0.5, 0.7, 0.6],
  [1, 1, 1, 1, 0.8, 1, 1, 1, 1, 1, 1, 1, 0.85, 1, 1, 0.4],
  [
    0.55, 0.6, 0.9, 0.6, 0.55, 0.6, 0.9, 0.6, 0.55, 0.6, 0.9, 0.6, 0.55, 0.6,
    0.9, 0.6,
  ],
];

const x = (steps: number[]) =>
  Array.from({ length: STEPS }, (_, step) => steps.includes(step));
const BEAT = [x([0, 4, 8, 12]), x([4, 12]), x([2, 6, 10, 14])];

/** A drum machine: draw a pattern and hear it. Press a step, or drag across several; the playing column lights up. */
export function SequencerDemo() {
  const [pattern, setPattern] = useState(BEAT);
  const patternRef = useRef(pattern);
  patternRef.current = pattern;
  const [velocities, setVelocities] = useState(VELOCITIES);
  const velocitiesRef = useRef(velocities);
  velocitiesRef.current = velocities;
  const [step] = useState(() => createSource(-1));
  const master = useMasterBus(VOLUME);

  const playStep = useCallback(
    (index: number, time: number) => {
      const ctx = webAudio.getContext();
      if (!(ctx && master)) {
        return;
      }
      patternRef.current.forEach((row, at) => {
        if (!row[index]) {
          return;
        }
        // Each hit goes through a gain of its own: the step's velocity.
        const hit = ctx.createGain();
        hit.gain.value = velocitiesRef.current[at]?.[index] ?? 1;
        hit.connect(master);
        TRIGGERS[at]?.(ctx, hit, time);
      });
    },
    [master]
  );
  const { getPosition, isPlaying, start, stop } = useTransportClock({
    bpm: BPM,
    onStep: playStep,
    stepsPerBeat: STEPS_PER_BEAT,
    totalSteps: STEPS,
  });

  useEffect(() => {
    if (!isPlaying) {
      step.set(-1);
      return;
    }
    let frame = 0;
    const tick = () => {
      step.set(Math.floor(getPosition()) % STEPS);
      frame = requestAnimationFrame(tick);
    };
    frame = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(frame);
  }, [getPosition, isPlaying, step]);

  const toggle = useCallback(
    () => (isPlaying ? stop() : start()),
    [isPlaying, start, stop]
  );
  const clear = useCallback(
    () =>
      setPattern(ROWS.map(() => Array.from({ length: STEPS }, () => false))),
    []
  );

  return (
    <div className="flex w-full flex-col gap-3">
      <Sequencer
        aria-label="Drum pattern"
        onValueChange={setPattern}
        onVelocityChange={setVelocities}
        rowLabels={ROWS}
        showVelocity
        source={step}
        value={pattern}
        velocities={velocities}
      />
      <div className="flex items-center justify-between gap-2">
        <Button
          aria-label={isPlaying ? "Stop" : "Play"}
          onClick={toggle}
          size="icon-sm"
          variant="outline"
        >
          {isPlaying ? <PauseIcon weight="fill" /> : <PlayIcon weight="fill" />}
        </Button>
        <Button
          data-agent-confirm="Clears every step of the pattern"
          onClick={clear}
          size="sm"
          variant="ghost"
        >
          Clear
        </Button>
      </div>
    </div>
  );
}
```

A step grid for a drum pattern or any row of on and off steps, with a velocity bar under each. The registry component is written on the sequencer primitive, which installs with it.

**In Vue, Svelte and Web**

The sequencer's behavior is one file, the primitive, written on the UI SDK kernel and copied into your project. It is a grid of steps to switch on and off, one row for each sound, as in a drum machine, with a velocity bar under each step. It holds two values, the pattern and the velocities. Each framework renders it with a thin adapter that you also own, and one suite of tests runs against each adapter.

## Installation

The primitives are built on the [UI SDK](https://ui-sdk.dev) 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`.

**In React**

The styled component, with sizes and themed steps, is one command:

**In React**

**CLI**

The styled component, with size variants and theme styles:

```bash
npx shadcn@latest add @audio-ui/sequencer
```

Or the primitive alone, to style yourself:

```bash
npx shadcn@latest add @audio-ui/sequencer-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:

```bash
npx shadcn@latest add @audio-ui/primitives-css
```

**In Vue, Svelte and Web**

**CLI**

The [CLI](/docs/cli) finds your framework in `package.json` and writes the primitive, its adapter and `primitive.ts` into `components/audio-ui/` (under `src/lib/` in SvelteKit). It installs the kernel's packages:

```bash
npx @audio-ui/cli add sequencer
```

The optional styles are one more name:

```bash
npx @audio-ui/cli add primitives-css
```

**manual**

### 1. The kernel

**React**

```bash
npm install @ui-sdk/core@0.1.0-beta.14 @ui-sdk/react@0.1.0-beta.14
```

**Vue**

```bash
npm install @ui-sdk/core@0.1.0-beta.14 @ui-sdk/vue@0.1.0-beta.14 vue
```

**Svelte**

```bash
npm install @ui-sdk/core@0.1.0-beta.14 @ui-sdk/svelte@0.1.0-beta.14 svelte
```

**JavaScript**

```bash
npm install @ui-sdk/core@0.1.0-beta.14 @ui-sdk/web@0.1.0-beta.14
```

### 2. The primitive

The same two files for every framework: `sequencer.ts`, the sequencer's behavior written on the kernel's helpers, and `primitive.ts`, the little every primitive shares. Copy them into `components/audio-ui/` (`src/lib/components/audio-ui/` in Svelte).

<ItemFiles name="sequencer-react" to={2} />

### 3. The adapter

Your framework's few parts, over the primitive. They import it by a relative path, from the same folder, so keep the files as shown, or update the path.

**React**

<ItemFiles name="sequencer-react" from={2} />

**Vue**

<ItemFiles name="sequencer-vue" from={2} />

**Svelte**

A component per part, and an `index.ts` that gathers them. Svelte keeps its files under `src/lib`, so the primitive goes in `src/lib/components/audio-ui/` too.

<ItemFiles name="sequencer-svelte" from={2} />

**JavaScript**

Import it once, and it registers the `audio-ui-sequencer` tags.

<ItemFiles name="sequencer-web" from={2} />

### 4. The styles

Optional, and the same for every framework: the anatomy of the primitives, drawn flat and square, in the look of the UI SDK docs. It reads the tokens of the UI SDK stylesheet (`--ui-*`) when the page has it, and falls back to its own. Every rule is in the `ui-sdk` layer, so a rule of yours outside the layer wins.

<ItemFiles name="primitives-css" />

Import it once.

**In React**

### 5. The styled component

Optional: size variants and theme styles over the primitive.

<ItemFiles name="sequencer" />

**agent**

Paste this prompt to your agent. It does the rest: it installs the skill, adds the component and wires it to your audio.

```text
Install the audio/ui skill with `npx skills add ouestlabs/audio-ui`, then follow it to add the sequencer from audio/ui to my project and wire it to my audio code. Read the docs at https://www.audio-ui.dev/llms.txt first.
```

To let the agent read the docs live, connect the [MCP server](/docs/mcp). More in [Agent Skill](/docs/agents).

## Usage

**In React**

```tsx
import { Sequencer } from "@/components/audio-ui/sequencer";

<Sequencer
  aria-label="Drum pattern"
  onValueChange={setPattern}
  rowLabels={["Kick", "Snare", "Hihat"]}
  source={step}
  value={pattern}
/>;
```

`value` is a row per sound, each a list of steps: `boolean[][]`. `velocities` is how loud each step plays, from 0 to 1, in the same shape; a step it does not list plays at full. Add `showVelocity` for a bar under each row to draw them. `source` is the step being played, a live `{ getSnapshot, subscribe }` such as one made with `createSource`: a clock ticking every step never re-renders React.

**In React**

The primitive alone, part by part:

A Root groups the rows. A Row is one sound, a Cell one of its steps, a `<button>` pressed when the step is on, and a Velocity a bar under a step. You write a Row for each row and a Cell for each step, with their `row` and `step`: the Root's content can read the state, `{ rows, steps }`, so the grid follows the pattern. That is a function in React, the default slot in Vue and a snippet in Svelte.

**React**

```tsx
import { Sequencer } from "@/components/audio-ui/sequencer/react";

const KIT = ["Kick", "Snare", "Hat"];

export function Beat({ pattern, onChange }: { pattern: boolean[][]; onChange: (pattern: boolean[][]) => void }) {
  return (
    <Sequencer.Root onValueChange={onChange} value={pattern}>
      {({ rows, steps }) =>
        Array.from({ length: rows }, (_, row) => (
          <Sequencer.Row aria-label={KIT[row]} className="flex gap-1" index={row} key={row}>
            {Array.from({ length: steps }, (_, step) => (
              <Sequencer.Cell
                aria-label={`${KIT[row]}, step ${step + 1}`}
                className="size-8 rounded bg-muted data-on:bg-primary data-current:ring-2"
                key={step}
                row={row}
                step={step}
              />
            ))}
          </Sequencer.Row>
        ))
      }
    </Sequencer.Root>
  );
}
```

**Vue**

```vue
<script setup lang="ts">
import { Sequencer } from "@/components/audio-ui/sequencer/vue";

const kit = ["Kick", "Snare", "Hat"];
defineProps<{ pattern: boolean[][] }>();
const emit = defineEmits<{ change: [pattern: boolean[][]] }>();
</script>

<template>
  <Sequencer.Root :value="pattern" @value-change="(next) => emit('change', next)">
    <template #default="{ state }">
      <Sequencer.Row v-for="row in state.rows" :key="row" :aria-label="kit[row - 1]" :index="row - 1" class="flex gap-1">
        <Sequencer.Cell
          v-for="step in state.steps"
          :key="step"
          :aria-label="`${kit[row - 1]}, step ${step}`"
          :row="row - 1"
          :step="step - 1"
          class="size-8 rounded bg-muted data-on:bg-primary data-current:ring-2"
        />
      </Sequencer.Row>
    </template>
  </Sequencer.Root>
</template>
```

**Svelte**

```svelte
<script lang="ts">
  import { Sequencer } from "$lib/components/audio-ui/sequencer/svelte";

  const kit = ["Kick", "Snare", "Hat"];
  let { pattern, onChange }: { pattern: boolean[][]; onChange: (pattern: boolean[][]) => void } = $props();
</script>

<Sequencer.Root value={pattern} onValueChange={onChange}>
  {#snippet children(state)}
    {#each Array.from({ length: state.rows }, (_, i) => i) as row (row)}
      <Sequencer.Row aria-label={kit[row]} class="flex gap-1" index={row}>
        {#each Array.from({ length: state.steps }, (_, i) => i) as step (step)}
          <Sequencer.Cell aria-label={`${kit[row]}, step ${step + 1}`} class="size-8 rounded bg-muted data-on:bg-primary data-current:ring-2" {row} {step} />
        {/each}
      </Sequencer.Row>
    {/each}
  {/snippet}
</Sequencer.Root>
```

With the latest SvelteKit, import through `#lib` in place of `$lib`: `#lib/components/audio-ui/sequencer/svelte`.

**JavaScript**

```html
<style>
  audio-ui-sequencer-row { display: flex; gap: 4px; }
  audio-ui-sequencer-cell button { width: 32px; height: 32px; }
  audio-ui-sequencer-cell button[data-on] { background: #2f7d4a; }
  audio-ui-sequencer-cell button[data-current] { outline: 2px solid #fff; }
</style>

<audio-ui-sequencer value='[[true,false,false,false],[false,false,true,false]]'></audio-ui-sequencer>

<script type="module">
  import "./audio-ui/sequencer/web.ts";

  const grid = document.querySelector("audio-ui-sequencer");
  grid.addEventListener("input", (event) => console.log(event.detail.value, event.detail.reason));
  // The step being played, from a live source: no render for a clock that ticks every step.
  // grid.source = clock; // any { getSnapshot, subscribe }
</script>
```

In the web adapter the sequencer makes the rows, the cells and the velocity bars itself, one for each step of its pattern. Add the `velocity-bars` attribute to get the bars.

**Velocity bars.** Write a Velocity for each step, in the Row, under the cells. Its height is the `--sequencer-velocity` CSS variable, from 0 to 1, which you read in your styles. It holds a native `<input type="range">` that carries its name and takes the keys.

### The step being played

Give the Root a `source`, a live value of the step being played, in the shape of an external store: `getSnapshot()` returns the step and `subscribe(listener)` is called on each tick. The step's cells get `data-current` straight in the DOM, so a clock ticking every step renders nothing. Any object with those two methods works, with nothing to install, and `useTransportClock` from the registry gives you one on the audio clock.

In the web adapter, `source` is a property, not an attribute.

## Interaction

**Gestures**

| Gesture | Effect |
| --- | --- |
| Press a step | Flips it. |
| Drag on | Paints the new state across every step the pointer crosses. |
| Arrow keys, `Home`, `End`, `Page Up`/`Down` | Move between steps, left and right following the reading direction; with `Ctrl` or `Cmd`, `Home` and `End` go to the corners. |
| `Space`, `Enter` | Flip the focused step. |
| Press or drag on a velocity bar | Sets the velocity where the pointer is, and draws across the steps of the row. |
| `Alt` + click, `Delete` on a velocity bar | Bring the step back to full. |
| Arrow keys, `Page Up`/`Down`, `Home`/`End` on a velocity bar | Change the velocity by 5%, by 25%, or to silent or full. |

The steps share one tab stop, and each is a toggle button (`aria-pressed`) named after its row and step.

## API reference

**In React**

### Registry component

`Sequencer` takes the `Root` props, plus:

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `rowLabels` | `string[]` |  | Names written beside each row, and its accessible name. |
| `beatEvery` | `number` | `4` | Highlights every nth step. |
| `showVelocity` | `boolean` | `false` | Draws a bar under each row to set how loud every step plays. |
| `size` | `'"default" \| "sm"'` | `'"default"'` | `sm` draws short steps and a thin bar under them, so many rows fit. |
| `shadeByVelocity` | `boolean` | `false` | Draws a step that plays fainter the softer it plays, by `velocities`. |
| `renderCell` | `(row: number, step: number) => ReactNode` |  | What a step shows inside it, such as how many times it repeats. |
| `renderRowLabel` | `(row: number) => ReactNode` |  | What sits beside a row in place of its written name, such as a button that sounds it; the row keeps its name for assistive technology. |

### Options

Set on the Root, as props (an attribute in kebab case in the web adapter, the grids as JSON).

| Option | Type | Default | |
| --- | --- | --- | --- |
| `value`, `defaultValue` | `boolean[][]` | | The pattern: a row for each sound, each a list of steps. Controlled, or the starting one. |
| `velocities`, `defaultVelocities` | `number[][]` | | How loud each step plays, from 0 to 1, a row for each sound like the pattern. A step it does not list plays at full. |
| `source` | `{ getSnapshot, subscribe }` | | The step being played, followed with no render. |
| `disabled` | `boolean` | `false` | Takes no gesture. |
| `onValueChange` | `(pattern, details) => void` | | A step is flipped. `details.cancel()` keeps the pattern as it was. |
| `onValueCommitted` | `(pattern, details) => void` | | A press, a paint or a key that changed the pattern ends. |
| `onVelocityChange` | `(velocities, details) => void` | | A velocity is drawn or nudged. |
| `onVelocityCommitted` | `(velocities, details) => void` | | A drag, a key or a reset that changed a velocity ends. |

### What a change reports

- Pattern: `reason` is `"press"`, `"paint"` or `"keyboard"`.
- Velocities: `reason` is `"drag"`, `"keyboard"`, `"input-change"` or `"reset"`.
- Both carry a `source` (`"mouse"`, `"touch"`, `"pen"`, `"keyboard"`) and the `event`.
- In the web adapter, the pattern is the `input` and `change` events, and the velocities the `velocitychange` and `velocitycommit` events, with `detail: { value, reason, source }`.

### States to style from

- `data-on` and `aria-pressed` on a cell while its step is on, `data-current` while it is the step being played, `data-row` and `data-step` for its place.
- `data-painting` on the Root and the cells while a pointer paints.
- `data-disabled` on every part.

### Parts

**In React, Vue and Svelte**

| Part | Element | Props |
| --- | --- | --- |
| `Sequencer.Cell` | `button` | `cell: key`, `row: number`, `step: number` |
| `Sequencer.Root` | `div` |  |
| `Sequencer.Row` | `div` | `index: key` |
| `Sequencer.Velocity` | `div` | `cell: key`, `row: number`, `step: number` |

**In Web**

The tags the web adapter registers: the inputs inside a part are made for you.

`audio-ui-sequencer`, `audio-ui-sequencer-cell`, `audio-ui-sequencer-row`, `audio-ui-sequencer-velocity`, `audio-ui-sequencer-velocity-input`

### Events

| React and Svelte | Vue | Web |
| --- | --- | --- |
| `onValueChange` | `@value-change` | `input` |
| `onValueCommitted` | `@value-committed` | `change` |
| `onVelocityChange` | `@velocity-change` | `velocitychange` |
| `onVelocityCommitted` | `@velocity-committed` | `velocitycommit` |

