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

feat(useMousePressed): add capture option #3392

Merged
merged 3 commits into from
Oct 7, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 19 additions & 9 deletions packages/core/useMousePressed/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
*/
drag?: boolean


Check failure on line 24 in packages/core/useMousePressed/index.ts

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed

Check failure on line 24 in packages/core/useMousePressed/index.ts

View workflow job for this annotation

GitHub Actions / lint

More than 1 blank line not allowed
/**
* Add event listerners with the `capture` option set to `true`
* (see [MDN](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#capture))
*
* @default false
*/
capture?: boolean

/**
* Initial values
*
Expand All @@ -44,6 +53,7 @@
const {
touch = true,
drag = true,
capture = false,
initialValue = false,
window = defaultWindow,
} = options
Expand All @@ -69,23 +79,23 @@

const target = computed(() => unrefElement(options.target) || window)

useEventListener(target, 'mousedown', onPressed('mouse'), { passive: true })
useEventListener(target, 'mousedown', onPressed('mouse'), { passive: true, capture })

useEventListener(window, 'mouseleave', onReleased, { passive: true })
useEventListener(window, 'mouseup', onReleased, { passive: true })
useEventListener(window, 'mouseleave', onReleased, { passive: true, capture })
useEventListener(window, 'mouseup', onReleased, { passive: true, capture })

if (drag) {
useEventListener(target, 'dragstart', onPressed('mouse'), { passive: true })
useEventListener(target, 'dragstart', onPressed('mouse'), { passive: true, capture })

useEventListener(window, 'drop', onReleased, { passive: true })
useEventListener(window, 'dragend', onReleased, { passive: true })
useEventListener(window, 'drop', onReleased, { passive: true, capture })
useEventListener(window, 'dragend', onReleased, { passive: true, capture })
}

if (touch) {
useEventListener(target, 'touchstart', onPressed('touch'), { passive: true })
useEventListener(target, 'touchstart', onPressed('touch'), { passive: true, capture })

useEventListener(window, 'touchend', onReleased, { passive: true })
useEventListener(window, 'touchcancel', onReleased, { passive: true })
useEventListener(window, 'touchend', onReleased, { passive: true, capture })
useEventListener(window, 'touchcancel', onReleased, { passive: true, capture })
}

return {
Expand Down