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(theme): sync body background-color with theme-color meta tag #9325

Merged
merged 4 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 0 additions & 3 deletions client/src/ui-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ export function UIProvider(props: any) {
}
dark.addEventListener("change", setDark);
const light = window.matchMedia("(prefers-color-scheme: light)");
if (dark.matches) {
setColorScheme("dark");
}
light.addEventListener("change", setLight);
return () => {
light.removeEventListener("change", setLight);
Expand Down
6 changes: 2 additions & 4 deletions client/src/ui/molecules/theme-switcher/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ export const ThemeSwitcher = () => {
console.warn("Unable to read theme from localStorage", e);
}

if (theme) {
switchTheme(theme, setActiveTheme);
}
}, [activeTheme]);
switchTheme(theme ?? "os-default", setActiveTheme);
}, []);

return (
<DropdownMenuWrapper
Expand Down
11 changes: 11 additions & 0 deletions client/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ export function switchTheme(theme: Theme, set: (theme: Theme) => void) {
if (window && html) {
html.className = theme;
html.style.backgroundColor = "";

setTimeout(() => {
const meta = document.querySelector<HTMLMetaElement>(
'meta[name="theme-color"]'
);
const color = getComputedStyle(document.body).backgroundColor;
if (meta && color) {
meta.content = color;
}
}, 1);

try {
window.localStorage.setItem("theme", theme);
} catch (err) {
Expand Down