-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
73 lines (64 loc) · 1.54 KB
/
Copy pathindex.js
File metadata and controls
73 lines (64 loc) · 1.54 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
import React, { useReducer } from 'react';
const toastReducer = (state, action) => {
switch (action.type) {
case 'add':
return [
...state,
{
id: action.id,
content: action.content,
metadata: action.metadata
}
];
case 'remove':
return [...state.filter(toast => toast.id !== action.id)];
default:
return state;
}
};
const timers = {};
const startTimer = (id, removeToast, delay) => {
if (!timers[id]) {
timers[id] = setTimeout(() => removeToast(id), delay);
}
};
const dismiss = (id, removeToast) => {
if (timers[id]) {
clearTimeout(timers[id]);
delete timers[id];
}
removeToast(id);
};
let uid = 0;
export default delay => {
const [toastList, dispatch] = useReducer(toastReducer, []);
const removeToast = id => dispatch({ type: 'remove', id });
const addToast = (content, metadata) => {
uid += 1;
startTimer(uid, removeToast, delay || 15000);
dispatch({ type: 'add', id: uid, content, metadata });
};
const Toaster = ({ style, className, toastClass, toastMargin }) => (
<div style={style} className={className}>
{toastList.map(({id, content}) => {
const remove = () => dismiss(id, removeToast);
return (
<div
key={id}
style={{ margin: toastMargin }}
className={toastClass}
onClick={remove}
>
{content}
</div>
);
}
)}
</div>
);
return [
Toaster,
addToast,
toastList
];
};