refactor(timeout): move setTimeout to afterUpdate method

This commit is contained in:
Eric Liu 2019-12-25 06:21:09 -08:00
commit 2cac297abf
3 changed files with 23 additions and 22 deletions

View file

@ -6,19 +6,25 @@
export let feedbackTimeout = 2000;
export let style = undefined;
import { onDestroy } from 'svelte';
import { afterUpdate, onDestroy } from 'svelte';
import Copy16 from 'carbon-icons-svelte/lib/Copy16';
import { cx } from '../../lib';
let animation = undefined;
let timeoutId = undefined;
onDestroy(() => {
if (timeoutId !== undefined) {
window.clearTimeout(timeoutId);
timeoutId = undefined;
afterUpdate(() => {
if (animation === 'fade-in') {
timeoutId = window.setTimeout(() => {
animation = 'fade-out';
}, feedbackTimeout);
}
});
onDestroy(() => {
window.clearTimeout(timeoutId);
timeoutId = undefined;
});
</script>
<button
@ -30,9 +36,6 @@
on:click
on:click={() => {
animation = 'fade-in';
timeoutId = window.setTimeout(() => {
animation = 'fade-out';
}, feedbackTimeout);
}}
on:mouseover
on:mouseenter