#49 : Add tappable event handlers
This commit is contained in:
15
src/actions/tappable/event.js
Normal file
15
src/actions/tappable/event.js
Normal 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)
|
||||
}
|
||||
1
src/actions/tappable/index.js
Normal file
1
src/actions/tappable/index.js
Normal file
@@ -0,0 +1 @@
|
||||
export * from './tappable'
|
||||
34
src/actions/tappable/tappable.js
Normal file
34
src/actions/tappable/tappable.js
Normal 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)
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user