-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5616 from WoltLab/user-color-scheme
Overhaul the color scheme selection
- Loading branch information
Showing
20 changed files
with
129 additions
and
257 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -1,105 +1,18 @@ | ||
/** | ||
* Offer users the ability to enforce a specific color scheme. | ||
* Dynamically updates the color scheme to match the system preference. | ||
* | ||
* @author Alexander Ebert | ||
* @copyright 2001-2023 WoltLab GmbH | ||
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | ||
* @since 6.0 | ||
*/ | ||
|
||
import { getPhrase } from "WoltLabSuite/Core/Language"; | ||
import { attach, create } from "../../Ui/Dropdown/Builder"; | ||
import { registerCallback } from "../../Ui/Dropdown/Simple"; | ||
|
||
export type ColorScheme = "dark" | "light" | "system"; | ||
|
||
let currentScheme: ColorScheme = "system"; | ||
let mediaQuery: MediaQueryList; | ||
let themeColor: HTMLMetaElement; | ||
|
||
function setScheme(scheme: ColorScheme): void { | ||
currentScheme = scheme; | ||
|
||
if (currentScheme === "light" || currentScheme === "dark") { | ||
document.documentElement.dataset.colorScheme = currentScheme; | ||
updateThemeColor(); | ||
} else { | ||
applySystemScheme(); | ||
} | ||
|
||
try { | ||
localStorage.setItem("wsc_colorScheme", currentScheme); | ||
} catch { | ||
/* Ignore any errors when accessing the `localStorage`. */ | ||
} | ||
} | ||
|
||
function applySystemScheme(): void { | ||
if (currentScheme === "system") { | ||
document.documentElement.dataset.colorScheme = mediaQuery.matches ? "dark" : "light"; | ||
updateThemeColor(); | ||
} | ||
} | ||
|
||
function updateThemeColor(): void { | ||
themeColor.content = window.getComputedStyle(document.body).getPropertyValue("--wcfPageThemeColor"); | ||
} | ||
|
||
function initializeButton(button: HTMLElement): void { | ||
const dropdownMenu = create([ | ||
{ | ||
identifier: "light", | ||
label: getPhrase("wcf.style.setColorScheme.light"), | ||
callback() { | ||
setScheme("light"); | ||
}, | ||
}, | ||
{ | ||
identifier: "dark", | ||
label: getPhrase("wcf.style.setColorScheme.dark"), | ||
callback() { | ||
setScheme("dark"); | ||
}, | ||
}, | ||
"divider", | ||
{ | ||
identifier: "system", | ||
label: getPhrase("wcf.style.setColorScheme.system"), | ||
callback() { | ||
setScheme("system"); | ||
}, | ||
}, | ||
]); | ||
|
||
attach(dropdownMenu, button); | ||
|
||
registerCallback(button.id, (_containerId, action) => { | ||
if (action === "open") { | ||
dropdownMenu.querySelectorAll(".active").forEach((element) => element.classList.remove("active")); | ||
dropdownMenu.querySelector(`[data-identifier="${currentScheme}"]`)!.classList.add("active"); | ||
} | ||
}); | ||
} | ||
|
||
export function setup(): void { | ||
const button = document.querySelector<HTMLElement>(".jsButtonStyleColorScheme"); | ||
if (button) { | ||
initializeButton(button); | ||
} | ||
|
||
try { | ||
const value = localStorage.getItem("wsc_colorScheme"); | ||
if (value === "light" || value === "dark") { | ||
currentScheme = value; | ||
} | ||
} catch { | ||
/* Ignore any errors when accessing the `localStorage`. */ | ||
} | ||
|
||
themeColor = document.querySelector('meta[name="theme-color"]')!; | ||
const themeColor = document.querySelector<HTMLMetaElement>('meta[name="theme-color"]')!; | ||
themeColor.content = window.getComputedStyle(document.body).getPropertyValue("--wcfPageThemeColor"); | ||
|
||
mediaQuery = matchMedia("(prefers-color-scheme: dark)"); | ||
mediaQuery.addEventListener("change", () => { | ||
applySystemScheme(); | ||
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", (event) => { | ||
document.documentElement.dataset.colorScheme = event.matches ? "dark" : "light"; | ||
themeColor.content = window.getComputedStyle(document.body).getPropertyValue("--wcfPageThemeColor"); | ||
}); | ||
} |
This file contains 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
wcfsetup/install/files/js/WoltLabSuite/Core/BootstrapFrontend.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.