Skip to content

Commit

Permalink
feat(useMouse): mouse position based by option.type (#993)
Browse files Browse the repository at this point in the history
Co-authored-by: Jelf <haichao.liang@parameters.cn>
  • Loading branch information
okxiaoliang4 and okxiaoliang4 committed Dec 1, 2021
1 parent 62adc22 commit 819f449
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions packages/core/useMouse/index.ts
Expand Up @@ -4,6 +4,12 @@ import { ConfigurableWindow, defaultWindow } from '../_configurable'
import { Position } from '../types'

export interface MouseOptions extends ConfigurableWindow {
/**
* Mouse position based by page or client
*
* @default 'page'
*/
type?: 'page' | 'client'
/**
* Listen to `touchmove` events
*
Expand Down Expand Up @@ -34,6 +40,7 @@ export type MouseSourceType = 'mouse' | 'touch' | null
*/
export function useMouse(options: MouseOptions = {}) {
const {
type = 'page',
touch = true,
resetOnTouchEnds = false,
initialValue = { x: 0, y: 0 },
Expand All @@ -45,8 +52,14 @@ export function useMouse(options: MouseOptions = {}) {
const sourceType = ref<MouseSourceType>(null)

const mouseHandler = (event: MouseEvent) => {
x.value = event.pageX
y.value = event.pageY
if (type === 'page') {
x.value = event.pageX
y.value = event.pageY
}
else if (type === 'client') {
x.value = event.clientX
y.value = event.clientY
}
sourceType.value = 'mouse'
}
const reset = () => {
Expand All @@ -55,8 +68,15 @@ export function useMouse(options: MouseOptions = {}) {
}
const touchHandler = (event: TouchEvent) => {
if (event.touches.length > 0) {
x.value = event.touches[0].clientX
y.value = event.touches[0].clientY
const touch = event.touches[0]
if (type === 'page') {
x.value = touch.pageX
y.value = touch.pageY
}
else if (type === 'client') {
x.value = touch.clientX
y.value = touch.clientY
}
sourceType.value = 'touch'
}
}
Expand Down

0 comments on commit 819f449

Please sign in to comment.