Spec Registry
Help your agents use open-source better. Learn more.
Find usage specs for your project’s dependencies
- Author
- tessl
- Last updated
- Spec files
npm-svelte
Describes: npm/svelte
- Description
- A cybernetically enhanced web application framework that compiles to highly optimized JavaScript with reactive state management and component-based architecture.
- Author
- tessl
- Last updated
motion.md docs/
1# Motion & Animation23Svelte provides powerful animation utilities including springs, tweens, and FLIP animations for creating smooth, physics-based motion effects.45## Capabilities67### Spring89A reactive spring animation class that moves values towards targets with physics-based motion.1011```typescript { .api }12/**13* A wrapper for a value that behaves in a spring-like fashion14*/15class Spring<T> {16constructor(value: T, options?: SpringOpts);1718/** Create a spring whose value is bound to the return value of fn */19static of<U>(fn: () => U, options?: SpringOpts): Spring<U>;2021/** Sets target value and returns promise that resolves when spring settles */22set(value: T, options?: SpringUpdateOpts): Promise<void>;2324/** Current value of the spring */25get current(): T;2627/** Target value the spring is moving towards */28target: T;2930/** Spring stiffness (0-1, higher = more responsive) */31stiffness: number;3233/** Spring damping (0-1, higher = less oscillation) */34damping: number;3536/** Precision threshold for settling */37precision: number;38}39```4041**Usage Examples:**4243```typescript44import { Spring } from "svelte/motion";4546// Basic spring47const spring = new Spring(0);4849// Spring with options50const spring = new Spring(0, {51stiffness: 0.1,52damping: 0.25,53precision: 0.0154});5556// Use in reactive context57let targetValue = $state(0);58const animatedValue = Spring.of(() => targetValue);5960// Set target values61spring.target = 100; // Sets target immediately62await spring.set(100); // Returns promise when settled6364// Set with options65await spring.set(100, {66instant: true, // Jump immediately67preserveMomentum: 500 // Continue current trajectory for 500ms68});6970// Access current value71console.log(spring.current); // Current animated value72```7374### Tween7576A reactive tween animation class that smoothly interpolates between values over time.7778```typescript { .api }79/**80* A wrapper for a value that tweens smoothly to its target value81*/82class Tween<T> {83constructor(value: T, options?: TweenedOptions<T>);8485/** Create a tween whose value is bound to the return value of fn */86static of<U>(fn: () => U, options?: TweenedOptions<U>): Tween<U>;8788/** Sets target value and returns promise when tween completes */89set(value: T, options?: TweenedOptions<T>): Promise<void>;9091/** Current value of the tween */92get current(): T;9394/** Target value the tween is moving towards */95get target(): T;96set target(value: T);97}98```99100**Usage Examples:**101102```typescript103import { Tween } from "svelte/motion";104import { cubicOut } from "svelte/easing";105106// Basic tween107const tween = new Tween(0);108109// Tween with options110const tween = new Tween(0, {111duration: 400,112easing: cubicOut,113delay: 100114});115116// Use in reactive context117let targetValue = $state(0);118const animatedValue = Tween.of(() => targetValue);119120// Set target values121await tween.set(100); // Uses default options122123// Set with custom options124await tween.set(100, {125duration: 1000,126easing: t => t * t, // Custom easing127delay: 200128});129130// Custom interpolation for complex values131const colorTween = new Tween({ r: 255, g: 0, b: 0 }, {132interpolate: (from, to) => t => ({133r: Math.round(from.r + (to.r - from.r) * t),134g: Math.round(from.g + (to.g - from.g) * t),135b: Math.round(from.b + (to.b - from.b) * t)136})137});138```139140### FLIP Animation141142The `flip` function calculates start and end positions of elements and animates between them using the FLIP technique.143144```typescript { .api }145/**146* FLIP animation for element position changes147* @param node - Element to animate148* @param params - From/to rectangles and animation options149* @returns Animation configuration150*/151function flip(152node: Element,153{ from, to }: { from: DOMRect; to: DOMRect },154params?: FlipParams155): AnimationConfig;156```157158**Usage Examples:**159160```typescript161import { flip } from "svelte/animate";162163// In a Svelte component with keyed each block164let items = $state([165{ id: 1, name: "Item 1" },166{ id: 2, name: "Item 2" },167{ id: 3, name: "Item 3" }168]);169170function shuffle() {171items = items.sort(() => Math.random() - 0.5);172}173174// Template usage with animate directive175/*176{#each items as item (item.id)}177<div animate:flip={{ duration: 300 }}>178{item.name}179</div>180{/each}181*/182183// Programmatic usage184function animateMove(element, fromRect, toRect) {185const animation = flip(element,186{ from: fromRect, to: toRect },187{ duration: 400, easing: cubicOut }188);189190// Apply animation manually191element.style.animation = animation.css(0, 1);192}193```194195### Legacy Motion Stores196197Legacy store-based motion utilities for Svelte 4 compatibility.198199```typescript { .api }200/**201* @deprecated Use Spring class instead202* Creates a spring store with reactive value203*/204function spring<T = any>(value?: T, opts?: SpringOpts): Spring<T>;205206/**207* @deprecated Use Tween class instead208* Creates a tweened store with reactive value209*/210function tweened<T>(value?: T, defaults?: TweenedOptions<T>): Tweened<T>;211```212213**Usage Examples:**214215```typescript216import { spring, tweened } from "svelte/motion";217218// Legacy spring store219const springStore = spring(0, {220stiffness: 0.1,221damping: 0.25222});223224springStore.set(100);225226// Legacy tweened store227const tweenedStore = tweened(0, {228duration: 400,229easing: t => t * t230});231232tweenedStore.set(100);233234// Subscribe to changes235springStore.subscribe(value => {236console.log("Spring value:", value);237});238```239240### Motion Utilities241242Additional utilities for motion and accessibility.243244```typescript { .api }245/**246* MediaQuery for reduced motion preference247*/248const prefersReducedMotion: MediaQuery;249```250251**Usage Examples:**252253```typescript254import { prefersReducedMotion } from "svelte/motion";255256// Check user's motion preference257const shouldAnimate = !prefersReducedMotion.current;258259// Use in animation decisions260const duration = prefersReducedMotion.current ? 0 : 400;261262// Reactive usage263$effect(() => {264if (prefersReducedMotion.current) {265// Disable animations266spring.set(targetValue, { instant: true });267} else {268// Enable animations269spring.set(targetValue);270}271});272```273274## Types275276```typescript { .api }277interface SpringOpts {278stiffness?: number;279damping?: number;280precision?: number;281}282283interface SpringUpdateOpts {284instant?: boolean;285preserveMomentum?: number;286}287288interface TweenedOptions<T> {289delay?: number;290duration?: number | ((from: T, to: T) => number);291easing?: (t: number) => number;292interpolate?: (a: T, b: T) => (t: number) => T;293}294295interface FlipParams {296delay?: number;297duration?: number | ((len: number) => number);298easing?: (t: number) => number;299}300301interface AnimationConfig {302delay?: number;303duration?: number;304easing?: (t: number) => number;305css?: (t: number, u: number) => string;306tick?: (t: number, u: number) => void;307}308309interface MediaQuery {310current: boolean;311}312```313314## Best Practices3153161. **Use Spring for interactive elements**: Springs feel more natural for user-driven animations3172. **Use Tween for choreographed sequences**: Tweens provide precise timing control3183. **Respect user preferences**: Always check `prefersReducedMotion` for accessibility3194. **Clean up animations**: Use cleanup functions in effects to cancel ongoing animations3205. **Performance considerations**: Prefer `transform` and `opacity` properties for smooth animations