logo svelte /motion v2.14.0

useMotionValueEvent

useMotionValueEvent subscribes to a motion value’s events: its changes, and when its animations start, finish or are cancelled. It mirrors Motion’s hook of the same name. It also accepts a Svelte readable store for 'change', skipping the synchronous emission a store makes on subscribe, so the callback only fires on actual changes.

<script>
  import { useMotionValueEvent, useSpring } from '@humanspeak/svelte-motion'
  import { onDestroy } from 'svelte'

  const x = useSpring(0)

  const unsub = useMotionValueEvent(x, 'change', (latest) => {
    console.log('x changed to', latest)
  })

  onDestroy(unsub)
</script>
<script>
  import { useMotionValueEvent, useSpring } from '@humanspeak/svelte-motion'
  import { onDestroy } from 'svelte'

  const x = useSpring(0)

  const unsub = useMotionValueEvent(x, 'change', (latest) => {
    console.log('x changed to', latest)
  })

  onDestroy(unsub)
</script>

Usage

Import

<script>
  import { useMotionValueEvent } from '@humanspeak/svelte-motion'
</script>
<script>
  import { useMotionValueEvent } from '@humanspeak/svelte-motion'
</script>

Subscribe to changes

Pass a motion value (or any readable store), the event 'change', and a callback:

<script>
  import { useMotionValueEvent, useSpring } from '@humanspeak/svelte-motion'
  import { onDestroy } from 'svelte'

  const scale = useSpring(1)

  const unsub = useMotionValueEvent(scale, 'change', (latest) => {
    if (latest > 1.5) {
      console.log('Scale is large!')
    }
  })

  onDestroy(unsub)
</script>

<div
  style="transform: scale({scale.current})"
  onpointerenter={() => scale.set(2)}
  onpointerleave={() => scale.set(1)}
>
  Hover me
</div>
<script>
  import { useMotionValueEvent, useSpring } from '@humanspeak/svelte-motion'
  import { onDestroy } from 'svelte'

  const scale = useSpring(1)

  const unsub = useMotionValueEvent(scale, 'change', (latest) => {
    if (latest > 1.5) {
      console.log('Scale is large!')
    }
  })

  onDestroy(unsub)
</script>

<div
  style="transform: scale({scale.current})"
  onpointerenter={() => scale.set(2)}
  onpointerleave={() => scale.set(1)}
>
  Hover me
</div>

Animation events

Listen for animationStart and animationComplete to know when a value is animating. Register both in one $effect and return a function that removes both:

<script>
  import { useMotionValueEvent, useSpring } from '@humanspeak/svelte-motion'

  const x = useSpring(0)
  let isAnimating = $state(false)

  $effect(() => {
    const offStart = useMotionValueEvent(x, 'animationStart', () => {
      isAnimating = true
    })
    const offComplete = useMotionValueEvent(x, 'animationComplete', () => {
      isAnimating = false
    })

    return () => {
      offStart()
      offComplete()
    }
  })
</script>

<button onclick={() => x.set(x.get() === 0 ? 200 : 0)}>
  {isAnimating ? 'Animating…' : 'Move'}
</button>
<script>
  import { useMotionValueEvent, useSpring } from '@humanspeak/svelte-motion'

  const x = useSpring(0)
  let isAnimating = $state(false)

  $effect(() => {
    const offStart = useMotionValueEvent(x, 'animationStart', () => {
      isAnimating = true
    })
    const offComplete = useMotionValueEvent(x, 'animationComplete', () => {
      isAnimating = false
    })

    return () => {
      offStart()
      offComplete()
    }
  })
</script>

<button onclick={() => x.set(x.get() === 0 ? 200 : 0)}>
  {isAnimating ? 'Animating…' : 'Move'}
</button>

Available events

EventCallback receivesFires when
'change'The latest valueThe value changes. Never on subscribe. Works with motion values and readable stores.
'animationStart'NothingAn animation starts on the value.
'animationComplete'NothingAn animation on the value finishes.
'animationCancel'NothingAn animation is stopped or replaced before it finishes.
'destroy'NothingThe value is destroyed.

