Skip to content

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

Components

Free

Angle Slider for UI SDK

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 React and UI SDK.

On this page9 sections

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

import {
  AngleSlider, AngleSliderControl, AngleSliderLabel, AngleSliderMarker,
  AngleSliderMarkerGroup, AngleSliderThumb, AngleSliderValueText,
} from "@/components/ui/angle-slider";

UI SDK has no angle slider, so this one is built from UI SDK's own behaviors, such as its drag and key handling, and installs nothing beyond @ui-sdk/react and @ui-sdk/core.

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 keys go round the top as a drag does, rather than stopping at 0 or at the last step.

Arrow Right
Increases the angle by one step, going round from the last step to 0. Right to left, it decreases it.
Arrow Left
Decreases the angle by one step, going round from 0 to the last step. Right to left, it increases it.
Arrow Up
Increases the angle by one step, going round from the last step to 0.
Arrow Down
Decreases the angle by one step, going round from 0 to the last step.
Shift+Arrow Up
Increases the angle by ten steps, at most a quarter turn.
Shift+Arrow Down
Decreases the angle by ten steps, at most a quarter turn.
Shift+Arrow Right
Increases the angle by ten steps, at most a quarter turn. Right to left, it decreases it.
Shift+Arrow Left
Decreases the angle by ten steps, at most a quarter turn. Right to left, it increases it.
Page Up
Increases the angle by ten steps, at most a quarter turn.
Page Down
Decreases the angle by ten steps, at most a quarter turn.
Home
Sets the angle to 0 degrees.
End
Sets the angle to the last step before the top: 359 degrees at the default step of 1.

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 React?

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, but never more than a quarter turn. A drag snaps to the nearest step, and the top always reads 0, never 360.

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 or Page Up and Page Down by ten steps, and Home and End go to 0 and to the last step before the top, 359 by default. The keys go round the top rather than stopping there.

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

PropType
value?number

The angle. Pass it to control the slider.

defaultValue?number

The angle at first, when uncontrolled. A form reset returns to it.

Default
0
onValueChange?(details: AngleSliderValueChangeDetails) → void

Runs on every change, from a drag, a key or setValue.

Type
(details: AngleSliderValueChangeDetails) → void
Parameters

details – { cancel: () → void; event?: Event; reason: AngleSliderValueChangeReason; source: ChangeSource; value: number; valueAsDegree: string }

Returns
void
disabled?boolean

Whether it's disabled.

form?string

The id of a form to belong to, when it isn't inside that form.

ids?{ label?: string }

Ids for the parts. label names the thumb even when the label is an element of your own.

invalid?boolean

Whether it's invalid.

name?string

The name the angle submits under.

onValueChangeEnd?(details: AngleSliderValueChangeEndDetails) → void

Runs once a drag or a key press that changed the angle ends.

Type
(details: AngleSliderValueChangeEndDetails) → void
Parameters

details – AngleSliderValueChangeEndDetails

Returns
void
readOnly?boolean

Keeps it focusable and submitted, but drags and keys don't change it.

step?number

How far a key or a drag moves, in degrees.

Default
1
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AngleSliderRootProvider

PropType
valueobject

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

Type
{ dragging: boolean; setValue: (value: number) → void; value: number; valueAsDegree: string }
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AngleSliderLabel

PropType
className?string

Merged with the part's classes.

AngleSliderControl

PropType
className?string

Merged with the part's classes.

CSS variableType
--sizestring
--track-widthstring

AngleSliderThumb

PropType
className?string

Merged with the part's classes.

CSS variableType
--anglestring
--sizestring
--thumb-widthstring

AngleSliderMarkerGroup

PropType
className?string

Merged with the part's classes.

AngleSliderMarker

PropType
className?string

Merged with the part's classes.

CSS variableType
--marker-valuestring

AngleSliderValueText

PropType
className?string

Merged with the part's classes.

AngleSliderHiddenInput

PropType
className?string

Merged with the part's classes.

useAngleSlider

PropType
value?number

The angle. Pass it to control the slider.

defaultValue?number

The angle at first, when uncontrolled. A form reset returns to it.

Default
0
onValueChange?(details: AngleSliderValueChangeDetails) → void

Runs on every change, from a drag, a key or setValue.

Type
(details: AngleSliderValueChangeDetails) → void
Parameters

details – { cancel: () → void; event?: Event; reason: AngleSliderValueChangeReason; source: ChangeSource; value: number; valueAsDegree: string }

Returns
void
disabled?boolean

Whether the der is disabled.

form?string

The id of a form to belong to, when it isn't inside that form.

ids?{ label?: string }

Ids for the parts. label names the thumb even when the label is an element of your own.

invalid?boolean

Whether the der is invalid.

name?string

The name the angle submits under.

onValueChangeEnd?(details: AngleSliderValueChangeEndDetails) → void

Runs once a drag or a key press that changed the angle ends.

Type
(details: AngleSliderValueChangeEndDetails) → void
Parameters

details – AngleSliderValueChangeEndDetails

Returns
void
readOnly?boolean

Keeps it focusable and submitted, but drags and keys don't change it.

step?number

How far a key or a drag moves, in degrees.

Default
1

AngleSliderContext