-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #404 from PrefectHQ/nicholas/feature-use-mouse-pos…
…ition-2024-03-15 Feature: `useMousePosition`
- Loading branch information
Showing
5 changed files
with
95 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# useMedia | ||
The `useMousePosition` composition is a utility composition that passively tracks the position of the mouse as well as the position of the mouse at last click | ||
|
||
## Example | ||
```typescript | ||
import { useMousePosition } from '@prefecthq/vue-compositions' | ||
|
||
const { position, positionAtLastClick } = useMousePosition() | ||
``` | ||
|
||
## Arguments | ||
None | ||
|
||
## Returns | ||
```ts | ||
type MousePosition { | ||
x: number, | ||
y: number | ||
} | ||
|
||
type UseMousePosition { | ||
position: MousePosition, | ||
positionAtLastClick: MousePosition | ||
} | ||
``` |
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 @@ | ||
export * from './useMousePosition' |
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,67 @@ | ||
import { onScopeDispose, reactive, ref } from 'vue' | ||
import { useGlobalEventListener } from '@/useGlobalEventListener' | ||
|
||
export type MousePosition = { | ||
x: number, | ||
y: number, | ||
} | ||
|
||
export type UseMousePosition = { | ||
position: MousePosition, | ||
positionAtLastClick: MousePosition, | ||
} | ||
|
||
const position = reactive<MousePosition>({ x: 0, y: 0 }) | ||
const positionAtLastClick = reactive<MousePosition>({ x: 0, y: 0 }) | ||
|
||
const updatePositionAtLastClick = (): void => { | ||
Object.assign(positionAtLastClick, position) | ||
} | ||
|
||
const updateMousePosition = (event: MouseEvent): void => { | ||
position.x = event.clientX | ||
position.y = event.clientY | ||
|
||
if (positionAtLastClick.x === 0 && positionAtLastClick.y === 0) { | ||
updatePositionAtLastClick() | ||
} | ||
} | ||
|
||
const listeners = ref(0) | ||
|
||
const { add: addMouseMoveEventListener, remove: removeMouseMoveEventListener } = useGlobalEventListener('mousemove', updateMousePosition, { passive: true }) | ||
const { add: addClickEventListener, remove: removeClickEventListener } = useGlobalEventListener('click', updatePositionAtLastClick, { capture: true }) | ||
const { add: addContextMenuEventListener, remove: removeContextMenuEventListener } = useGlobalEventListener('contextmenu', updatePositionAtLastClick, { capture: true }) | ||
|
||
function tryTeardownEventListeners(): void { | ||
if (listeners.value > 0) { | ||
return | ||
} | ||
|
||
removeMouseMoveEventListener() | ||
removeClickEventListener() | ||
removeContextMenuEventListener() | ||
} | ||
|
||
function addEventListeners(): void { | ||
// These have no effect if the event listeners are already added | ||
addMouseMoveEventListener() | ||
addClickEventListener() | ||
addContextMenuEventListener() | ||
} | ||
|
||
export function useMousePosition(): UseMousePosition { | ||
listeners.value += 1 | ||
|
||
addEventListeners() | ||
|
||
onScopeDispose(() => { | ||
listeners.value -= 1 | ||
tryTeardownEventListeners() | ||
}) | ||
|
||
return { | ||
position, | ||
positionAtLastClick, | ||
} | ||
} |