forked from vitest-dev/vitest
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(usePointer): new function (vitest-dev#728)
- Loading branch information
Showing
10 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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 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,17 @@ | ||
import { defineComponent, reactive } from 'vue-demi' | ||
import { usePointer, UsePointerOptions } from '@vueuse/core' | ||
|
||
export const UsePointer = defineComponent<UsePointerOptions>({ | ||
name: 'UsePointer', | ||
props: [ | ||
'pointerTypes', | ||
'initialValue', | ||
] as unknown as undefined, | ||
setup(props, { slots }) { | ||
const data = reactive(usePointer(props)) | ||
return () => { | ||
if (slots.default) | ||
return slots.default(data) | ||
} | ||
}, | ||
}) |
This file contains 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,11 @@ | ||
<script setup lang="ts"> | ||
import { reactive } from 'vue-demi' | ||
import { stringify } from '@vueuse/docs-utils' | ||
import { usePointer } from '.' | ||
const pointer = reactive(usePointer()) | ||
</script> | ||
|
||
<template> | ||
<pre class="select-none" style="touch-action: none">{{ pointer }}</pre> | ||
</template> |
This file contains 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,23 @@ | ||
--- | ||
category: Sensors | ||
--- | ||
|
||
# usePointer | ||
|
||
Reactive [pointer state](https://developer.mozilla.org/en-US/docs/Web/API/Pointer_events). | ||
|
||
## Basic Usage | ||
|
||
```js | ||
import { usePointer } from '@vueuse/core' | ||
|
||
const { x, y, pressure, pointerType } = usePointer() | ||
``` | ||
|
||
## Component | ||
```html | ||
<UsePointer v-slot="{ x, y }"> | ||
x: {{ x }} | ||
y: {{ y }} | ||
</UsePointer> | ||
``` |
This file contains 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,75 @@ | ||
import { MaybeRef, objectPick, toRefs } from '@vueuse/shared' | ||
import { ref, Ref } from 'vue-demi' | ||
import { useEventListener } from '../useEventListener' | ||
import { ConfigurableWindow, defaultWindow } from '../_configurable' | ||
import { PointerType, Position } from '../_types' | ||
|
||
export interface UsePointerState extends Position { | ||
pressure: number | ||
pointerId: number | ||
tiltX: number | ||
tiltY: number | ||
width: number | ||
height: number | ||
twist: number | ||
pointerType: PointerType | null | ||
} | ||
|
||
export interface UsePointerOptions extends ConfigurableWindow { | ||
/** | ||
* Pointer types that listen to. | ||
* | ||
* @default ['mouse', 'touch', 'pen'] | ||
*/ | ||
pointerTypes?: PointerType[] | ||
|
||
/** | ||
* Initial values | ||
*/ | ||
initialValue?: MaybeRef<Partial<UsePointerState>> | ||
} | ||
|
||
const defaultState: UsePointerState = /* #__PURE__ */ { | ||
x: 0, | ||
y: 0, | ||
pointerId: 0, | ||
pressure: 0, | ||
tiltX: 0, | ||
tiltY: 0, | ||
width: 0, | ||
height: 0, | ||
twist: 0, | ||
pointerType: null, | ||
} | ||
const keys = /* #__PURE__ */ Object.keys(defaultState) as (keyof UsePointerState)[] | ||
|
||
/** | ||
* Reactive pointer state. | ||
* | ||
* @see https://vueuse.org/usePointer | ||
* @param options | ||
*/ | ||
export function usePointer(options: UsePointerOptions = {}) { | ||
const { | ||
window = defaultWindow, | ||
} = options | ||
|
||
const state = ref(options.initialValue || {}) as unknown as Ref<UsePointerState> | ||
Object.assign(state.value, defaultState, state.value) | ||
|
||
const handler = (event: PointerEvent) => { | ||
if (options.pointerTypes && !options.pointerTypes.includes(event.pointerType as PointerType)) | ||
return | ||
|
||
state.value = objectPick(event, keys, false) as UsePointerState | ||
} | ||
|
||
if (window) { | ||
useEventListener(window, 'pointerdown', handler, { passive: true }) | ||
useEventListener(window, 'pointermove', handler, { passive: true }) | ||
} | ||
|
||
return { | ||
...toRefs(state), | ||
} | ||
} |
This file contains 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 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