<script lang="ts">
import { Slider } from "$lib/components/ui/slider/index.js";
let value = $state(50);
</script>
<Slider type="single" bind:value max={100} step={1} class="max-w-[70%]" /> Installation
pnpm dlx shadcn-svelte@latest add slider Install bits-ui:
pnpm add bits-ui -D Copy and paste the following code into your project.
import Root from './slider.svelte';
export {
Root,
//
Root as Slider
};
<script lang="ts">
import { Slider as SliderPrimitive } from 'bits-ui';
import { cn, type WithoutChildrenOrChild } from '$UTILS$.js';
let {
ref = $bindable(null),
value = $bindable(),
orientation = 'horizontal',
class: className,
...restProps
}: WithoutChildrenOrChild<SliderPrimitive.RootProps> = $props();
</script>
<!--
Discriminated Unions + Destructing (required for bindable) do not
get along, so we shut typescript up by casting `value` to `never`.
-->
<SliderPrimitive.Root
bind:ref
bind:value={value as never}
data-slot="slider"
{orientation}
class={cn(
'data-vertical:min-h-40 relative flex w-full touch-none items-center select-none data-disabled:opacity-50 data-vertical:h-full data-vertical:w-auto data-vertical:flex-col',
className
)}
{...restProps}
>
{#snippet children({ thumbItems })}
<span
data-slot="slider-track"
data-orientation={orientation}
class={cn(
'bg-zinc-800 data-horizontal:h-px data-horizontal:w-full data-vertical:h-full data-vertical:w-px relative grow overflow-hidden'
)}
>
<SliderPrimitive.Range
data-slot="slider-range"
class={cn('bg-[#d0e891] absolute select-none data-horizontal:h-full data-vertical:w-full')}
/>
</span>
{#each thumbItems as thumb (thumb.index)}
<SliderPrimitive.Thumb
data-slot="slider-thumb"
index={thumb.index}
class="border border-[#d0e891] bg-background hover:ring-zinc-300/30 focus-visible:ring-zinc-300/30 size-3 rounded-full transition-colors hover:ring-2 focus-visible:ring-2 focus-visible:outline-hidden block shrink-0 select-none disabled:pointer-events-none disabled:opacity-50"
/>
{/each}
{/snippet}
</SliderPrimitive.Root>
Usage
<script lang="ts">
import { Slider } from '$lib/components/ui/slider/index.js';
let value = $state(33);
</script> <Slider type="single" bind:value max={100} step={1} />