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.
Install
npm install @hipuku/motionUsage
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 capA 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
| Piece | For |
|---|---|
capDuration | Holds a duration to its role's cap |
springToLinear and friends | A spring as a CSS linear() easing, with its settle time |
VelocityTracker, releaseTarget, rubberBand | Dragging a sheet or drawer, and where it lands |
presence, isMounted | Keeping an element mounted while it animates out |
API
| Export | Signature |
|---|---|
capDuration | (role: MotionRole, ms: number) => number |
SPRING_LINEAR_POINTS | 40 |
SPRING_POINTS_PER_CYCLE | 16 |
SPRING_REST_DELTA | 0.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_MODES | readonly ["subtle", "expressive", "reduced"] |
MOTION_TOKENS | Record<"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_TRANSITIONS | Record<"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>> |
STAGGER | Record<"expressive" | "reduced" | "subtle", { stepMs: number; maxMs: number; }> |
DISTANCE_MIN_SHARE | 0.5 |
DISTANCE_REFERENCE_PX | 400 |
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_SHARE | 0.5 |
DISMISS_VELOCITY | 0.5 |
VELOCITY_WINDOW_MS | 100 |
VelocityTracker | typeof 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.