UNPKG

@elastic/eui

Version:

Elastic UI Component Library

53 lines (51 loc) 1.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useIsPointerDown = useIsPointerDown; var _react = require("react"); /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0 and the Server Side Public License, v 1; you may not use this file except * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ /** * A hook that tracks whether the pointer is currently down/pressed. * Useful for detecting text selection in progress. */ function useIsPointerDown(container) { var isPointerDownRef = (0, _react.useRef)(false); (0, _react.useEffect)(function () { var handlePointerDown = function handlePointerDown(event) { if (container !== null && container !== void 0 && container.current && !container.current.contains(event.target)) { return; } isPointerDownRef.current = true; }; var handlePointerUpOrCancel = function handlePointerUpOrCancel() { isPointerDownRef.current = false; }; var handleVisibilityChange = function handleVisibilityChange() { if (document.visibilityState === 'hidden') { isPointerDownRef.current = false; } }; var controller = new AbortController(); var options = { capture: true, signal: controller.signal }; document.addEventListener('pointerdown', handlePointerDown, options); document.addEventListener('pointerup', handlePointerUpOrCancel, options); document.addEventListener('pointercancel', handlePointerUpOrCancel, options); document.addEventListener('visibilitychange', handleVisibilityChange, { signal: controller.signal }); return function () { controller.abort(); }; }, [container]); return isPointerDownRef; }