Skip to content

Commit

Permalink
feat(theme): sync body background-color with theme-color meta tag (#9325
Browse files Browse the repository at this point in the history
)

Ensures that the status bar color on some mobile browsers adapts to the theme.

Co-authored-by: Claas Augner <[email protected]>
  • Loading branch information
zica87 and caugner authored Oct 30, 2023
1 parent 1f11e2c commit 5001b04
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
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

0 comments on commit 5001b04

Please sign in to comment.