UNPKG

@elastic/eui

Version:

Elastic UI Component Library

753 lines (741 loc) 36.3 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); } var _excluded = ["anchorPosition", "button", "insert", "isOpen", "ownFocus", "children", "className", "closePopover", "panelClassName", "panelPaddingSize", "panelProps", "panelRef", "panelStyle", "popoverScreenReaderText", "popoverRef", "hasArrow", "arrowChildren", "repositionOnScroll", "repositionToCrossAxis", "zIndex", "attachToAnchor", "display", "offset", "onPositionChange", "buffer", "aria-label", "aria-labelledby", "aria-live", "container", "focusTrapProps", "initialFocus", "tabIndex"]; function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], t.indexOf(o) >= 0 || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; } 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 _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, 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); } function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } import PropTypes from "prop-types"; function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } /* * 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 React, { Component } from 'react'; import classNames from 'classnames'; import { focusable } from 'tabbable'; import { EuiFocusTrap } from '../focus_trap'; import { keys, getTransitionTimings, getWaitDuration, performOnFrame, htmlIdGenerator, focusTrapPubSub } from '../../services'; import { setMultipleRefs } from '../../services/hooks/useCombinedRefs'; import { EuiScreenReaderOnly } from '../accessibility'; import { EuiPortal } from '../portal'; import { EuiMutationObserver } from '../observer/mutation_observer'; import { findPopoverPosition, getElementZIndex } from '../../services/popover'; import { createRepositionOnScroll } from '../../services/popover/reposition_on_scroll'; import { EuiI18n } from '../i18n'; import { EuiOutsideClickDetector } from '../outside_click_detector'; import { EuiPopoverArrow } from './popover_arrow'; import { euiPopoverStyles } from './popover.styles'; import { EuiPopoverPanel } from './popover_panel'; import { EuiComponentDefaultsContext } from '../provider/component_defaults'; import { jsx as ___EmotionJSX } from "@emotion/react"; export var popoverAnchorPosition = ['upCenter', 'upLeft', 'upRight', 'downCenter', 'downLeft', 'downRight', 'leftCenter', 'leftUp', 'leftDown', 'rightCenter', 'rightUp', 'rightDown']; var anchorPositionToPopoverPositionMap = { up: 'top', right: 'right', down: 'bottom', left: 'left' }; export function getPopoverPositionFromAnchorPosition(anchorPosition) { // maps the anchor position to the matching popover position // e.g. "upLeft" -> "top", "downRight" -> "bottom" // extract the first positional word from anchorPosition: // starts at the beginning (" ^ ") of anchorPosition and // captures all of the characters (" (.*?) ") until the // first capital letter (" [A-Z] ") is encountered var _ref = anchorPosition.match(/^(.*?)[A-Z]/), _ref2 = _slicedToArray(_ref, 2), primaryPosition = _ref2[1]; return anchorPositionToPopoverPositionMap[primaryPosition]; } export function getPopoverAlignFromAnchorPosition(anchorPosition) { // maps the gravity to the matching popover position // e.g. "upLeft" -> "left", "rightDown" -> "bottom" // extract the second positional word from anchorPosition: // starts a capture group at the first capital letter // and includes everything after it var _ref3 = anchorPosition.match(/([A-Z].*)/), _ref4 = _slicedToArray(_ref3, 2), align = _ref4[1]; // this performs two tasks: // 1. normalizes the align position by lowercasing it // 2. `center` doesn't exist in the lookup map which converts it to `undefined` meaning no align return anchorPositionToPopoverPositionMap[align.toLowerCase()]; } var DEFAULT_POPOVER_STYLES = { top: 50, left: 50 }; var returnFocusConfig = { preventScroll: true }; var closingTransitionTime = 250; // TODO: DRY out var when converting to CSS-in-JS export var EuiPopover = /*#__PURE__*/function (_Component) { function EuiPopover(props) { var _this; _classCallCheck(this, EuiPopover); _this = _callSuper(this, EuiPopover, [props]); _defineProperty(_this, "repositionOnScroll", void 0); _defineProperty(_this, "repositionTimeout", void 0); _defineProperty(_this, "strandedFocusTimeout", void 0); _defineProperty(_this, "closingTransitionTimeout", void 0); _defineProperty(_this, "closingTransitionAnimationFrame", void 0); _defineProperty(_this, "button", null); _defineProperty(_this, "panel", null); _defineProperty(_this, "idGenerator", htmlIdGenerator('euiPopover')); _defineProperty(_this, "panelId", _this.idGenerator('panelId')); _defineProperty(_this, "descriptionId", _this.idGenerator('descriptionId')); _defineProperty(_this, "closePopover", function () { if (_this.props.isOpen) { _this.props.closePopover(); } }); _defineProperty(_this, "onEscapeKey", function (event) { if (_this.props.isOpen) { event.preventDefault(); event.stopPropagation(); _this.closePopover(); _this.handleStrandedFocus(); } }); _defineProperty(_this, "getFocusableToggleButton", function () { if (_this.button) { try { var focusableItems = focusable(_this.button); if (focusableItems.length) { return focusableItems[0]; } } catch (_unused) { // tabbable's focusable() can throw in environments that don't // fully support CSS selector parsing (e.g. jsdom with :has()) } } }); _defineProperty(_this, "handleStrandedFocus", function () { _this.strandedFocusTimeout = window.setTimeout(function () { var _this$panel; // If `returnFocus` failed and focus was stranded, // attempt to manually restore focus to the toggle button. // The stranded focus is either in most cases on body but // it will be on the panel instead on mount when isOpen=true if (document.activeElement === document.body || (_this$panel = _this.panel) !== null && _this$panel !== void 0 && _this$panel.contains(document.activeElement) // if focus is on OR within this.panel ) { var toggleButton = _this.getFocusableToggleButton(); if (toggleButton) { toggleButton.focus(returnFocusConfig); } } }, closingTransitionTime); }); _defineProperty(_this, "onKeyDown", function (event) { if (event.key === keys.ESCAPE) { _this.onEscapeKey(event); } }); _defineProperty(_this, "onClickOutside", function (event) { // only close the popover if the event source isn't the anchor button // otherwise, it is up to the anchor to toggle the popover's open status if (_this.button && _this.button.contains(event.target) === false) { _this.closePopover(); } }); _defineProperty(_this, "onOpenPopover", function () { clearTimeout(_this.strandedFocusTimeout); clearTimeout(_this.closingTransitionTimeout); if (_this.closingTransitionAnimationFrame) { cancelAnimationFrame(_this.closingTransitionAnimationFrame); } // We need to set this state a beat after the render takes place, so that the CSS // transition can take effect. _this.closingTransitionAnimationFrame = window.requestAnimationFrame(function () { _this.setState({ isOpening: true, isClosing: false }); }); // for each child element of `this.panel`, find any transition duration we should wait for before stabilizing var _Array$prototype$slic = Array.prototype.slice.call(_this.panel ? [_this.panel].concat(_toConsumableArray(Array.from(_this.panel.children))) : []).reduce(function (_ref5, element) { var durationMatch = _ref5.durationMatch, delayMatch = _ref5.delayMatch; var transitionTimings = getTransitionTimings(element); return { durationMatch: Math.max(durationMatch, transitionTimings.durationMatch), delayMatch: Math.max(delayMatch, transitionTimings.delayMatch) }; }, { durationMatch: 0, delayMatch: 0 }), durationMatch = _Array$prototype$slic.durationMatch, delayMatch = _Array$prototype$slic.delayMatch; clearTimeout(_this.repositionTimeout); _this.repositionTimeout = window.setTimeout(function () { _this.setState({ isOpenStable: true }, function () { _this.positionPopoverFixed(); focusTrapPubSub.publish(); }); }, durationMatch + delayMatch); }); /** * Updates ARIA attributes on the popover trigger button * Only applies ARIA when the trigger is button-like (semantic <button> or role="button"). * Avoids adding incorrect ARIA on inputs or other non-button elements. */ _defineProperty(_this, "updateTriggerButtonAriaAttributes", function (toggleButton, isOpen) { var _toggleButton$tagName, _toggleButton$getAttr; if (!toggleButton) return; var tag = (_toggleButton$tagName = toggleButton.tagName) === null || _toggleButton$tagName === void 0 ? void 0 : _toggleButton$tagName.toLowerCase(); var role = (_toggleButton$getAttr = toggleButton.getAttribute('role')) === null || _toggleButton$getAttr === void 0 ? void 0 : _toggleButton$getAttr.toLowerCase(); var isButtonLike = tag === 'button' || role === 'button'; if (!isButtonLike) return; toggleButton.setAttribute('aria-expanded', isOpen ? 'true' : 'false'); if (isOpen) { toggleButton.setAttribute('aria-controls', _this.panelId); } else { toggleButton.removeAttribute('aria-controls'); } }); _defineProperty(_this, "onMutation", function (records) { var waitDuration = getWaitDuration(records); _this.positionPopoverFixed(); performOnFrame(waitDuration, _this.positionPopoverFixed); }); _defineProperty(_this, "positionPopover", function (allowEnforcePosition) { if (_this.button == null || _this.panel == null) return; var _ref6 = _this.props, anchorPosition = _ref6.anchorPosition, _offset = _ref6.offset; var offset = _offset != null ? _offset : _this.props.hasArrow ? 0 : 4; var position = getPopoverPositionFromAnchorPosition(anchorPosition); var forcePosition = undefined; if (allowEnforcePosition && _this.state.isOpenStable && _this.state.openPosition != null) { position = _this.state.openPosition; forcePosition = true; } var _findPopoverPosition = findPopoverPosition({ container: _this.props.container, position: position, forcePosition: forcePosition, align: getPopoverAlignFromAnchorPosition(anchorPosition), anchor: _this.button, popover: _this.panel, offset: _this.props.attachToAnchor ? offset : _this.props.hasArrow ? 16 + offset : 8 + offset, arrowConfig: _this.props.hasArrow ? { arrowWidth: 16, arrowBuffer: 10 } : { arrowWidth: 0, arrowBuffer: 0 }, returnBoundingBox: _this.props.attachToAnchor, allowCrossAxis: _this.props.repositionToCrossAxis, buffer: _this.props.buffer }), top = _findPopoverPosition.top, left = _findPopoverPosition.left, foundPosition = _findPopoverPosition.position, arrow = _findPopoverPosition.arrow; // the popover's z-index must inherit from the button // this keeps a button's popover under a flyout that would cover the button // but a popover triggered inside a flyout will appear over that flyout var zIndexProp = _this.props.zIndex; var zIndex = zIndexProp == null ? getElementZIndex(_this.button, _this.panel) + 2000 : zIndexProp; var popoverStyles = _objectSpread(_objectSpread({}, _this.props.panelStyle), {}, { top: top, left: left, zIndex: zIndex }); var willRenderArrow = !_this.props.attachToAnchor && _this.props.hasArrow; var arrowStyles = willRenderArrow ? arrow : undefined; var arrowPosition = foundPosition; _this.props.onPositionChange && _this.props.onPositionChange(arrowPosition); _this.setState({ popoverStyles: popoverStyles, arrowStyles: arrowStyles, arrowPosition: arrowPosition, openPosition: foundPosition }); }); _defineProperty(_this, "positionPopoverFixed", function () { _this.positionPopover(true); }); _defineProperty(_this, "positionPopoverFluid", function () { _this.positionPopover(false); }); _defineProperty(_this, "panelRef", function (node) { _this.panel = node; _this.props.panelRef && _this.props.panelRef(node); if (node == null) { // panel has unmounted, restore the state defaults _this.setState({ popoverStyles: DEFAULT_POPOVER_STYLES, arrowStyles: {}, arrowPosition: null, openPosition: null, isOpenStable: false }); window.removeEventListener('resize', _this.positionPopoverFluid); } else { // panel is coming into existence _this.positionPopoverFluid(); window.addEventListener('resize', _this.positionPopoverFluid); } }); _defineProperty(_this, "popoverRef", function (node) { _this.button = node; setMultipleRefs([_this.props.popoverRef], node); }); _this.state = { prevProps: { isOpen: props.isOpen }, suppressingPopover: props.isOpen, // only suppress if created with isOpen=true isClosing: false, isOpening: false, popoverStyles: DEFAULT_POPOVER_STYLES, arrowStyles: {}, arrowPosition: null, openPosition: null, // once a stable position has been found, keep the contents on that side isOpenStable: false // wait for any initial opening transitions to finish before marking as stable }; _this.repositionOnScroll = createRepositionOnScroll(function () { return { repositionOnScroll: _this.props.repositionOnScroll, componentDefaults: _this.context.EuiPopover, repositionFn: _this.positionPopoverFixed }; }); return _this; } _inherits(EuiPopover, _Component); return _createClass(EuiPopover, [{ key: "componentDidMount", value: function componentDidMount() { var _this2 = this, _this$props$isOpen; if (this.state.suppressingPopover) { // component was created with isOpen=true; now that it's mounted // stop suppressing and start opening this.setState({ suppressingPopover: false, isOpening: true }, function () { _this2.onOpenPopover(); }); } this.updateTriggerButtonAriaAttributes(this.getFocusableToggleButton(), (_this$props$isOpen = this.props.isOpen) !== null && _this$props$isOpen !== void 0 ? _this$props$isOpen : false); this.repositionOnScroll.subscribe(); } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { var _this3 = this; // The popover is being opened. if (!prevProps.isOpen && this.props.isOpen) { this.onOpenPopover(); } // Update ARIA attributes on the toggle when open state changes if (prevProps.isOpen !== this.props.isOpen) { var _this$props$isOpen2; this.updateTriggerButtonAriaAttributes(this.getFocusableToggleButton(), (_this$props$isOpen2 = this.props.isOpen) !== null && _this$props$isOpen2 !== void 0 ? _this$props$isOpen2 : false); } // ensure recalculation of panel position on prop updates if (this.props.isOpen && (prevProps.anchorPosition !== this.props.anchorPosition || prevProps.buffer !== this.props.buffer || prevProps.offset !== this.props.offset || prevProps.panelPaddingSize !== this.props.panelPaddingSize)) { this.positionPopoverFluid(); } // update scroll listener this.repositionOnScroll.update(); // The popover is being closed. if (prevProps.isOpen && !this.props.isOpen) { // If the user has just closed the popover, queue up the removal of the content after the // transition is complete. this.closingTransitionTimeout = window.setTimeout(function () { _this3.setState({ isClosing: false }); focusTrapPubSub.publish(); }, closingTransitionTime); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this.repositionOnScroll.cleanup(); clearTimeout(this.repositionTimeout); clearTimeout(this.strandedFocusTimeout); clearTimeout(this.closingTransitionTimeout); cancelAnimationFrame(this.closingTransitionAnimationFrame); focusTrapPubSub.publish(); } }, { key: "render", value: function render() { var _panelProps$tabIndex, _this4 = this; var _this$props = this.props, anchorPosition = _this$props.anchorPosition, button = _this$props.button, insert = _this$props.insert, isOpen = _this$props.isOpen, ownFocus = _this$props.ownFocus, children = _this$props.children, className = _this$props.className, closePopover = _this$props.closePopover, panelClassName = _this$props.panelClassName, panelPaddingSize = _this$props.panelPaddingSize, panelProps = _this$props.panelProps, panelRef = _this$props.panelRef, panelStyle = _this$props.panelStyle, popoverScreenReaderText = _this$props.popoverScreenReaderText, popoverRef = _this$props.popoverRef, hasArrow = _this$props.hasArrow, arrowChildren = _this$props.arrowChildren, repositionOnScroll = _this$props.repositionOnScroll, repositionToCrossAxis = _this$props.repositionToCrossAxis, zIndex = _this$props.zIndex, attachToAnchor = _this$props.attachToAnchor, display = _this$props.display, offset = _this$props.offset, onPositionChange = _this$props.onPositionChange, buffer = _this$props.buffer, ariaLabel = _this$props['aria-label'], ariaLabelledBy = _this$props['aria-labelledby'], ariaLiveProp = _this$props['aria-live'], container = _this$props.container, focusTrapProps = _this$props.focusTrapProps, initialFocusProp = _this$props.initialFocus, _tabIndexProp = _this$props.tabIndex, rest = _objectWithoutProperties(_this$props, _excluded); var tabIndexProp = (_panelProps$tabIndex = panelProps === null || panelProps === void 0 ? void 0 : panelProps.tabIndex) !== null && _panelProps$tabIndex !== void 0 ? _panelProps$tabIndex : _tabIndexProp; var styles = euiPopoverStyles(); var popoverStyles = [styles.euiPopover, { display: display, label: display }]; var classes = classNames('euiPopover', { 'euiPopover-isOpen': this.state.isOpening }, className); var showArrow = hasArrow && !attachToAnchor; var panel; if (!this.state.suppressingPopover && (isOpen || this.state.isClosing)) { var tabIndex = tabIndexProp; var initialFocus = initialFocusProp; var ariaDescribedby; var ariaLive; var panelAriaModal = panelProps !== null && panelProps !== void 0 && panelProps.hasOwnProperty('aria-modal') ? panelProps['aria-modal'] : 'true'; var panelRole = panelProps !== null && panelProps !== void 0 && panelProps.hasOwnProperty('role') ? panelProps.role : 'dialog'; if (ownFocus || panelAriaModal !== 'true') { tabIndex = tabIndexProp !== null && tabIndexProp !== void 0 ? tabIndexProp : 0; ariaLive = 'off'; if (!initialFocus) { initialFocus = function initialFocus() { return _this4.panel; }; } } else { ariaLive = ariaLiveProp !== null && ariaLiveProp !== void 0 ? ariaLiveProp : 'assertive'; } var focusTrapScreenReaderText; if (ownFocus || popoverScreenReaderText) { ariaDescribedby = this.descriptionId; focusTrapScreenReaderText = ___EmotionJSX(EuiScreenReaderOnly, null, ___EmotionJSX("p", { id: this.descriptionId }, ownFocus && ___EmotionJSX(EuiI18n, { token: "euiPopover.screenReaderAnnouncement", default: "You are in a dialog. Press Escape, or tap/click outside the dialog to close." }), popoverScreenReaderText)); } var returnFocus = this.state.isOpenStable ? returnFocusConfig : false; panel = ___EmotionJSX(EuiPortal, insert && { insert: insert }, ___EmotionJSX(EuiFocusTrap, _extends({ clickOutsideDisables: true, onClickOutside: this.onClickOutside, returnFocus: returnFocus // Ignore temporary state of indecisive focus , initialFocus: initialFocus, onEscapeKey: this.onEscapeKey, disabled: !ownFocus || !this.state.isOpenStable || this.state.isClosing }, focusTrapProps), ___EmotionJSX(EuiPopoverPanel, _extends({ id: this.panelId }, panelProps, { panelRef: this.panelRef, isOpen: this.state.isOpening, position: this.state.arrowPosition, isAttached: attachToAnchor, className: classNames(panelClassName, panelProps === null || panelProps === void 0 ? void 0 : panelProps.className), hasShadow: false, paddingSize: panelPaddingSize, tabIndex: tabIndex, "aria-live": ariaLive, role: panelRole, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-modal": panelAriaModal, "aria-describedby": ariaDescribedby, style: _objectSpread(_objectSpread({}, this.state.popoverStyles), {}, { // Adding `will-change` to reduce risk of a blurry animation in Chrome 86+ willChange: !this.state.isOpenStable ? 'transform, opacity' : undefined }) }), showArrow && this.state.arrowPosition && ___EmotionJSX(EuiPopoverArrow, { position: this.state.arrowPosition, style: this.state.arrowStyles }, arrowChildren), focusTrapScreenReaderText, ___EmotionJSX(EuiMutationObserver, { observerOptions: { attributes: true, // element attribute changes childList: true, // added/removed elements characterData: true, // text changes subtree: true // watch all child elements }, onMutation: this.onMutation }, function (mutationRef) { return ___EmotionJSX("div", { ref: mutationRef }, children); })))); } // react-focus-on and related do not register outside click detection // when disabled, so we still need to conditionally check for that ourselves if (ownFocus) { return ___EmotionJSX("div", _extends({ css: popoverStyles, className: classes, ref: this.popoverRef }, rest), button instanceof HTMLElement ? null : button, panel); } else { return ___EmotionJSX(EuiOutsideClickDetector, { onOutsideClick: this.closePopover }, ___EmotionJSX("div", _extends({ css: popoverStyles, className: classes, ref: this.popoverRef, onKeyDown: this.onKeyDown }, rest), button instanceof HTMLElement ? null : button, panel)); } } }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(nextProps, prevState) { if (prevState.prevProps.isOpen && !nextProps.isOpen) { return { prevProps: { isOpen: nextProps.isOpen }, isClosing: true, isOpening: false }; } if (prevState.prevProps.isOpen !== nextProps.isOpen) { return { prevProps: { isOpen: nextProps.isOpen } }; } return null; } }]); }(Component); _defineProperty(EuiPopover, "contextType", EuiComponentDefaultsContext); _defineProperty(EuiPopover, "defaultProps", { isOpen: false, ownFocus: true, repositionToCrossAxis: true, anchorPosition: 'downLeft', panelPaddingSize: 'm', hasArrow: false, display: 'inline-block' }); EuiPopover.propTypes = { /** * Alignment of the popover and arrow relative to the button * @default downLeft */ anchorPosition: PropTypes.any, /** * Style and position alteration for arrow-less attachment. * Intended for use with inputs as anchors, e.g. EuiInputPopover */ attachToAnchor: PropTypes.bool, /** * Triggering element for which to align the popover to */ button: PropTypes.any.isRequired, /** * Callback to handle hiding of the popover */ closePopover: PropTypes.func.isRequired, /** * Restrict the popover's position within this element */ container: PropTypes.any, /** * CSS display type for both the popover and anchor */ display: PropTypes.any, /** * Object of props passed to EuiFocusTrap */ focusTrapProps: PropTypes.any, /** * Show arrow indicating to originating button * @default false */ hasArrow: PropTypes.bool, /** * Specifies what element should initially have focus; Can be a DOM * node, or a selector string (which will be passed to * document.querySelector() to find the DOM node), or a function that * returns a DOM node. * * If not passed, initial focus defaults to the popover panel. */ initialFocus: PropTypes.any, /** * Passed directly to EuiPortal for DOM positioning. Both properties are * required if prop is specified */ insert: PropTypes.shape({ sibling: PropTypes.any.isRequired, position: PropTypes.oneOf(["before", "after"]).isRequired }), /** * Visibility state of the popover */ isOpen: PropTypes.bool, /** * Traps tab focus within the popover contents */ ownFocus: PropTypes.bool, /** * Custom class added to the EuiPanel containing the popover contents */ panelClassName: PropTypes.string, /** * EuiPanel padding on all sides */ panelPaddingSize: PropTypes.any, /** * Standard DOM `style` attribute. Passed to the EuiPanel */ panelStyle: PropTypes.any, /** * Object of props passed to EuiPanel. See {@link EuiPopoverPanelProps} */ panelProps: PropTypes.shape({ element: PropTypes.oneOf(["div"]), /** * Padding for all four sides */ paddingSize: PropTypes.any, /** * Corner border radius */ borderRadius: PropTypes.any, /** * When true the panel will grow in height to match `EuiFlexItem` */ grow: PropTypes.bool, panelRef: PropTypes.any, className: PropTypes.string, "aria-label": PropTypes.string, "data-test-subj": PropTypes.string, css: PropTypes.any }), panelRef: PropTypes.any, /** * Optional screen reader instructions to announce upon popover open, * in addition to EUI's default popover instructions for Escape on close. * Useful for popovers that may have additional keyboard capabilities such as * arrow navigation. */ popoverScreenReaderText: PropTypes.oneOfType([PropTypes.string.isRequired, PropTypes.node.isRequired]), popoverRef: PropTypes.any, /** * When `true`, the popover's position is re-calculated when the user * scrolls, this supports having fixed-position popover anchors. When nesting * an `EuiPopover` in a scrollable container, `repositionOnScroll` should be `true` */ repositionOnScroll: PropTypes.bool, /** * By default, popovers will attempt to position themselves along the initial * axis specified. If there is not enough room either vertically or horizontally * however, the popover will attempt to reposition itself along the secondary * cross axis if there is room there instead. * * If you do not want this repositioning to occur (and it is acceptable for * the popover to appear offscreen), set this to false to disable this behavior. * * @default true */ repositionToCrossAxis: PropTypes.bool, /** * By default, popover content inherits the z-index of the anchor * component; pass `zIndex` to override */ zIndex: PropTypes.number, /** * Distance away from the anchor that the popover will render * @default 4 (0 when `hasArrow=true`) */ offset: PropTypes.number, /** * Minimum distance between the popover and the bounding container; * Pass an array of 4 values to adjust each side differently: `[top, right, bottom, left]` * @default 16 */ buffer: PropTypes.oneOfType([PropTypes.number.isRequired, PropTypes.any.isRequired]), /** * Element to pass as the child element of the arrow; * Use case is typically limited to an accompanying `EuiBeacon` */ arrowChildren: PropTypes.node, /** * Provide a name to the popover panel */ "aria-label": PropTypes.string, /** * Alternative option to `aria-label` that takes an `id`. * Usually takes the `id` of the popover title */ "aria-labelledby": PropTypes.string, /** * Function callback for when the popover positon changes */ onPositionChange: PropTypes.func, className: PropTypes.string, "data-test-subj": PropTypes.string, css: PropTypes.any };