Skip to content

Commit 1f83b3f

Browse files
stefan-lauxstefan-laux
and
stefan-laux
authored
#170 implement toast stack example (#315)
* add changes to doc * build docs --------- Co-authored-by: stefan-laux <[email protected]>
1 parent 04c597d commit 1f83b3f

File tree

3 files changed

+152
-16
lines changed

3 files changed

+152
-16
lines changed

docs/components/six-alert.md

+48-3
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,56 @@ You can also create your own utility that emits toast notifications with a funct
260260

261261
## The Toast Stack
262262

263-
The toast stack is a fixed position singleton element created and managed internally by the alert component. It will be added and removed from the DOM as needed when toasts are shown. When more than one toast is visible, they will stack vertically in the toast stack.
263+
The Toast Stack allows multiple toast notifications to be displayed in a clean, organized stack. Below is an example where 5 toast notifications are dynamically created and displayed with a short delay between each toast.
264264

265-
By default, the toast stack is positioned at the top-right of the viewport. You can change its position by targeting `.six-toast-stack` in your stylesheet. To make toasts appear at the top-left of the viewport, for example, use the following styles.
265+
<docs-demo-six-alert-7></docs-demo-six-alert-7>
266+
267+
```html
268+
<div class="toast-stack-wrapper">
269+
<six-button type="primary">Create 5 Toasts</six-button>
270+
</div>
271+
272+
<script type="module">
273+
(() => {
274+
const toastStackWrapper = document.querySelector('.toast-stack-wrapper');
275+
const createToastButton = toastStackWrapper.querySelector('six-button');
276+
277+
// Always escape HTML for text arguments!
278+
function escapeHtml(html) {
279+
const div = document.createElement('div');
280+
div.textContent = html;
281+
return div.innerHTML;
282+
}
283+
284+
// Custom function to emit toast notifications
285+
function createToast(message, type = 'primary', icon = 'info', duration = 4000) {
286+
const toast = Object.assign(document.createElement('six-alert'), {
287+
type: type,
288+
closable: true,
289+
duration: duration,
290+
innerHTML: `
291+
<six-icon slot="icon">${icon}</six-icon>
292+
${escapeHtml(message)}`,
293+
});
294+
document.body.appendChild(toast);
295+
toast.toast();
296+
}
297+
298+
createToastButton.addEventListener('click', () => {
299+
const toastTypes = ['primary', 'success', 'info', 'warning', 'danger'];
300+
let delay = 0;
301+
for (let i = 1; i <= 5; i++) {
302+
const randomType = toastTypes[Math.floor(Math.random() * toastTypes.length)];
303+
setTimeout(() => {
304+
createToast(`Toast #${i}`, randomType, 'info');
305+
}, delay);
306+
delay += 200;
307+
}
308+
});
309+
})();
310+
</script>
311+
```
266312

267-
By design, it is not possible to show toasts in more than one stack simultaneously. Such behavior is confusing and makes for a poor user experience.
268313

269314

270315
<!-- Auto Generated Below -->
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<template>
2+
<div>
3+
4+
<div class="toast-stack-wrapper">
5+
<six-button type="primary">Create 5 Toasts</six-button>
6+
</div>
7+
8+
9+
10+
</div>
11+
</template>
12+
<style>
13+
14+
</style>
15+
<script>
16+
export default {
17+
name: 'docs-demo-six-alert-7',
18+
mounted() {
19+
(() => {
20+
const toastStackWrapper = document.querySelector('.toast-stack-wrapper');
21+
const createToastButton = toastStackWrapper.querySelector('six-button');
22+
23+
// Always escape HTML for text arguments!
24+
function escapeHtml(html) {
25+
const div = document.createElement('div');
26+
div.textContent = html;
27+
return div.innerHTML;
28+
}
29+
30+
// Custom function to emit toast notifications
31+
function createToast(message, type = 'primary', icon = 'info', duration = 4000) {
32+
const toast = Object.assign(document.createElement('six-alert'), {
33+
type: type,
34+
closable: true,
35+
duration: duration,
36+
innerHTML: `
37+
<six-icon slot="icon">${icon}</six-icon>
38+
${escapeHtml(message)}`,
39+
});
40+
document.body.appendChild(toast);
41+
toast.toast();
42+
}
43+
44+
createToastButton.addEventListener('click', () => {
45+
const toastTypes = ['primary', 'success', 'info', 'warning', 'danger'];
46+
let delay = 0;
47+
for (let i = 1; i <= 5; i++) {
48+
const randomType = toastTypes[Math.floor(Math.random() * toastTypes.length)];
49+
setTimeout(() => {
50+
createToast(`Toast #${i}`, randomType, 'info');
51+
}, delay);
52+
delay += 200;
53+
}
54+
});
55+
})();
56+
}
57+
}
58+
</script>

libraries/ui-library/src/components/six-alert/index.html

+46-13
Original file line numberDiff line numberDiff line change
@@ -297,22 +297,55 @@ <h2>Creating Toasts Imperatively</h2>
297297
<h2>The Toast Stack</h2>
298298

299299
<p>
300-
The toast stack is a fixed position singleton element created and managed internally by the alert component. It
301-
will be added and removed from the DOM as needed when toasts are shown. When more than one toast is visible,
302-
they will stack vertically in the toast stack.
300+
The Toast Stack allows multiple toast notifications to be displayed in a clean, organized stack. Below is an
301+
example where 5 toast notifications are dynamically created and displayed with a short delay between each toast.
303302
</p>
304303

305-
<p>
306-
By default, the toast stack is positioned at the top-right of the viewport. You can change its position by
307-
targeting
308-
<code>.six-toast-stack</code> in your stylesheet. To make toasts appear at the top-left of the viewport, for
309-
example, use the following styles.
310-
</p>
304+
<section>
305+
<div class="toast-stack-wrapper">
306+
<six-button type="primary">Create 5 Toasts</six-button>
307+
</div>
311308

312-
<p class="warn">
313-
By design, it is not possible to show toasts in more than one stack simultaneously. Such behavior is confusing
314-
and makes for a poor user experience.
315-
</p>
309+
<script type="module">
310+
(() => {
311+
const toastStackWrapper = document.querySelector('.toast-stack-wrapper');
312+
const createToastButton = toastStackWrapper.querySelector('six-button');
313+
314+
// Always escape HTML for text arguments!
315+
function escapeHtml(html) {
316+
const div = document.createElement('div');
317+
div.textContent = html;
318+
return div.innerHTML;
319+
}
320+
321+
// Custom function to emit toast notifications
322+
function createToast(message, type = 'primary', icon = 'info', duration = 4000) {
323+
const toast = Object.assign(document.createElement('six-alert'), {
324+
type: type,
325+
closable: true,
326+
duration: duration,
327+
innerHTML: `
328+
<six-icon slot="icon">${icon}</six-icon>
329+
${escapeHtml(message)}`,
330+
});
331+
document.body.appendChild(toast);
332+
toast.toast();
333+
}
334+
335+
createToastButton.addEventListener('click', () => {
336+
const toastTypes = ['primary', 'success', 'info', 'warning', 'danger'];
337+
let delay = 0;
338+
for (let i = 1; i <= 5; i++) {
339+
const randomType = toastTypes[Math.floor(Math.random() * toastTypes.length)];
340+
setTimeout(() => {
341+
createToast(`Toast #${i}`, randomType, 'info');
342+
}, delay);
343+
delay += 200;
344+
}
345+
});
346+
})();
347+
</script>
348+
</section>
316349
</div>
317350
</body>
318351
</html>

0 commit comments

Comments
 (0)