Skip to content

Polar Slider Circular Slider

Pick a value along a wide ring with a hole in the middle, like a volume knob. ring keeps the thumb on the ring, the thumb slot holds the handle, a <datalist> in markers-angle adds the ticks, and the background slot paints the progress on the band.

One valuechange listener writes the value into a CSS variable that the progress gradient and a CSS counter in the readout read.

Code

The following example assumes that you have imported the component and set up the theme.

Circular Slider
<!-- eslint-disable html/use-baseline -->
<div class="circular-slider">
26 collapsed lines
<sv-polar-slider
id="circular-slider-ring"
class="ring"
aria-label="Volume"
max="100"
ring
value="40"
>
<div class="progress" slot="background"></div>
<div class="handle" slot="thumb"></div>
<datalist slot="markers-angle">
<option value="0"></option>
<option value="10"></option>
<option value="20"></option>
<option value="30"></option>
<option value="40"></option>
<option value="50"></option>
<option value="60"></option>
<option value="70"></option>
<option value="80"></option>
<option value="90"></option>
</datalist>
<output id="circular-slider-output" class="readout"></output>
</sv-polar-slider>
</div>
<script type="application/x-typescript" module>
9 collapsed lines
const ring = document.querySelector<PolarSlider>("#circular-slider-ring")!;
ring.valueLabelHandler = value => `${value}%`;
ring.addEventListener("valuechange", updateRing);
updateRing();
function updateRing() {
ring.style.setProperty("--value", String(ring.angle));
}
</script>
<style>
37 collapsed lines
.circular-slider {
display: grid;
justify-items: center;
.ring {
--sv-polar-slider-track-thickness: 1.8em;
--sv-polar-slider-thumb-sz: 1.6em;
--sv-polar-slider-marker-offset: 12px;
--sv-polar-slider-tick-length: 6px;
}
.handle {
inline-size: 3px;
block-size: 1.8em;
background: var(--sv-gray-1);
border-radius: 3px;
}
.progress {
block-size: 100%;
background: conic-gradient(var(--sv-accent) calc(var(--value) * 1%), transparent 0);
}
.readout {
position: absolute;
inset: 20%;
display: grid;
place-content: center;
font-size: 2em;
font-variant-numeric: tabular-nums;
counter-reset: value var(--value);
&::after {
content: counter(value) "%";
}
}
}
</style>