# Knob

> A rotary control for continuous parameters, with pointer, wheel and keyboard input.

**In Vue**

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

const marks = [
  { label: "0", value: 0 },
  25,
  50,
  75,
  { label: "100", value: 100 },
];
</script>

<template>
  <Knob
    aria-label="Level"
    size="lg"
    :default-value="50"
    :marks="marks"
    :max="100"
    :min="0"
    :step="1"
  />
</template>
```

**In Svelte**

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

const marks = [
  { label: "0", value: 0 },
  25,
  50,
  75,
  { label: "100", value: 100 },
];
</script>

<Knob
  aria-label="Level"
  defaultValue={50}
  {marks}
  max={100}
  min={0}
  size="lg"
  step={1}
/>
```

**In Web**

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

const NEEDLE = `<svg aria-hidden="true" data-slot="knob-needle" viewBox="0 0 48 48"><line stroke="currentColor" stroke-linecap="round" stroke-width="2.75" x1="24" x2="24" y1="11.53" y2="18.84" /></svg>`;

/** A graduation around the dial, with its label when it has one. */
const mark = (value, label) => `
      <audio-ui-knob-mark value="${value}">
        <span data-slot="knob-mark-tick"></span>${label === undefined ? "" : `<span data-slot="knob-mark-label">${label}</span>`}
      </audio-ui-knob-mark>`;

/** Draws the knob into `element`, and returns what takes it away. Its look is the knob's sheet, on its parts and attributes. */
export function knobWebDemo(element) {
  element.innerHTML = `
    <audio-ui-knob data-marks="labels" data-readout="auto" data-size="lg" max="100" min="0" step="1" value="50">
      <audio-ui-knob-control>
        <div data-slot="knob-body"></div>
        <audio-ui-knob-track></audio-ui-knob-track>
        <audio-ui-knob-indicator></audio-ui-knob-indicator>
        <audio-ui-knob-thumb aria-label="Level">${NEEDLE}</audio-ui-knob-thumb>
      </audio-ui-knob-control>${mark(0, "0")}${mark(25)}${mark(50)}${mark(75)}${mark(100, "100")}
      <audio-ui-knob-value></audio-ui-knob-value>
    </audio-ui-knob>`;
  return () => {
    element.innerHTML = "";
  };
}
```

**In React**

```tsx
import { Knob } from "@/components/audio-ui/knob";

const MARKS = [
  { label: "0", value: 0 },
  25,
  50,
  75,
  { label: "100", value: 100 },
];

export function KnobDemo() {
  return (
    <Knob
      aria-label="Level"
      defaultValue={50}
      editableValue
      marks={MARKS}
      max={100}
      min={0}
      size="lg"
      step={1}
    />
  );
}
```

A rotary control for continuous parameters such as level, cutoff or pan. The registry component is written on the knob primitive, which installs with it, and adds size variants and theme styles.

**In Vue, Svelte and Web**

The knob's behavior is one file, the primitive, written on the UI SDK kernel and copied into your project. Each framework renders it with a thin adapter that you also own: a few parts, about 120 to 150 lines. The drag, the wheel, the keys, the scale and the reset are the same in all four, and one suite of tests runs against each adapter.

## Overview

- **Input:** drag around the knob (`dragMode="circular"`, the default), up and down (`"vertical"`), or by full turns (`"revolution"`). The mouse wheel and the keyboard also work. A double-click or a double-tap returns to `defaultValue`.
- **Display:** a track, an arc from `anchor` to the value, and a needle that turns with the value. The needle follows the pointer without easing, so it stays in line with the arc.
- **Accessibility:** the knob holds a native `<input type="range">` that takes focus and carries the accessible name (`aria-label` or `aria-labelledby`), the value text (formatted with `format`, or set with `getAriaValueText` on the primitive's `Thumb`) and the form value (`name`).

## 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**

**CLI**

The styled component, with size variants and theme styles:

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

Or the primitive alone, to style yourself:

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

Or the primitive alone, to style yourself:

```bash
npx @audio-ui/cli add knob --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: `knob.ts`, the knob'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="knob-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="knob-react" from={2} />

**Vue**

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

**JavaScript**

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

<ItemFiles name="knob-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="knob" />

**In Vue, Svelte and Web**

### 5. The styled component

