-
Notifications
You must be signed in to change notification settings - Fork 29
Automic dark mode #346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
grische
wants to merge
5
commits into
freifunk:main
Choose a base branch
from
freifunkMUC:auto-dark-mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Automic dark mode #346
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ae80213
locale: add i18n keys for theme toggle button
grische 5497cbc
lib: add theme module for light/dark/auto toggle
grische 2acf9d8
map: decouple theme from baselayerchange
grische ab46b48
gui: add theme toggle button
grische 29d12cf
index: inline pre-paint script to prevent theme flash
grische File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| export type Theme = "light" | "dark" | "auto"; | ||
|
|
||
| // Keep STORAGE_KEY and DARK_CLASS in sync with the pre-paint inline script in index.html. | ||
| const STORAGE_KEY = "meshviewer.theme"; | ||
| const DARK_CLASS = "theme_night"; | ||
| const THEMES: Theme[] = ["light", "dark", "auto"]; | ||
|
|
||
| let inMemory: Theme = "auto"; | ||
|
|
||
| const darkQuery = | ||
| typeof window !== "undefined" && window.matchMedia ? window.matchMedia("(prefers-color-scheme: dark)") : null; | ||
|
|
||
| function isTheme(value: string | null): value is Theme { | ||
| return value === "light" || value === "dark" || value === "auto"; | ||
| } | ||
|
|
||
| function read(): Theme { | ||
| try { | ||
| let v = localStorage.getItem(STORAGE_KEY); | ||
| if (isTheme(v)) { | ||
| return v; | ||
| } | ||
| } catch (e) { | ||
| /* localStorage inaccessible (private mode, sandboxed iframe, file://) */ | ||
| } | ||
| return inMemory; | ||
| } | ||
|
|
||
| function write(value: Theme): void { | ||
| inMemory = value; | ||
| try { | ||
| localStorage.setItem(STORAGE_KEY, value); | ||
| } catch (e) { | ||
| /* keep in-memory only */ | ||
| } | ||
| } | ||
|
|
||
| export function resolveTheme(): "light" | "dark" { | ||
| let current = read(); | ||
| if (current === "dark") { | ||
| return "dark"; | ||
| } | ||
| if (current === "auto" && darkQuery && darkQuery.matches) { | ||
| return "dark"; | ||
| } | ||
| return "light"; | ||
| } | ||
|
|
||
| function apply(): void { | ||
| let dark = resolveTheme() === "dark"; | ||
| document.documentElement.classList.toggle(DARK_CLASS, dark); | ||
| document.documentElement.dispatchEvent(new CustomEvent("themechange")); | ||
| } | ||
|
|
||
| export function getTheme(): Theme { | ||
| return read(); | ||
| } | ||
|
|
||
| export function cycleTheme(): Theme { | ||
| let next = THEMES[(THEMES.indexOf(read()) + 1) % THEMES.length]; | ||
| write(next); | ||
| apply(); | ||
| return next; | ||
| } | ||
|
|
||
| export function initTheme(): void { | ||
| inMemory = read(); | ||
| apply(); | ||
| if (darkQuery) { | ||
| darkQuery.addEventListener("change", function () { | ||
| if (read() === "auto") { | ||
| apply(); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| const SVG_OPEN = '<svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">'; | ||
| const SVG_CLOSE = "</svg>"; | ||
|
|
||
| const SUN_RAYS = | ||
| '<rect x="11" y="1" width="2" height="3" rx="1"/>' + | ||
| '<rect x="11" y="20" width="2" height="3" rx="1"/>' + | ||
| '<rect x="1" y="11" width="3" height="2" rx="1"/>' + | ||
| '<rect x="20" y="11" width="3" height="2" rx="1"/>'; | ||
|
|
||
| const SUN = | ||
| SVG_OPEN + | ||
| '<circle cx="12" cy="12" r="5"/>' + | ||
| "<g>" + | ||
| SUN_RAYS + | ||
| "</g>" + | ||
| '<g transform="rotate(45 12 12)">' + | ||
| SUN_RAYS + | ||
| "</g>" + | ||
| SVG_CLOSE; | ||
|
|
||
| const MOON = SVG_OPEN + '<path d="M21 12.79A9 9 0 1111.21 3 7 7 0 0021 12.79z"/>' + SVG_CLOSE; | ||
|
|
||
| const AUTO = | ||
| SVG_OPEN + | ||
| '<path d="M12 4a8 8 0 000 16V4z"/>' + | ||
| '<circle cx="12" cy="12" r="8" fill="none" stroke="currentColor" stroke-width="1.5"/>' + | ||
| SVG_CLOSE; | ||
|
|
||
| export function themeIconSVG(theme: Theme): string { | ||
| if (theme === "dark") { | ||
| return MOON; | ||
| } | ||
| if (theme === "auto") { | ||
| return AUTO; | ||
| } | ||
| return SUN; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.