{"version":3,"file":"autoplay.mjs","names":[],"sources":["../../../src/plugins/autoplay.tsx"],"sourcesContent":["import { makeTimer } from \"@solid-primitives/timer\";\nimport { KeenSliderInstance } from \"keen-slider\";\nimport { Accessor, createEffect, onCleanup } from \"solid-js\";\n\n/**\n * Provides an autoplay plugin.\n *\n * Call this from within a component (or other reactive owner) so the pause\n * signal is tracked and the timer is cleaned up with it.\n *\n * @param interval - Interval in milliseconds for switching slides\n * @param options - Options to customize the autoplay\n * @param options.pause - A pause signal to control the autoplay\n * @param options.pauseOnDrag - Denotes of the autoplay should pause on drag.\n * @param options.animation - Allows for control of duration and easing.\n * @param options.duration - Duration for transitioning the slide.\n * @param options.easing - Easing function for the transition animation.\n *\n * @example\n * ```ts\n * const [create] = createSlider({}, autoplay(1000, {}));\n * ```\n */\nexport const autoplay = (\n  interval: number = 1000,\n  options: {\n    pause?: Accessor<boolean>;\n    pauseOnDrag?: boolean;\n    animation?: {\n      duration?: number;\n      easing?: (t: number) => number;\n    };\n  } = {},\n) => {\n  let slider: KeenSliderInstance | undefined;\n  let dispose: (() => void) | undefined;\n  let paused = false;\n  const stop = () => {\n    dispose?.();\n    dispose = undefined;\n  };\n  const start = () => {\n    stop();\n    dispose = makeTimer(\n      () =>\n        slider!.moveToIdx(\n          slider!.track.details.position + 1,\n          true,\n          options.animation,\n        ),\n      interval,\n      setInterval,\n    );\n  };\n  const sync = () => {\n    if (!slider) return;\n    paused ? stop() : start();\n  };\n  // Reactive pieces are created here, in the caller's owner. Plugins are\n  // invoked by KeenSlider outside any owner, so they must not create them.\n  createEffect(\n    () => !!options.pause && options.pause() !== false,\n    (isPaused) => {\n      paused = isPaused;\n      sync();\n    },\n  );\n  onCleanup(stop);\n  return (instance: KeenSliderInstance) => {\n    slider = instance;\n    // Pause the slider on drag\n    if (options.pauseOnDrag !== false) {\n      slider.on(\"dragStarted\", stop);\n    }\n    slider.on(\"created\", sync);\n    slider.on(\"destroyed\", stop);\n  };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAsBA,MAAa,YAAY,WAAmB,KAAK,UAO7C,CAAC,MAAM;CACV,IAAI;CACJ,IAAI;CACJ,IAAI,SAAS;CACb,MAAM,aAAa;EAClB,UAAU;EACV,UAAU,KAAA;CACX;CACA,MAAM,cAAc;EACnB,KAAK;EACL,UAAU,gBAAgB,OAAQ,UAAU,OAAQ,MAAM,QAAQ,WAAW,GAAG,MAAM,QAAQ,SAAS,GAAG,UAAU,WAAW;CAChI;CACA,MAAM,aAAa;EAClB,IAAI,CAAC,QAAQ;EACb,SAAS,KAAK,IAAI,MAAM;CACzB;CAGA,mBAAmB,CAAC,CAAC,QAAQ,SAAS,QAAQ,MAAM,MAAM,QAAQ,aAAa;EAC9E,SAAS;EACT,KAAK;CACN,CAAC;CACD,UAAU,IAAI;CACd,QAAQ,aAAiC;EACxC,SAAS;EAET,IAAI,QAAQ,gBAAgB,OAC3B,OAAO,GAAG,eAAe,IAAI;EAE9B,OAAO,GAAG,WAAW,IAAI;EACzB,OAAO,GAAG,aAAa,IAAI;CAC5B;AACD"}