Open History to see the last commit
1import { Slider as SliderPrimitive } from '@ark-ui/solid'
2import { Index, splitProps } from 'solid-js'
3import { Label } from '@/components/ui/label'
4import { cn } from '@/utils'
5β
6// Ported from the original shadcn/ui + Radix UI + React component.
7// This wraps the Ark UI + SolidJS primitive and preserves shadcn's Tailwind-based
8// design-system API, variants, and styling.
9// https://ui.shadcn.com/
10β
11type SliderRootProps = SliderPrimitive.RootProps
12β
13type SliderControlProps = Omit<SliderPrimitive.ControlProps, 'children'>
14β
15type SliderLabelProps = Omit<SliderPrimitive.LabelProps, 'asChild'>
16β
17type SliderValueProps = SliderPrimitive.ValueTextProps
18β
19type SliderProps = Omit<SliderRootProps, 'children'>
20β
21/**
22 * The root component that owns slider state and provides context to its parts.
23 *
24 * @example
25 * ```tsx
26 * <SliderRoot defaultValue={[25]}>
27 * <SliderLabel>Volume</SliderLabel>
28 * <SliderValue />
29 * <SliderControl />
30 * </SliderRoot>
31 * ```
32 */
33function SliderRoot(props: SliderRootProps) {
34 const [local, others] = splitProps(props, [
35 'class',
36 'children',
37 'defaultValue',
38 'value',
39 'min',
40 'max',
41 ])
42β
43 const min = () => local.min ?? 0
44 const max = () => local.max ?? 100
45 const defaultValue = () =>
46 local.defaultValue ??
47 (local.value === undefined ? [min(), max()] : undefined)
48β
49 return (
50 <SliderPrimitive.Root
51 {...others}
52 data-slot="slider"
53 defaultValue={defaultValue()}
54 value={local.value}
55 min={min()}
56 max={max()}
57 class={cn(
58 'relative grid w-full grid-cols-[minmax(0,1fr)_auto] items-center gap-x-2 gap-y-3 select-none data-disabled:opacity-50 data-vertical:h-full data-vertical:min-h-40 data-vertical:w-auto',
59 local.class,
60 )}
61 >
62 {local.children}
63 </SliderPrimitive.Root>
64 )
65}
66β
67/**
68 * The interactive slider track, range, and value thumbs.
69 */
70function SliderControl(props: SliderControlProps) {
71 const [local, others] = splitProps(props, ['class'])
72β
73 return (
74 <SliderPrimitive.Control
75 {...others}
76 data-slot="slider-control"
77 class={cn(
78 'relative col-span-full flex grow touch-none items-center data-horizontal:w-full data-vertical:h-full data-vertical:min-h-40 data-vertical:flex-col',
79 local.class,
80 )}
81 >
82 <SliderPrimitive.Track
83 data-slot="slider-track"
84 class="relative grow overflow-hidden rounded-full bg-muted data-horizontal:h-1 data-horizontal:w-full data-vertical:h-full data-vertical:w-1"
85 >
86 <SliderPrimitive.Range
87 data-slot="slider-range"
88 class="absolute bg-primary select-none data-horizontal:h-full data-vertical:w-full"
89 />
90 </SliderPrimitive.Track>
91 <SliderPrimitive.Context>
92 {(slider) => (
93 <Index each={slider().value}>
94 {(_, index) => (
95 <SliderPrimitive.Thumb
96 data-slot="slider-thumb"
97 index={index}
98 class="relative block size-3 shrink-0 rounded-full border border-ring bg-white ring-ring/50 transition-[color,box-shadow] select-none after:absolute after:-inset-2 hover:ring-3 focus-visible:ring-3 focus-visible:outline-hidden active:ring-3 disabled:pointer-events-none disabled:opacity-50"
99 >
100 <SliderPrimitive.HiddenInput />
101 </SliderPrimitive.Thumb>
102 )}
103 </Index>
104 )}
105 </SliderPrimitive.Context>
106 </SliderPrimitive.Control>
107 )
108}
109β
110/**
111 * A visible label connected to every thumb in its SliderRoot.
112 */
113function SliderLabel(props: SliderLabelProps) {
114 const [local, others] = splitProps(props, ['class'])
115β
116 return (
117 <SliderPrimitive.Label
118 {...others}
119 asChild={(sliderLabelProps) => (
120 <Label
121 {...sliderLabelProps({ class: local.class })}
122 data-slot="slider-label"
123 />
124 )}
125 />
126 )
127}
128β
129/**
130 * Displays the current slider value or custom value text.
131 */
132function SliderValue(props: SliderValueProps) {
133 const [local, others] = splitProps(props, ['class'])
134β
135 return (
136 <SliderPrimitive.ValueText
137 {...others}
138 data-slot="slider-value"
139 class={cn('col-start-2 text-sm text-muted-foreground', local.class)}
140 />
141 )
142}
143β
144/**
145 * A styled slider that supports single values, ranges, and horizontal or
146 * vertical orientation.
147 *
148 * @example
149 * ```tsx
150 * <Slider defaultValue={[25]} />
151 * ```
152 */
153function Slider(props: SliderProps) {
154 return (
155 <SliderRoot {...props}>
156 <SliderControl />
157 </SliderRoot>
158 )
159}
160β
161export {
162 Slider,
163 SliderControl,
164 SliderLabel,
165 SliderRoot,
166 SliderValue,
167 type SliderControlProps,
168 type SliderLabelProps,
169 type SliderProps,
170 type SliderRootProps,
171 type SliderValueProps,
172}