no. 13 · sliders
Pill slider
A tiny bar that swells on hover, fills as you drag and stretches past the ends.
Preview
Install
npx shadcn@latest add https://curio-components.vercel.app/r/pill-slider.jsonFirst curio component? Also add @import "./curio.css"; to your global CSS. See installation.
Usage
'use client'
import { useState } from 'react'
import { PillSlider } from '@/components/curio/sliders/pill-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 <PillSlider options={themes} value={value} onChange={setValue} />
}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. |
| width | number | 72 | Track width in px. |
Source
This is exactly what the command copies into your project.
components/curio/sliders/pill-slider.tsx
'use client'
import { useState } from 'react'
import { sliderAria, sliderKeys, useTrackDrag, type SliderProps } from '../lib/slider'
// a tiny bar that swells on hover, fills as you drag and stretches past the ends
export function PillSlider(props: SliderProps & { width?: number }) {
const { options, value, onChange, width = 72 } = props
const count = options.length
const [hover, setHover] = useState(false)
const { ref, dragging, overshoot, handlers } = useTrackDrag(width, count, onChange)
const open = hover || dragging
const stretch = 1 + Math.min(Math.abs(overshoot), 40) / 260
return (
<div
ref={ref}
{...sliderAria(props)}
onKeyDown={sliderKeys(value, count, onChange)}
{...handlers}
onPointerEnter={() => setHover(true)}
onPointerLeave={() => setHover(false)}
className="relative flex h-7 cursor-grab touch-none select-none items-center outline-none active:cursor-grabbing"
style={{ width }}
>
<div
className="relative w-full overflow-hidden rounded-full"
style={{
height: open ? 18 : 6,
background: 'var(--curio-chip)',
transform: `scaleX(${stretch}) scaleY(${2 - stretch})`,
transformOrigin: overshoot < 0 ? 'right' : 'left',
transition: dragging && overshoot ? 'height 200ms ease' : 'height 220ms cubic-bezier(.23,1,.32,1), transform 400ms cubic-bezier(.34,1.56,.64,1)',
}}
>
<div
className="absolute inset-y-0 left-0 rounded-full"
style={{
width: `${(value / (count - 1)) * 100}%`,
minWidth: open ? 18 : 6,
background: open ? 'var(--curio-accent)' : 'var(--curio-fg)',
transition: 'width 280ms cubic-bezier(.23,1,.32,1), background-color 150ms',
}}
/>
{options.slice(1, -1).map((_, j) => {
const i = j + 1
return (
<span
key={i}
className="absolute top-1/2 h-1 w-1 -translate-x-1/2 -translate-y-1/2 rounded-full transition-opacity"
style={{ left: `${(i / (count - 1)) * 100}%`, background: 'var(--curio-muted)', opacity: open && i !== value ? 0.6 : 0 }}
/>
)
})}
</div>
</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,
},
}
}