beyonk-group / svelte-carousel

A super lightweight, super simple Carousel for Svelte 3
MIT License
213 stars 38 forks source link

fix: resets interval timer on control click #56

Open kindoflew opened 3 years ago

kindoflew commented 3 years ago

This fixes the second half of #45. I set it up as an action:

<button class="left" on:click={left} use:resetInterval={autoplay} aria-label="left">

...

function resetInterval(node, condition) {
    function handleReset(event) {
        pause();
        resume();
    }

    if(condition) {
        node.addEventListener('click', handleReset);
    }

    return {
        destroy() {
            node.removeEventListener('click', handleReset);
        }
    }
}

REPL Example: https://svelte.dev/repl/a363db348ba4485d965c5b5464428a73?version=3.31.2

I didn't try implementing the first part of the issue (pause on hover) because I noticed that it was a part of the API and I wasn't sure if you wanted it to remain that way or if you'd rather it become a standard feature. If you did want that change I'd be happy to submit another PR for it.

Since this is an "under-the-hood" fix I didn't edit anything in the README. Let me know if anything is off! Thanks!