Skip to content
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

Replaces click #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ Vue.directive('longclick', longClickInstance)
| Prop | Type | Default | Description |
|-----------------------|-----------------|-------------|------------------------------------------|
| delay | Integer (milliseconds) | 400 | Delay until long click function is fired |
| interval | Integer (milliseconds) | 50 | If value is greater than 0, handler function will be fire every `interval` milliseconds when component is pressed
| interval | Integer (milliseconds) | 50 | If value is greater than 0, handler function will be fire every `interval` milliseconds when component is pressed |
| replacesClick | Boolean | false | Suppress regular click events during long click |

## Development

Expand Down
20 changes: 19 additions & 1 deletion src/directives/longclick.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default ({delay = 400, interval = 50}) => ({
export default ({delay = 400, interval = 50, replacesClick = false}) => ({
bind: function (el, binding, vNode) {
if (typeof binding.value !== 'function') {
const compName = vNode.context.name
Expand All @@ -9,6 +9,7 @@ export default ({delay = 400, interval = 50}) => ({

let pressTimer = null
let pressInterval = null
let suppressNextClick = false

const start = (e) => {
if (e.type === 'click' && e.button !== 0) {
Expand Down Expand Up @@ -37,13 +38,30 @@ export default ({delay = 400, interval = 50}) => ({
clearInterval(pressInterval)
pressInterval = null
}

suppressNextClick = false
}

const click = (e) => {
if (suppressNextClick) {
// prevent any handlers for regular click firing
e.stopImmediatePropagation()
}

cancel()
}

// Run Function
const handler = (e) => {
binding.value(e)

suppressNextClick = replacesClick
}

;['mousedown', 'touchstart'].forEach(e => el.addEventListener(e, start))
;['click', 'mouseout', 'touchend', 'touchcancel'].forEach(e => el.addEventListener(e, cancel))

// suppress relevant click events before they are handled anywhere else
el.addEventListener('click', click, { capture: true })
}
})