#49 : Add tappable event handlers

This commit is contained in:
Vadim
2021-07-24 16:16:23 +03:00
parent da7d8ac705
commit e837e09789
5 changed files with 58 additions and 6 deletions

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -0,0 +1 @@
export * from './tappable'

View File

@@ -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)
},
}
}