Skip to content

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

Components

Free

Angle Slider for Svelte

A round dial with one thumb for an angle from 0 to 359 degrees, like a rotation, a compass heading or a gradient's direction, for Svelte and Ark UI.

On this page9 sections
UI library: Ark UI

An angle slider is a round dial with one thumb that sets an angle in degrees, from 0 at the top, clockwise to 359: the rotation of an image or a layer, a compass heading, the direction of a gradient. Drag the thumb, or press anywhere on the dial to move it there.

For a value on a straight scale, such as volume or a price, use a Slider. A Circular Slider is a round track with a start and an end, for a timer or a gauge.

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

Angle Slider anatomy: label, control, marker, thumb, value text

Usage

<script lang="ts">
import {
  AngleSlider, AngleSliderControl, AngleSliderLabel, AngleSliderMarker,
  AngleSliderMarkerGroup, AngleSliderThumb, AngleSliderValueText,
} from "@/components/ui/angle-slider";
</script>

AngleSliderLabel names the thumb for screen readers, the markers draw ticks at the angles you list, and AngleSliderValueText prints the angle, such as 45deg, unless you give it children of your own.

const MARKERS = [0, 45, 90, 135, 180, 225, 270, 315];

const Example = () => (
  <AngleSlider>
    <AngleSliderLabel>Rotation</AngleSliderLabel>
    <AngleSliderControl>
      <AngleSliderMarkerGroup>
        {MARKERS.map((value) => (
          <AngleSliderMarker key={value} value={value} />
        ))}
      </AngleSliderMarkerGroup>
      <AngleSliderThumb />
    </AngleSliderControl>
    <AngleSliderValueText />
  </AngleSlider>
);

Accessibility

The thumb has the slider role, takes focus with Tab and reports its angle as its value, named by AngleSliderLabel, or by aria-label on AngleSlider when no label shows. Clicking the label moves focus to the thumb. The thumb's value text adds a degree sign to the angle, so a screen reader reads 45° with its unit rather than a bare 45. The arrow keys stop at 0 and 359 instead of wrapping round, and only a drag crosses the top.

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 dial 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

The readout under the dial reads the angle from AngleSliderContext, which passes the slider's state to whatever it renders, so it shows 0° and follows the thumb with no state of your own. The label stays Rotation, since it names the thumb. Use the context when a part inside the slider needs the value, or to print the angle in your own format instead of the 0deg of AngleSliderValueText.

Preview width

Controlled

The angle starts at 45 in state: value passes it in, and onValueChange writes each new angle back from details.value. Control the dial when something else shows or uses the same angle, such as a preview that turns with it, or when a change may be refused. When only the final angle matters, wait for the end of the drag instead.

Preview width

Disabled

The dial is set to 45 with defaultValue and has disabled on it, so it fades, a drag does nothing and the thumb leaves the tab order. Use it when another setting has locked the rotation, such as a layer that follows its parent, and the angle is still worth seeing. Say why it is locked in the label or beside it, since a faded dial doesn't explain itself.

Preview width

Root provider

useAngleSlider creates the slider's state outside it, and AngleSliderRootProvider hands that state to the parts, so the Set to 90 button below the dial can call setValue on it. Use the provider when a control outside the slider has to read or set the angle. For one value you already keep in state, a controlled value is simpler.

Preview width

Step

step is 15, and the label says so. Dragging and the arrow keys move the thumb in 15 degree jumps, while the markers stay on every 45 degrees. Use a coarse step for a compass or a rotation people think of in chunks, and keep the default of 1 when a gradient needs a precise angle.

Preview width

FAQ

When should I use an Angle Slider instead of a Slider?

Use an Angle Slider for a value that goes round a circle and means a direction: a rotation, a compass heading, the angle of a gradient. The dial shows the angle as it is, which a straight track can only hint at.

For a value on a scale, such as volume or a price, use a Slider. A Circular Slider is round too, but it is a track with a start and an end, such as a timer or a gauge.

How do I read the Angle Slider value in Svelte?

Pass value and update it in onValueChange, which receives an object with the angle as a number in value and as a CSS string, such as 45deg, in valueAsDegree. The Controlled demo keeps it in state this way.

For a starting angle without state of your own, set defaultValue. The keys keep the value between 0 and 359, and so does a drag at the default step of 1.

How do I rotate an image with the Angle Slider?

Keep the angle in state, as the Controlled demo does, and set the image's CSS rotate to it, using the valueAsDegree string or the number with deg after it. The rotation then follows the thumb as it is dragged.

The slider also sets an --angle variable on its own root, but only elements inside the slider can read it, so an image elsewhere on the page needs the value from state.

How do I snap the Angle Slider to fixed angles?

Set step on AngleSlider, 1 by default. A step of 15 or 45 makes dragging and the arrow keys move in those jumps, as in the Step demo, and Shift with an arrow moves ten steps, landing on a multiple of ten steps. A drag rounds up to the next step clockwise, so in the last jump before the top it reads 360, which the keys never reach.

Markers are separate from the step. List the angles that matter in AngleSliderMarker elements inside AngleSliderMarkerGroup to draw ticks there. Markers the thumb has passed take a tint of the primary color, and one exactly at the value takes it in full.

How do I save the Angle Slider value only when dragging stops?

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 a setting or send a request, so the server doesn't hear about every degree the thumb passes through on its way.

Can I submit the Angle Slider value with a form?

Yes. Give AngleSlider a name and put an AngleSliderHiddenInput inside it: the hidden input carries the angle as a number of degrees and submits with the form. Without a name, the input is there but sends nothing.

readOnly keeps the thumb focusable and the angle in the form while refusing changes. disabled removes the thumb from the tab order, as the Disabled demo shows, and leaves the angle out of the form, as with any disabled field.

How do I make the Angle Slider bigger?

Set the --size variable on AngleSliderControl, which is 100 pixels as installed, with a class such as [--size:160px]. The dial, the thumb's needle and the markers all scale from it.

Two more variables set the details: --track-width for the ring around the dial and --thumb-width for the thumb. Change them on the same element, or edit the defaults in components/ui/angle-slider.

Is the Angle Slider accessible with the keyboard?

Yes. The thumb takes focus with Tab, the arrow keys turn it by one 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.

The thumb gives its angle with a degree sign, so a screen reader hears the unit along with the number. Name the slider with an AngleSliderLabel, or with aria-label on AngleSlider when no label shows. The Accessibility section lists each key.

API Reference

AngleSlider

<div> data-slot="angle-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.

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.

step?number

The step value for the slider.

Default
1
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.

AngleSliderLabel

<label> data-slot="angle-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.

AngleSliderThumb

<div> data-slot="angle-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
--thumb-widthstring

AngleSliderValueText

<span> data-slot="angle-slider-value-text"

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.

AngleSliderControl

<div> data-slot="angle-slider-control"

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
--track-widthstring

AngleSliderMarkerGroup

<div> data-slot="angle-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.

AngleSliderMarker

<span> data-slot="angle-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-valuestring

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

AngleSliderHiddenInput

<input> data-slot="angle-slider-hidden-input"

PropType
asChild?Snippet<[PropsFn]>

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

Type
Snippet<[PropsFn]>

AngleSliderRootProvider

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

PropType
valueUseAngleSliderReturn

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

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

useAngleSlider

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
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.

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.

step?number

The step value for the slider.

Default
1

AngleSliderContext

PropType
draggingboolean

Whether the slider is being dragged.

setValue(value: number) → void

Sets the value of the angle slider.

Parameters

value – number

Returns
void
valuenumber

The current value of the angle slider.

valueAsDegreestring

The current value as a degree string.