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

25 - useMouse - TS (With proper types + generic) #2702

Open
sky0matic opened this issue Apr 23, 2024 · 0 comments
Open

25 - useMouse - TS (With proper types + generic) #2702

sky0matic opened this issue Apr 23, 2024 · 0 comments

Comments

@sky0matic
Copy link

sky0matic commented Apr 23, 2024

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';

// Implement ...
function useEventListener<E extends Event>(target: EventTarget, event: keyof DocumentEventMap, callback: (event: E) => void) {
  onMounted(() => target.addEventListener(event, callback));
  onUnmounted(() => target.removeEventListener(event, callback));
}

// Implement ...
function useMouse() {
  const x = ref(0);
  const y = ref(0);

  useEventListener<MouseEvent>(window, 'mousemove', event => {
    x.value = event.x;
    y.value = event.y;
  });

  return { x, y };
}
const { x, y } = useMouse()
</script>

<template>Mouse position is at: {{ x }}, {{ y }}</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant