refactor: move toast container to base template

This commit is contained in:
2026-06-16 11:05:16 +02:00
committed by Milas Holsting
parent 656ddbd005
commit ac33f1c0dd
2 changed files with 4 additions and 15 deletions

View File

@@ -5,20 +5,8 @@ interface ToastOptions {
duration?: number; duration?: number;
} }
/** Lazily create and return the toast container element. */ /** Return the toast container element. */
const toastContainer = (): HTMLElement => { const toastContainer = (): HTMLElement | null => document.getElementById("toast-container");
let container = document.getElementById("toast-container");
if (!container) {
container = document.createElement("div");
container.id = "toast-container";
container.className = "fixed bottom-4 right-4 z-100 flex flex-col gap-2";
container.setAttribute("role", "status");
container.setAttribute("aria-live", "polite");
container.setAttribute("aria-relevant", "additions");
document.body.appendChild(container);
}
return container;
};
/** /**
* Show a toast notification with optional auto-dismiss. * Show a toast notification with optional auto-dismiss.
@@ -28,7 +16,7 @@ const showToast = ({ message, duration = 3000 }: ToastOptions): void => {
const container = toastContainer(); const container = toastContainer();
const template = document.getElementById("toast-template") as HTMLTemplateElement | null; const template = document.getElementById("toast-template") as HTMLTemplateElement | null;
if (!template) { if (!container || !template) {
return; return;
} }

View File

@@ -64,5 +64,6 @@
</main> </main>
{{end}} {{end}}
</div> </div>
<div id="toast-container" class="fixed bottom-4 right-4 z-100 flex flex-col gap-2" role="status" aria-live="polite" aria-relevant="additions"></div>
</body> </body>
</html> </html>