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
<script lang="ts">
import { Transition } from '@inflector/keeno'
type Kind = 'success' | 'info' | 'error'
type Toast = { id: number; kind: Kind; title: string; text: string }
const MESSAGES: Record<Kind, [string, string][]> = {
success: [
['Saved', 'Your changes are live.'],
['Deployed', 'Production is on the latest build.'],
['Invite sent', '[email protected] will get an email.'],
],
info: [
['3 new comments', 'On “Ship the docs”.'],
['Update available', 'Version 0.3 is out.'],
['Syncing', 'Running in the background.'],
],
error: [
['Network error', 'Check your connection and retry.'],
['Permission denied', 'Ask an owner for access.'],
['Payment failed', 'The card was declined.'],
],
}
const ICONS: Record<Kind, string> = { success: '✓', info: 'i', error: '!' }
const KINDS: Kind[] = ['success', 'info', 'error']
const toasts = state<Toast[]>([])
let id = 0
function push(kind: Kind) {
const list = MESSAGES[kind]
const [title, text] = list[Math.floor(Math.random() * list.length)]
toasts.set([{ id: ++id, kind, title, text }, ...toasts].slice(0, 3))
const toast = id
setTimeout(() => dismiss(toast), 4000)
}
const dismiss = (id: number) => toasts.set(toasts.filter((t) => t.id !== id))
</script>
<div class="buttons">
{KINDS.map((k) => (
<button key={k} class={k} onClick={() => push(k)}><span class="dot"></span>{k}</button>
))}
</div>
<div class="area">
{toasts.length === 0 && <p class="empty">Notifications show up here</p>}
<ul class="stack">
<Transition name="toast">
{toasts.map((t) => (
<li key={t.id} class="toast-wrap">
<div class={`toast ${t.kind}`}>
<span class="icon">{ICONS[t.kind]}</span>
<div class="text">
<strong>{t.title}</strong>
<span>{t.text}</span>
</div>
<button class="close" aria-label="Dismiss" onClick={() => dismiss(t.id)}>✕</button>
</div>
</li>
))}
</Transition>
</ul>
</div>
<style>
.buttons {
display: flex;
gap: 0.5rem;
}
.buttons button {
text-transform: capitalize;
}
.dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--c);
}
.success {
--c: var(--success);
}
.info {
--c: var(--info);
}
.error {
--c: var(--danger);
}
.area {
position: relative;
min-height: 13rem;
margin-top: 0.875rem;
padding: 0.75rem;
border: 1px dashed var(--border);
border-radius: 0.75rem;
}
.empty {
position: absolute;
inset: 0;
display: grid;
place-items: center;
margin: 0;
font-size: 0.8rem;
color: var(--faint);
}
.stack {
display: grid;
gap: 0.5rem;
margin: 0;
padding: 0;
list-style: none;
}
.toast {
display: flex;
align-items: flex-start;
gap: 0.625rem;
padding: 0.625rem 0.5rem 0.625rem 0.75rem;
border: 1px solid var(--border);
border-radius: 0.625rem;
background: var(--bg);
box-shadow: 0 8px 24px -12px rgb(0 0 0 / 0.3);
}
.icon {
display: grid;
place-items: center;
flex: none;
width: 18px;
height: 18px;
margin-top: 1px;
border-radius: 50%;
background: color-mix(in srgb, var(--c) 15%, transparent);
color: var(--c);
font-size: 10px;
font-weight: 700;
}
.text {
display: grid;
flex: 1;
min-width: 0;
line-height: 1.35;
}
.text strong {
font-size: 0.85rem;
font-weight: 500;
}
.text span {
overflow: hidden;
font-size: 0.8rem;
color: var(--muted);
text-overflow: ellipsis;
white-space: nowrap;
}
.close {
min-height: 1.375rem;
padding: 0 0.375rem;
border: none;
background: none;
box-shadow: none;
color: var(--faint);
font-size: 0.7rem;
}
.close:hover:not(:disabled) {
background: var(--hover);
color: var(--fg);
}
.toast-enter-active,
.toast-leave-active {
transition:
opacity 0.25s ease,
transform 0.3s var(--ease-spring);
}
.toast-enter-from {
opacity: 0;
transform: translateY(-8px) scale(0.97);
}
.toast-leave-to {
opacity: 0;
transform: translateX(24px);
}
</style>