Skip to content

Commit bf10b6f

Browse files
authored
Merge pull request #2312 from Particular/john/improve-cleartimeout
Clears previous timeout when copying
2 parents a6522d3 + ac15f13 commit bf10b6f

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/Frontend/src/components/CopyToClipboard.vue

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang="ts">
22
import { Tippy, TippyComponent } from "vue-tippy";
3-
import { useTemplateRef } from "vue";
3+
import { ref, useTemplateRef, watch } from "vue";
44
55
const props = withDefaults(
66
defineProps<{
@@ -11,14 +11,16 @@ const props = withDefaults(
1111
);
1212
1313
const tippyRef = useTemplateRef<TippyComponent | null>("tippyRef");
14-
let timeoutId: number;
14+
const timeoutId = ref(0);
1515
1616
async function copyToClipboard() {
1717
await navigator.clipboard.writeText(props.value);
18-
window.clearTimeout(timeoutId);
18+
1919
tippyRef.value?.show();
20-
timeoutId = window.setTimeout(() => tippyRef.value?.hide(), 3000);
20+
timeoutId.value = window.setTimeout(() => tippyRef.value?.hide(), 3000);
2121
}
22+
23+
watch(timeoutId, (_, previousTimeoutId) => window.clearTimeout(previousTimeoutId));
2224
</script>
2325

2426
<template>

0 commit comments

Comments
 (0)