Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Circular Slider for Svelte

A round knob that fills a ring as it turns, with the value in its center and optional ticks, returning 0 to 359 degrees. For Svelte and Ark UI.

On this page9 sections
UI library: Ark UI

A circular slider is a knob: a ring that fills clockwise from the top as its thumb turns, with room in the middle for the value, a unit or an icon. It returns an angle from 0 to 359 degrees, which you map to your own scale, such as a percentage for a volume knob.

For a bare dial with a needle and tick marks, use the Angle Slider. A value people compare against a straight scale reads better on a Slider.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Circular Slider anatomy: root, track, thumb, value

Usage

<script lang="ts">
import {
  CircularSlider, CircularSliderValue,
} from "@/components/ui/circular-slider";
</script>

CircularSlider draws the ring, the thumb and any ticks itself. What you put inside it, here the value with a degree sign, sits in the center.

<CircularSlider aria-label="Angle" defaultValue={45}>
  <CircularSliderValue suffix="°" />
</CircularSlider>

Accessibility

The thumb has the slider role, takes focus with Tab and reports the angle as its value, and invalid as aria-invalid. Name it with a CircularSliderLabel inside the knob, or with aria-label on CircularSlider when no label shows: without either, a screen reader announces a nameless slider. The value is read as a bare number of degrees, whatever scale you map it to, so put the unit in the name, such as Hue in degrees. The ring is drawn for sight only and is hidden from assistive technology.

Arrow Right
Increments the angle slider based on defined step.
Arrow Left
Decrements the angle slider based on defined step.
Arrow Up
Decreases the value by the step amount.
Arrow Down
Increases the value by the step amount.
Shift+Arrow Up
Decreases the value by a larger step.
Shift+Arrow Down
Increases the value by a larger step.
Home
Sets the value to 0 degrees.
End
Sets the value to 359 degrees, the largest angle the knob holds.
Shift+Arrow Right
Increases the value by a larger step, landing on a multiple of ten steps.
Shift+Arrow Left
Decreases the value by a larger step, landing on a multiple of ten steps.

Demos

Context

CircularSliderContext hands its child the slider's state, and this heading knob turns slider.value into a compass point, so the center reads NE 45° and changes as the thumb turns. Reach for the context when the center shows something calculated from the angle, such as a direction, a percentage or a time of day, and keep prefix and suffix for a plain unit.

Preview width

Controlled

The angle starts at 45 in state, goes in as value and comes back through onValueChange, and a line under the ring prints it with a degree sign. Hold the angle yourself when a caption, a reset button or a save handler reads it. When nothing reads it until the form is sent, defaultValue and a name are enough.

Preview width

Custom markers

markers set to [0, 90, 180, 270] draws four ticks, at the top, right, bottom and left, and each takes the primary color once the ring reaches it. Use a list when only a few angles mean something, such as the points of a compass. The thumb still moves by step, one degree here, so it doesn't stop on the ticks.

Preview width

Disabled

disabled fades the knob at 45 degrees, ignores the pointer and takes the thumb out of the tab order. Use it when a preset owns the value or the setting isn't available yet, and the angle is still worth showing. For a value that is wrong and must be fixed, keep the knob enabled and mark it invalid instead.

Preview width

Invalid

invalid turns the filled arc and the thumb's outline to the destructive color and marks the thumb with aria-invalid, so a screen reader says the value is invalid. Use it when the angle fails a rule the form checks, and keep a text message beside the knob, since a color alone doesn't say what is wrong.

Preview width

Read only

readOnly keeps the knob at 90 degrees and refuses drags and keys, while the thumb stays in the tab order so people can still focus it and hear the value. Use it for a confirmed setting people should be able to inspect. It looks like an editable knob, so say nearby that it can't be changed, or use disabled when it should fade.

Preview width

Root provider

useCircularSlider creates the state outside the knob, starting at 45 and named Angle through its aria-label option, and CircularSliderRootProvider hands it to the knob. The line above prints slider.value, and the Set to 90 button calls setValue. Use it when a preset or a neighbouring control must move the knob or read its value.

Preview width

Size

