@coreui/react
Version:
UI Components Library for React.js
67 lines (64 loc) • 3.47 kB
JavaScript
import { __rest, __assign } from '../../node_modules/tslib/tslib.es6.js';
import React, { forwardRef, useRef, useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import classNames from '../../_virtual/index.js';
import { CToastContext } from './CToastContext.js';
import { useForkedRef } from '../../hooks/useForkedRef.js';
import '@popperjs/core';
import { colorPropType } from '../../props.js';
import Transition from '../../node_modules/react-transition-group/esm/Transition.js';
var CToast = forwardRef(function (_a, ref) {
var children = _a.children, _b = _a.animation, animation = _b === void 0 ? true : _b, _c = _a.autohide, autohide = _c === void 0 ? true : _c, className = _a.className, color = _a.color, _d = _a.delay, delay = _d === void 0 ? 5000 : _d, index = _a.index, innerKey = _a.innerKey, _e = _a.visible, visible = _e === void 0 ? false : _e, onClose = _a.onClose, onShow = _a.onShow, rest = __rest(_a, ["children", "animation", "autohide", "className", "color", "delay", "index", "innerKey", "visible", "onClose", "onShow"]);
var toastRef = useRef(null);
var forkedRef = useForkedRef(ref, toastRef);
var _f = useState(false), _visible = _f[0], setVisible = _f[1];
var timeout = useRef(undefined);
useEffect(function () {
setVisible(visible);
}, [visible]);
var contextValues = {
visible: _visible,
setVisible: setVisible,
};
// triggered on mount and destroy
useEffect(function () { return function () { return clearTimeout(timeout.current); }; }, []);
useEffect(function () {
_autohide();
}, [_visible]);
var _autohide = function () {
if (autohide) {
clearTimeout(timeout.current);
timeout.current = window.setTimeout(function () {
setVisible(false);
}, delay);
}
};
return (React.createElement(Transition, { in: _visible, nodeRef: toastRef, onEnter: function () { return onShow && onShow(index !== null && index !== void 0 ? index : null); }, onExited: function () { return onClose && onClose(index !== null && index !== void 0 ? index : null); }, timeout: 250, unmountOnExit: true }, function (state) {
var _a;
return (React.createElement(CToastContext.Provider, { value: contextValues },
React.createElement("div", __assign({ className: classNames('toast', (_a = {
fade: animation
},
_a["bg-".concat(color)] = color,
_a['border-0'] = color,
_a['show showing'] = state === 'entering' || state === 'exiting',
_a.show = state === 'entered',
_a), className), "aria-live": "assertive", "aria-atomic": "true", role: "alert", onMouseEnter: function () { return clearTimeout(timeout.current); }, onMouseLeave: function () { return _autohide(); } }, rest, { key: innerKey, ref: forkedRef }), children)));
}));
});
CToast.propTypes = {
animation: PropTypes.bool,
autohide: PropTypes.bool,
children: PropTypes.node,
className: PropTypes.string,
color: colorPropType,
delay: PropTypes.number,
index: PropTypes.number,
innerKey: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
onClose: PropTypes.func,
onShow: PropTypes.func,
visible: PropTypes.bool,
};
CToast.displayName = 'CToast';
export { CToast };
//# sourceMappingURL=CToast.js.map