@blueprintjs/core
Version:
Core styles & components
80 lines • 4.58 kB
JavaScript
/*
* Copyright 2024 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Toast2 = void 0;
var tslib_1 = require("tslib");
var classnames_1 = tslib_1.__importDefault(require("classnames"));
var React = tslib_1.__importStar(require("react"));
var icons_1 = require("@blueprintjs/icons");
var common_1 = require("../../common");
var props_1 = require("../../common/props");
var useTimeout_1 = require("../../hooks/useTimeout");
var buttonGroup_1 = require("../button/buttonGroup");
var buttons_1 = require("../button/buttons");
var icon_1 = require("../icon/icon");
/**
* Toast2 component.
*
* Compared to the deprecated `Toast` component, this is a function component which forwards DOM
* refs and is thus compatible with `Overlay2`.
*
* @see https://blueprintjs.com/docs/#core/components/toast2
*/
exports.Toast2 = React.forwardRef(function (props, ref) {
var action = props.action, className = props.className, icon = props.icon, intent = props.intent, _a = props.isCloseButtonShown, isCloseButtonShown = _a === void 0 ? true : _a, message = props.message, onDismiss = props.onDismiss, _b = props.timeout, timeout = _b === void 0 ? 5000 : _b;
var _c = React.useState(false), isTimeoutStarted = _c[0], setIsTimeoutStarted = _c[1];
var startTimeout = React.useCallback(function () { return setIsTimeoutStarted(true); }, []);
var clearTimeout = React.useCallback(function () { return setIsTimeoutStarted(false); }, []);
// Per docs: "Providing a value less than or equal to 0 will disable the timeout (this is discouraged)."
var isTimeoutEnabled = timeout != null && timeout > 0;
// timeout is triggered & cancelled by updating `isTimeoutStarted` state
(0, useTimeout_1.useTimeout)(function () {
triggerDismiss(true);
}, isTimeoutStarted && isTimeoutEnabled ? timeout : null);
// start timeout on mount or change, cancel on unmount
React.useEffect(function () {
if (isTimeoutEnabled) {
startTimeout();
}
else {
clearTimeout();
}
return clearTimeout;
}, [clearTimeout, startTimeout, isTimeoutEnabled, timeout]);
var triggerDismiss = React.useCallback(function (didTimeoutExpire) {
clearTimeout();
onDismiss === null || onDismiss === void 0 ? void 0 : onDismiss(didTimeoutExpire);
}, [clearTimeout, onDismiss]);
var handleCloseClick = React.useCallback(function () { return triggerDismiss(false); }, [triggerDismiss]);
var handleActionClick = React.useCallback(function (e) {
var _a;
(_a = action === null || action === void 0 ? void 0 : action.onClick) === null || _a === void 0 ? void 0 : _a.call(action, e);
triggerDismiss(false);
}, [action, triggerDismiss]);
return (React.createElement("div", { className: (0, classnames_1.default)(common_1.Classes.TOAST, common_1.Classes.intentClass(intent), className),
// Pause timeouts if users are hovering over or click on the toast. The toast may have
// actions the user wants to click. It'd be a poor experience to "pull the toast" out
// from under them.
onBlur: startTimeout, onFocus: clearTimeout, onMouseEnter: clearTimeout, onMouseLeave: startTimeout, ref: ref, tabIndex: 0 },
React.createElement(icon_1.Icon, { icon: icon }),
React.createElement("span", { className: common_1.Classes.TOAST_MESSAGE, role: "alert" }, message),
React.createElement(buttonGroup_1.ButtonGroup, { variant: "minimal" },
action && React.createElement(buttons_1.AnchorButton, tslib_1.__assign({}, action, { intent: undefined, onClick: handleActionClick })),
isCloseButtonShown && React.createElement(buttons_1.Button, { "aria-label": "Close", icon: React.createElement(icons_1.Cross, null), onClick: handleCloseClick }))));
});
exports.Toast2.displayName = "".concat(props_1.DISPLAYNAME_PREFIX, ".Toast2");
//# sourceMappingURL=toast2.js.map
;