diff --git a/assets/css/base/_theme.css b/assets/css/base/_theme.css index 0cc31dd..332554a 100644 --- a/assets/css/base/_theme.css +++ b/assets/css/base/_theme.css @@ -43,14 +43,17 @@ --color-accent-content: #ffffff; --color-neutral: #587068; --color-neutral-content: #f4f0e6; - --color-info: oklch(62% 0.214 259.815); - --color-info-content: oklch(97% 0.014 254.604); + /* Toasts use the brand palette rather than the stock blue/red: info is the + 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-content: oklch(98% 0.014 180.72); --color-warning: oklch(66% 0.179 58.318); --color-warning-content: oklch(98% 0.022 95.277); - --color-error: oklch(58% 0.253 17.585); - --color-error-content: oklch(96% 0.015 12.422); + --color-error: #c74c33; + --color-error-content: #ffffff; } html[data-theme="dark"] { @@ -87,12 +90,12 @@ html[data-theme="dark"] { --color-accent-content: #0f1714; --color-neutral: #2b3a33; --color-neutral-content: #e9efe9; - --color-info: oklch(70% 0.14 240); - --color-info-content: oklch(15% 0.02 240); + --color-info: #24403a; + --color-info-content: #e9efe9; --color-success: oklch(72% 0.13 165); --color-success-content: oklch(15% 0.02 165); --color-warning: oklch(78% 0.15 75); --color-warning-content: oklch(20% 0.03 75); - --color-error: oklch(66% 0.19 22); - --color-error-content: oklch(15% 0.02 22); + --color-error: #c74c33; + --color-error-content: #ffffff; } \ No newline at end of file diff --git a/assets/js/nav.js b/assets/js/nav.js index 962181c..810c94e 100644 --- a/assets/js/nav.js +++ b/assets/js/nav.js @@ -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 }); diff --git a/lib/chesstrainer_web/components/core_components.ex b/lib/chesstrainer_web/components/core_components.ex index 83d6563..9fa4e41 100644 --- a/lib/chesstrainer_web/components/core_components.ex +++ b/lib/chesstrainer_web/components/core_components.ex @@ -78,12 +78,13 @@ defmodule ChesstrainerWeb.CoreComponents do :if={msg = render_slot(@inner_block) || Phoenix.Flash.get(@flash, @kind)} id={@id} phx-click={JS.push("lv:clear-flash", value: %{key: @kind}) |> hide("##{@id}")} + data-flash-dismiss role="alert" - class="fixed right-4 top-4 z-50 flex flex-col gap-2" + class="pointer-events-auto" {@rest} >