svelte-awesome-slider

highly customizable slider component library

This library may not be popular but it’s used by my other library svelte-awesome-color-picker!

A huge thanks to MacFJA who helped me making this library!

Summary

Examples

Common props

Exemple (temperature: 50)

<p id="temperature">Exemple (temperature)</p>
<Slider min={0} max={100} step={5} bind:value name="temperature" ariaLabelledBy="temperature" />

Track customization

Exemple (color)

<script>
	const str = `
		linear-gradient(to right, rgba(0,0,0,0), rgba(255,0,0,1)),
		linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%) top left / 20px,
		linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%) 10px 10px / 20px`;
</script>

<Slider --track-background={str} --track-width="50%" --track-height="20px" />

Thumb customization

Exemple (amount)

<Slider --thumb-background="radial-gradient(circle, #84cc16 0%, #365314 100%)" --thumb-size="20px" />

Vertical slider

Exemple (price)

<Slider
	direction="vertical"
	--track-width="360px"
	--track-height="24px"
	--track-background="#eee"
	--thumb-size="24px"
/>

API

Props

The Slider component has the following props:

nametypedefault valueusage
minstring | number0min value of the slider
maxstring | number100max value of the slider
stepstring | number1step value of the slider
valuenumber50bindable
value of the slider
ariaValueText(current: number) => string(current)method to convert the current value to a string representation of the value for the aria-valuetext attribute. For example, a battery meter value might be conveyed as aria-valuetext=“8% (34 minutes) remaining”. See MDN documentation
namestring | undefinedinput name of the slider
direction'horizontal' | 'vertical''horizontal'direction of the slider
reversebooleanfalseif true, the min and max values will be reversed
keyboardOnlybooleanfalsedisables mouse events
sliderHTMLDivElement | undefineddiv element representing the slider
ariaLabelstring | undefinedaria-label props
ariaLabelledBystring | undefinedaria-labelledby props
ariaControlsstring | undefinedaria-controls props
isDraggingbooleanfalseindicate if the slider is being dragged
onInput(value: number) => voidevent
listener, dispatch an event when the user drags, clicks or tabs at the slider

** Be sure to include either ariaLabel or ariaLabelledBy for accessibility purpose.

CSS variables

The slider component can be customized with the following css variables:

nametypedefault valueusage
--thumb-backgroundbackgroundlinear-gradientThumb background
--thumb-borderbordernoneThumb border
--thumb-sizedimension16pxThumb size
--track-backgroundbackgroundlinear-gradientTrack background
--track-borderbordernoneTrack border
--track-heightdimension6pxTrack height
--track-widthdimensionunsetTrack width
--focus-colorcolorredFocus color
--margin-blockdimension8pxMargin block (top and bottom)