--- name: motion-sv description: Documentation and patterns for using motion-sv, a Svelte 5 port of Motion (Framer Motion). Use this when the user wants animations, gestures, or transitions in Svelte. --- # Motion for Svelte (motion-sv) A port of the Motion library (formerly Framer Motion) specifically for Svelte 5. It aligns closely with the **motion-v** API structure rather than the React version. ## Stack Requirements - MUST be used with Svelte 5 (Runes). - Package name: `motion-sv`. ## Core Components ### `motion` The primary component factory. Use dot notation to render **any** HTML element. ```svelte Headline Go to About ``` ### Styling Pattern (Important) ALWAYS pass styles as an object via `style={{ key: value }}`, never as a string. This is required to bind `MotionValue`s correctly without triggering re-renders. ```svelte ``` ### Type Safety & Variants For better developer experience and type safety, define variants using the `Variants` type. ```svelte ``` ### Supported Props - **Animation:** `initial`, `animate`, `exit`, `transition`, `variants`. - **Gestures:** `whileHover`, `whilePress` (preferred over `whileTap`), `whileDrag`, `whileFocus`. - **Drag:** `drag` (boolean | "x" | "y"), `dragConstraints`, `dragElastic`, `dragMomentum`. - **Events:** `onAnimationStart`, `onAnimationComplete`, `onUpdate`. - **Gesture Events:** `onHoverStart`, `onHoverEnd`, `onPress`, `onPressStart`, `onPressEnd`. ### Viewport / Scroll Animations (Vue API Style) **Important:** Use `inViewOptions` instead of the React `viewport` prop. ```svelte Hello ``` ### `AnimatePresence` Enables exit animations. _Modes:_ `"sync"` (default), `"wait"`, `"popLayout"`. ```svelte {#if show} {/if} ``` ## Layout Animations (CRITICAL DIFFERENCE) Svelte lacks `getSnapshotBeforeUpdate`. You **MUST** use `createLayoutMotion` for layout animations. **Pattern:** 1. Create a proxy object: `const layout = createLayoutMotion(motion)`. 2. Use `` instead of ``. 3. Wrap state updates with `layout.update.with(fn)`. ```svelte
``` ## Reorder (Drag & Drop Lists) Use specific components for reordering lists. ```svelte {#each items as item (item)} {item} {/each} ``` ## Performance (Lazy Motion) Reduce bundle size by loading features on demand. ```svelte ``` ## Best Practices - **API Alignment:** Follow `motion-v` prop naming (e.g., `inViewOptions`, `whilePress`). - **Directives:** Do NOT use Svelte's `transition:fn`. - **Styles:** Always use the object syntax `style={{ ... }}`.