@mantine/notifications
Version:
Mantine notifications system
87 lines (86 loc) • 2.89 kB
JavaScript
"use client";
import { randomId } from "@mantine/hooks";
import { createStore, useStore } from "@mantine/store";
//#region packages/@mantine/notifications/src/notifications.store.ts
function getDistributedNotifications(data, defaultPosition, limit) {
const queue = [];
const notifications = [];
const count = {};
for (const item of data) {
const position = item.position || defaultPosition;
count[position] = count[position] || 0;
count[position] += 1;
if (count[position] <= limit) notifications.push(item);
else queue.push(item);
}
return {
notifications,
queue
};
}
const createNotificationsStore = () => createStore({
notifications: [],
queue: [],
defaultPosition: "bottom-right",
limit: 5
});
const notificationsStore = createNotificationsStore();
const useNotifications = (store = notificationsStore) => useStore(store);
function updateNotificationsState(store, update) {
const state = store.getState();
const updated = getDistributedNotifications(update([...state.notifications, ...state.queue]), state.defaultPosition, state.limit);
store.setState({
notifications: updated.notifications,
queue: updated.queue,
limit: state.limit,
defaultPosition: state.defaultPosition
});
}
function showNotification(notification, store = notificationsStore) {
const id = notification.id || randomId();
updateNotificationsState(store, (notifications) => {
if (notification.id && notifications.some((n) => n.id === notification.id)) return notifications;
return [...notifications, {
...notification,
id
}];
});
return id;
}
function hideNotification(id, store = notificationsStore) {
updateNotificationsState(store, (notifications) => notifications.filter((notification) => {
if (notification.id === id) {
notification.onClose?.(notification);
return false;
}
return true;
}));
return id;
}
function updateNotification(notification, store = notificationsStore) {
updateNotificationsState(store, (notifications) => notifications.map((item) => {
if (item.id === notification.id) return {
...item,
...notification
};
return item;
}));
return notification.id;
}
function cleanNotifications(store = notificationsStore) {
updateNotificationsState(store, () => []);
}
function cleanNotificationsQueue(store = notificationsStore) {
updateNotificationsState(store, (notifications) => notifications.slice(0, store.getState().limit));
}
const notifications = {
show: showNotification,
hide: hideNotification,
update: updateNotification,
clean: cleanNotifications,
cleanQueue: cleanNotificationsQueue,
updateState: updateNotificationsState
};
//#endregion
export { cleanNotifications, cleanNotificationsQueue, createNotificationsStore, hideNotification, notifications, notificationsStore, showNotification, updateNotification, updateNotificationsState, useNotifications };
//# sourceMappingURL=notifications.store.mjs.map