Try 'magnetic button'
View code

Number 110: Tick slider with a wake

Specification

Catalogue
No. 110 of 110
Interaction
Drag
Element
Input
Preview
Live component
Imports
react 19.3.1
Added

Notes

Forty thin ticks for a value from 0 to 100: bright up to the value, dim after it, and the ticks near the pointer stand up as it passes, like a wake behind a boat.

The control underneath is a native range input laid over the ticks at zero opacity, so dragging, the arrow keys, Page Up and Down and screen readers all work without any code of ours. The wake is a smoothstep falloff over 46px either side of the pointer, applied as scaleY on each tick, with a short transition so it trails a little behind the hand.

Code

React and plain CSS
"use client";

import { useEffect, useRef, useState, type CSSProperties, type PointerEvent } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import styles from "./wake-slider.module.css";

const TICKS = 40;
/** How far either side of the pointer the ticks stand up, in layout pixels. */
const WAKE = 46;
const ROW = 240;
const DEMO = [50, 95, 13, 50];
const SEGMENT_MS = 1100;

const ease = (t: number) => 1 - Math.pow(1 - t, 3);

export default function WakeSlider({ active, context }: LivePreviewProps) {
  const card = context === "card";
  const [value, setValue] = useState(50);
  const [pointer, setPointer] = useState<number | null>(null);
  const rowRef = useRef<HTMLDivElement>(null);
  const demo = card && active;
  const [demoState, setDemoState] = useState<{ value: number; x: number | null }>({ value: 50, x: null });

  // The card demo drags the value from 50 to 95, down to 13 and back, with the wake under the hand.
  useEffect(() => {
    if (!demo) return;
    let raf = 0;
    const start = performance.now();
    const frame = (now: number) => {
      const t = (now - start) / SEGMENT_MS;
      const seg = Math.floor(t) % (DEMO.length - 1);
      const from = DEMO[seg] ?? 50;
      const to = DEMO[seg + 1] ?? 50;
      const v = from + (to - from) * ease(Math.min(1, t - Math.floor(t)));
      setDemoState({ value: Math.round(v), x: (v / 100) * ROW });
      raf = requestAnimationFrame(frame);
    };
    raf = requestAnimationFrame(frame);
    return () => cancelAnimationFrame(raf);
  }, [demo]);

  const shown = demo ? demoState.value : card ? 50 : value;
  const wakeX = demo ? demoState.x : card ? null : pointer;
  const filled = Math.round((shown / 100) * TICKS);

  function track(event: PointerEvent<HTMLInputElement>) {
    const row = rowRef.current;
    if (!row) return;
    const rect = row.getBoundingClientRect();
    const scale = rect.width / row.offsetWidth || 1;
    setPointer((event.clientX - rect.left) / scale);
  }

  return (
    <div className={styles.area}>
      <div className={styles.slider}>
        <div ref={rowRef} className={styles.ticks} aria-hidden="true">
          {Array.from({ length: TICKS }, (_, i) => {
            const x = ((i + 0.5) / TICKS) * ROW;
            const lift = wakeX === null ? 0 : Math.max(0, 1 - Math.abs(x - wakeX) / WAKE);
            const style = { "--lift": (lift * lift * (3 - 2 * lift)).toFixed(3) } as CSSProperties;
            return <i key={i} className={styles.tick} data-on={i < filled || undefined} style={style} />;
          })}
          <input
            type="range"
            min={0}
            max={100}
            value={shown}
            tabIndex={card ? -1 : 0}
            aria-label="Volume"
            className={styles.input}
            onChange={(e) => setValue(Number(e.target.value))}
            onPointerMove={card ? undefined : track}
            onPointerLeave={() => setPointer(null)}
            onFocus={() => setPointer(null)}
          />
        </div>
        <output className={styles.value} aria-hidden="true">
          {shown}
        </output>
      </div>
    </div>
  );
}

More like this

Estimate slider

Drag, TypeForms and inputs

Price range over the prices it filters

DragForms and inputs

Time ruler you can fling

DragPickers and booking

Sliding tabs

Layout AnimationTabs

Was this helpful

FeedbackForms and inputs

Sign-up form that waits before it complains

Type, FeedbackForms and inputs