#49 : Add tappable event handlers
This commit is contained in:
@@ -1,21 +1,15 @@
|
|||||||
// focusin event
|
// focusin event
|
||||||
export function addFocusinEventListener(source, cb) {
|
export function addFocusinEventListener(source, cb) {
|
||||||
source.addEventListener('mouseenter', cb)
|
source.addEventListener('mouseenter', cb)
|
||||||
source.addEventListener('touchstart', cb)
|
|
||||||
}
|
}
|
||||||
export function removeFocusinEventListener(source, cb) {
|
export function removeFocusinEventListener(source, cb) {
|
||||||
source.removeEventListener('mouseenter', cb)
|
source.removeEventListener('mouseenter', cb)
|
||||||
source.removeEventListener('touchstart', cb)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// focusout event
|
// focusout event
|
||||||
export function addFocusoutEventListener(source, cb) {
|
export function addFocusoutEventListener(source, cb) {
|
||||||
source.addEventListener('mouseleave', cb)
|
source.addEventListener('mouseleave', cb)
|
||||||
source.addEventListener('touchend', cb)
|
|
||||||
source.addEventListener('touchcancel', cb)
|
|
||||||
}
|
}
|
||||||
export function removeFocusoutEventListener(source, cb) {
|
export function removeFocusoutEventListener(source, cb) {
|
||||||
source.removeEventListener('mouseleave', cb)
|
source.removeEventListener('mouseleave', cb)
|
||||||
source.removeEventListener('touchend', cb)
|
|
||||||
source.removeEventListener('touchcancel', cb)
|
|
||||||
}
|
}
|
||||||
|
|||||||
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)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
import { NEXT, PREV } from '../../direction'
|
import { NEXT, PREV } from '../../direction'
|
||||||
import { swipeable } from '../../actions/swipeable'
|
import { swipeable } from '../../actions/swipeable'
|
||||||
import { focusable } from '../../actions/focusable'
|
import { focusable } from '../../actions/focusable'
|
||||||
|
import { tappable } from '../../actions/tappable'
|
||||||
import {
|
import {
|
||||||
addResizeEventListener,
|
addResizeEventListener,
|
||||||
removeResizeEventListener
|
removeResizeEventListener
|
||||||
@@ -292,9 +293,13 @@
|
|||||||
function handleSwipeEnd() {
|
function handleSwipeEnd() {
|
||||||
showPage(currentPageIndex)
|
showPage(currentPageIndex)
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleFocused(event) {
|
function handleFocused(event) {
|
||||||
focused = event.detail.value
|
focused = event.detail.value
|
||||||
}
|
}
|
||||||
|
function handleTapped() {
|
||||||
|
focused = !focused
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="sc-carousel__carousel-container">
|
<div class="sc-carousel__carousel-container">
|
||||||
@@ -315,6 +320,9 @@
|
|||||||
bind:this={pageWindowElement}
|
bind:this={pageWindowElement}
|
||||||
use:focusable
|
use:focusable
|
||||||
on:focused={handleFocused}
|
on:focused={handleFocused}
|
||||||
|
|
||||||
|
use:tappable
|
||||||
|
on:tapped={handleTapped}
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
class="sc-carousel__pages-container"
|
class="sc-carousel__pages-container"
|
||||||
|
|||||||
Reference in New Issue
Block a user