#49 : Separate handlers for touchable and non toachable devices
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { createDispatcher } from '../../utils/event'
|
||||
import { get } from '../../utils/object'
|
||||
import {
|
||||
addFocusinEventListener,
|
||||
removeFocusinEventListener,
|
||||
@@ -7,23 +8,29 @@ import {
|
||||
} from './event'
|
||||
import { TAP_DURATION_MS } from '../../units'
|
||||
|
||||
export function tappable(node) {
|
||||
const dispatch = createDispatcher(node)
|
||||
/**
|
||||
* tappable events are for touchable devices only
|
||||
*/
|
||||
export function tappable(node, options) {
|
||||
// pass custom dispatch fn in order to re-translate dispatched event
|
||||
const dispatch = get(options, 'dispatch', createDispatcher(node))
|
||||
|
||||
let tapStartedAt = 0
|
||||
|
||||
function handleTapstart() {
|
||||
tapStartedAt = Date.now()
|
||||
addFocusoutEventListener(node, handleTapend)
|
||||
}
|
||||
|
||||
function handleTapend() {
|
||||
removeFocusoutEventListener(node, handleTapend)
|
||||
if (Date.now() - tapStartedAt <= TAP_DURATION_MS) {
|
||||
dispatch('tapped')
|
||||
}
|
||||
}
|
||||
|
||||
addFocusinEventListener(node, handleTapstart)
|
||||
addFocusoutEventListener(node, handleTapend)
|
||||
|
||||
|
||||
return {
|
||||
destroy() {
|
||||
removeFocusinEventListener(node, handleTapstart)
|
||||
|
||||
Reference in New Issue
Block a user