UNPKG

@elastic/eui

Version:

Elastic UI Component Library

208 lines (205 loc) 11.6 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.EuiCallOut = exports.COLOR_TO_NOTIFICATION_ICON_MAP = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _react = _interopRequireWildcard(require("react")); var _classnames = _interopRequireDefault(require("classnames")); var _euiThemeCommon = require("@elastic/eui-theme-common"); var _services = require("../../services"); var _global_styling = require("../../global_styling"); var _icon = require("../icon"); var _button = require("../button"); var _text = require("../text"); var _panel = require("../panel"); var _title = require("../title"); var _i18n = require("../i18n"); var _live_announcer = require("../accessibility/live_announcer"); var _notification_icon = require("../notification_icon/notification_icon"); var _use_layout_observer = require("./use_layout_observer"); var _call_out_action = require("./call_out_action"); var _call_out = require("./call_out.styles"); var _notification_icon2 = require("../notification_icon/notification_icon.styles"); var _react2 = require("@emotion/react"); var _excluded = ["title", "text", "color", "size", "iconType", "children", "actionProps", "className", "heading", "onDismiss", "dismissButtonProps", "announceOnMount", "style", "data-test-subj"]; /* * 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 COLOR_TO_NOTIFICATION_ICON_MAP = exports.COLOR_TO_NOTIFICATION_ICON_MAP = { primary: 'info', success: 'success', warning: 'warning', danger: 'error' }; /** Get correct color with fallback */ var getCallOutColor = function getCallOutColor(color) { return typeof color === 'string' && COLOR_TO_NOTIFICATION_ICON_MAP.hasOwnProperty(color) ? color : 'primary'; }; var EuiCallOut = exports.EuiCallOut = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) { var title = _ref.title, text = _ref.text, _ref$color = _ref.color, _color = _ref$color === void 0 ? 'primary' : _ref$color, _ref$size = _ref.size, size = _ref$size === void 0 ? 'm' : _ref$size, iconType = _ref.iconType, children = _ref.children, actionProps = _ref.actionProps, className = _ref.className, _ref$heading = _ref.heading, heading = _ref$heading === void 0 ? 'p' : _ref$heading, onDismiss = _ref.onDismiss, dismissButtonProps = _ref.dismissButtonProps, _ref$announceOnMount = _ref.announceOnMount, announceOnMount = _ref$announceOnMount === void 0 ? false : _ref$announceOnMount, style = _ref.style, dataTestSubj = _ref['data-test-subj'], rest = (0, _objectWithoutProperties2.default)(_ref, _excluded); var _useEuiTheme = (0, _services.useEuiTheme)(), euiTheme = _useEuiTheme.euiTheme; var color = getCallOutColor(_color); /* Uses resize observer to determine the container width/layout instead of native container queries, because callouts can be placed in containers without defined size (absolute positioned, no-grow flex layout etc.) where container queries would collapse by design instead of adjusting to the content dimensions. */ var layoutRef = (0, _use_layout_observer.useLayoutObserver)(size); // Memoize the refs array so `useCombinedRefs` returns a stable callback-ref across renders var combinedRefs = (0, _react.useMemo)(function () { return [layoutRef, ref]; }, [layoutRef, ref]); var panelRef = (0, _services.useCombinedRefs)(combinedRefs); var borderColors = (0, _global_styling.useEuiBorderColorCSS)(); var styles = (0, _services.useEuiMemoizedStyles)(_call_out.euiCallOutStyles); var cssStyles = [styles.euiCallOut, borderColors[color], onDismiss && styles.hasDismissButton]; var highlightColorToken = (0, _euiThemeCommon.getTokenName)('borderStrong', color); var typeColor = euiTheme.colors[highlightColorToken]; var cssVariables = (0, _react.useMemo)(function () { return { '--euiCallOutTypeColor': typeColor }; }, [typeColor]); var classes = (0, _classnames.default)('euiCallOut', (0, _defineProperty2.default)({}, "euiCallOut--".concat(color), color), className); var dismissButton = (0, _react.useMemo)(function () { if (!onDismiss) return; var cssStyles = [styles.dismissButton.euiCallOut__dismissButton]; return (0, _react2.jsx)(_i18n.EuiI18n, { token: "euiCallOut.dismissAriaLabel", default: "Dismiss this callout" }, function (dismissAriaLabel) { return (0, _react2.jsx)(_button.EuiButtonIcon, (0, _extends2.default)({ "data-test-subj": "euiDismissCalloutButton", "aria-label": dismissAriaLabel, css: cssStyles }, dismissButtonProps, { iconType: "cross", color: color, size: "xs", onClick: onDismiss })); }); }, [onDismiss, styles, color, dismissButtonProps]); var headerStyles = (0, _services.useEuiMemoizedStyles)(_call_out.euiCallOutHeaderStyles); var iconStyles = (0, _services.useEuiMemoizedStyles)(_notification_icon2.euiNotificationIconStyles); var header = (0, _react.useMemo)(function () { if (!title) return; var H = heading; var cssStyles = [headerStyles.euiCallOutHeader]; return (0, _react2.jsx)(_title.EuiTitle, { size: size === 's' ? 'xxs' : 'xs', css: cssStyles }, (0, _react2.jsx)(H, { className: "euiCallOutHeader__title" }, title)); }, [title, heading, size, headerStyles]); var icon = (0, _react.useMemo)(function () { if (!iconType) { var _COLOR_TO_NOTIFICATIO; var _iconType = (_COLOR_TO_NOTIFICATIO = COLOR_TO_NOTIFICATION_ICON_MAP[color]) !== null && _COLOR_TO_NOTIFICATIO !== void 0 ? _COLOR_TO_NOTIFICATIO : 'info'; return (0, _react2.jsx)(_notification_icon.EuiNotificationIcon, { css: styles.icon, type: _iconType, size: size === 's' ? 'm' : 'l' }); } return (0, _react2.jsx)(_icon.EuiIcon, { css: [styles.icon, size === 'm' && iconStyles.size.l, ";label:icon;"], type: iconType, size: size === 's' ? 'm' : 'l', "aria-hidden": "true", color: typeColor }); }, [iconType, color, size, typeColor, styles, iconStyles]); var optionalChildren = (text || children) && (0, _react2.jsx)(_react.default.Fragment, null, text && (0, _react2.jsx)(_text.EuiText, { size: "s", color: "default", className: "euiCallOut__text", "data-test-subj": dataTestSubj && "".concat(dataTestSubj, "__message") }, text), children && (0, _react2.jsx)(_text.EuiText, { className: "euiCallOut__additionalContent", size: size === 's' ? 'xs' : 's', color: "default", "data-test-subj": dataTestSubj && "".concat(dataTestSubj, "__content") }, children)); var actionControls = (0, _react.useMemo)(function () { var actionPrimaryProps = _objectSpread(_objectSpread({}, actionProps === null || actionProps === void 0 ? void 0 : actionProps.primary), {}, { color: color }); var actionSecondaryProps = _objectSpread(_objectSpread({}, actionProps === null || actionProps === void 0 ? void 0 : actionProps.secondary), {}, { color: color }); var hasActionPrimary = Boolean(actionProps === null || actionProps === void 0 ? void 0 : actionProps.primary); var hasActionSecondary = Boolean(actionProps === null || actionProps === void 0 ? void 0 : actionProps.secondary); if (!hasActionPrimary && !hasActionSecondary) return null; var actionPrimary = hasActionPrimary && (0, _react2.jsx)(_call_out_action.EuiCallOutAction, (0, _extends2.default)({ actionType: "primary" }, actionPrimaryProps)); var actionSecondary = hasActionSecondary && (0, _react2.jsx)(_call_out_action.EuiCallOutAction, (0, _extends2.default)({ actionType: "secondary" }, actionSecondaryProps)); return (0, _react2.jsx)("div", { css: styles.actions, "data-test-subj": dataTestSubj && "".concat(dataTestSubj, "__actions") }, actionPrimary, actionSecondary); }, [actionProps, color, styles, dataTestSubj]); var announcement = (0, _react.useMemo)(function () { if (!announceOnMount || !(title || text || children)) return null; return (0, _react2.jsx)(_live_announcer.EuiLiveAnnouncer, null, title, title && text && ",\xA0", text, (title || text) && children && ",\xA0", children); }, [announceOnMount, title, text, children]); return (0, _react2.jsx)(_panel.EuiPanel, (0, _extends2.default)({ borderRadius: "none", color: color, css: cssStyles // uses custom padding , paddingSize: "none", className: classes, panelRef: panelRef, grow: false, style: _objectSpread(_objectSpread({}, cssVariables), style), "data-size": size, "data-test-subj": dataTestSubj }, rest), (0, _react2.jsx)("div", { css: styles.wrapper }, (0, _react2.jsx)("div", { css: styles.body }, icon, (0, _react2.jsx)("div", { css: styles.content }, // Note: the DOM position of the dismiss button matters to screen reader users. // We generally want them to have some context of _what_ they're dismissing, // instead of navigating to the dismiss button first before the callout content header && optionalChildren ? (0, _react2.jsx)(_react.default.Fragment, null, header, dismissButton, optionalChildren) : (0, _react2.jsx)(_react.default.Fragment, null, header || optionalChildren, dismissButton))), actionControls, announcement)); }); EuiCallOut.displayName = 'EuiCallOut';