getMousePressed
Category Sensors
Reactive values for mouse/touch/drag pressing state.
Demo
{
"isPressed": false,
"type": null
} Click anywhere in the document
Usage
Type definitions
import type { CleanupFunction } from '../../__internal__/types.js';
type GetMousePressedPressAndReleaseEvent<EnableTouch extends boolean, EnableDrag extends boolean> = EnableTouch extends true ? EnableDrag extends true ? MouseEvent | TouchEvent | DragEvent : MouseEvent | TouchEvent : EnableDrag extends true ? MouseEvent | DragEvent : MouseEvent;
type GetMousePressedOptions<EnableTouch extends boolean, EnableDrag extends boolean> = {
/**
* Whether to auto-cleanup the event listeners or not.
*
* If set to `true`, it must run in the component initialization lifecycle.
* @default true
*/
autoCleanup?: boolean;
/**
* Only trigger if the click happened inside `target`.
* @default window
*/
target?: Window | HTMLElement;
/**
* Whether to detect touch events or not.
* @default true
*/
enableTouch?: EnableTouch;
/**
* Whether to detect drag events or not.
* @default true
*/
enableDrag?: EnableDrag;
/**
* Callback for when the mouse/tap is pressed.
* @default () => {}
*/
onPressed?: (event: GetMousePressedPressAndReleaseEvent<EnableTouch, EnableDrag>) => void;
/**
* Callback for when the mouse/tap is released.
* @default () => {}
*/
onReleased?: (event: GetMousePressedPressAndReleaseEvent<EnableTouch, EnableDrag>) => void;
};
type GetMousePressedType = 'mouse' | 'touch' | null;
type GetMousePressedReturn = {
readonly isPressed: boolean;
readonly type: GetMousePressedType;
/**
* Cleans up the event listeners.
* @note Is called automatically if `options.autoCleanup` is set to `true`.
*/
cleanup: CleanupFunction;
};
/**
* Reactive values for mouse/touch/drag pressing state.
* @param options Additional options to customize the behavior.
* @see https://svelte-librarian.github.io/sv-use/docs/core/sensors/get-mouse-pressed
*/
export declare function getMousePressed<EnableTouch extends boolean = true, EnableDrag extends boolean = true>(options?: GetMousePressedOptions<EnableTouch, EnableDrag>): GetMousePressedReturn;
export {};