UNPKG

@elastic/eui

Version:

Elastic UI Component Library

91 lines (90 loc) 4.38 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } /* * 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. */ import { Children, cloneElement, useRef, useEffect, forwardRef } from 'react'; import { useGeneratedHtmlId } from '../../services/accessibility'; export var EuiOutsideClickDetector = /*#__PURE__*/forwardRef(function (props, ref) { // the id is used to identify which EuiOutsideClickDetector // is the source of a click event; as the click event bubbles // up and reaches the click detector's child component the // id value is stamped on the event. This id is inspected // in the document's click handler, and if the id doesn't // exist or doesn't match this detector's id, then trigger // the outsideClick callback. // // Taking this approach instead of checking if the event's // target element exists in this component's DOM sub-tree is // necessary for handling clicks originating from children // rendered through React's portals (EuiPortal). The id tracking // works because React guarantees the event bubbles through the // virtual DOM and executes EuiClickDetector's onClick handler, // stamping the id even though the event originates outside // this component's reified DOM tree. var id = useGeneratedHtmlId(); var capturedDownIds = useRef([]); useEffect(function () { function onClickOutside(e) { var isDisabled = props.isDisabled, onOutsideClick = props.onOutsideClick; if (isDisabled) { capturedDownIds.current = []; return; } var event = e; if (event.euiGeneratedBy && event.euiGeneratedBy.includes(id) || capturedDownIds.current.includes(id)) { capturedDownIds.current = []; return; } capturedDownIds.current = []; return onOutsideClick(event); } document.addEventListener('mouseup', onClickOutside); document.addEventListener('touchend', onClickOutside); return function () { document.removeEventListener('mouseup', onClickOutside); document.removeEventListener('touchend', onClickOutside); }; }, [props.isDisabled, props.onOutsideClick]); function onChildClick(event, cb) { // to support nested click detectors, build an array // of detector ids that have been encountered; if (event.nativeEvent.hasOwnProperty('euiGeneratedBy')) { event.nativeEvent.euiGeneratedBy.push(id); } else { event.nativeEvent.euiGeneratedBy = [id]; } if (cb) cb(event); } function onChildMouseDown(event) { onChildClick(event, function (e) { var nativeEvent = e.nativeEvent; capturedDownIds.current = nativeEvent.euiGeneratedBy; if (props.onMouseDown) props.onMouseDown(e); if (props.onTouchStart) props.onTouchStart(e); }); } function onChildMouseUp(event) { onChildClick(event, function (e) { if (props.onMouseUp) props.onMouseUp(e); if (props.onTouchEnd) props.onTouchEnd(e); }); } var child = Children.only(props.children); var childProps = _objectSpread(_objectSpread({}, props.children.props), {}, { onMouseDown: onChildMouseDown, onTouchStart: onChildMouseDown, onMouseUp: onChildMouseUp, onTouchEnd: onChildMouseUp }, ref ? { ref: ref } : {}); return /*#__PURE__*/cloneElement(child, childProps); }); EuiOutsideClickDetector.displayName = 'EuiOutsideClickDetector';