Command Palette

Search for a command to run...

1.7k

Command Palette

Search for a command to run...

Components

Elastic Slider

Slider with elastic rubber-band drag and magnetic snap feedback.

Loading…
"use client"
 
import { useState } from "react"
 
import { ElasticSlider } from "@/components/elastic-slider"
 
export default function ElasticSliderDemo() {
  const [opacity, setOpacity] = useState(0.5)
 
  return (
    <div className="flex w-50 flex-col gap-4">
      <ElasticSlider
        label="Opacity"
        min={0}
        max={1}
        value={opacity}
        onValueChange={setOpacity}
      />
 
      <ElasticSlider
        label="Blur"
        min={0}
        max={100}
        step={1}
        defaultValue={20}
        formatValue={(v) => `${v}px`}
      />
 
      <ElasticSlider
        label="Saturation"
        min={0}
        max={10}
        step={0.1}
        defaultValue={8}
      />
    </div>
  )
}

Features

  • Track stretches with rubber-band tension when dragged past bounds.
  • Snaps magnetically to exact steps or continuous deciles.
  • Inline label and value text dynamically fade to avoid handle overlap.
  • Supports keyboard navigation and respects prefers-reduced-motion

Want to craft components with meticulous attention to detail—like an Elastic Slider? I highly recommend the Interface Craft course by Josh Puckett.

Installation

$ pnpm dlx shadcn@latest add @ncdai/elastic-slider

Usage

import { ElasticSlider } from "@/components/elastic-slider"
const [opacity, setOpacity] = useState(0.5)
 
<ElasticSlider
  label="Opacity"
  min={0}
  max={1}
  value={opacity}
  onValueChange={setOpacity}
/>

Uncontrolled:

<ElasticSlider label="Opacity" min={0} max={1} defaultValue={0.5} />

Accessibility

Keyboard

KeyDescription
ArrowRight ArrowUpIncrease by one step
ArrowLeft ArrowDownDecrease by one step
Shift + ArrowIncrease or decrease by step*10
HomeJump to min
EndJump to max

Reduced motion

When prefers-reduced-motion is on, rubber-band stretch and spring-based fill motion are skipped; the handle still updates without spring transitions on opacity and scale.

API Reference

ElasticSlider

Prop

Type

Styling

CSS Variables

VariableMeaning
--elastic-slider-heightTotal height of the control (theme spacing scale).
--elastic-slider-radiusCorner radius of the track.
--elastic-slider-bgTrack background.
--elastic-slider-fillFill behind the thumb when the track is idle.
--elastic-slider-fill-activeFill behind the thumb when hovered, keyboard-focused, or dragging.
--elastic-slider-hashStep or decile tick marks when the track is active.
--elastic-slider-handleThumb color.
--elastic-slider-labelLabel text color; base tint for the numeric value.
--elastic-slider-focusNumeric value color when the track is active (stronger than the label).

Data Slots

SlotDescription
elastic-sliderRoot wrapper; CSS variables are defined here.
elastic-slider-trackFocusable track, rubber-band width, focus ring.
elastic-slider-hash-marksContainer for tick marks.
elastic-slider-fillFilled portion of the track.
elastic-slider-handleVertical handle.
elastic-slider-labelInline label.
elastic-slider-valueFormatted value.

References