Rooks
Animation & Timing

useEasing

A hook for creating controlled easing animations with start/stop/reset capabilities.

useEasing

useEasing is a hook for creating controlled easing animations with start/stop/reset capabilities. It returns a progress value (0 to 1) along with controls for managing the animation state.

Usage

Basic Usage

import { useEasing } from "rooks";

function App() {
  const [progress] = useEasing(1000);

  return (
    <div style={{ opacity: progress }}>
      Fading In...
    </div>
  );
}

With Controls

import { useEasing, Easing } from "rooks";

function App() {
  const [progress, { start, stop, reset, state }] = useEasing(1000, {
    easing: Easing.easeInOutQuad,
    autoStart: false,
  });

  return (
    <div>
      <div style={{
        width: 100,
        height: 100,
        background: 'blue',
        transform: `translateX(${progress * 200}px)`
      }} />
      <button onClick={start} disabled={state === "running"}>Start</button>
      <button onClick={stop} disabled={state === "idle"}>Stop</button>
      <button onClick={reset}>Reset</button>
    </div>
  );
}

Looping Animation

import { useEasing, Easing } from "rooks";

function App() {
  const [progress] = useEasing(1000, {
    loop: true,
    alternate: true,
    easing: Easing.easeInOutQuad,
  });

  return (
    <div style={{
      width: 50,
      height: 50,
      background: 'red',
      opacity: 0.3 + progress * 0.7
    }} />
  );
}

Tracking Iterations

import { useEasing } from "rooks";

function App() {
  const [progress, { endCount, restart }] = useEasing(2000, {
    onEnd: () => console.log('Animation ended'),
  });

  return (
    <div>
      <div>Progress: {(progress * 100).toFixed(0)}%</div>
      <div>Completed: {endCount} times</div>
      <button onClick={restart}>Restart</button>
    </div>
  );
}

Arguments

ArgumentTypeDefaultDescription
durationnumber-Duration of the animation in milliseconds.
optionsUseEasingOptionsConfiguration options.

UseEasingOptions

PropertyTypeDefaultDescription
easing(t: number) => numberEasing.linearEasing function to use.
autoStartbooleantrueWhether to start animation on mount.
loopbooleanfalseWhether to loop the animation.
alternatebooleanfalseWhether to alternate direction on each loop (ping-pong).
delaynumber0Delay before starting animation in milliseconds.
onEnd() => voidundefinedCallback fired each time animation reaches the end.

Return Value

Returns a tuple [progress, controls]:

progress

A number between 0 and 1 representing the current eased progress of the animation.

controls

PropertyTypeDescription
start() => voidStart or resume the animation.
stop() => voidStop/pause the animation at current position.
reset() => voidReset to initial state (progress=0, direction=forward, endCount=0).
restart() => voidReset and start (convenience method).
state"idle" | "running"Current animation state.
direction"forward" | "backward"Current animation direction.
endCountnumberNumber of times animation has reached the end.

Easing Presets

The Easing object provides common easing functions:

FunctionDescription
Easing.linearLinear interpolation (no easing).
Easing.easeInQuadQuadratic ease-in (accelerating).
Easing.easeOutQuadQuadratic ease-out (decelerating).
Easing.easeInOutQuadQuadratic ease-in-out.
Easing.easeInCubicCubic ease-in.
Easing.easeOutCubicCubic ease-out.
Easing.easeInOutCubicCubic ease-in-out.

You can also provide your own easing function:

const customEasing = (t) => 1 - Math.pow(1 - t, 3);

const [progress] = useEasing(1000, { easing: customEasing });

On this page