UNPKG

@sv-use/core

Version:

A collection of Svelte 5 utilities.

36 lines (35 loc) 1.03 kB
import { onDestroy } from 'svelte'; import { BROWSER } from 'esm-env'; import { handleEventListener } from '../handle-event-listener/index.svelte.js'; import { noop } from '../__internal__/utils.svelte.js'; /** * Retrieves information about the mouse. * @param options Additional options to customize the behavior. * @see https://svelte-librarian.github.io/sv-use/docs/core/get-mouse */ export function getMouse(options = {}) { const { autoCleanup = true, initial = { x: 0, y: 0 }, onMove = noop } = options; let cleanup = noop; let _x = $state(initial.x); let _y = $state(initial.y); if (BROWSER) { cleanup = handleEventListener('mousemove', onMouseMove, { autoCleanup }); } if (autoCleanup) { onDestroy(() => cleanup()); } function onMouseMove(event) { _x = event.pageX; _y = event.pageY; onMove(event); } return { get x() { return _x; }, get y() { return _y; }, cleanup }; }