size is the diameter in pixels, 180 here against the default of 100. The ring grows with it while the thumb keeps its size, which follows thickness. Use a large knob when it is the main control on the screen, such as a volume knob or a thermostat, rather than one field among many in a form.

Preview width

Step

step is 60, so dragging and the arrow keys move in 60 degree jumps from a start of 120, and markers with markersAtSteps puts a tick on each stop from 0 to 300. A drag past 300 rounds up to 360 and fills the ring, which the keys never do. Use the pair for a coarse choice, such as one of six presets.

Preview width

Thickness

thickness is the ring's stroke in pixels, 14 here against the default of 6, and the thumb grows with it, as ticks would, while the diameter stays at size. Use a heavy ring when the knob is small and a thin track would vanish, or when the fill itself is what people watch.

Preview width

With label

CircularSliderLabel puts Hue in the middle of the knob and names the thumb, so a screen reader says Hue and the angle. Use a visible label when nothing else on the screen says what the knob sets. When the word would crowd the center, pass aria-label to CircularSlider instead, as most demos here do.

Preview width

With markers

markers set to true draws six ticks, one every 60 degrees from the top, with no list of angles to write, and the ticks the ring has reached take the primary color. Use them for a sense of position on a full turn, such as a heading or a timer. They don't make the thumb stop on them, since step decides that.

Preview width

With value

CircularSliderValue fills the center with a thermometer icon from prefix, the angle and a degree sign from suffix, on a 120 pixel knob with a 10 pixel ring. The icon is hidden from screen readers, since the knob's aria-label already says Temperature. Use prefix and suffix for a plain unit, and the context for a calculated one.

Preview width

FAQ

What value does a Circular Slider return?

An angle in whole degrees, from 0 at the top, clockwise to 359, moving by step, which is 1 by default. The ring fills in proportion to it. With a coarser step, a drag rounds up to the next step clockwise, so past the last one it reaches 360 and fills the ring, a value the keys never set.

Map it to your own scale where you read it. For a volume knob from 0 to 100, divide by 3.6 and round, which reaches 100 at 359. For a scale that doesn't start at zero, add its minimum after scaling.

How do I control the Circular Slider value in Svelte?

Hold the angle in state, pass it as value and update it in onValueChange, which receives an object, so read the new angle from details.value. The Controlled demo does exactly this.

For a starting angle without state of your own, set defaultValue. To move the knob from a button elsewhere on the page, the Root provider demo's setValue is the other way in.

How do I run code only when the user releases the Circular Slider?

Use onValueChangeEnd. It fires once when the pointer is released and once after each key press, while onValueChange fires on every move of a drag.

That is the place to save the value or call an API, so a request doesn't go out for every degree the knob passes on its way round.

How do I add tick marks to a Circular Slider?

Set markers to true for six ticks, one every 60 degrees, or pass an array of angles for your own, as the With markers and Custom markers demos do. With markers set to true and markersAtSteps, a tick goes on every step instead.

Ticks show positions and never make the thumb stop on them. Set step to the gap between ticks when it should.

Can a Circular Slider be used with the keyboard?

Yes. The thumb takes focus with Tab, the arrow keys move it by step, Shift with an arrow by a larger jump, and Home and End go to 0 and 359. The keys stop at both ends rather than wrapping round.

Give the knob a name with CircularSliderLabel or aria-label, and put the unit in it, since a screen reader hears the value as a bare number. The Accessibility section lists every key.

Does the Circular Slider submit with a form?

Yes. The knob always renders a hidden input, so give CircularSlider a name, and the angle submits with the form as a number of degrees.

A knob with disabled disables that input too, so its angle stays out of the form data, as a native disabled field's would. One with readOnly still sends its value, which suits a confirmed setting the form has to carry.

How do I change the size of a Circular Slider?

Set size for the knob's diameter and thickness for its ring, both in pixels, 100 and 6 by default, as the Size and Thickness demos show. The thumb follows thickness, so a bigger knob with a thin ring keeps a small thumb.

size sets the knob's width and height directly, so a class can't make it fluid. For a knob that scales with its container, measure the container and pass the number.

How do I change the colors of a Circular Slider?

