no. 16 · sliders
Swatch slider
Tiny squares in each option’s colours that spread apart on hover.
Preview
Day
Install
npx shadcn@latest add https://curio-components.vercel.app/r/swatch-slider.jsonFirst curio component? Also add @import "./curio.css"; to your global CSS. See installation.
Usage
'use client'
import { useState } from 'react'
import { SwatchSlider } from '@/components/curio/sliders/swatch-slider'
const themes = [
{ label: 'Sunrise', swatch: '#fff4e6', accent: '#ea580c' },
{ label: 'Day', swatch: '#ffffff', accent: '#18181b' },
{ label: 'Midnight', swatch: '#09090b', accent: '#fafafa' },
]
export function ThemePicker() {
const [value, setValue] = useState(0)
return <SwatchSlider options={themes} value={value} onChange={setValue} />
}Uses each option’s swatch and accent colours.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| options | ThemeOption[] | - | Each step: label, swatch?, accent? |
| value | number | - | Index of the selected option. |
| onChange | (index: number) => void | - | Called with the new index. |
| label | string | "theme" | Accessible name. |
Source
This is exactly what the command copies into your project.
components/curio/sliders/swatch-slider.tsx
'use client'
import { useState } from 'react'
import { sliderAria, sliderKeys, type SliderProps } from '../lib/slider'
// tiny squares in each option's colours; they spread apart on hover
export function SwatchSlider(props: SliderProps) {
const { options, value, onChange } = props
const [hover, setHover] = useState<number | null>(null)
const [open, setOpen] = useState(false)
const shown = hover ?? value
return (
<div className="flex items-center gap-3">
<div
{...sliderAria(props)}
onKeyDown={sliderKeys(value, options.length, onChange)}
onPointerEnter={() => setOpen(true)}
onPointerLeave={() => {
setOpen(false)
setHover(null)
}}
className="flex items-center outline-none transition-[gap] duration-300"
style={{ gap: open ? 6 : 2 }}
>
{options.map((o, i) => {
const accent = o.accent ?? 'var(--curio-accent)'
return (
<button
key={o.label}
type="button"
tabIndex={-1}
aria-label={o.label}
onPointerEnter={() => setHover(i)}
onClick={() => onChange(i)}
className="relative h-4 w-4 cursor-pointer border transition-transform duration-200"
style={{
background: o.swatch ?? 'var(--curio-chip)',
borderColor: i === value ? accent : 'var(--curio-border)',
outline: i === value ? `1.5px solid ${accent}` : 'none',
outlineOffset: 2,
transform: hover === i ? 'translateY(-2px)' : 'none',
}}
>
<span className="absolute bottom-[2px] right-[2px] h-[5px] w-[5px]" style={{ background: accent }} />
</button>
)
})}
</div>
<span className="w-14 text-xs" style={{ color: 'var(--curio-muted)' }}>
{options[shown]?.label}
</span>
</div>
)
}components/curio/lib/slider.ts
'use client'
import { useRef, useState } from 'react'
import type { ThemeOption } from './types'
export type SliderProps = {
options: ThemeOption[]
value: number
onChange: (index: number) => void
/** accessible name */
label?: string
}
export const clampTo = (n: number, count: number) => Math.max(0, Math.min(count - 1, n))
// arrow keys / home / end
export function sliderKeys(value: number, count: number, onChange: (i: number) => void) {
return (e: React.KeyboardEvent) => {
const next =
e.key === 'ArrowRight' || e.key === 'ArrowUp'
? value + 1
: e.key === 'ArrowLeft' || e.key === 'ArrowDown'
? value - 1
: e.key === 'Home'
? 0
: e.key === 'End'
? count - 1
: null
if (next === null) return
e.preventDefault()
onChange(clampTo(next, count))
}
}
export function sliderAria({ options, value, label = 'Theme' }: SliderProps) {
return {
role: 'slider' as const,
tabIndex: 0,
'aria-label': label,
'aria-valuemin': 0,
'aria-valuemax': options.length - 1,
'aria-valuenow': value,
'aria-valuetext': options[value]?.label,
}
}
// drag along x; reports the nearest step and how far past either end you pulled
export function useTrackDrag(width: number, count: number, onStep: (i: number) => void) {
const ref = useRef<HTMLDivElement>(null)
const [dragging, setDragging] = useState(false)
const [overshoot, setOvershoot] = useState(0)
const update = (clientX: number) => {
const rect = ref.current!.getBoundingClientRect()
const x = clientX - rect.left
setOvershoot(x < 0 ? x : x > width ? x - width : 0)
onStep(clampTo(Math.round((x / width) * (count - 1)), count))
}
const stop = () => {
setDragging(false)
setOvershoot(0)
}
return {
ref,
dragging,
overshoot,
handlers: {
onPointerDown: (e: React.PointerEvent) => {
;(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId)
setDragging(true)
update(e.clientX)
},
onPointerMove: (e: React.PointerEvent) => dragging && update(e.clientX),
onPointerUp: stop,
onPointerCancel: stop,
},
}
}