diff --git a/src/actions/focusable/event.js b/src/actions/focusable/event.js index a52ddaf..7bd13f4 100644 --- a/src/actions/focusable/event.js +++ b/src/actions/focusable/event.js @@ -1,21 +1,15 @@ // focusin event export function addFocusinEventListener(source, cb) { source.addEventListener('mouseenter', cb) - source.addEventListener('touchstart', cb) } export function removeFocusinEventListener(source, cb) { source.removeEventListener('mouseenter', cb) - source.removeEventListener('touchstart', cb) } // focusout event export function addFocusoutEventListener(source, cb) { source.addEventListener('mouseleave', cb) - source.addEventListener('touchend', cb) - source.addEventListener('touchcancel', cb) } export function removeFocusoutEventListener(source, cb) { source.removeEventListener('mouseleave', cb) - source.removeEventListener('touchend', cb) - source.removeEventListener('touchcancel', cb) } diff --git a/src/actions/tappable/event.js b/src/actions/tappable/event.js new file mode 100644 index 0000000..855e11d --- /dev/null +++ b/src/actions/tappable/event.js @@ -0,0 +1,15 @@ +// tap start event +export function addFocusinEventListener(source, cb) { + source.addEventListener('touchstart', cb) +} +export function removeFocusinEventListener(source, cb) { + source.removeEventListener('touchstart', cb) +} + +// tap end event +export function addFocusoutEventListener(source, cb) { + source.addEventListener('touchend', cb) +} +export function removeFocusoutEventListener(source, cb) { + source.removeEventListener('touchend', cb) +} diff --git a/src/actions/tappable/index.js b/src/actions/tappable/index.js new file mode 100644 index 0000000..807b7a4 --- /dev/null +++ b/src/actions/tappable/index.js @@ -0,0 +1 @@ +export * from './tappable' diff --git a/src/actions/tappable/tappable.js b/src/actions/tappable/tappable.js new file mode 100644 index 0000000..352a5eb --- /dev/null +++ b/src/actions/tappable/tappable.js @@ -0,0 +1,34 @@ +import { createDispatcher } from '../../utils/event' +import { + addFocusinEventListener, + removeFocusinEventListener, + addFocusoutEventListener, + removeFocusoutEventListener, +} from './event' + +let timeStart + +export function tappable(node) { + const dispatch = createDispatcher(node) + + function handleTapstart() { + timeStart = Date.now() + } + + function handleTapend() { + const diffMs = Date.now() - timeStart + if (diffMs <= 200) { + dispatch('tapped') + } + } + + addFocusinEventListener(node, handleTapstart) + addFocusoutEventListener(node, handleTapend) + + return { + destroy() { + removeFocusinEventListener(node, handleTapstart) + removeFocusoutEventListener(node, handleTapend) + }, + } +} diff --git a/src/components/Carousel/Carousel.svelte b/src/components/Carousel/Carousel.svelte index 0589c37..4eefe5b 100644 --- a/src/components/Carousel/Carousel.svelte +++ b/src/components/Carousel/Carousel.svelte @@ -7,6 +7,7 @@ import { NEXT, PREV } from '../../direction' import { swipeable } from '../../actions/swipeable' import { focusable } from '../../actions/focusable' + import { tappable } from '../../actions/tappable' import { addResizeEventListener, removeResizeEventListener @@ -292,9 +293,13 @@ function handleSwipeEnd() { showPage(currentPageIndex) } + function handleFocused(event) { focused = event.detail.value } + function handleTapped() { + focused = !focused + }