UNPKG

@elastic/eui

Version:

Elastic UI Component Library

107 lines (106 loc) 5.62 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } 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; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /* * 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 PropTypes from "prop-types"; 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.propTypes = { /** * ReactNode to render as this component's content */ children: PropTypes.element.isRequired, onOutsideClick: PropTypes.func.isRequired, isDisabled: PropTypes.bool, onMouseDown: PropTypes.func, onMouseUp: PropTypes.func, onTouchStart: PropTypes.func, onTouchEnd: PropTypes.func }; EuiOutsideClickDetector.displayName = 'EuiOutsideClickDetector';