-
Notifications
You must be signed in to change notification settings - Fork 357
[#2142] touch events handling #3150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
aluarius
wants to merge
8
commits into
epicmaxco:develop
Choose a base branch
from
aluarius:feat/2142-touch-events
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e2a4be0
[#2142] use touch composable
aluarius 44f4406
Merge branch 'develop' into feat/2142-touch-events
aluarius 37d0af5
refactor: minor description fix
aluarius cbf544a
refactor: improve demo for mobile devices
rustem-nasyrov ce418d3
feat: add demo examples
rustem-nasyrov 01ed18d
fix: use responsive input width
rustem-nasyrov 6b34139
Merge branch 'develop' into feat/2142-touch-events
rustem-nasyrov 8ae6d98
refactor: review suggestions
rustem-nasyrov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { ref, onBeforeUnmount, type Ref } from 'vue' | ||
|
||
import { useEvent, useWindow } from './' | ||
|
||
type UseTouchCallback = (e: TouchEvent) => void | ||
type UseTouchCallbacks = { short: UseTouchCallback, long: UseTouchCallback, double?: UseTouchCallback } | ||
|
||
/** | ||
* Provides touch events handling. | ||
* | ||
* TODO: make a part of useMouseEvent (implement it first) hook. | ||
* | ||
* @param target - element that will be listened for touch start event. | ||
* @param callbacks - callbacks for short, long and double touch events. | ||
* @param ignoreDoubleClicks - if true, double clicks will be ignored. | ||
* @param timings - timings for long and double touch events. | ||
* | ||
* @example | ||
* ```ts | ||
* useTouch(target, { | ||
* short: () => {}, | ||
* long: () => {}, | ||
* double: () => {}, | ||
* ignoreDoubleClicks: true, | ||
* }) | ||
* ``` | ||
*/ | ||
export const useTouch = ( | ||
target: Ref<HTMLElement>, | ||
callbacks: UseTouchCallbacks, | ||
ignoreDoubleClicks = false, | ||
timings = { long: 500, double: 250 }, | ||
) => { | ||
const window = useWindow() | ||
|
||
if (!window.value || !('ontouchstart' in window.value)) { return } | ||
|
||
let previousTouchTime = 0 | ||
let shortTouchTimer: ReturnType<typeof setTimeout> | undefined | ||
|
||
const onTouchStart = (event: TouchEvent) => { | ||
// preventing same (click, dblclick, contextmenu) mouse events to be triggered | ||
event.preventDefault() | ||
event.stopPropagation() | ||
|
||
const longTouchTimer = setTimeout(() => { | ||
callbacks.long(event) | ||
|
||
removeTouchCancelListeners() | ||
}, timings.long) | ||
|
||
const clearTouchTimeouts = () => { | ||
if (longTouchTimer) { clearTimeout(longTouchTimer) } | ||
if (shortTouchTimer) { clearTimeout(shortTouchTimer) } | ||
} | ||
|
||
const cancelTouch = () => { | ||
clearTouchTimeouts() | ||
removeTouchCancelListeners() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. useEvent must be used outside of onTouchStart function and there will no be needed to add removeTouchCancelListeners... |
||
|
||
// if long touch callback is released, short/double won't be called because of removeTouchCancelListeners | ||
if (ignoreDoubleClicks) { | ||
callbacks.short(event) | ||
|
||
return | ||
} | ||
|
||
const isDoubleClick = previousTouchTime && (Date.now() - previousTouchTime < timings.double) | ||
if (isDoubleClick) { | ||
callbacks.double && callbacks.double(event) | ||
|
||
previousTouchTime = 0 | ||
} else { | ||
shortTouchTimer = setTimeout(() => { | ||
callbacks.short(event) | ||
}, timings.double) | ||
|
||
previousTouchTime = Date.now() | ||
} | ||
} | ||
|
||
const { removeListeners: removeTouchCancelListeners } = useEvent(['touchmove', 'touchend', 'touchcancel'], cancelTouch, true) | ||
} | ||
|
||
const { removeListeners: removeTouchStartListeners } = useEvent('touchstart', onTouchStart, target) | ||
onBeforeUnmount(removeTouchStartListeners) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer direct import by path to prevent circular dependecies...