UNPKG

baseui

Version:

A React Component library implementing the Base design language

247 lines (245 loc) • 10.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var React = _interopRequireWildcard(require("react")); var _overrides = require("../helpers/overrides"); var _delete = _interopRequireDefault(require("../icon/delete")); var _styledComponents = require("./styled-components"); var _constants = require("./constants"); var _locale = require("../locale"); var _focusVisible = require("../utils/focusVisible"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (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 && Object.prototype.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 _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(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); } /* Copyright (c) Uber Technologies, Inc. This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree. */ /* global document */ class Toast extends React.Component { constructor(props) { super(props); _defineProperty(this, "autoHideTimeout", void 0); _defineProperty(this, "animateInTimer", void 0); _defineProperty(this, "animateOutCompleteTimer", void 0); _defineProperty(this, "closeRef", void 0); _defineProperty(this, "previouslyFocusedElement", void 0); _defineProperty(this, "state", { isVisible: false, isRendered: true, isFocusVisible: false }); _defineProperty(this, "handleFocus", event => { if ((0, _focusVisible.isFocusVisible)(event)) { this.setState({ isFocusVisible: true }); } }); // eslint-disable-next-line @typescript-eslint/no-unused-vars _defineProperty(this, "handleBlur", event => { if (this.state.isFocusVisible !== false) { this.setState({ isFocusVisible: false }); } }); _defineProperty(this, "animateIn", () => { // Defer to next event loop this.animateInTimer = setTimeout(() => { this.setState({ isVisible: true }); }, 0); }); _defineProperty(this, "animateOut", (callback = () => {}) => { this.setState({ isVisible: false }); // Remove the toast from the DOM after animation finishes this.animateOutCompleteTimer = setTimeout(() => { this.setState({ isRendered: false }); callback(); }, 600); }); _defineProperty(this, "dismiss", () => { this.animateOut(this.props.onClose); if (this.props.autoFocus && this.previouslyFocusedElement) { this.previouslyFocusedElement.focus(); } }); _defineProperty(this, "onFocus", e => { if (!this.state.isVisible) return; // @ts-ignore clearTimeout(this.autoHideTimeout); // @ts-ignore clearTimeout(this.animateOutCompleteTimer); typeof this.props.onFocus === 'function' && this.props.onFocus(e); }); _defineProperty(this, "onMouseEnter", e => { if (!this.state.isVisible) return; // @ts-ignore clearTimeout(this.autoHideTimeout); // @ts-ignore clearTimeout(this.animateOutCompleteTimer); typeof this.props.onMouseEnter === 'function' && this.props.onMouseEnter(e); }); _defineProperty(this, "onBlur", e => { this.startTimeout(); typeof this.props.onBlur === 'function' && this.props.onBlur(e); }); _defineProperty(this, "onMouseLeave", e => { this.startTimeout(); typeof this.props.onMouseLeave === 'function' && this.props.onMouseLeave(e); }); this.closeRef = /*#__PURE__*/React.createRef(); this.previouslyFocusedElement = null; } componentDidMount() { this.animateIn(); this.startTimeout(); if (typeof document !== 'undefined' && this.props.autoFocus && this.closeRef && this.closeRef.current && this.closeRef.current.focus && typeof this.closeRef.current.focus === 'function') { // todo(flow->ts): double check if typecast is correct this.previouslyFocusedElement = document.activeElement; this.closeRef.current.focus(); this.setState({ isFocusVisible: true }); } } componentDidUpdate(prevProps) { if (this.props.autoHideDuration !== prevProps.autoHideDuration || this.props.__updated !== prevProps.__updated) { this.startTimeout(); } } componentWillUnmount() { this.clearTimeout(); } startTimeout() { if (this.props.autoHideDuration) { if (this.autoHideTimeout) { clearTimeout(this.autoHideTimeout); } this.autoHideTimeout = setTimeout(this.dismiss, this.props.autoHideDuration); } } clearTimeout() { [this.autoHideTimeout, this.animateInTimer, this.animateOutCompleteTimer].forEach(timerId => { if (timerId) { clearTimeout(timerId); } }); } getSharedProps() { const { kind, notificationType, closeable } = this.props; const { isRendered, isVisible } = this.state; return { $kind: kind, $type: notificationType, $closeable: closeable, $isRendered: isRendered, $isVisible: isVisible }; } render() { const { children, closeable, autoFocus } = this.props; const isAlertDialog = closeable && autoFocus; const { isRendered } = this.state; const { // @ts-ignore Body: BodyOverride, // @ts-ignore CloseIcon: CloseIconOverride, // @ts-ignore InnerContainer: InnerContainerOverride } = this.props.overrides; const [Body, bodyProps] = (0, _overrides.getOverrides)(BodyOverride, _styledComponents.Body); const [InnerContainer, innerContainerProps] = (0, _overrides.getOverrides)(InnerContainerOverride, _styledComponents.InnerContainer); const [CloseIcon, closeIconProps] = (0, _overrides.getOverrides)(CloseIconOverride, _styledComponents.CloseIconSvg); const closeIconOverrides = (0, _overrides.mergeOverrides)({ Svg: { component: CloseIcon } }, { Svg: CloseIconOverride }); const sharedProps = this.getSharedProps(); if (!isRendered) { return null; } // Default role is alert unless given a role in props or the toast has an autofocus close button const role = this.props.hasOwnProperty('role') ? this.props.role : isAlertDialog ? 'alertdialog' : 'alert'; const ariaLive = !this.props.hasOwnProperty('role') && isAlertDialog || this.props.role == 'alertdialog' ? 'assertive' : this.props.role == 'alert' || !this.props.hasOwnProperty('role') ? undefined // adding both aria-live and role="alert" causes double speaking issues : 'polite'; return /*#__PURE__*/React.createElement(_locale.LocaleContext.Consumer, null, locale => /*#__PURE__*/React.createElement(Body, _extends({ role: role, "data-baseweb": this.props['data-baseweb'] || 'toast' }, sharedProps, bodyProps, { "aria-atomic": true, "aria-live": ariaLive // the properties below have to go after overrides , onBlur: this.onBlur, onFocus: this.onFocus, onMouseEnter: this.onMouseEnter, onMouseLeave: this.onMouseLeave }), /*#__PURE__*/React.createElement(InnerContainer, _extends({}, sharedProps, innerContainerProps), typeof children === 'function' ? children({ dismiss: this.dismiss }) : children), closeable ? /*#__PURE__*/React.createElement(_delete.default // @ts-ignore , _extends({ ref: this.closeRef, "aria-hidden": true, role: "button", tabIndex: -1, $isFocusVisible: this.state.isFocusVisible, onClick: this.dismiss, onKeyPress: event => { if (event.key === 'Enter') { this.dismiss(); } }, title: locale.toast.close }, sharedProps, closeIconProps, { onFocus: (0, _focusVisible.forkFocus)(closeIconProps, this.handleFocus), onBlur: (0, _focusVisible.forkBlur)(closeIconProps, this.handleBlur), overrides: closeIconOverrides })) : null)); } } _defineProperty(Toast, "defaultProps", { autoFocus: false, autoHideDuration: 0, closeable: true, kind: _constants.KIND.info, notificationType: _constants.TYPE.toast, // Do we need a separate handler for // when a notification dismisses automatically onClose: () => {}, onBlur: () => {}, onFocus: () => {}, onMouseEnter: () => {}, onMouseLeave: () => {}, overrides: {} }); var _default = exports.default = Toast;