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:
2026-11-05 15:44:42 +01:00
parent 55d2e2a913
commit 84eee4057e
3 changed files with 58 additions and 10 deletions
+11 -8
View File
@@ -43,14 +43,17 @@
--color-accent-content: #ffffff; --color-accent-content: #ffffff;
--color-neutral: #587068; --color-neutral: #587068;
--color-neutral-content: #f4f0e6; --color-neutral-content: #f4f0e6;
--color-info: oklch(62% 0.214 259.815); /* Toasts use the brand palette rather than the stock blue/red: info is the
--color-info-content: oklch(97% 0.014 254.604); deep ink green, error is the coral used by primary actions. Both pass AA
against their content colour. */
--color-info: #17352d;
--color-info-content: #fffcf4;
--color-success: oklch(70% 0.14 182.503); --color-success: oklch(70% 0.14 182.503);
--color-success-content: oklch(98% 0.014 180.72); --color-success-content: oklch(98% 0.014 180.72);
--color-warning: oklch(66% 0.179 58.318); --color-warning: oklch(66% 0.179 58.318);
--color-warning-content: oklch(98% 0.022 95.277); --color-warning-content: oklch(98% 0.022 95.277);
--color-error: oklch(58% 0.253 17.585); --color-error: #c74c33;
--color-error-content: oklch(96% 0.015 12.422); --color-error-content: #ffffff;
} }
html[data-theme="dark"] { html[data-theme="dark"] {
@@ -87,12 +90,12 @@ html[data-theme="dark"] {
--color-accent-content: #0f1714; --color-accent-content: #0f1714;
--color-neutral: #2b3a33; --color-neutral: #2b3a33;
--color-neutral-content: #e9efe9; --color-neutral-content: #e9efe9;
--color-info: oklch(70% 0.14 240); --color-info: #24403a;
--color-info-content: oklch(15% 0.02 240); --color-info-content: #e9efe9;
--color-success: oklch(72% 0.13 165); --color-success: oklch(72% 0.13 165);
--color-success-content: oklch(15% 0.02 165); --color-success-content: oklch(15% 0.02 165);
--color-warning: oklch(78% 0.15 75); --color-warning: oklch(78% 0.15 75);
--color-warning-content: oklch(20% 0.03 75); --color-warning-content: oklch(20% 0.03 75);
--color-error: oklch(66% 0.19 22); --color-error: #c74c33;
--color-error-content: oklch(15% 0.02 22); --color-error-content: #ffffff;
} }
+44
View File
@@ -41,3 +41,47 @@ document.addEventListener("keydown", (event) => {
setOpen(false); 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 });
@@ -78,12 +78,13 @@ defmodule ChesstrainerWeb.CoreComponents do
:if={msg = render_slot(@inner_block) || Phoenix.Flash.get(@flash, @kind)} :if={msg = render_slot(@inner_block) || Phoenix.Flash.get(@flash, @kind)}
id={@id} id={@id}
phx-click={JS.push("lv:clear-flash", value: %{key: @kind}) |> hide("##{@id}")} phx-click={JS.push("lv:clear-flash", value: %{key: @kind}) |> hide("##{@id}")}
data-flash-dismiss
role="alert" role="alert"
class="fixed right-4 top-4 z-50 flex flex-col gap-2" class="pointer-events-auto"
{@rest} {@rest}
> >
<div class={[ <div class={[
"flex w-80 max-w-80 items-start gap-3 rounded-lg border px-4 py-3 text-sm text-wrap shadow-lg sm:w-96 sm:max-w-96", "flex w-full items-start gap-3 rounded-lg border px-4 py-3 text-sm text-wrap shadow-lg",
@kind == :info && "border-info bg-info text-info-content", @kind == :info && "border-info bg-info text-info-content",
@kind == :error && "border-error bg-error text-error-content" @kind == :error && "border-error bg-error text-error-content"
]}> ]}>