UNPKG

@elastic/eui

Version:

Elastic UI Component Library

284 lines (274 loc) 15 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.TYPES = exports.SIZES = exports.SIDES = exports.PADDING_SIZES = exports.EuiFlyout = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _react = _interopRequireWildcard(require("react")); var _classnames = _interopRequireDefault(require("classnames")); var _services = require("../../services"); var _global_styling = require("../../global_styling"); var _focus_trap = require("../focus_trap"); var _overlay_mask = require("../overlay_mask"); var _i18n = require("../i18n"); var _resize_observer = require("../observer/resize_observer"); var _portal = require("../portal"); var _accessibility = require("../accessibility"); var _flyout_close_button = require("./_flyout_close_button"); var _flyout = require("./flyout.styles"); var _react2 = require("@emotion/react"); var _excluded = ["className", "children", "as", "hideCloseButton", "closeButtonProps", "closeButtonPosition", "onClose", "ownFocus", "side", "size", "paddingSize", "maxWidth", "style", "maskProps", "type", "outsideClickCloses", "pushMinBreakpoint", "pushAnimation", "focusTrapProps", "includeFixedHeadersInFocusTrap", "aria-describedby"]; /* * 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. */ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } 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; } var TYPES = exports.TYPES = ['push', 'overlay']; var SIDES = exports.SIDES = ['left', 'right']; var SIZES = exports.SIZES = ['s', 'm', 'l']; /** * Custom type checker for named flyout sizes since the prop * `size` can also be CSSProperties['width'] (string | number) */ function isEuiFlyoutSizeNamed(value) { return SIZES.includes(value); } var PADDING_SIZES = exports.PADDING_SIZES = ['none', 's', 'm', 'l']; var defaultElement = 'div'; var EuiFlyout = exports.EuiFlyout = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) { var className = _ref.className, children = _ref.children, as = _ref.as, _ref$hideCloseButton = _ref.hideCloseButton, hideCloseButton = _ref$hideCloseButton === void 0 ? false : _ref$hideCloseButton, closeButtonProps = _ref.closeButtonProps, _ref$closeButtonPosit = _ref.closeButtonPosition, closeButtonPosition = _ref$closeButtonPosit === void 0 ? 'inside' : _ref$closeButtonPosit, onClose = _ref.onClose, _ref$ownFocus = _ref.ownFocus, ownFocus = _ref$ownFocus === void 0 ? true : _ref$ownFocus, _ref$side = _ref.side, side = _ref$side === void 0 ? 'right' : _ref$side, _ref$size = _ref.size, size = _ref$size === void 0 ? 'm' : _ref$size, _ref$paddingSize = _ref.paddingSize, paddingSize = _ref$paddingSize === void 0 ? 'l' : _ref$paddingSize, _ref$maxWidth = _ref.maxWidth, maxWidth = _ref$maxWidth === void 0 ? false : _ref$maxWidth, style = _ref.style, maskProps = _ref.maskProps, _ref$type = _ref.type, type = _ref$type === void 0 ? 'overlay' : _ref$type, outsideClickCloses = _ref.outsideClickCloses, _ref$pushMinBreakpoin = _ref.pushMinBreakpoint, pushMinBreakpoint = _ref$pushMinBreakpoin === void 0 ? 'l' : _ref$pushMinBreakpoin, _ref$pushAnimation = _ref.pushAnimation, pushAnimation = _ref$pushAnimation === void 0 ? false : _ref$pushAnimation, _focusTrapProps = _ref.focusTrapProps, _ref$includeFixedHead = _ref.includeFixedHeadersInFocusTrap, includeFixedHeadersInFocusTrap = _ref$includeFixedHead === void 0 ? true : _ref$includeFixedHead, _ariaDescribedBy = _ref['aria-describedby'], rest = (0, _objectWithoutProperties2.default)(_ref, _excluded); var Element = as || defaultElement; var maskRef = (0, _react.useRef)(null); var windowIsLargeEnoughToPush = (0, _services.useIsWithinMinBreakpoint)(pushMinBreakpoint); var isPushed = type === 'push' && windowIsLargeEnoughToPush; /** * Setting up the refs on the actual flyout element in order to * accommodate for the `isPushed` state by adding padding to the body equal to the width of the element */ var _useState = (0, _react.useState)(null), _useState2 = (0, _slicedToArray2.default)(_useState, 2), resizeRef = _useState2[0], setResizeRef = _useState2[1]; var setRef = (0, _services.useCombinedRefs)([setResizeRef, ref]); var _useResizeObserver = (0, _resize_observer.useResizeObserver)(isPushed ? resizeRef : null, 'width'), width = _useResizeObserver.width; (0, _react.useEffect)(function () { /** * Accomodate for the `isPushed` state by adding padding to the body equal to the width of the element */ if (isPushed) { var paddingSide = side === 'left' ? 'paddingInlineStart' : 'paddingInlineEnd'; document.body.style[paddingSide] = "".concat(width, "px"); return function () { document.body.style[paddingSide] = ''; }; } }, [isPushed, side, width]); /** * This class doesn't actually do anything by EUI, but is nice to add for consumers (JIC) */ (0, _react.useEffect)(function () { document.body.classList.add('euiBody--hasFlyout'); return function () { // Remove the hasFlyout class when the flyout is unmounted document.body.classList.remove('euiBody--hasFlyout'); }; }, []); /** * ESC key closes flyout (always?) */ var onKeyDown = (0, _react.useCallback)(function (event) { if (!isPushed && event.key === _services.keys.ESCAPE) { event.preventDefault(); onClose(event); } }, [onClose, isPushed]); /** * Set inline styles */ var inlineStyles = (0, _react.useMemo)(function () { var widthStyle = !isEuiFlyoutSizeNamed(size) && (0, _global_styling.logicalStyle)('width', size); var maxWidthStyle = typeof maxWidth !== 'boolean' && (0, _global_styling.logicalStyle)('max-width', maxWidth); return _objectSpread(_objectSpread(_objectSpread({}, style), widthStyle), maxWidthStyle); }, [style, maxWidth, size]); var styles = (0, _services.useEuiMemoizedStyles)(_flyout.euiFlyoutStyles); var cssStyles = [styles.euiFlyout, styles.paddingSizes[paddingSize], isEuiFlyoutSizeNamed(size) && styles[size], maxWidth === false && styles.noMaxWidth, isPushed ? styles.push.push : styles.overlay, isPushed && styles.push[side], isPushed && !pushAnimation && styles.push.noAnimation, styles[side]]; var classes = (0, _classnames.default)('euiFlyout', className); /* * If not disabled, automatically add fixed EuiHeaders as shards * to EuiFlyout focus traps, to prevent focus fighting */ var flyoutToggle = (0, _react.useRef)(document.activeElement); var _useState3 = (0, _react.useState)([]), _useState4 = (0, _slicedToArray2.default)(_useState3, 2), fixedHeaders = _useState4[0], setFixedHeaders = _useState4[1]; (0, _react.useEffect)(function () { if (includeFixedHeadersInFocusTrap) { var fixedHeaderEls = document.querySelectorAll('.euiHeader[data-fixed-header]'); setFixedHeaders(Array.from(fixedHeaderEls)); // Flyouts that are toggled from fixed headers do not have working // focus trap autoFocus, so we need to focus the flyout wrapper ourselves fixedHeaderEls.forEach(function (header) { if (header.contains(flyoutToggle.current)) { resizeRef === null || resizeRef === void 0 || resizeRef.focus(); } }); } else { // Clear existing headers if necessary, e.g. switching to `false` setFixedHeaders(function (headers) { return headers.length ? [] : headers; }); } }, [includeFixedHeadersInFocusTrap, resizeRef]); var focusTrapProps = (0, _react.useMemo)(function () { return _objectSpread(_objectSpread({}, _focusTrapProps), {}, { shards: [].concat((0, _toConsumableArray2.default)(fixedHeaders), (0, _toConsumableArray2.default)((_focusTrapProps === null || _focusTrapProps === void 0 ? void 0 : _focusTrapProps.shards) || [])) }); }, [fixedHeaders, _focusTrapProps]); /* * Provide meaningful screen reader instructions/details */ var hasOverlayMask = ownFocus && !isPushed; var descriptionId = (0, _services.useGeneratedHtmlId)(); var ariaDescribedBy = (0, _classnames.default)(descriptionId, _ariaDescribedBy); var screenReaderDescription = (0, _react.useMemo)(function () { return (0, _react2.jsx)(_accessibility.EuiScreenReaderOnly, null, (0, _react2.jsx)("p", { id: descriptionId }, hasOverlayMask ? (0, _react2.jsx)(_i18n.EuiI18n, { token: "euiFlyout.screenReaderModalDialog", default: "You are in a modal dialog. Press Escape or tap/click outside the dialog on the shadowed overlay to close." }) : (0, _react2.jsx)(_i18n.EuiI18n, { token: "euiFlyout.screenReaderNonModalDialog", default: "You are in a non-modal dialog. To close the dialog, press Escape." }), ' ', fixedHeaders.length > 0 && (0, _react2.jsx)(_i18n.EuiI18n, { token: "euiFlyout.screenReaderFixedHeaders", default: "You can still continue tabbing through the page headers in addition to the dialog." }))); }, [hasOverlayMask, descriptionId, fixedHeaders.length]); /* * Trap focus even when `ownFocus={false}`, otherwise closing * the flyout won't return focus to the originating button. * * Set `clickOutsideDisables={true}` when `ownFocus={false}` * to allow non-keyboard users the ability to interact with * elements outside the flyout. * * Set `onClickOutside={onClose}` when `ownFocus` and `type` are the defaults, * or if `outsideClickCloses={true}` to close on clicks that target * (both mousedown and mouseup) the overlay mask. */ var onClickOutside = (0, _react.useCallback)(function (event) { // Do not close the flyout for any external click if (outsideClickCloses === false) return undefined; if (hasOverlayMask) { // The overlay mask is present, so only clicks on the mask should close the flyout, regardless of outsideClickCloses if (event.target === maskRef.current) return onClose(event); } else { // No overlay mask is present, so any outside clicks should close the flyout if (outsideClickCloses === true) return onClose(event); } // Otherwise if ownFocus is false and outsideClickCloses is undefined, outside clicks should not close the flyout return undefined; }, [onClose, hasOverlayMask, outsideClickCloses]); return (0, _react2.jsx)(EuiFlyoutWrapper, { hasOverlayMask: hasOverlayMask, maskProps: _objectSpread(_objectSpread({}, maskProps), {}, { maskRef: (0, _services.useCombinedRefs)([maskProps === null || maskProps === void 0 ? void 0 : maskProps.maskRef, maskRef]) }), isPortalled: !isPushed }, (0, _react2.jsx)(_services.EuiWindowEvent, { event: "keydown", handler: onKeyDown }), (0, _react2.jsx)(_focus_trap.EuiFocusTrap, (0, _extends2.default)({ disabled: isPushed, scrollLock: hasOverlayMask, clickOutsideDisables: !ownFocus, onClickOutside: onClickOutside }, focusTrapProps), (0, _react2.jsx)(Element, (0, _extends2.default)({ className: classes, css: cssStyles, style: inlineStyles, ref: setRef }, rest, { role: !isPushed ? 'dialog' : rest.role, tabIndex: !isPushed ? 0 : rest.tabIndex, "aria-describedby": !isPushed ? ariaDescribedBy : _ariaDescribedBy, "data-autofocus": !isPushed || undefined }), !isPushed && screenReaderDescription, !hideCloseButton && onClose && (0, _react2.jsx)(_flyout_close_button.EuiFlyoutCloseButton, (0, _extends2.default)({}, closeButtonProps, { onClose: onClose, closeButtonPosition: closeButtonPosition, side: side })), children))); } // React.forwardRef interferes with the inferred element type // Casting to ensure correct element prop type checking for `as` // e.g., `href` is not on a `div` ); // Recast to allow `displayName` EuiFlyout.displayName = 'EuiFlyout'; /** * Light wrapper for conditionally rendering portals or overlay masks: * - If ownFocus is set, wrap with an overlay and allow the user to click it to close it. * - Otherwise still wrap within an EuiPortal so it appends to the bottom of the window. * Push flyouts have no overlay OR portal behavior. */ var EuiFlyoutWrapper = function EuiFlyoutWrapper(_ref2) { var children = _ref2.children, hasOverlayMask = _ref2.hasOverlayMask, maskProps = _ref2.maskProps, isPortalled = _ref2.isPortalled; if (hasOverlayMask) { return (0, _react2.jsx)(_overlay_mask.EuiOverlayMask, (0, _extends2.default)({ headerZindexLocation: "below" }, maskProps), children); } else if (isPortalled) { return (0, _react2.jsx)(_portal.EuiPortal, null, children); } else { return (0, _react2.jsx)(_react.default.Fragment, null, children); } };