Skip to example

Press and hold to repeat

An example of a stepper that keeps counting while held, made with Motion for React. The tap prop's hold and repeat options fire onTap on each step after a short hold.

React

Source code

"use client"

import { AnimatePresence, motion, useAnimate } from "motion/react"
import { useRef, useState } from "react"

const snap = { type: "spring", stiffness: 1218, damping: 70 } as const
const ui = { type: "spring", stiffness: 305, damping: 33 } as const
const lively = { type: "spring", stiffness: 622, damping: 17 } as const

const min = 0
const max = 99

export default function Stepper() {
  const [value, setValue] = useState(2)
  const [direction, setDirection] = useState(1)
  const [valueRef, animateValue] = useAnimate()
  const latest = useRef(value)

  const step = (change: number) => {
    const next = Math.min(max, Math.max(min, latest.current + change))

    if (next === latest.current) {
      // At a limit: a small shake says no