UNPKG

@elastic/eui

Version:

Elastic UI Component Library

109 lines (108 loc) 5.15 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.EuiOutsideClickDetector = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = require("react"); var _propTypes = _interopRequireDefault(require("prop-types")); var _accessibility = require("../../services/accessibility"); 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) { (0, _defineProperty2.default)(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. */ var EuiOutsideClickDetector = exports.EuiOutsideClickDetector = /*#__PURE__*/(0, _react.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 = (0, _accessibility.useGeneratedHtmlId)(); var capturedDownIds = (0, _react.useRef)([]); (0, _react.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 = _react.Children.only(props.children); var childProps = _objectSpread(_objectSpread({}, props.children.props), {}, { onMouseDown: onChildMouseDown, onTouchStart: onChildMouseDown, onMouseUp: onChildMouseUp, onTouchEnd: onChildMouseUp }, ref ? { ref: ref } : {}); return /*#__PURE__*/(0, _react.cloneElement)(child, childProps); }); EuiOutsideClickDetector.propTypes = { /** * ReactNode to render as this component's content */ children: _propTypes.default.element.isRequired, onOutsideClick: _propTypes.default.func.isRequired, isDisabled: _propTypes.default.bool, onMouseDown: _propTypes.default.func, onMouseUp: _propTypes.default.func, onTouchStart: _propTypes.default.func, onTouchEnd: _propTypes.default.func }; EuiOutsideClickDetector.displayName = 'EuiOutsideClickDetector';