-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathToast.tsx
More file actions
194 lines (169 loc) · 6.33 KB
/
Copy pathToast.tsx
File metadata and controls
194 lines (169 loc) · 6.33 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
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
import { CheckCircle } from '@phosphor-icons/react/dist/csr/CheckCircle';
import { CircleNotch } from '@phosphor-icons/react/dist/csr/CircleNotch';
import { Info } from '@phosphor-icons/react/dist/csr/Info';
import { Warning } from '@phosphor-icons/react/dist/csr/Warning';
import { WarningCircle } from '@phosphor-icons/react/dist/csr/WarningCircle';
import { X } from '@phosphor-icons/react/dist/csr/X';
import React, { useEffect, useState } from 'react';
import ReactDOM from 'react-dom';
import { useTranslation } from 'react-i18next';
import { ThemeProvider, useTheme } from 'styled-components';
import {
ANIMATION_DURATION_MS,
ToastActionButton,
ToastBody,
ToastCloseButton,
ToastContainer,
ToastWrapper,
} from '@components/components/Toast/components';
import { ToastAPI, ToastOptions, ToastVariant } from '@components/components/Toast/types';
const MAX_TOASTS = 5;
const DEFAULT_DURATIONS: Record<ToastVariant, number> = {
success: 3,
error: 4,
info: 3,
warning: 4,
loading: 0,
};
const VARIANT_ICONS: Record<ToastVariant, React.ReactNode> = {
success: <CheckCircle size={18} weight="fill" />,
error: <WarningCircle size={18} weight="fill" />,
warning: <Warning size={18} weight="fill" />,
info: <Info size={18} weight="fill" />,
loading: <CircleNotch size={18} weight="bold" />,
};
const ASSERTIVE_VARIANTS: ReadonlySet<ToastVariant> = new Set(['error', 'warning']);
interface ToastEntry {
id: string;
variant: ToastVariant;
content: React.ReactNode;
options?: ToastOptions;
exiting?: boolean;
}
type Listener = () => void;
let toasts: ToastEntry[] = [];
let listeners: Listener[] = [];
const timers = new Map<string, ReturnType<typeof setTimeout>>();
function emit() {
listeners.forEach((l) => l());
}
function subscribe(listener: Listener) {
listeners.push(listener);
return () => {
listeners = listeners.filter((l) => l !== listener);
};
}
function removeToast(id: string) {
const target = toasts.find((t) => t.id === id);
if (!target || target.exiting) return;
toasts = toasts.map((t) => (t.id === id ? { ...t, exiting: true } : t));
emit();
setTimeout(() => {
const entry = toasts.find((t) => t.id === id);
if (entry?.options?.onClose) entry.options.onClose();
toasts = toasts.filter((t) => t.id !== id);
timers.delete(id);
emit();
}, ANIMATION_DURATION_MS);
}
function addToast(variant: ToastVariant, content: string | React.ReactNode, options?: ToastOptions) {
const id = options?.key || `toast-${Date.now()}-${Math.random().toString(36).slice(2, 7)}`;
const duration = options?.duration !== undefined ? options.duration : DEFAULT_DURATIONS[variant];
const existing = toasts.find((t) => t.id === id);
if (existing) {
toasts = toasts.map((t) => (t.id === id ? { ...t, variant, content, options, exiting: false } : t));
const prevTimer = timers.get(id);
if (prevTimer) clearTimeout(prevTimer);
} else {
toasts = [...toasts, { id, variant, content, options }];
// Evict oldest when over the limit
while (toasts.length > MAX_TOASTS) {
const oldest = toasts[0];
toasts = toasts.slice(1);
const oldTimer = timers.get(oldest.id);
if (oldTimer) clearTimeout(oldTimer);
timers.delete(oldest.id);
}
}
emit();
if (duration && duration > 0) {
const timer = setTimeout(() => removeToast(id), duration * 1000);
timers.set(id, timer);
}
}
function destroyToast(key?: string) {
if (key) {
removeToast(key);
} else {
timers.forEach((timer) => clearTimeout(timer));
timers.clear();
const pending = toasts;
toasts = [];
emit();
pending.forEach((entry) => entry.options?.onClose?.());
}
}
const ToastItem = React.memo(({ entry }: { entry: ToastEntry }) => {
const { t: tc } = useTranslation('common.actions');
const icon = entry.options?.icon ?? VARIANT_ICONS[entry.variant];
const liveRegion = ASSERTIVE_VARIANTS.has(entry.variant) ? 'assertive' : 'polite';
return (
<ToastWrapper $variant={entry.variant} $exiting={entry.exiting} role="status" aria-live={liveRegion}>
<ToastBody>
<span className="toast-icon">{icon}</span>
<span className="toast-text">{entry.content}</span>
</ToastBody>
{entry.options?.actions?.map((action) => (
<ToastActionButton
key={action.label}
className="toast-btn"
type="button"
onClick={action.onClick}
aria-label={action.label}
title={action.label}
>
{action.icon ?? action.label}
</ToastActionButton>
))}
<ToastCloseButton
className="toast-btn"
type="button"
onClick={() => removeToast(entry.id)}
aria-label={tc('dismiss')}
>
<X size={14} weight="bold" />
</ToastCloseButton>
</ToastWrapper>
);
});
/**
* Mount once inside the app's ThemeProvider tree.
* Renders toast notifications into a portal at the top-right of the viewport.
*/
export function ToastRenderer() {
const [, setTick] = useState(0);
const theme = useTheme();
useEffect(() => {
return subscribe(() => setTick((t) => t + 1));
}, []);
const currentToasts = toasts;
if (currentToasts.length === 0) return null;
return ReactDOM.createPortal(
<ThemeProvider theme={theme}>
<ToastContainer data-testid="toast-notification-container">
{currentToasts.map((entry) => (
<ToastItem key={entry.id} entry={entry} />
))}
</ToastContainer>
</ThemeProvider>,
document.body,
);
}
export const toast: ToastAPI = {
success: (content, options) => addToast('success', content, options),
error: (content, options) => addToast('error', content, options),
info: (content, options) => addToast('info', content, options),
warning: (content, options) => addToast('warning', content, options),
loading: (content, options) => addToast('loading', content, options),
destroy: destroyToast,
};