# XY Pad

> A two-dimensional control for two parameters at once, with pointer, wheel and keyboard input.

**In Vue**

```tsx
<script lang="ts" setup>
import XYPad from "@/components/audio-ui/xypad/XYPad.vue";
</script>

<template>
  <XYPad
    aria-label="Position"
    style="width: 12rem"
    :default-value="{ x: 50, y: 50 }"
    :max-x="100"
    :max-y="100"
    :min-x="0"
    :min-y="0"
  />
</template>
```

**In Svelte**

```tsx
<script lang="ts">
import XYPad from "$lib/components/audio-ui/xypad/XYPad.svelte";
</script>

<XYPad
  aria-label="Position"
  defaultValue={{ x: 50, y: 50 }}
  maxX={100}
  maxY={100}
  minX={0}
  minY={0}
  style="width: 12rem"
/>
```

**In Web**

```tsx
import "@/components/audio-ui/xypad/web";

const LINES = [20, 40, 60, 80];

/** A line of the grid, across or down, at a share of the pad. */
const line = (orientation, position) =>
  `<div data-orientation="${orientation}" data-slot="xypad-grid-line" style="${orientation === "vertical" ? "left" : "top"}: ${position}%"></div>`;

/** Draws the pad into `element`, and returns what takes it away. Its look is the pad's sheet, on its parts and attributes. */
export function xypadWebDemo(element) {
  element.innerHTML = `
    <audio-ui-xypad data-size="default" max-x="100" max-y="100" min-x="0" min-y="0" style="width: 12rem" value='{"x":50,"y":50}'>
      <audio-ui-xypad-control>
        <div aria-hidden="true" data-slot="xypad-grid">${LINES.map((position) => line("vertical", position)).join("")}${LINES.map((position) => line("horizontal", position)).join("")}</div>
        <div aria-hidden="true" data-orientation="vertical" data-slot="xypad-crosshair"></div>
        <div aria-hidden="true" data-orientation="horizontal" data-slot="xypad-crosshair"></div>
        <audio-ui-xypad-thumb aria-label="Position">
          <div data-slot="xypad-cursor-glow"></div>
          <div data-slot="xypad-cursor-dot"></div>
          <div data-slot="xypad-cursor-highlight"></div>
        </audio-ui-xypad-thumb>
        <audio-ui-xypad-value></audio-ui-xypad-value>
      </audio-ui-xypad-control>
    </audio-ui-xypad>`;
  return () => {
    element.innerHTML = "";
  };
}
```

**In React**

```tsx
import { useState } from "react";
import { XYPad } from "@/components/audio-ui/xypad";

export function XYPadLiveValueDemo() {
  const [value, setValue] = useState({ x: 50, y: 50 });

  return (
    <div className="flex flex-col items-center gap-2">
      <XYPad
        aria-label="Position"
        className="aspect-square"
        maxX={100}
        maxY={100}
        minX={0}
        minY={0}
        onValueChange={setValue}
        value={value}
      />
      <output
        aria-live="polite"
        className="font-mono text-foreground text-sm tabular-nums"
      >
        X: {value.x} - Y: {value.y}
      </output>
    </div>
  );
}
```

Controls two values at once, such as filter cutoff and resonance, pan and mix, or two macros. The registry component is written on the XY pad primitive, which installs with it, and adds a grid, a crosshair, a cursor and a value display.

**In Vue, Svelte and Web**

The XY pad's behavior is one file, the primitive, written on the UI SDK kernel and copied into your project. Its value is a point, `{ x, y }`, two parameters moved at once. Each framework renders it with a thin adapter that you also own. The press, the drag, the wheel, the keys, the grid and the reset are the same in all four, and one suite of tests runs against each adapter.

## Overview