Optional: the knob 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 knob
```

By hand:

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

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

**Vue**

One component over the adapter, with the props of the React knob, `editableValue` included.

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

**Svelte**

One component over the adapter, with the props of the React knob, `editableValue` included.

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

**JavaScript**

The custom elements take the same sheet. Write the parts the example above writes, and set `data-size`, `data-marks` (`ticks` or `labels`) and `data-readout` (`auto` or `always`) on `audio-ui-knob`, as the styled component does.

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

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

```tsx
<Knob defaultValue={50} max={100} min={0} step={1} />
```

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

```tsx
const [cutoff, setCutoff] = useState(1000);

<Knob
  max={20_000}
  min={20}
  onValueChange={setCutoff}
  step={1}
  value={cutoff}
/>
```

Pass `defaultValue` even when the knob is controlled if you use the double-tap reset: it sets the value the knob returns to, clamped and stepped.

**In React**

The primitive alone, part by part:

The parts are the same everywhere: a Root that owns the value, a Control that takes the pointer and the wheel, a Track and an Indicator that draw the arcs, a Thumb that holds the focusable input, and a Value for the text. A part takes the classes and attributes of its element, and a state attribute (`data-dragging`, `data-disabled`, `data-drag-mode`) on each part to style from.

**React**

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

export function Cutoff() {
  const [value, setValue] = useState(40);
  return (
    <Knob.Root
      format={{ style: "unit", unit: "percent" }}
      onValueChange={setValue}
      value={value}
    >
      <Knob.Control className="relative size-24">
        <Knob.Track className="absolute inset-0 text-muted" />
        <Knob.Indicator className="absolute inset-0 text-primary" />
        <Knob.Thumb aria-label="Cutoff" className="absolute inset-3 rounded-full bg-card" />
      </Knob.Control>
      <Knob.Value />
    </Knob.Root>
  );
}
```

**Vue**

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

const value = ref(40);
</script>

<template>
  <Knob.Root
    :format="{ style: 'unit', unit: 'percent' }"
    :value="value"
    @value-change="(next: number) => (value = next)"
  >
    <Knob.Control class="relative size-24">
      <Knob.Track class="absolute inset-0 text-muted" />
      <Knob.Indicator class="absolute inset-0 text-primary" />
      <Knob.Thumb aria-label="Cutoff" class="absolute inset-3 rounded-full bg-card" />
    </Knob.Control>
    <Knob.Value />
  </Knob.Root>
</template>
```

**Svelte**

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

  let value = $state(40);
</script>

<Knob.Root
  format={{ style: "unit", unit: "percent" }}
  {value}
  onValueChange={(next: number) => (value = next)}
>
  <Knob.Control class="relative size-24">
    <Knob.Track class="absolute inset-0 text-muted" />
    <Knob.Indicator class="absolute inset-0 text-primary" />
    <Knob.Thumb aria-label="Cutoff" class="absolute inset-3 rounded-full bg-card" />
  </Knob.Control>
  <Knob.Value />
</Knob.Root>
```

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

**JavaScript**

