UNPKG

@mantine/notifications

Version:

Mantine notifications system

100 lines (99 loc) 3.44 kB
"use client"; import { randomId } from "@mantine/hooks"; import { createStore, useStore } from "@mantine/store"; //#region packages/@mantine/notifications/src/notifications.store.ts let notificationSequence = 0; function getDistributedNotifications(data, defaultPosition, limit) { const queue = []; const notifications = []; const groups = /* @__PURE__ */ new Map(); for (const item of data) { const position = item.position || defaultPosition; const group = groups.get(position); if (group) group.push(item); else groups.set(position, [item]); } for (const group of groups.values()) { group.sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0) || (a.__sequence ?? 0) - (b.__sequence ?? 0)); group.forEach((item, index) => { if (index < 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 notifications = update([...state.notifications, ...state.queue]); for (const item of notifications) if (item.__sequence === void 0) { item.__sequence = notificationSequence; notificationSequence += 1; } const updated = getDistributedNotifications(notifications, 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) { const { defaultPosition, limit } = store.getState(); updateNotificationsState(store, (notifications) => getDistributedNotifications(notifications, defaultPosition, limit).notifications); } 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