Only 'change' works with a plain Svelte readable store. The other events need a motion value.

Cleanup

useMotionValueEvent returns an unsubscribe function. Call it inside onDestroy or at the end of an $effect:

<script>
  import { useMotionValueEvent, useSpring } from '@humanspeak/svelte-motion'
  import { onDestroy } from 'svelte'

  const y = useSpring(0)

  // Option 1: onDestroy
  const unsub = useMotionValueEvent(y, 'change', (v) => console.log(v))
  onDestroy(unsub)
</script>
<script>
  import { useMotionValueEvent, useSpring } from '@humanspeak/svelte-motion'
  import { onDestroy } from 'svelte'

  const y = useSpring(0)

  // Option 1: onDestroy
  const unsub = useMotionValueEvent(y, 'change', (v) => console.log(v))
  onDestroy(unsub)
</script>
<script>
  import { useMotionValueEvent, useSpring } from '@humanspeak/svelte-motion'

  const y = useSpring(0)

  // Option 2: $effect cleanup
  $effect(() => {
    const unsub = useMotionValueEvent(y, 'change', (v) => console.log(v))
    return unsub
  })
</script>
<script>
  import { useMotionValueEvent, useSpring } from '@humanspeak/svelte-motion'

  const y = useSpring(0)

  // Option 2: $effect cleanup
  $effect(() => {
    const unsub = useMotionValueEvent(y, 'change', (v) => console.log(v))
    return unsub
  })
</script>

Advanced — raw subscribe

For full control you can use store.subscribe() directly. Note that Svelte stores fire the callback immediately with the current value on subscribe:

<script>
  import { useSpring } from '@humanspeak/svelte-motion'
  import { onDestroy } from 'svelte'

  const x = useSpring(0)

  // Fires immediately with current value, then on every change
  const unsub = x.subscribe((value) => {
    console.log('value:', value)
  })

  onDestroy(unsub)
</script>
<script>
  import { useSpring } from '@humanspeak/svelte-motion'
  import { onDestroy } from 'svelte'

  const x = useSpring(0)

  // Fires immediately with current value, then on every change
  const unsub = x.subscribe((value) => {
    console.log('value:', value)
  })

  onDestroy(unsub)
</script>

useMotionValueEvent is preferred when you only want to react to changes and not the initial value.

API Reference

Signature

import type { MotionValueEventCallbacks } from 'motion-dom'

// Motion values: every event
useMotionValueEvent<V, EventName extends keyof MotionValueEventCallbacks<V>>(
  value: MotionValue<V>,
  event: EventName,
  callback: MotionValueEventCallbacks<V>[EventName]
): () => void

// Svelte readable stores: 'change' only
useMotionValueEvent<T>(
  store: Readable<T>,
  event: 'change',
  callback: (latest: T) => void
): () => void
import type { MotionValueEventCallbacks } from 'motion-dom'

// Motion values: every event
useMotionValueEvent<V, EventName extends keyof MotionValueEventCallbacks<V>>(
  value: MotionValue<V>,
  event: EventName,
  callback: MotionValueEventCallbacks<V>[EventName]
): () => void

// Svelte readable stores: 'change' only
useMotionValueEvent<T>(
  store: Readable<T>,
  event: 'change',
  callback: (latest: T) => void
): () => void

Parameters

  • value MotionValue<V> | Readable<T> — a motion value (the return value of any hook, such as useSpring, useTransform or useTime, or motionValue()), or, for 'change' only, any Svelte readable store
  • event — one of 'change', 'animationStart', 'animationComplete', 'animationCancel' or 'destroy'
  • callback — receives the latest value for 'change', and no arguments for the other events

Returns

() => void — an unsubscribe function that stops the subscription.

See also


Based on Motion’s useMotionValueEvent API.