UNPKG

@dvcol/svelte-utils

Version:

Svelte library for common utility functions and constants

29 lines (28 loc) 960 B
import { useSwipe } from './touch.svelte.js'; /** * Register touch events handler for swipe detection. * * @param node * @param options */ export const swipe = (node, options) => { let handlers; function destroy() { if (handlers?.ontouchstart) node.removeEventListener('touchstart', handlers.ontouchstart); if (handlers?.ontouchend) node.removeEventListener('touchend', handlers.ontouchend); } function update(_options) { destroy(); const hooks = typeof _options === 'function' ? { onSwipe: _options } : _options; handlers = useSwipe({ onSwipe: hooks?.onSwipe }, { container: node, ...hooks?.tolerances }, hooks?.scroll); node.addEventListener('touchstart', handlers.ontouchstart, { passive: true }); node.addEventListener('touchend', handlers.ontouchend, { passive: true }); } update(options); return { update, destroy, }; };