Skip to Content
Getting startedFoundationsComponentsPackagesTools

Motion

The maths behind haus's motion, as plain functions over numbers, with no DOM and no framework.

The numbers behind haus's motion roles: duration caps, springs compiled to CSS, sheet drag physics and presence state. Plain functions, so the same values serve CSS, tests and canvas.

@hipuku/motion0.1.1Source on GitHub

Install

npm install @hipuku/motion

Usage

import { capDuration, springToLinear } from '@hipuku/motion'

springToLinear({ visualDuration: 0.2, bounce: 0 })
// { durationMs: 353, easing: 'linear(0, 0.0229, 0.0789, …, 1)' }

capDuration('highlight', 500) // 300: the highlight role's cap

A spring is described by how long it looks like it moves (visualDuration) and how far it overshoots (bounce). durationMs is how long CSS has to run for it to settle, which is longer than it looks. These are the same values the --haus-motion-* custom properties carry.

What it covers

PieceFor
capDurationHolds a duration to its role's cap
springToLinear and friendsA spring as a CSS linear() easing, with its settle time
VelocityTracker, releaseTarget, rubberBandDragging a sheet or drawer, and where it lands
presence, isMountedKeeping an element mounted while it animates out

API

ExportSignature
capDuration(role: MotionRole, ms: number) => number
SPRING_LINEAR_POINTS40
SPRING_POINTS_PER_CYCLE16
SPRING_REST_DELTA0.001
springPosition(options: SpringOptions, t: number) => number
springSettleTime(options: SpringOptions) => number
springToLinear(options: SpringOptions) => { durationMs: number; easing: string; }
DURATION_CAPS_MS{ readonly press: 200; readonly highlight: 300; readonly feedback: 300; readonly layout: 450; readonly 'popover-enter': 300; readonly 'popover-exit': 300; readonly 'dialog-enter': 450; readonly 'dialog-exit': 450; readonly 'sheet-enter': 500; readonly 'sheet-exit': 500; readonly 'toast-enter': 450; readonly 'toast-exit': 450; readonly indicator: 400; readonly draw: 500; }
MOTION_LIMITS{ readonly bounceMax: 0.3; readonly offsetMaxPx: 24; readonly scaleMin: 0.88; readonly reducedDurationMaxMs: 120; }
MOTION_MODESreadonly ["subtle", "expressive", "reduced"]
MOTION_TOKENSRecord<"expressive" | "reduced" | "subtle", Record<"dialog-enter" | "dialog-exit" | "draw" | "feedback" | "highlight" | "indicator" | "layout" | "popover-enter" | "popover-exit" | "press" | "sheet-enter" | "sheet-exit" | "toast-enter" | "toast-exit", RoleToken>>
MOTION_TRANSITIONSRecord<"expressive" | "reduced" | "subtle", Record<"dialog-enter" | "dialog-exit" | "draw" | "feedback" | "highlight" | "indicator" | "layout" | "popover-enter" | "popover-exit" | "press" | "sheet-enter" | "sheet-exit" | "toast-enter" | "toast-exit", MotionTransition>>
STAGGERRecord<"expressive" | "reduced" | "subtle", { stepMs: number; maxMs: number; }>
DISTANCE_MIN_SHARE0.5
DISTANCE_REFERENCE_PX400
durationForDistance(role: Extract<MotionRole, 'sheet-enter' | 'sheet-exit' | 'layout'>, tokenMs: number, distancePx: number) => number
highlightMove(from: Rect | null, to: Rect, cause: HighlightCause) => { rect: Rect; slide: boolean; }
originFromTrigger(trigger: Rect, surface: Rect) => string
DISMISS_DISTANCE_SHARE0.5
DISMISS_VELOCITY0.5
VELOCITY_WINDOW_MS100
VelocityTrackertypeof VelocityTracker
dragMovesSheet(listScrollTop: number, deltaY: number) => boolean
releaseTarget(offsetPx: number, heightPx: number, velocity: number) => 'close' | 'open'
rubberBand(overshootPx: number, limitPx: number) => number
isMounted(state: PresenceState) => boolean
presence(state: PresenceState, event: PresenceEvent) => PresenceState

The full reference is the package README.

On this page