Motion+

iOS slider

An example of recreating the iOS slider using Motion for React. This example uses useTransform to map input to slider state, and slider state to pull, squish and squash animations. It uses React'setModal onFocus and onBlur to set up keyboard-accessible inputs, with Motion's whileFocus prop to animate a custom focus indicator.

React
Tutorial time
5 min
Difficulty

Tutorial

Introduction

The iOS slider example recreates the playful brightness slider found in the iOS shortcut screen using Motion for React. It features an intuitive drag interaction with a satisfying elastic pull effect, squish and stretch animations, and keyboard accessibility.

In this tutorial, we'll learn to use:

Get started

Let's start by setting up the basic structure of our component:

"use client"

import { useRef } from "react"

export default function Slider({
    maxPull = 20,
    maxSquish = 0.92,
    maxStretch = 1.08,
    keyboardStep = 0.05,
    keyboardSpring = { stiffness: 200, damping: 60 },
}) {
    const ref = useRef(null)

    return (
        <div style={container}>
            <div ref={ref} style={slider}>
                <div style={indicator} />
                <div>
                    <SunIcon />
                </div>
            </div>
        </div>
    )
}

/** Copy icons and styles from the example source */

This provides us with a basic slider that looks right, but no interactivity or animations yet.

Related examples

Latest in React

Motion+

Unlock all 400+ examples

  • Source code for every Plus example.
  • Provide examples direct to your agent via Motion's MCP.
  • Lifetime access to new examples and APIs.