UNPKG

@blueprintjs/core

Version:
75 lines 2.86 kB
/* * Copyright 2016 Palantir Technologies, Inc. All rights reserved. * * Licensed under the terms of the LICENSE file distributed with this project. */ import classNames from "classnames"; import * as React from "react"; import { AbstractPureComponent } from "../../common/abstractPureComponent"; import * as Classes from "../../common/classes"; import { DISPLAYNAME_PREFIX } from "../../common/props"; import { safeInvoke } from "../../common/utils"; import { ButtonGroup } from "../button/buttonGroup"; import { AnchorButton, Button } from "../button/buttons"; import { Icon } from "../icon/icon"; export class Toast extends AbstractPureComponent { constructor() { super(...arguments); this.handleActionClick = (e) => { safeInvoke(this.props.action.onClick, e); this.triggerDismiss(false); }; this.handleCloseClick = () => this.triggerDismiss(false); this.startTimeout = () => { this.clearTimeouts(); if (this.props.timeout > 0) { this.setTimeout(() => this.triggerDismiss(true), this.props.timeout); } }; } render() { const { className, icon, intent, message } = this.props; return (React.createElement("div", { className: classNames(Classes.TOAST, Classes.intentClass(intent), className), onBlur: this.startTimeout, onFocus: this.clearTimeouts, onMouseEnter: this.clearTimeouts, onMouseLeave: this.startTimeout, tabIndex: 0 }, React.createElement(Icon, { icon: icon }), React.createElement("span", { className: Classes.TOAST_MESSAGE }, message), React.createElement(ButtonGroup, { minimal: true }, this.maybeRenderActionButton(), React.createElement(Button, { icon: "cross", onClick: this.handleCloseClick })))); } componentDidMount() { this.startTimeout(); } componentDidUpdate(prevProps) { if (prevProps.timeout !== this.props.timeout) { if (this.props.timeout > 0) { this.startTimeout(); } else { this.clearTimeouts(); } } } componentWillUnmount() { this.clearTimeouts(); } maybeRenderActionButton() { const { action } = this.props; if (action == null) { return undefined; } else { return React.createElement(AnchorButton, Object.assign({}, action, { intent: undefined, onClick: this.handleActionClick })); } } triggerDismiss(didTimeoutExpire) { this.clearTimeouts(); safeInvoke(this.props.onDismiss, didTimeoutExpire); } } Toast.defaultProps = { className: "", message: "", timeout: 5000, }; Toast.displayName = `${DISPLAYNAME_PREFIX}.Toast`; //# sourceMappingURL=toast.js.map