mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
Dark mode: - Theme toggle in the header (System → Light → Dark); "System" follows the OS setting and its changes. The choice is kept per browser and applied before first paint, so there's no light flash. - Implemented by remapping the palette under :root[data-theme=dark]: grays flip, color tints become translucent, dark colored text becomes its light counterpart. Page, surface, header and accent colors are semantic tokens. Native controls follow via color-scheme. - Chart text and grid colors follow the theme. - Public share pages get the toggle too. - Contrast checked against WCAG 4.5:1 (primary button text and error text adjusted for dark). Tables: - Shared table style no longer forces header text left, so right-aligned numeric headers line up with their columns everywhere. - Flight log: units in headers, muted dashes for empty cells, GPS marker beside the route instead of its own column. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
51 lines
1.7 KiB
JavaScript
51 lines
1.7 KiB
JavaScript
import { Controller } from "@hotwired/stimulus"
|
|
|
|
const CHOICES = ["system", "light", "dark"]
|
|
const LABELS = { system: "System", light: "Light", dark: "Dark" }
|
|
|
|
// Light / dark / follow-the-OS theme toggle. The choice is kept in
|
|
// localStorage; the layout's inline script applies it before first paint.
|
|
export default class extends Controller {
|
|
static targets = ["button"]
|
|
|
|
connect() {
|
|
this.media = window.matchMedia("(prefers-color-scheme: dark)")
|
|
this.onSystemChange = () => { if (this.choice === "system") this.apply() }
|
|
this.media.addEventListener("change", this.onSystemChange)
|
|
this.apply()
|
|
}
|
|
|
|
disconnect() {
|
|
this.media.removeEventListener("change", this.onSystemChange)
|
|
}
|
|
|
|
cycle() {
|
|
this.choice = CHOICES[(CHOICES.indexOf(this.choice) + 1) % CHOICES.length]
|
|
this.apply()
|
|
}
|
|
|
|
get choice() {
|
|
try { return localStorage.getItem("theme") || "system" } catch { return "system" }
|
|
}
|
|
|
|
set choice(value) {
|
|
try { localStorage.setItem("theme", value) } catch {}
|
|
}
|
|
|
|
apply() {
|
|
const choice = this.choice
|
|
const dark = choice === "dark" || (choice === "system" && this.media.matches)
|
|
const theme = dark ? "dark" : "light"
|
|
const changed = document.documentElement.dataset.theme !== theme
|
|
document.documentElement.dataset.theme = theme
|
|
|
|
this.element.querySelectorAll("[data-theme-icon]").forEach((icon) => {
|
|
icon.classList.toggle("hidden", icon.dataset.themeIcon !== choice)
|
|
})
|
|
const label = `Theme: ${LABELS[choice]}${choice === "system" ? ` (${theme})` : ""}`
|
|
this.element.title = label
|
|
this.element.setAttribute("aria-label", label)
|
|
if (changed) window.dispatchEvent(new CustomEvent("theme:change", { detail: { theme } }))
|
|
}
|
|
}
|