audiocn-svelte
Esc

Start typing to search the docs

Knob

A rotary control for dense layouts, drawn in SVG.

5 min read · 909 words

0 dB Gain
C Pan
120 Low cut

Installation

$ pnpm dlx shadcn-svelte@latest add https://juddisjudd.github.io/audiocn-svelte/r/knob.json

Usage

<script lang="ts">
	import { Knob } from "#lib/components/ui/knob/index.js";

	let gain = $state(0);
</script>

<Knob min={-24} max={24} origin={0} bind:value={gain} />

Drag the dial up to increase the value and down to decrease it. Set dragDirection="circular" to turn it by circling around the dial, clockwise to increase and anticlockwise to decrease.

ActionResult
DragTurns the knob
Shift+drag, Shift+wheelTen times finer, for precise adjustment. Press or release Shift mid-drag without a jump
Double-click the dial, Alt+clickResets to resetValue
Double-click the value or label, Enter on the dialType a value; Enter applies it, Escape cancels

The value keeps one width across the whole range, so the layout never moves while you turn it.

Anatomy

<Knob>
	<KnobDial>
		<KnobTrack />
		<KnobRange />
		<KnobPointer />
	</KnobDial>
	<KnobValue />
	<KnobLabel />
</Knob>

KnobTrack, KnobRange and KnobPointer are SVG elements inside a 100 × 100 view box, so you can add your own shapes next to them. KnobScale and KnobCap are another set of parts for the same dial, shown in the volume dial and the metal knobs.

Examples

Drag directions

Choose dragDirection to match how you want to move the dial:

  • vertical, the default: drag up to increase the value, down to decrease it. The cursor shows an up/down arrow while dragging.
  • horizontal: drag right to increase the value, left to decrease it. The cursor shows a left/right arrow while dragging.
  • circular: turn clockwise to increase the value, anticlockwise to decrease it.

Hold Shift for finer control in all three modes. For vertical and horizontal drags, sensitivity sets the distance in pixels for the full range. The default is 200.

0.0 dB Vertical
0.0 dB Horizontal
0.0 dB Circular

Sizes and disabled

sm
default
lg
disabled

Volume dial

A hi-fi volume dial: KnobScale draws numbered ticks around the dial and KnobCap a brushed aluminium cap with an indicator dot. Give it room for the numbers with a larger --knob-size. clickSound adds a soft tick on each long graduation.

Volume

Ticks between origin and the value light up, so a bipolar knob lights from its centre. The ticks are spread evenly along the arc, so they follow scale="log" too.

Metal knobs

The same aluminium at everyday sizes: swap KnobPointer for <KnobCap variant="mini" />. The mini cap fills the inside of KnobTrack and marks the value with an engraved line, which still reads at size="sm".

0 dB Gain
C Pan
120 Low cut
65% Mix
<KnobDial>
	<KnobTrack />
	<KnobRange />
	<KnobCap variant="mini" />
</KnobDial>

Theming

Variable or attributeMeaning
--knob-sizeSet by size
--knob-angleThe pointer angle, live, for custom skins
data-draggingOn the dial while dragging
data-at-originThe value sits at origin
--knob-cap-metalKnobCap's metal, white by default
--knob-cap-shadeKnobCap's shading, black by default
--knob-cap-pitchSpacing of KnobCap's brushed rings
data-activeOn KnobScale ticks inside the lit range
data-majorOn KnobScale's long ticks

Style the SVG parts with stroke-* and fill-* classes. Restyle the lit ticks from the scale, for example <KnobScale class="**:data-active:stroke-primary" />.

Accessibility

The dial is a focusable slider with aria-valuetext from format, named by KnobLabel. It supports the same keys as the fader, and Enter opens the value editor.

API reference

PropTypeDefault
value

Bindable.

numberresetValue ?? min
onValueChange

details.reason: drag, keyboard, wheel, reset or input.

(value, details) => void—
onValueCommit (value) => void—
min / max number0 / 100
step / largeStep / fineStep

fineStep applies to Shift+drag, Shift+wheel and Alt+arrow.

number1 / 10 / step ÷ 10
resetValue

Double-click or Alt+click restores it.

numberthe first value, or min
origin

Where the arc starts; the centre for bipolar knobs.

numbermin
arc

Sweep in degrees.

number270
dragDirection

Circular turns the knob as you circle around it. Vertical and horizontal drag in a straight line, as in most DAWs.

"vertical" | "horizontal" | "circular""vertical"
sensitivity

Pixels of vertical or horizontal drag for the full range.

number200
scale "linear" | "log""linear"
allowWheel booleanfalse
clickSound

Plays a soft tick on each graduation: KnobScale's long ticks, or every largeStep without a scale. At most every 30 ms, through the shared AudioContext.

booleanfalse
format (value: number) => stringString
parse

Reads a typed value. The default takes the first number and reads "k" as thousands. Use parsePan for pan knobs.

(text: string) => number | nullparseKnobValue
size "sm" | "default" | "lg""default"
disabled booleanfalse

KnobValue

PropTypeDefault
editable

Double-click, or Enter on the dial, to type a value.

booleantrue

KnobScale

PropTypeDefault
ticks

Divisions across the arc; draws ticks + 1 marks.

number50
majorEvery

Every nth tick is long.

number5
labelEvery

Every nth tick is numbered. 0 hides the numbers.

number10
format (value: number) => stringthe knob's format

KnobCap

PropTypeDefault
variant

default sits inside KnobScale with an indicator dot. mini fills the inside of KnobTrack with an engraved line, for small knobs.

"default" | "mini""default"
Last updated on October 7, 2026