Skip to content

Commit

Permalink
feat: change theme when user changes settings
Browse files Browse the repository at this point in the history
  • Loading branch information
jonyw4 committed Nov 30, 2021
1 parent 2475e67 commit f8b3ab2
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion webapp/src/components/organisms/Layout/Layout.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,30 @@ import { useTheme } from '../../global';
import React from 'react';

export function Layout({ children }: LayoutProps) {
const { theme } = useTheme();
const { theme, changeTheme } = useTheme();

React.useEffect(() => {
const themeClassName = `${theme}-mode`;
document.querySelector('body').className = themeClassName;
}, [theme])

const changeThemeByColorScheme = (event: MediaQueryListEvent) => {
const theme = event.matches ? "dark" : "light";
changeTheme(theme);
};

React.useEffect(() => {
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", changeThemeByColorScheme);

return () => {
window
.matchMedia("(prefers-color-scheme: dark)")
.removeEventListener("keydown", changeThemeByColorScheme);
};
});

return (
<>
<Header />
Expand Down

1 comment on commit f8b3ab2

@vercel
Copy link

@vercel vercel bot commented on f8b3ab2 Nov 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.