-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathNotification.tsx
More file actions
137 lines (127 loc) · 3.85 KB
/
Notification.tsx
File metadata and controls
137 lines (127 loc) · 3.85 KB
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
import { FC } from 'react';
import styled from 'styled-components';
import { NotificationProps } from './Notification.types';
import { Cross } from 'blocks';
import { toast, Toaster } from 'sonner';
import { getTextVariantStyles } from 'blocks/Blocks.utils';
import { deviceMediaQ } from 'blocks/theme';
const NotificationContainer = styled.div`
position: relative;
background-color: var(--components-in-app-notification-background-default);
border-radius: var(--radius-xxs);
box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.05);
display: flex;
flex-direction: row;
align-items: stretch;
height: 115px;
width: 398px;
cursor: pointer;
box-sizing: border-box;
border: var(--border-sm) solid var(--components-in-app-notification-stroke-bg);
overflow: hidden;
@media${deviceMediaQ.mobileL} {
width: -webkit-fill-available;
}
`;
const StyledToaster = styled(Toaster)`
width: 412px;
@media${deviceMediaQ.mobileL} {
width: 100%;
}
`;
const TextContainer = styled.div`
display: flex;
flex-direction: column;
align-items: flex-start;
justify-content: flex-start;
padding: var(--spacing-sm);
flex: 1;
box-sizing: border-box;
`;
const NotificationTitle = styled.span`
${() => getTextVariantStyles('h5-semibold', 'components-in-app-notification-text-default')}
`;
const NotificationDescription = styled.span`
${() => getTextVariantStyles('bes-regular', 'components-in-app-notification-text-secondary')}
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 3;
line-clamp: 3;
-webkit-box-orient: vertical;
`;
const IconContainer = styled.div`
display: flex;
align-items: center;
padding: var(--spacing-sm) var(--spacing-md);
border-radius: var(--radius-xxs) var(--radius-none) var(--radius-none) var(--radius-xxs);
background: radial-gradient(79.55% 79.55% at 50% 50%, #7e3bb9 0%, #171717 100%);
`;
const CloseButton = styled.div`
background-color: var(--surface-transparent);
cursor: pointer;
color: var(--components-in-app-notification-icon-default);
padding: var(--spacing-none);
position: absolute;
right: var(--spacing-xxs);
top: var(--spacing-xxs);
`;
const Container = styled.div``;
const NotificationItem: FC<NotificationProps> = ({ overlay, onClose, title, description, image, onClick }) => {
const handleNotificationClick = () => onClick?.();
const handleNotificationClose = () => {
onClose?.();
notification.hide();
};
return (
<Container onClick={handleNotificationClick}>
{overlay ? (
<>{overlay}</>
) : (
<NotificationContainer>
{image && <IconContainer>{image}</IconContainer>}
<CloseButton
onClick={(e) => {
e.stopPropagation();
handleNotificationClose();
}}
>
<Cross size={16} />
</CloseButton>
<TextContainer>
<NotificationTitle>{title}</NotificationTitle>
<NotificationDescription>{description}</NotificationDescription>
</TextContainer>
</NotificationContainer>
)}
</Container>
);
};
const Notification = () => {
return (
<StyledToaster
offset={15}
visibleToasts={5}
/>
);
};
// Store the toastId(s) in an array to manage multiple notifications
const toastIds: Array<string | number> = [];
// Export the notification object with show and hide methods
const notification = {
show: (config: NotificationProps, id?: string) => {
const toastId = toast.custom(() => <NotificationItem {...config} />, {
id: id,
duration: config.duration || Infinity,
position: config.position || 'bottom-right',
onAutoClose: config.onAutoClose,
});
if (!toastIds.find((toastId) => toastId === id)) toastIds.push(toastId);
},
hide: () => {
if (toastIds.length > 0) {
toast.dismiss();
}
},
};
export { notification, Notification };