-
Notifications
You must be signed in to change notification settings - Fork 36
/
notifications-provider.tsx
225 lines (191 loc) · 6.37 KB
/
notifications-provider.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import {
Accessor,
createMemo,
createUniqueId,
For,
JSX,
onCleanup,
onMount,
splitProps,
} from "solid-js";
import { Portal } from "solid-js/web";
import { TransitionGroup } from "solid-transition-group";
import { createQueue } from "../../hooks/create-queue";
import { PositionProps } from "../../styled-system/props/position";
import { classNames } from "../../utils/css";
import { Box } from "../box/box";
import { NOTIFICATIONS_EVENTS } from "./notification.events";
import {
notificationListStyles,
NotificationListVariants,
notificationTransitionName,
} from "./notification.styles";
import { NotificationConfig, ShowNotificationProps } from "./notification.types";
import { NotificationContainer } from "./notification-container";
import {
NotificationsProviderContext,
NotificationsProviderContextValue,
} from "./notifications-provider.context";
interface NotificationsProviderProps extends NotificationListVariants {
/**
* Maximum amount of notifications displayed at a time,
* other new notifications will be added to queue.
*/
limit?: number;
/**
* The delay (in ms) before the notification hides.
*/
duration?: number;
/**
* If `true`, duration will be ignored and notifications will never dismiss.
*/
persistent?: boolean;
/**
* If `true`, notifications will show a close button.
*/
closable?: boolean;
/**
* The `z-index` css property of all notifications.
*/
zIndex?: PositionProps["zIndex"];
/**
* The children of the notifications provider.
*/
children: JSX.Element;
}
const hopeNotificationListClass = "hope-notification__list";
const DEFAULT_NOTIFICATION_DURATION = 5_000;
/**
* Context provider for the notification system.
*/
export function NotificationsProvider(props: NotificationsProviderProps) {
const [local] = splitProps(props, [
"children",
"placement",
"duration",
"persistent",
"closable",
"limit",
"zIndex",
]);
const notificationQueue = createMemo(() => {
return createQueue<NotificationConfig>({
initialValues: [],
limit: local.limit ?? 10,
});
});
const finalPlacement: Accessor<NotificationsProviderProps["placement"]> = () =>
local.placement ?? "top-end";
const notificationsAccessor = () => notificationQueue().state.current();
const queueAccessor = () => notificationQueue().state.queue();
const showNotification = (notification: ShowNotificationProps) => {
const id = notification.id ?? `hope-notification-${createUniqueId()}`;
const persistent = notification.persistent ?? local.persistent ?? false;
const duration = notification.duration ?? local.duration ?? DEFAULT_NOTIFICATION_DURATION;
const closable = notification.closable ?? local.closable ?? true;
notificationQueue().update(notifications => {
if (notification.id && notifications.some(n => n.id === notification.id)) {
return notifications;
}
const newNotification: NotificationConfig = {
...notification,
id,
persistent,
duration,
closable,
};
return [...notifications, newNotification];
});
return id;
};
const updateNotification = (id: string, notification: NotificationConfig) => {
notificationQueue().update(notifications => {
const index = notifications.findIndex(n => n.id === id);
if (index === -1) {
return notifications;
}
const newNotifications = [...notifications];
newNotifications[index] = notification;
return newNotifications;
});
};
const hideNotification = (id: string) => {
notificationQueue().update(notifications => {
return notifications.filter(notification => {
if (notification.id === id) {
notification.onClose?.(notification.id);
return false;
}
return true;
});
});
};
const clear = () => notificationQueue().update(() => []);
const clearQueue = () => notificationQueue().clearQueue();
const classes = () => {
return classNames(
hopeNotificationListClass,
notificationListStyles({
placement: finalPlacement(),
})
);
};
const transitionName = () => {
switch (finalPlacement()) {
case "top-start":
return notificationTransitionName.slideInLeft;
case "top":
return notificationTransitionName.slideInTop;
case "top-end":
return notificationTransitionName.slideInRight;
case "bottom-start":
return notificationTransitionName.slideInLeft;
case "bottom":
return notificationTransitionName.slideInBottom;
case "bottom-end":
return notificationTransitionName.slideInRight;
default:
return notificationTransitionName.slideInRight;
}
};
const context: NotificationsProviderContextValue = {
notifications: notificationsAccessor,
queue: queueAccessor,
showNotification,
updateNotification,
hideNotification,
clear,
clearQueue,
};
const showHandler = (event: any) => showNotification(event.detail);
const updateHandler = (event: any) => updateNotification(event.detail.id, event.detail);
const hideHandler = (event: any) => hideNotification(event.detail);
onMount(() => {
window.addEventListener(NOTIFICATIONS_EVENTS.show, showHandler);
window.addEventListener(NOTIFICATIONS_EVENTS.update, updateHandler);
window.addEventListener(NOTIFICATIONS_EVENTS.hide, hideHandler);
window.addEventListener(NOTIFICATIONS_EVENTS.clear, clear);
window.addEventListener(NOTIFICATIONS_EVENTS.clearQueue, clearQueue);
});
onCleanup(() => {
window.removeEventListener(NOTIFICATIONS_EVENTS.show, showHandler);
window.removeEventListener(NOTIFICATIONS_EVENTS.update, updateHandler);
window.removeEventListener(NOTIFICATIONS_EVENTS.hide, hideHandler);
window.removeEventListener(NOTIFICATIONS_EVENTS.clear, clear);
window.removeEventListener(NOTIFICATIONS_EVENTS.clearQueue, clearQueue);
});
return (
<NotificationsProviderContext.Provider value={context}>
<Portal>
<Box class={classes()} zIndex={local.zIndex}>
<TransitionGroup name={transitionName()}>
<For each={context.notifications()}>
{notification => <NotificationContainer {...notification} />}
</For>
</TransitionGroup>
</Box>
</Portal>
{local.children}
</NotificationsProviderContext.Provider>
);
}