motion icon indicating copy to clipboard operation
motion copied to clipboard

How do you animate on leave/exit?

Open jetlej opened this issue 2 years ago • 6 comments

I don't see any mentions in the documentation. Is this possible to do?

jetlej avatar Jul 17 '23 09:07 jetlej

https://motion.vueuse.org/features/variants#leave

StitiFatah avatar Jul 25 '23 00:07 StitiFatah

I'm sorry but I can't get the leave variant to work with the demo .... Does someone can post a working full exemple ?

Soahr avatar Aug 02 '23 08:08 Soahr

Same here. Can't get any form of leave transition working. There is a link to an example demo in the docs but not example code (from what I can see).

I have found other examples but this is the only one using composition API https://codesandbox.io/s/vueuse-motion-playground-forked-61xpz?file=/src/components/Modal.vue:122-176 - I have replicated this but in a Nuxt 3 project, is there anything else required on top of the above example to get leave animations working with Nuxt 3?

alexrichardsweb avatar Sep 19 '23 00:09 alexrichardsweb

Also been having issues trying to get it to work and what I have isn't perfect but it's just some observations I have.

  • The element you're trying to animate in/out have to be wrapped in a <Transition> element
  • I found that I had to both account for both @enter and @leave events on the transition
  • The :leave property never seemed to register, so I basically had the following
<script setup lang="ts">
const myElement = ref<HTMLElement>();
const { leave, apply } = useMotion(lowerthird);

async function onEnter(_: HTMLElement, done: () => void) {
  await apply({
    y: 0,
    opacity: 1,
  });
  done();
}

async function onLeave(_: HTMLElement, done: () => void) {
  await apply({
    y: 100,
    opacity: 0,
  });
  leave(done);
}
</script>

<template>
  <Transition @enter="onEnter" @leave="onLeave">
    <div ref="myElement" v-if="isVisible" v-motion...></div>
  </Transition>
</template>

gazreyn avatar Oct 02 '23 10:10 gazreyn