<!-- Source: https://motion.svelte.page/examples/scope-motion -->

# useScopeMotion

> Motion 14.5 scopes in Svelte with useScopeMotion — a staggered list that re-runs on a breakpoint, and a reset button that cancels every animation and restores inline styles.

**Source:** [https://motion.svelte.page/examples/scope-motion](https://motion.svelte.page/examples/scope-motion)

**Markdown mirror:** [https://motion.svelte.page/examples/scope-motion.md](https://motion.svelte.page/examples/scope-motion.md)

---

This mirror preserves the prose, implementation notes, and runnable Svelte source behind the live example page.

## FIG-001: a stagger that re-runs on a breakpoint.

Setup staggers the list in from one side and adds a hover nudge. With matchMedia conditions, crossing 800px cancels the scope and runs setup again, so the list re-staggers from the other end.

**Metadata:** tag: `MEDIA` | matchMedia: `wide · narrow`

### Notes

- `matchMedia: &#123; wide: '>=800', narrow: '<800' &#125;` — resize the window across 800px and `scope.media` flips, setup runs again and the stagger changes direction.
- The stagger `animate()` call passes only a delay. The spring comes from the scope's default `transition`.
- `hover('li', …)` is collected by the scope, so each re-run replaces it instead of stacking listeners.

### Source

#### BreakpointStagger.svelte

Source file: [src/lib/examples/scope-motion/demos/BreakpointStagger.svelte](https://github.com/humanspeak/svelte-motion/blob/main/docs/src/lib/examples/scope-motion/demos/BreakpointStagger.svelte)

```svelte
<script lang="ts">
    import { animate, hover, stagger, useScopeMotion } from '@humanspeak/svelte-motion'

    // A staggered list in one useScopeMotion scope with media conditions.
    // Setup runs while either condition matches; crossing 800px cancels the
    // scope (restoring inline styles) and runs setup again, so the list
    // re-staggers in the new direction. The hover gesture is collected too.

    const items = ['Spring', 'Tween', 'Inertia', 'Keyframes', 'Stagger']

    let mode = $state('—')
    let runs = $state(0)

    const scope = useScopeMotion(
        ({ media }) => {
            runs += 1
            mode = media.wide ? 'wide · from first' : 'narrow · from last'

            // No per-call transition: the scope's default applies.
            animate(
                'li',
                { opacity: [0, 1], x: media.wide ? [-24, 0] : [24, 0] },
                { delay: stagger(0.07, { from: media.wide ? 'first' : 'last' }) }
            )

            hover('li', (element) => {
                animate(element, { x: media.wide ? 8 : -8 }, { duration: 0.2 })
                return () => animate(element, { x: 0 }, { duration: 0.2 })
            })
        },
        {
            transition: { type: 'spring', visualDuration: 0.45, bounce: 0.2 },
            matchMedia: { wide: '>=800', narrow: '<800' }
        }
    )
</script>

<!-- dk-strip: docs-kit positioning shell — stripped from the published code. -->
<div class="dk-demo-shell">
    <div class="strip">
        <div class="strip-head">
            <span class="micro">// breakpoint stagger</span>
            <span class="micro readout">{mode}</span>
        </div>

        <ul class="list" {@attach scope}>
            {#each items as item (item)}
                <li>{item}</li>
            {/each}
        </ul>

        <div class="controls">
            <button type="button" onclick={() => scope.current?.refresh()}>Replay</button>
            <span class="micro">setup runs · {runs}</span>
        </div>
    </div>
</div>

<style>
    .dk-demo-shell {
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 1.5rem;
        min-height: 400px;
    }

    .strip {
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
        width: min(100%, 360px);
    }

    .micro {
        font-family: var(--brut-mono, monospace);
        font-size: 0.6875rem;
        letter-spacing: 0.08em;
        text-transform: uppercase;
        color: var(--brut-ink-3, #9a9a9a);
    }

    .strip-head {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 1rem;
        border-bottom: 1px dashed var(--brut-rule-2, #bbc4c0);
        padding-bottom: 0.5rem;
    }

    .readout {
        color: var(--brut-accent, #247768);
    }

    .list {
        display: grid;
        gap: 0.5rem;
        margin: 0;
        padding: 0;
        list-style: none;
    }

    li {
        box-sizing: border-box;
        padding: 0.75rem 1rem;
        border: 1px solid var(--brut-ink, #0a0a0a);
        background: var(--brut-bg, #f8fcfb);
        color: var(--brut-ink, #0a0a0a);
        font-family: var(--brut-mono, monospace);
        font-size: 0.875rem;
        cursor: default;
    }

    li:nth-child(odd) {
        background: var(--brut-accent, #247768);
        color: var(--brut-bg, #f8fcfb);
    }

    .controls {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 1rem;
        border-top: 1px dashed var(--brut-rule-2, #bbc4c0);
        padding-top: 0.75rem;
    }

    button {
        padding: 0.5rem 1rem;
        border: 1px solid var(--brut-ink, #0a0a0a);
        background: var(--brut-ink, #0a0a0a);
        color: var(--brut-bg, #f8fcfb);
        font-family: var(--brut-mono, monospace);
        font-size: 0.75rem;
        letter-spacing: 0.08em;
        text-transform: uppercase;
        cursor: pointer;
    }
</style>
```

## FIG-002: a reset button that cancels everything.

Scatter animates every tile inside the scope with run(). Reset calls cancel(): the springs stop and each tile’s inline style is restored, so they snap straight home.

**Metadata:** tag: `CANCEL` | method: `cancel()`

### Notes

- `cancel()` stops everything in the scope and restores the `style` attribute of every element it animated. `stop()` would freeze the tiles where they are instead.
- Event handlers reach the live scope through `scope.current`; wrapping work in `run()` (or `bind()`) is what gets it collected.

### Source

#### CancelReset.svelte

Source file: [src/lib/examples/scope-motion/demos/CancelReset.svelte](https://github.com/humanspeak/svelte-motion/blob/main/docs/src/lib/examples/scope-motion/demos/CancelReset.svelte)

```svelte
<script lang="ts">
    import { animate, useScopeMotion } from '@humanspeak/svelte-motion'

    // Scatter runs inside the scope with run(), so its animations are
    // collected. Reset calls cancel(): every running animation stops and the
    // tiles' inline styles are restored to what they were before the scope
    // touched them, so they snap straight home.

    const tiles = Array.from({ length: 9 }, (_, index) => index + 1)
    const random = (range: number) => (Math.random() * 2 - 1) * range

    let root = $state<HTMLElement | null>(null)
    let label = $state('active')

    const scope = useScopeMotion(() => {}, {
        transition: { type: 'spring', visualDuration: 0.6, bounce: 0.3 }
    })

    const scatter = () => {
        const current = scope.current
        if (!current || !root) return
        // A cancelled scope collects again once it is refreshed.
        if (current.state !== 'active') current.refresh()
        const elements = root.querySelectorAll<HTMLElement>('.tile')
        current.run(() => {
            elements.forEach((element, index) => {
                animate(
                    element,
                    { x: random(90), y: random(60), rotate: random(45) },
                    { delay: index * 0.03 }
                )
            })
        })
        label = current.state
    }

    const reset = () => {
        scope.current?.cancel()
        label = scope.current?.state ?? 'detached'
    }
</script>

<!-- dk-strip: docs-kit positioning shell — stripped from the published code. -->
<div class="dk-demo-shell">
    <div class="strip">
        <div class="strip-head">
            <span class="micro">// cancel resets</span>
            <span class="micro readout">scope · {label}</span>
        </div>

        <div class="stage">
            <div class="grid" bind:this={root} {@attach scope}>
                {#each tiles as tile (tile)}
                    <div class="tile" class:accent={tile % 3 === 0}>
                        {String(tile).padStart(2, '0')}
                    </div>
                {/each}
            </div>
        </div>

        <div class="controls">
            <button type="button" onclick={scatter}>Scatter</button>
            <button type="button" onclick={reset}>Reset (cancel)</button>
        </div>
    </div>
</div>

<style>
    .dk-demo-shell {
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 1.5rem;
        min-height: 400px;
    }

    .strip {
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
        width: min(100%, 360px);
    }

    .micro {
        font-family: var(--brut-mono, monospace);
        font-size: 0.6875rem;
        letter-spacing: 0.08em;
        text-transform: uppercase;
        color: var(--brut-ink-3, #9a9a9a);
    }

    .strip-head {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 1rem;
        border-bottom: 1px dashed var(--brut-rule-2, #bbc4c0);
        padding-bottom: 0.5rem;
    }

    .readout {
        color: var(--brut-accent, #247768);
    }

    .stage {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 240px;
    }

    .grid {
        display: grid;
        grid-template-columns: repeat(3, 56px);
        gap: 0.5rem;
    }

    .tile {
        display: flex;
        align-items: center;
        justify-content: center;
        box-sizing: border-box;
        width: 56px;
        height: 56px;
        border: 1px solid var(--brut-ink, #0a0a0a);
        background: var(--brut-bg, #f8fcfb);
        color: var(--brut-ink, #0a0a0a);
        font-family: var(--brut-mono, monospace);
        font-size: 0.875rem;
    }

    .tile.accent {
        background: var(--brut-accent, #247768);
        color: var(--brut-bg, #f8fcfb);
    }

    .controls {
        display: flex;
        gap: 0.5rem;
        border-top: 1px dashed var(--brut-rule-2, #bbc4c0);
        padding-top: 0.75rem;
    }

    button {
        padding: 0.5rem 1rem;
        border: 1px solid var(--brut-ink, #0a0a0a);
        background: var(--brut-ink, #0a0a0a);
        color: var(--brut-bg, #f8fcfb);
        font-family: var(--brut-mono, monospace);
        font-size: 0.75rem;
        letter-spacing: 0.08em;
        text-transform: uppercase;
        cursor: pointer;
    }
</style>
```
