/
NotificationsProvider.tsx
170 lines (152 loc) · 4.96 KB
/
NotificationsProvider.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
import React, { useRef } from 'react';
import { Transition, TransitionGroup } from 'react-transition-group';
import {
DefaultProps,
Portal,
MantineStyleSystemSize,
getDefaultZIndex,
Box,
PortalProps,
} from '@mantine/core';
import { useReducedMotion, useForceUpdate, useDidUpdate } from '@mantine/hooks';
import { NotificationsContext } from '../Notifications.context';
import { NotificationsProviderPositioning } from '../types';
import { useNotificationsEvents } from '../events';
import getPositionStyles from './get-position-styles/get-position-styles';
import getNotificationStateStyles from './get-notification-state-styles/get-notification-state-styles';
import NotificationContainer from '../NotificationContainer/NotificationContainer';
import useStyles from './NotificationsProvider.styles';
import useNotificationsState from './use-notifications-state/use-notifications-state';
const POSITIONS = [
'top-left',
'top-right',
'top-center',
'bottom-left',
'bottom-right',
'bottom-center',
] as const;
export interface NotificationProviderProps
extends Omit<DefaultProps, MantineStyleSystemSize>,
React.ComponentPropsWithoutRef<'div'> {
/** Notifications position */
position?:
| 'top-left'
| 'top-right'
| 'top-center'
| 'bottom-left'
| 'bottom-right'
| 'bottom-center';
/** Auto close timeout for all notifications, false to disable auto close, can be overwritten for individual notifications by showNotification function */
autoClose?: number | false;
/** Notification transitions duration, 0 to turn transitions off */
transitionDuration?: number;
/** Notification width in px, cannot exceed 100% */
containerWidth?: number;
/** Notification max-height in px, used for transitions */
notificationMaxHeight?: number;
/** Maximum amount of notifications displayed at a time, other new notifications will be added to queue */
limit?: number;
/** Notifications container z-index */
zIndex?: React.CSSProperties['zIndex'];
/** Your application */
children?: React.ReactNode;
/** Target element of Portal component */
target?: PortalProps['target'];
}
export function NotificationsProvider({
className,
position = 'bottom-right',
autoClose = 4000,
transitionDuration = 250,
containerWidth = 440,
notificationMaxHeight = 200,
limit = 5,
zIndex = getDefaultZIndex('overlay'),
style,
children,
target,
...others
}: NotificationProviderProps) {
const forceUpdate = useForceUpdate();
const refs = useRef<Record<string, HTMLDivElement>>({});
const previousLength = useRef<number>(0);
const {
notifications,
queue,
showNotification,
updateNotification,
hideNotification,
clean,
cleanQueue,
} = useNotificationsState({ limit });
const { classes, cx, theme } = useStyles({ zIndex });
const shouldReduceMotion = useReducedMotion();
const reduceMotion = theme.respectReducedMotion ? shouldReduceMotion : false;
const duration = reduceMotion ? 1 : transitionDuration;
const positioning = (POSITIONS.includes(position) ? position : 'bottom-right').split(
'-'
) as NotificationsProviderPositioning;
useDidUpdate(() => {
if (notifications.length > previousLength.current) {
setTimeout(() => forceUpdate(), 0);
}
previousLength.current = notifications.length;
}, [notifications]);
useNotificationsEvents({
show: showNotification,
hide: hideNotification,
update: updateNotification,
clean,
cleanQueue,
});
const items = notifications.map((notification) => (
<Transition
key={notification.id}
timeout={duration}
onEnter={() => refs.current[notification.id].offsetHeight}
nodeRef={{ current: refs.current[notification.id] }}
>
{(state) => (
<NotificationContainer
innerRef={(node) => {
refs.current[notification.id] = node;
}}
notification={notification}
onHide={hideNotification}
className={classes.notification}
autoClose={autoClose}
sx={[
{
...getNotificationStateStyles({
state,
positioning,
transitionDuration: duration,
maxHeight: notificationMaxHeight,
}),
},
...(Array.isArray(notification.sx) ? notification.sx : [notification.sx]),
]}
/>
)}
</Transition>
));
return (
<NotificationsContext.Provider value={{ notifications, queue }}>
<Portal target={target}>
<Box
className={cx(classes.notifications, className)}
style={style}
sx={{
maxWidth: containerWidth,
...getPositionStyles(positioning, theme.spacing.md),
}}
{...others}
>
<TransitionGroup>{items}</TransitionGroup>
</Box>
</Portal>
{children}
</NotificationsContext.Provider>
);
}
NotificationsProvider.displayName = '@mantine/notifications/NotificationsProvider';