@phonehtut/react-native-sonner
Version:
A simple and customizable toast notification system for React Native applications
32 lines (31 loc) • 1.3 kB
JavaScript
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useToast = useToast;
var react_1 = require("react");
function useToast() {
var _a = (0, react_1.useState)([]), toasts = _a[0], setToasts = _a[1];
var idCounter = (0, react_1.useRef)(0);
var showToast = function (message, type, duration) {
if (type === void 0) { type = 'info'; }
if (duration === void 0) { duration = 3000; }
var id = "toast-".concat(idCounter.current++);
setToasts(function (prev) { return __spreadArray(__spreadArray([], prev, true), [{ id: id, message: message, type: type, duration: duration }], false); });
};
var removeToast = function (id) {
setToasts(function (prev) { return prev.filter(function (t) { return t.id !== id; }); });
};
return {
toasts: toasts,
showToast: showToast,
removeToast: removeToast,
};
}
;