mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
restyle flash toasts and auto-dismiss them
- use brand info/error colours that pass AA contrast in both modes - dismiss flashes on click on controller pages, where no LiveView handles it - auto-dismiss real flashes after a few seconds - keep connection notices pinned until the connection recovers
This commit is contained in:
@@ -41,3 +41,47 @@ document.addEventListener("keydown", (event) => {
|
||||
setOpen(false);
|
||||
}
|
||||
});
|
||||
|
||||
// Flash toasts also render on controller pages (the landing page, after logging
|
||||
// out, for example), where there is no LiveView to handle the component's
|
||||
// phx-click. Dismiss them with plain DOM there; LiveView pages keep clearing
|
||||
// the server-side flash through `lv:clear-flash`.
|
||||
document.addEventListener("click", (event) => {
|
||||
const target = event.target instanceof Element ? event.target : null;
|
||||
const flash = target?.closest("[data-flash-dismiss]");
|
||||
if (!flash) return;
|
||||
if (window.liveSocket?.isConnected?.()) return;
|
||||
|
||||
flash.remove();
|
||||
});
|
||||
|
||||
// Auto-dismiss real flash messages after a few seconds. Synthetic clicks reuse
|
||||
// the manual dismiss path, so LiveView pages also clear the server-side flash.
|
||||
// The connectivity notices (`client-error`/`server-error`) start hidden and are
|
||||
// left alone: they should stay until the connection recovers.
|
||||
const FLASH_TIMEOUT_MS = 5000;
|
||||
const scheduledFlashes = new WeakSet();
|
||||
|
||||
const scheduleFlashDismiss = (flash) => {
|
||||
if (scheduledFlashes.has(flash) || flash.hasAttribute("hidden")) return;
|
||||
|
||||
scheduledFlashes.add(flash);
|
||||
setTimeout(() => flash.click(), FLASH_TIMEOUT_MS);
|
||||
};
|
||||
|
||||
const scheduleFlashes = (root) => {
|
||||
if (root.matches?.("[data-flash-dismiss]")) scheduleFlashDismiss(root);
|
||||
root.querySelectorAll?.("[data-flash-dismiss]").forEach(scheduleFlashDismiss);
|
||||
};
|
||||
|
||||
document.querySelectorAll("[data-flash-dismiss]").forEach(scheduleFlashDismiss);
|
||||
|
||||
// Flashes rendered later (LiveView updates, live navigation) arrive after the
|
||||
// initial pass, so watch for them.
|
||||
new MutationObserver((mutations) => {
|
||||
for (const mutation of mutations) {
|
||||
for (const node of mutation.addedNodes) {
|
||||
if (node instanceof Element) scheduleFlashes(node);
|
||||
}
|
||||
}
|
||||
}).observe(document.body, { childList: true, subtree: true });
|
||||
|
||||
Reference in New Issue
Block a user