- **Input:** press or drag on the pad, or use the mouse wheel, a trackpad or the keyboard, on both axes.
- **Axes:** X and Y have their own range and step (`minX`, `maxX`, `stepX`; `minY`, `maxY`, `stepY`).
- **Display:** grid lines, a crosshair, a cursor and an optional value display. The position is exposed as the CSS variables `--xy-pad-x` (from the left) and `--xy-pad-y` (from the top).
- **Accessibility:** the cursor holds one native `<input type="range">` per axis. Both carry the accessible name (`aria-label` becomes "… X" and "… Y", or set each with `getAriaLabel` on the primitive's `Thumb`). The X input takes focus, and the arrow keys move both axes.

## 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 a grid, a crosshair, a cursor and a value display, is one command:

**In React**

**CLI**

The styled component, with size variants and theme styles:

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

Or the primitive alone, to style yourself:

```bash
npx shadcn@latest add @audio-ui/xypad-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 styled component with what draws it: the primitive, its adapter, `primitive.ts` and the sheet of your style, with an index to import once. It installs the kernel's packages:

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

Or the primitive alone, to style yourself:

```bash
npx @audio-ui/cli add xypad --headless
```

**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: `xypad.ts`, the XY pad'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="xypad-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="xypad-react" from={2} />

**Vue**

<ItemFiles name="xypad-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="xypad-svelte" from={2} />

**JavaScript**

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

<ItemFiles name="xypad-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.

```bash
npm install class-variance-authority
```

<ItemFiles name="xypad" />

**In Vue, Svelte and Web**

### 5. The styled component

Optional: the XY pad as the registry draws it in React, with no classes and no Tailwind. Its look is one sheet of plain CSS on its parts, a sheet for each style (`nova`, `vega`, `maia`, `lyra`, `mira`, `luma`, `rhea`, `sera`). Copy the sheet of your style into your project, with the theme's tokens when the page has no shadcn theme.

The [CLI](/docs/cli) writes the component, its adapter, the sheet and an index to import once:

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

By hand:

```html
<link rel="stylesheet" href="/css/theme.css" />
<link rel="stylesheet" href="/css/nova/xypad.css" />
```

Both are served at `https://www.audio-ui.dev/css/`.

**Vue**

One component over the adapter, with the props of the React XY pad.

<ItemFiles name="xypad-element-vue" />

**Svelte**

One component over the adapter, with the props of the React XY pad.

<ItemFiles name="xypad-element-svelte" />

**JavaScript**

The custom elements take the same sheet. Write the parts the example above writes (the grid, the crosshair and the cursor), and set `data-size` on `audio-ui-xypad`, with `data-fill` when it fills a column.

**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 xypad 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 { XYPad } from "@/components/audio-ui/xypad";
```

**Uncontrolled:** set only the initial value.

```tsx
<XYPad
  defaultValue={{ x: 0, y: 0 }}
  maxX={100}
  maxY={1}
  minX={-100}
  minY={-1}
  stepX={1}
  stepY={0.01}
/>
```

**Controlled:** drive the value from state.

```tsx
const [position, setPosition] = useState({ x: 50, y: 50 });

<XYPad
  maxX={100}
  maxY={100}
  minX={0}
  minY={0}
  onValueChange={setPosition}
  onValueCommitted={(value, details) => console.log("Committed:", value, details.reason)}
  value={position}
/>
```

**In React**

The primitive alone, part by part:

The parts are a Root that owns the point, a Control that takes the pointer and the wheel, a Thumb that marks the point and holds the two inputs, a Value for the text, and an optional Label. The Thumb is placed for you, from the left edge and the top edge of the Control, and the Root carries the same place as two CSS variables, `--xy-pad-x` and `--xy-pad-y`, for a crosshair or a guide. Give the Control its size.

**React**

```tsx
import { useState } from "react";
import { XYPad } from "@/components/audio-ui/xypad/react";

export function Filter() {
  const [point, setPoint] = useState({ x: 25, y: 75 });
  return (
    <XYPad.Root onValueChange={setPoint} value={point}>
      <XYPad.Control className="relative size-40 rounded-md bg-muted">
        <XYPad.Thumb aria-label="Cutoff and resonance" className="absolute size-3 rounded-full bg-primary" />
      </XYPad.Control>
      <XYPad.Value />
    </XYPad.Root>
  );
}
```

**Vue**

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

const point = ref({ x: 25, y: 75 });
</script>

<template>
  <XYPad.Root :value="point" @value-change="(next) => (point = next)">
    <XYPad.Control class="relative size-40 rounded-md bg-muted">
      <XYPad.Thumb aria-label="Cutoff and resonance" class="absolute size-3 rounded-full bg-primary" />
    </XYPad.Control>
    <XYPad.Value />
  </XYPad.Root>
</template>
```

**Svelte**

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

  let point = $state({ x: 25, y: 75 });
</script>

<XYPad.Root value={point} onValueChange={(next) => (point = next)}>
  <XYPad.Control class="relative size-40 rounded-md bg-muted">
    <XYPad.Thumb aria-label="Cutoff and resonance" class="absolute size-3 rounded-full bg-primary" />
  </XYPad.Control>
  <XYPad.Value />
</XYPad.Root>
```

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

**JavaScript**

```html
<audio-ui-xypad value='{"x":25,"y":75}'>
  <audio-ui-xypad-control>
    <audio-ui-xypad-thumb aria-label="Cutoff and resonance"></audio-ui-xypad-thumb>
  </audio-ui-xypad-control>
  <audio-ui-xypad-value></audio-ui-xypad-value>
</audio-ui-xypad>

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

  document.querySelector("audio-ui-xypad").addEventListener("input", (event) => {
    console.log(event.detail.value, event.detail.reason, event.detail.source);
  });
</script>
```

The point is JSON in an attribute. Each part binds a microtask after it connects, all together: read the element's state after that, not in the line that wrote the markup.

### Two inputs, one name

The Thumb holds a native `<input type="range">` for each axis. Both carry the name, which is the Thumb's `aria-label` followed by "X" or "Y" (`getAriaLabel(axis)` on the Thumb says it in your own words), and the form names `<name>.x` and `<name>.y`. Only the X input is in the tab order, and the arrow keys move both axes from it.

## Interaction

### Pointer

**Behavior**

| Gesture | Effect |
| --- | --- |
| Press / drag on pad | A press jumps the value to the pointer; a drag follows it. Reported as `eventDetails.reason` `"press"` then `"drag"`. |
| Clamping | Values always clamp to the configured axis ranges and snap to `stepX` / `stepY`. |
| Commit event | `onValueCommitted` fires when a gesture that changed the value ends. |
| Lagging value | While dragging, the cursor follows the pointer even if a controlled `value` updates late. Reject a value with `eventDetails.cancel()`. |

### Keyboard

On the focused pad:

**Keys**

| Key | Effect |
| --- | --- |
| `ArrowLeft` / `ArrowRight` | Decrease / increase X by `stepX` |
| `ArrowUp` / `ArrowDown` | Increase / decrease Y by `stepY` |
| `Shift` + arrow | Move by 10 steps |
| `PageUp` / `PageDown` | Increase / decrease Y by `stepY × 10` |
| `Home` / `End` | X to `minX` / `maxX` |

### Wheel

Wheel and trackpad input over the pad moves the point by the scrolled distance: scrolling up raises Y, scrolling right raises X. Reported as `eventDetails.reason === "wheel"`.

## API reference

**In React**

### Registry component

`XYPad` accepts style props (`size`, `formatValue`, `valueDisplay`, `className`) plus the props of the `XYPad` primitive's Root. `aria-label`, `aria-labelledby`, and `aria-describedby` are forwarded to the axis inputs.

#### Value and state

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `{ x: number; y: number }` |  | Controlled value. |
| `defaultValue` | `{ x: number; y: number }` | `center` | Uncontrolled starting value. Defaults to the center of the pad. |
| `minX / maxX` | `number` | `0 / 100` | X range. |
| `minY / maxY` | `number` | `0 / 100` | Y range. |
| `stepX / stepY` | `number` | `1` | Step per axis. |
| `disabled` | `boolean` | `false` | Disables pointer, wheel, and keyboard. |
| `format` | `Intl.NumberFormatOptions` |  | Formats each axis for `XYPad.Value` and the inputs' `aria-valuetext`. |
| `locale` | `Intl.LocalesArgument` | `runtime` | Locale used by `format`. |
| `onValueChange` | `(value, eventDetails) => void` |  | Fires on every change: press, drag, keyboard, wheel and input changes. |
| `onValueCommitted` | `(value, eventDetails) => void` |  | Fires when an interaction ends with a new value. |
| `aria-label` | `string` |  | Accessible name for the pad's inputs. |
| `aria-labelledby` | `string` |  | ID of a labeling element. |

#### Layout and display

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `size` | `"sm" \| "default" \| "lg" \| "xl"` | `"default"` | Size variant (affects height). |
| `formatValue` | `(value: { x: number; y: number }) => React.ReactNode` |  | Custom formatter for the value display. |
| `valueDisplay` | `"visible" \| "hidden"` | `"visible"` | Use `"hidden"` to hide the overlay. |
| `className` | `string` |  | Classes for the pad surface. |

The `XYPad` primitive that installs with it exposes `XYPad.Root`, `XYPad.Control` (the pad surface), `XYPad.Thumb` (positioned at the value, holds the axis inputs), `XYPad.Value` (formatted `<output>`), and `XYPad.Label`. Every part accepts a `render` prop, `className` / `style` as functions of state, and exposes `data-disabled` / `data-dragging`. Draw grids and crosshairs with your own elements and the `--xy-pad-x` / `--xy-pad-y` variables.

### Options

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

| Option | Type | Default | |
| --- | --- | --- | --- |
| `value`, `defaultValue` | `{ x, y }` | the center | The controlled point, or the starting one. `defaultValue` is also where a reset returns. |
| `minX`, `maxX`, `minY`, `maxY` | `number` | `0`, `100` | The range of each axis. |
| `stepX`, `stepY` | `number` | `1` | The grid each axis snaps to. |
| `format`, `locale` | `Intl.NumberFormat` options, a locale | | How each axis is written, for the Value and the inputs' `aria-valuetext`. |
| `disabled` | `boolean` | `false` | Takes no key, wheel or press. |
| `name` | `string` | | The form field's name. |
| `onValueChange` | `(point, details) => void` | | Every change, which `details.cancel()` refuses. |
| `onValueCommitted` | `(point, details) => void` | | The end of a gesture. |

The Value shows both axes as one text, with the separator of the locale. Its content can read `formatted.x`, `formatted.y` and `value` from the state.

### What a change reports

`details` carries what moved the point and how:

- `reason`: `"press"`, `"drag"`, `"keyboard"`, `"wheel"`, `"input-change"` (a typed value) or `"reset"`.
- `source`: `"mouse"`, `"touch"`, `"pen"`, `"keyboard"`, `"programmatic"` or `"agent"`.
- `event`: the DOM event behind it, when there is one.
- `cancel()`: refuses the change. A controlled pad keeps the point you give it.

In the web adapter, `onValueChange` is an `input` event and `onValueCommitted` a `change` event, both with `detail: { value, reason, source }`, where `value` is the point, and `event.preventDefault()` refuses a change.

### Parts

**In React, Vue and Svelte**

| Part | Element | Props |
| --- | --- | --- |
| `XYPad.Control` | `div` |  |
| `XYPad.Label` | `div` |  |
| `XYPad.Root` | `div` |  |
| `XYPad.Thumb` | `div` |  |
| `XYPad.Value` | `output` |  |

**In Web**

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

`audio-ui-xypad`, `audio-ui-xypad-control`, `audio-ui-xypad-input-x`, `audio-ui-xypad-input-y`, `audio-ui-xypad-label`, `audio-ui-xypad-thumb`, `audio-ui-xypad-value`

### Events

| React and Svelte | Vue | Web |
| --- | --- | --- |
| `onValueChange` | `@value-change` | `input` |
| `onValueCommitted` | `@value-committed` | `change` |

**In React**

## Examples

### Bipolar ranges (pan and mix style)

```tsx
import { useState } from "react";
import { XYPad } from "@/components/audio-ui/xypad";

const formatPanMix = (next: { x: number; y: number }) =>
  `Pan ${next.x} | Mix ${next.y.toFixed(2)}`;

export function XYPadBipolarRangeDemo() {
  const [value, setValue] = useState({ x: 0, y: 0 });

  return (
    <div className="flex flex-col items-center gap-2">
      <XYPad
        aria-label="Pan and mix"
        className="aspect-square"
        defaultValue={{ x: 0, y: 0 }}
        formatValue={formatPanMix}
        maxX={100}
        maxY={1}
        minX={-100}
        minY={-1}
        onValueChange={setValue}
        stepX={1}
        stepY={0.01}
        value={value}
      />
      <output
        aria-live="polite"
        className="font-mono text-foreground text-sm tabular-nums"
      >
        Pan: {value.x} - Mix: {value.y.toFixed(2)}
      </output>
    </div>
  );
}
```

### Pocket synth

An oscillator, a filter and an XY pad that plays them: drag the pad to sweep the sound.

```tsx
import { useCallback, useState } from "react";
import {
  type PocketSynthNote,
  type PocketSynthState,
  pocketSynthDevice,
} from "@/components/audio-ui/devices/pocket-synth/device";
import { createPocketSynthEngine } from "@/components/audio-ui/devices/pocket-synth/engine";
import { PocketSynthPanel } from "@/components/audio-ui/devices/pocket-synth/panel";
import { useDeviceEngines } from "@/hooks/use-device-engines";
import { useMasterBus } from "@/hooks/use-master-bus";

/**
 * A synth you can plug in: its state, a panel that draws and plays it and an
 * engine that makes the sound, each its own file and none knowing the others.
 * Press the pad and move: pitch goes across, the filter cutoff up.
 */
export function BlockPocketSynth() {
  const [state, setState] = useState<PocketSynthState>(() =>
    pocketSynthDevice.defaults()
  );
  const master = useMasterBus(1);
  const [engine] = useDeviceEngines({
    create: createPocketSynthEngine,
    destination: master,
    state,
  });

  const play = useCallback(
    (note: PocketSynthNote) => engine?.noteOn(note),
    [engine]
  );
  const release = useCallback(() => engine?.noteOff(), [engine]);

  return (
    <div className="rounded-xl h-full w-full min-w-0">
      <PocketSynthPanel
        onNoteOff={release}
        onNoteOn={play}
        onValueChange={setState}
        value={state}
      />
    </div>
  );
}
```

### Wave shaper

Distortion, tone and detune on a synth, with the XY pad and knobs side by side.

```tsx
import { useCallback, useState } from "react";
import {
  type WaveShaperNote,
  type WaveShaperState,
  waveShaperDevice,
} from "@/components/audio-ui/devices/wave-shaper/device";
import { createWaveShaperEngine } from "@/components/audio-ui/devices/wave-shaper/engine";
import { WaveShaperPanel } from "@/components/audio-ui/devices/wave-shaper/panel";
import { useDeviceEngines } from "@/hooks/use-device-engines";
import { useMasterBus } from "@/hooks/use-master-bus";

/**
 * A wave shaper synth you can plug in: its state, a panel that draws and plays
 * it and an engine that makes the sound, each its own file and none knowing
 * the others. Press the pad and move: pitch goes across, the resonance up.
 */
export function BlockWaveShaper() {
  const [state, setState] = useState<WaveShaperState>(() =>
    waveShaperDevice.defaults()
  );
  const master = useMasterBus(1);
  const [engine] = useDeviceEngines({
    create: createWaveShaperEngine,
    destination: master,
    state,
  });

  const play = useCallback(
    (note: WaveShaperNote) => engine?.noteOn(note),
    [engine]
  );
  const release = useCallback(() => engine?.noteOff(), [engine]);

  return (
    <div className="rounded-xl h-full w-full min-w-0">
      <WaveShaperPanel
        onNoteOff={release}
        onNoteOn={play}
        onValueChange={setState}
        value={state}
      />
    </div>
  );
}
```

## For an agent

The pad declares one action, `setValue(x, y)`, and the name on its Thumb is what an agent finds it by. The bridge of the framework you use (`createAgent` in `@ui-sdk/<framework>/agent`) sets the point with the source `"agent"`. Each axis is a change of its own, so `onValueChange` hears two, and an app can refuse either.
