react-snap-toast
Version:
A lightweight, customizable React toast notification library with zero-config and snap-fast setup.
141 lines (135 loc) • 4.04 kB
JavaScript
;
var React = require('react');
function _arrayLikeToArray(r, a) {
(null == a || a > r.length) && (a = r.length);
for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
return n;
}
function _arrayWithHoles(r) {
if (Array.isArray(r)) return r;
}
function _arrayWithoutHoles(r) {
if (Array.isArray(r)) return _arrayLikeToArray(r);
}
function _iterableToArray(r) {
if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r);
}
function _iterableToArrayLimit(r, l) {
var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
if (null != t) {
var e,
n,
i,
u,
a = [],
f = !0,
o = !1;
try {
if (i = (t = t.call(r)).next, 0 === l) {
if (Object(t) !== t) return;
f = !1;
} else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
} catch (r) {
o = !0, n = r;
} finally {
try {
if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
} finally {
if (o) throw n;
}
}
return a;
}
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _slicedToArray(r, e) {
return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
}
function _toConsumableArray(r) {
return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread();
}
function _unsupportedIterableToArray(r, a) {
if (r) {
if ("string" == typeof r) return _arrayLikeToArray(r, a);
var t = {}.toString.call(r).slice(8, -1);
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
}
}
function ToastContainer() {
var _useState = React.useState([]),
_useState2 = _slicedToArray(_useState, 2),
toasts = _useState2[0],
setToasts = _useState2[1];
React.useEffect(function () {
_register(function (toast) {
setToasts(function (prev) {
return [].concat(_toConsumableArray(prev), [toast]);
});
setTimeout(function () {
setToasts(function (prev) {
return prev.slice(1);
});
}, 3000);
});
}, []);
return /*#__PURE__*/React.createElement("div", {
style: {
position: 'fixed',
top: 20,
left: '50%',
transform: 'translateX(-50%)',
zIndex: 9999,
display: 'flex',
flexDirection: 'column',
gap: '8px',
width: 'auto',
maxWidth: '90vw'
}
}, toasts.map(function (t, i) {
return /*#__PURE__*/React.createElement("div", {
key: i,
style: {
padding: '10px 20px',
borderRadius: 4,
color: '#000',
backgroundColor: t.type === 'success' ? '#d4edda' : t.type === 'error' ? '#f8d7da' : '#fff',
boxShadow: '0 2px 6px rgba(0,0,0,0.15)',
minWidth: 200,
maxWidth: 400
}
}, t.message);
}));
}
var addToast;
var toast = {
show: function show(message) {
addToast && addToast({
message: message,
type: 'default'
});
},
success: function success(message) {
addToast && addToast({
message: message,
type: 'success'
});
},
error: function error(message) {
addToast && addToast({
message: message,
type: 'error'
});
}
};
function _register(fn) {
addToast = fn;
}
exports.ToastContainer = ToastContainer;
exports._register = _register;
exports.toast = toast;
//# sourceMappingURL=index.js.map