Edit components/ui/circular-slider, which is your code once installed. The ring is two circles in CircularSliderProgressRing, the track with stroke-muted and the fill with stroke-primary, and the thumb is a white dot with a ring-border outline in CircularSliderThumb.

With invalid, the fill and the thumb's outline switch to stroke-destructive and ring-destructive through in-data-invalid: classes on the same two parts, so change those classes for a different error color.

API Reference

CircularSlider

<div> data-slot="circular-slider"

PropType
value?number

The value of the slider.

defaultValue?number

The initial value of the slider. Use when you don't need to control the value of the slider.

Default
0
onValueChange?(details: ValueChangeDetails) → void

The callback function for when the value changes.

Type
(details: ValueChangeDetails) → void
Parameters

details – { value: number; valueAsDegree: string }

Returns
void
dir?"ltr" | "rtl"

The document's text/writing direction.

Type
"ltr" | "rtl"
Default
"ltr"
disabled?boolean

Whether the slider is disabled.

id?string

The unique identifier of the machine.

ids?object

The ids of the elements in the machine. Useful for composition.

Type
{ root?: string; thumb?: string; hiddenInput?: string; control?: string; valueText?: string; label?: string }
invalid?boolean

Whether the slider is invalid.

markers?boolean | number[]

Tick marks. true uses clock ticks (every 60°). A number array is custom angles.

Type
boolean | number[]
markersAtSteps?boolean

When markers is true, place a tick at every step instead of clock ticks.

Default
false
name?string

The name of the slider. Useful for form submission.

onValueChangeEnd?(details: ValueChangeDetails) → void

The callback function for when the value changes ends.

Type
(details: ValueChangeDetails) → void
Parameters

details – { value: number; valueAsDegree: string }

Returns
void
readOnly?boolean

Whether the slider is read-only.

size?number

Diameter of the ring, in pixels.

Default
100
step?number

The step value for the slider.

Default
1
thickness?number

Stroke width of the ring, in pixels.

Default
6
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled.

data-invalid?string

Present when invalid.

data-readonly?string

Present when read-only.

CSS variableType
--anglestring

The angle in degrees.

--valuestring

The current value.

CircularSliderLabel

<label> data-slot="circular-slider-label"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled.

data-invalid?string

Present when invalid.

data-readonly?string

Present when read-only.

CircularSliderThumb

<div> data-slot="circular-slider-thumb"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled.

data-invalid?string

Present when invalid.

data-readonly?string

Present when read-only.

CSS variableType
--sizestring

CircularSliderValue

<span> data-slot="circular-slider-value"

PropType
prefix?Snippet

Shown before the value, such as an icon.

suffix?Snippet

Shown after the value, such as a unit (°).

asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

CircularSliderMarkerGroup

<div> data-slot="circular-slider-marker-group"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

CircularSliderMarker

<span> data-slot="circular-slider-marker"

PropType
valuenumber

The value of the marker.

asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled.

data-state"under-value" | "over-value" | "at-value"

"under-value", "over-value" or "at-value".

Type
"under-value" | "over-value" | "at-value"
data-valuestring

The value of the item.

CSS variableType
--marker-display-valuestring

The rotation angle for display (mirrored in RTL).

--marker-heightstring
--marker-offsetstring
--marker-valuestring

The logical marker value (e.g. 0, 45, 90).

--marker-widthstring

CircularSliderRootProvider

<svg> data-slot="circular-slider"

PropType
valueUseAngleSliderReturn

What useCircularSlider() returns: the state and API, made outside the component to control it.

markers?boolean | number[]

Tick marks. true uses clock ticks (every 60°). A number array is custom angles.

Type
boolean | number[]
markersAtSteps?boolean

When markers is true, place a tick at every step instead of clock ticks.

Default
false
size?number

Diameter of the ring, in pixels.

Default
100
step?number

Used with markersAtSteps to place ticks. Should match the machine step. * * @default 1.

Default
1
thickness?number

Stroke width of the ring, in pixels.

Default
6
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled is true.

data-invalid?string

Present when invalid is true.

data-readonly?string

Present when readOnly is true.

CSS variableType
--anglestring
--valuestring

useCircularSlider

CircularSliderContext