Skip to content
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

feat: auto update window color #4858

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/components/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,20 @@ export function useSwitchTheme() {
document.body.classList.remove("light");
document.body.classList.remove("dark");

let meta = document.querySelector(`meta[name="theme-color"]`);

if (!meta) {
meta = document.createElement("meta");
meta.setAttribute("name", "theme-color");
document.head.appendChild(meta);
}
justypist marked this conversation as resolved.
Show resolved Hide resolved

if (config.theme === "dark") {
document.body.classList.add("dark");
meta.setAttribute("content", "#000000");
} else if (config.theme === "light") {
document.body.classList.add("light");
meta.setAttribute("content", "#FFFFFF");
}

const metaDescriptionDark = document.querySelector(
Expand All @@ -79,6 +89,12 @@ export function useSwitchTheme() {
if (config.theme === "auto") {
metaDescriptionDark?.setAttribute("content", "#151515");
metaDescriptionLight?.setAttribute("content", "#fafafa");

const query = window.matchMedia("(prefers-color-scheme: dark)");
meta.setAttribute("content", query.matches ? "#000000" : "#FFFFFF");
query.addEventListener("change", () => {
meta?.setAttribute("content", query.matches ? "#000000" : "#FFFFFF");
});
justypist marked this conversation as resolved.
Show resolved Hide resolved
} else {
const themeColor = getCSSVar("--theme-color");
metaDescriptionDark?.setAttribute("content", themeColor);
Expand Down
Loading