Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear timers and event listeners before component unmounts #188

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions docs/src/components/CodeExample.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import IconJavaScript from "./IconJavaScript.vue"
import IconSvelte from "./IconSvelte.vue"
import IconAngular from "./IconAngular.vue"
import IconNuxt from "./IconNuxt.vue"
import { computed, ref } from "vue"
import { computed, ref, onBeforeUnmount } from "vue";
import { vAutoAnimate } from "../../../src"
import "../../assets/prism.css"

const timeOutID = ref();

type LanguageOption =
| "react"
| "preact"
Expand Down Expand Up @@ -77,10 +79,14 @@ const copyStatus = ref(false)
function copyCode(value: string) {
window.navigator.clipboard.writeText(value)
copyStatus.value = true
setTimeout(() => {
timeOutID.value = setTimeout(() => {
copyStatus.value = false
}, 2000)
}

onBeforeUnmount(() => {
clearTimeout(timeOutID.value);
});
</script>

<template>
Expand Down
18 changes: 14 additions & 4 deletions docs/src/components/Navigation.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<script setup lang="ts">
import { ref } from "vue"
import { ref, onBeforeUnmount } from "vue";
import IconDown from "./IconDown.vue"
import PoliteAd from "./PoliteAd.vue"
import { vAutoAnimate } from "../../../src/index"

const controller = new window.AbortController();

const show = ref(false)
const activeTitle = ref("Docs Navigation")

Expand All @@ -19,14 +21,22 @@ const handleClick = () => {
show.value = window.innerWidth >= 672
}

let resizeTimer
const resizeTimer = ref<NodeJS.Timeout | undefined>();

if (typeof window !== "undefined") {
show.value = window.innerWidth >= 672
clearTimeout(resizeTimer)
clearTimeout(resizeTimer.value)
window.addEventListener("resize", () => {
resizeTimer = setTimeout(applySizing, 200)
resizeTimer.value = setTimeout(applySizing, 200)
}, {
signal: controller?.signal
})
}

onBeforeUnmount(() => {
controller.abort();
clearTimeout(resizeTimer.value)
});
</script>

<template>
Expand Down
4 changes: 4 additions & 0 deletions docs/src/components/TheLogo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ onMounted(() => {
clearInterval(interval)
}
}, duration)

this.$once('hook:beforeUnmount', () => {
clearInterval(interval);
});
})
</script>

Expand Down
10 changes: 8 additions & 2 deletions docs/src/examples/cards/ActualCards.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<script setup>
import { ref } from "vue"
import { ref, onBeforeUnmount } from "vue";
import { vAutoAnimate } from "../../../../src/index"

const timeOutID = ref();

const showForm = ref(false)
const cards = ref([
{
Expand All @@ -20,10 +22,14 @@ const cards = ref([

function createCard(card) {
cards.value.unshift(card)
setTimeout(() => {
timeOutID.value = setTimeout(() => {
showForm.value = false
}, 300)
}

onBeforeUnmount(() => {
clearTimeout(timeOutID.value);
});
</script>

<template>
Expand Down
11 changes: 9 additions & 2 deletions docs/src/examples/disable/ActualDisable.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<script setup lang="ts">
import { ref } from "vue"
import { ref, onBeforeUnmount } from "vue";
import { useAutoAnimate } from "../../../../src/vue"

const intervalID = ref();

const balls = ref(["red", "green", "blue"])
const [parent, enable] = useAutoAnimate({ duration: 500 })

Expand All @@ -10,9 +12,14 @@ function toggle() {
isEnabled.value ? enable(false) : enable(true)
isEnabled.value = !isEnabled.value
}
setInterval(() => {

intervalID.value = setInterval(() => {
balls.value.push(balls.value.shift()!)
}, 600)

onBeforeUnmount(() => {
clearInterval(intervalID.value);
});
</script>

<template>
Expand Down
11 changes: 9 additions & 2 deletions docs/src/examples/disable/disable.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<script setup>
import { ref } from "vue"
import { ref, onBeforeUnmount } from "vue";
import { useAutoAnimate } from "../../../../src/vue"

const intervalID = ref();

const balls = ref(["red", "green", "blue"])
const [parent, enable] = useAutoAnimate({ duration: 500 })

Expand All @@ -10,9 +12,14 @@ function toggle() {
isEnabled.value ? enable(false) : enable(true)
isEnabled.value = !isEnabled.value
}
setInterval(() => {

intervalID.value = setInterval(() => {
balls.value.push(balls.value.shift()!)
}, 600)

onBeforeUnmount(() => {
clearInterval(intervalID.value);
});
</script>

<template>
Expand Down