```html
<audio-ui-knob value="40" format='{"style":"unit","unit":"percent"}'>
  <audio-ui-knob-control>
    <audio-ui-knob-track></audio-ui-knob-track>
    <audio-ui-knob-indicator></audio-ui-knob-indicator>
    <audio-ui-knob-thumb aria-label="Cutoff"></audio-ui-knob-thumb>
  </audio-ui-knob-control>
  <audio-ui-knob-value></audio-ui-knob-value>
</audio-ui-knob>

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

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

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.

## Interaction

### Pointer

**Behavior**

| Gesture | Effect |
| --- | --- |
| Circular drag (default) | The value follows the rotation around the center, from the value at pointer down. The dial's 288° sweep spans `min…max`. |
| Vertical drag | `dragMode="vertical"`: drag up to increase, like a fader; 150px of travel spans `min…max`. |
| Revolution drag | `dragMode="revolution"`: one full 360° turn spans `min…max`. |
| Fine drag | Hold `Shift` while dragging, in any mode, to turn four times slower. Only the stretch moved with `Shift` held is slowed, and small moves add up rather than rounding away. |
| Double-tap / double-click | Two quick taps (each moving ≤ ~12px, within ~320ms) reset to `defaultValue`, or to the middle of the dial's travel without one. Reports `eventDetails.reason === "reset"`. |

Values stay within `min…max` and do not wrap. While dragging, the knob follows the pointer even if a controlled `value` updates late.

### Scale

`scale` sets how the value maps onto the dial's travel. `"linear"` (the default) suits levels and times; `"log"` suits frequency, resonance and ratios, which are heard as proportions: on a 20 Hz–20 kHz cutoff, every decade gets the same share of the dial, and 632 Hz sits at the middle. A log scale needs a positive `min`. For anything else, pass your own `{ to, from }`, mapping a value into an even space and back.

```tsx
<Knob aria-label="Cutoff" max={20_000} min={20} scale="log" />
```

Away from linear, the keys and the wheel move a share of the travel (1%, or 10% with `Shift` and the Page keys) rather than a `step`, and always by at least one `step`.

### Keyboard

On the focused knob:

**Keys**

| Key | Effect |
| --- | --- |
| `ArrowUp` / `ArrowRight` | Increase by `step` |
| `ArrowDown` / `ArrowLeft` | Decrease by `step` |
| `PageUp` / `Shift` + `ArrowUp` | Increase by `largeStep` (default `10`) |
| `PageDown` / `Shift` + `ArrowDown` | Decrease by `largeStep` (default `10`) |
| `Home` | Set to `min` |
| `End` | Set to `max` |

On an editable readout, given `editable` on `Value` (the `editable` attribute on `audio-ui-knob-value` in plain HTML, `editableValue` on the styled knob), in every framework:

**Editable readout**

| Gesture | Effect |
| --- | --- |
| Double click / `Enter` / `F2` | Lays a text field over the readout, its text selected |
| `Enter` / leaving the field | Sets the number typed, as a change of the knob |
| `Escape` | Keeps the value |

The readout carries `data-editable`, and `data-editing` while the field is open.

### Wheel

The mouse wheel over the knob moves the value by `step` and reports `eventDetails.reason === "wheel"`.

## API reference

**In React**

### Registry component

`Knob` accepts `size` and `className` plus the props of the `Knob` primitive's Root. `aria-label`, `aria-labelledby`, and `aria-describedby` are forwarded to the knob's input.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `number` |  | Where the mark sits, on the knob's scale. |
| `defaultValue` | `number` | `middle of the travel` | Uncontrolled starting value, and the reset target of a double tap, an Alt + click and the Delete key. Defaults to the middle of the dial's travel. |
| `min` | `number` | `0` | Minimum. |
| `max` | `number` | `100` | Maximum. |
| `step` | `number` | `1` | Quantization step (drag, wheel, keyboard, reset). |
| `largeStep` | `number` | `10` | Step for Page Up/Page Down and Shift + Arrow keys. |
| `anchor` | `number` | `min` | Value the indicator arc grows from (e.g. `0` on a -1…1 pan knob). Defaults to `min`. |
| `dragMode` | `"circular" \| "vertical" \| "revolution"` | `"circular"` | How pointer travel turns into value. |
| `scale` | `"linear" \| "log" \| { to, from }` | `"linear"` | How the value maps onto the dial's travel. |
| `marks` | `(number \| { value, label? })[]` |  | Graduations drawn around the dial, on its scale. Those the value has reached from `anchor` light up. |
| `valueLabel` | `"auto" \| "always" \| "hidden"` | `"auto"` | When the value shows under the dial: `auto` while hovered, turned or focused from the keyboard. |
| `formatValue` | `(value: number) => string` | `format` | How the value prints in that readout and in what screen readers announce, e.g. `"8.7 kHz"`. |
| `editableValue` | `boolean` | `false` | Lets you type the value in the readout: a double click, or Enter while it has focus. |
| `disabled` | `boolean` | `false` | Disables pointer, wheel, and keyboard. |
| `format` | `Intl.NumberFormatOptions` |  | Formats the value for `Knob.Value` and the input's `aria-valuetext`. |
| `locale` | `Intl.LocalesArgument` | `runtime` | Locale used by `format`. |
| `name` | `string` |  | Identifies the field when a form is submitted. |
| `onValueChange` | `(value: number, eventDetails) => void` |  | Fires on every change: drag, keyboard, wheel, reset and input changes. |
| `onValueCommitted` | `(value: number, eventDetails) => void` |  | Fires when an interaction ends with a new value. |
| `size` | `"xs" \| "sm" \| "default" \| "lg" \| "xl"` | `"default"` | Diameter variant. |
| `className` | `string` |  | On the root wrapper. |

The knob primitive that installs with it exposes `Knob.Root`, `Knob.Control` (pointer, wheel, reset), `Knob.Track` and `Knob.Indicator` (SVG arcs; `strokeWidth` in the 48×48 viewBox), `Knob.Thumb` (rotates with the value and holds the input), `Knob.Value` (formatted `<output>`), `Knob.Provider` (the default `dragMode` of the knobs below it), `Knob.Mark` (a graduation at a `value`, rotated onto the dial's scale, with `data-active` once reached), and `Knob.Label`. Every part accepts a `render` prop, `className` / `style` as functions of state, and exposes `data-disabled` / `data-dragging`.

### Options

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

| Option | Type | Default | |
| --- | --- | --- | --- |
| `value`, `defaultValue` | `number` | the midpoint | The controlled value, or the starting one. `defaultValue` is also where a reset returns. |
| `min`, `max`, `step` | `number` | `0`, `100`, `1` | The range and the grid the value snaps to. |
| `largeStep` | `number` | | What Page Up, Page Down and Shift with an arrow move by. |
| `anchor` | `number` | | Where the Indicator's arc starts, like `0` for a pan. |
| `scale` | `"linear"`, `"log"` or `{ to, from }` | `"linear"` | How the value maps onto the dial. |
| `dragMode` | `"circular"`, `"revolution"`, `"vertical"` | `"circular"` | How pointer travel turns into value. |
| `format`, `locale` | `Intl.NumberFormat` options, a locale | | The Value's text and the input's `aria-valuetext`. |
| `disabled` | `boolean` | `false` | Takes no key, wheel or drag. |
| `name` | `string` | | The form field's name, carried by the input. |
| `onValueChange` | `(value, details) => void` | | Every change, which `details.cancel()` refuses. |
| `onValueCommitted` | `(value, details) => void` | | The end of a gesture. |

### What a change reports

`details` carries what moved the value and how:

- `reason`: `"drag"`, `"wheel"`, `"keyboard"` or `"reset"`.
- `source`: `"mouse"`, `"touch"`, `"pen"`, `"keyboard"`, `"programmatic"` or `"agent"`, for analytics and for telling a person from an agent. It is never a reason to behave differently.
- `event`: the DOM event behind it, when there is one.
- `cancel()`: refuses the change. A controlled knob keeps the value you give it.

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

### Parts

**In React, Vue and Svelte**

| Part | Element | Props |
| --- | --- | --- |
| `Knob.Control` | `div` |  |
| `Knob.Indicator` | `svg` | `strokeWidth: number` |
| `Knob.Label` | `div` |  |
| `Knob.Mark` | `div` | `value: number` |
| `Knob.Provider` | provider of default options |  |
| `Knob.Root` | `div` |  |
| `Knob.Thumb` | `div` |  |
| `Knob.Track` | `svg` | `strokeWidth: number` |
| `Knob.Value` | `output` |  |

**In Web**

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

`audio-ui-knob`, `audio-ui-knob-control`, `audio-ui-knob-indicator`, `audio-ui-knob-input`, `audio-ui-knob-label`, `audio-ui-knob-mark`, `audio-ui-knob-thumb`, `audio-ui-knob-track`, `audio-ui-knob-value`

### Events

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

**In React**

## Examples

### Rail, anchor, and range highlight

Use `anchor` to show a reference point on the rail (for example the center of a bipolar control). The arc highlights the interval between `anchor` and the current value.

### Logarithmic scale

`scale="log"` spreads a frequency evenly by ratio, so a quarter turn goes from 1 kHz to 8.7 kHz instead of packing the useful range into the first few degrees.

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

const formatHertz = (hertz: number) =>
  hertz >= 1000 ? `${(hertz / 1000).toFixed(1)} kHz` : `${hertz} Hz`;

export function KnobFilterControlDemo() {
  const [cutoff, setCutoff] = useState(1000);

  return (
    <div className="flex flex-col items-center gap-2">
      <Knob
        aria-label="Cutoff"
        max={20_000}
        min={20}
        onValueChange={setCutoff}
        scale="log"
        step={1}
        value={cutoff}
        valueLabel="hidden"
      />
      <output className="text-sm tabular-nums">{formatHertz(cutoff)}</output>
    </div>
  );
}
```

### Vertical drag

`dragMode="vertical"` turns the knob like a fader: drag up to increase, down to decrease, with 150px of travel for the whole range. It suits a small knob, or one next to others, where going around it would be fiddly.

To turn every knob of an app that way, wrap it once in `Knob.Provider`: each `Knob.Root` below it takes that mode, unless it sets its own `dragMode`.

```tsx
import { Knob } from "@/components/audio-ui/knob/react"

<Knob.Provider dragMode="vertical">
  <Mixer />
</Knob.Provider>
```

## For an agent

The knob declares one action, `setValue`, 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`) reads its value and range, sets it with the source `"agent"`, and the same `onValueChange` hears it, so an app can refuse a value as it would a person's.
