@elastic/eui
Version:
Elastic UI Component Library
246 lines (239 loc) • 12.6 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.TOAST_FADE_OUT_MS = exports.SIDES = exports.EuiGlobalToastList = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
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 _common = require("../common");
var _services = require("../../services");
var _time = require("../../services/time");
var _global_toast_list_item = require("./global_toast_list_item");
var _toast = require("./toast");
var _global_toast_list = require("./global_toast_list.styles");
var _react2 = require("@emotion/react");
var _excluded = ["className", "toasts", "dismissToast", "toastLifeTimeMs", "side"],
_excluded2 = ["text", "toastLifeTimeMs"];
/*
* 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(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var sideToClassNameMap = {
left: 'euiGlobalToastList--left',
right: 'euiGlobalToastList--right'
};
var SIDES = (0, _common.keysOf)(sideToClassNameMap);
exports.SIDES = SIDES;
var TOAST_FADE_OUT_MS = 250;
exports.TOAST_FADE_OUT_MS = TOAST_FADE_OUT_MS;
var EuiGlobalToastList = function EuiGlobalToastList(_ref) {
var className = _ref.className,
_ref$toasts = _ref.toasts,
toasts = _ref$toasts === void 0 ? [] : _ref$toasts,
dismissToastProp = _ref.dismissToast,
toastLifeTimeMs = _ref.toastLifeTimeMs,
_ref$side = _ref.side,
side = _ref$side === void 0 ? 'right' : _ref$side,
rest = (0, _objectWithoutProperties2.default)(_ref, _excluded);
var _useState = (0, _react.useState)({}),
_useState2 = (0, _slicedToArray2.default)(_useState, 2),
toastIdToDismissedMap = _useState2[0],
setToastIdToDismissedMap = _useState2[1];
var _useState3 = (0, _react.useState)(),
_useState4 = (0, _slicedToArray2.default)(_useState3, 2),
toastToDismiss = _useState4[0],
setToastToDismiss = _useState4[1];
var prevToasts = (0, _react.useRef)([]);
var dismissTimeoutIds = (0, _react.useRef)([]);
var toastIdToTimerMap = (0, _react.useRef)({});
var isScrollingToBottom = (0, _react.useRef)(false);
var isScrolledToBottom = (0, _react.useRef)(true);
var isUserInteracting = (0, _react.useRef)(false);
// See [Return Value](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestAnimationFrame#Return_value)
// for information on initial value of 0
var isScrollingAnimationFrame = (0, _react.useRef)(0);
var startScrollingAnimationFrame = (0, _react.useRef)(0);
var listElement = (0, _react.useRef)(null);
var euiTheme = (0, _services.useEuiTheme)();
var styles = (0, _global_toast_list.euiGlobalToastListStyles)(euiTheme);
var cssStyles = [styles.euiGlobalToastList, styles[side]];
var startScrollingToBottom = function startScrollingToBottom() {
isScrollingToBottom.current = true;
var scrollToBottom = function scrollToBottom() {
// Although we cancel the requestAnimationFrame in componentWillUnmount,
// it's possible for this.listElement to become null in the meantime
if (!listElement.current) {
return;
}
var position = listElement.current.scrollTop;
var destination = listElement.current.scrollHeight - listElement.current.clientHeight;
var distanceToDestination = destination - position;
if (distanceToDestination < 5) {
listElement.current.scrollTop = destination;
isScrollingToBottom.current = false;
isScrolledToBottom.current = true;
return;
}
listElement.current.scrollTop = position + distanceToDestination * 0.25;
if (isScrollingToBottom) {
isScrollingAnimationFrame.current = window.requestAnimationFrame(scrollToBottom);
}
};
startScrollingAnimationFrame.current = window.requestAnimationFrame(scrollToBottom);
};
var onMouseEnter = function onMouseEnter() {
// Stop scrolling to bottom if we're in mid-scroll, because the user wants to interact with
// the list.
isScrollingToBottom.current = false;
isUserInteracting.current = true;
// Don't let toasts dismiss themselves while the user is interacting with them.
for (var _toastId in toastIdToTimerMap.current) {
if (toastIdToTimerMap.current.hasOwnProperty(_toastId)) {
var timer = toastIdToTimerMap.current[_toastId];
timer.pause();
}
}
};
var onMouseLeave = function onMouseLeave() {
isUserInteracting.current = false;
for (var _toastId2 in toastIdToTimerMap.current) {
if (toastIdToTimerMap.current.hasOwnProperty(_toastId2)) {
var timer = toastIdToTimerMap.current[_toastId2];
timer.resume();
}
}
};
var onScroll = function onScroll() {
if (listElement.current) {
isScrolledToBottom.current = listElement.current.scrollHeight - listElement.current.scrollTop === listElement.current.clientHeight;
}
};
var dismissToast = (0, _react.useCallback)(function (toast) {
// Remove the toast after it's done fading out.
dismissTimeoutIds.current.push(window.setTimeout(function () {
setToastToDismiss(toast);
}, TOAST_FADE_OUT_MS));
setToastIdToDismissedMap(function (prev) {
return _objectSpread(_objectSpread({}, prev), {}, (0, _defineProperty2.default)({}, toast.id, true));
});
}, []);
var scheduleToastForDismissal = (0, _react.useCallback)(function (toast) {
// Start fading the toast out once its lifetime elapses.
toastIdToTimerMap.current[toast.id] = new _time.Timer(function () {
return dismissToast(toast);
}, toast.toastLifeTimeMs != null ? toast.toastLifeTimeMs : toastLifeTimeMs);
}, [dismissToast, toastLifeTimeMs]);
var scheduleAllToastsForDismissal = (0, _react.useCallback)(function () {
toasts.forEach(function (toast) {
if (!toastIdToTimerMap.current[toast.id]) {
scheduleToastForDismissal(toast);
}
});
}, [scheduleToastForDismissal, toasts]);
var addListeners = function addListeners() {
if (listElement.current) {
listElement.current.addEventListener('scroll', onScroll);
listElement.current.addEventListener('mouseenter', onMouseEnter);
listElement.current.addEventListener('mouseleave', onMouseLeave);
}
};
var removeListeners = function removeListeners() {
if (listElement.current) {
listElement.current.removeEventListener('scroll', onScroll);
listElement.current.removeEventListener('mouseenter', onMouseEnter);
listElement.current.removeEventListener('mouseleave', onMouseLeave);
}
};
// componentDidMount
(0, _react.useEffect)(function () {
addListeners();
// componentWillUnmount
return function () {
if (isScrollingAnimationFrame.current !== 0) {
window.cancelAnimationFrame(isScrollingAnimationFrame.current);
}
if (startScrollingAnimationFrame.current !== 0) {
window.cancelAnimationFrame(startScrollingAnimationFrame.current);
}
removeListeners();
dismissTimeoutIds.current.forEach(clearTimeout); // eslint-disable-line react-hooks/exhaustive-deps
for (var _toastId3 in toastIdToTimerMap.current) {
if (toastIdToTimerMap.current.hasOwnProperty(_toastId3)) {
var timer = toastIdToTimerMap.current[_toastId3]; // eslint-disable-line react-hooks/exhaustive-deps
timer.clear();
}
}
};
}, []); // eslint-disable-line react-hooks/exhaustive-deps
// componentDidUpdate
(0, _react.useEffect)(function () {
scheduleAllToastsForDismissal();
if (!isUserInteracting.current) {
// If the user has scrolled up the toast list then we don't want to annoy them by scrolling
// all the way back to the bottom.
if (isScrolledToBottom.current) {
if (prevToasts.current.length < toasts.length) {
startScrollingToBottom();
}
}
}
prevToasts.current = toasts;
}, [toasts, scheduleAllToastsForDismissal]);
// Toast dismissal side effect
// Ensure the callback has correct state by not enclosing it in `setTimeout`
(0, _react.useEffect)(function () {
var toast = toastToDismiss;
// Because this is triggered by a setTimeout, and because React does not guarantee when
// state updates happen, it is possible to double-dismiss a toast
// including by double-clicking the "x" button on the toast
// so, first check to make sure we haven't already dismissed this toast
if (toast && toastIdToTimerMap.current.hasOwnProperty(toast.id)) {
dismissToastProp(toast);
toastIdToTimerMap.current[toast.id].clear();
delete toastIdToTimerMap.current[toast.id];
setToastIdToDismissedMap(function (prev) {
var toastIdToDismissedMap = _objectSpread({}, prev);
delete toastIdToDismissedMap[toast.id];
return toastIdToDismissedMap;
});
}
}, [toastToDismiss, dismissToastProp]);
var renderedToasts = toasts.map(function (toast) {
var text = toast.text,
toastLifeTimeMs = toast.toastLifeTimeMs,
rest = (0, _objectWithoutProperties2.default)(toast, _excluded2);
var onClose = function onClose() {
return dismissToast(toast);
};
return (0, _react2.jsx)(_global_toast_list_item.EuiGlobalToastListItem, {
key: toast.id,
isDismissed: toastIdToDismissedMap[toast.id]
}, (0, _react2.jsx)(_toast.EuiToast, (0, _extends2.default)({
onClose: onClose,
onFocus: onMouseEnter,
onBlur: onMouseLeave
}, rest), text));
});
var classes = (0, _classnames.default)('euiGlobalToastList', className);
return (0, _react2.jsx)("div", (0, _extends2.default)({
"aria-live": "polite",
role: "region",
ref: listElement,
css: cssStyles,
className: classes
}, rest), renderedToasts);
};
exports.EuiGlobalToastList = EuiGlobalToastList;