Skip to content

Commit

Permalink
[FE] FEAT: toggleType이 DEVICE 일때만 darkMode 상태가 업데이트 되도록 구현 #1649
Browse files Browse the repository at this point in the history
  • Loading branch information
gykoh42 committed Sep 25, 2024
1 parent 6cd7b0a commit 8a64ec8
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const getInitialDisplayStyle = (
};

const DisplayStyleCardContainer = () => {
var darkModeQuery = window.matchMedia("(prefers-color-scheme: dark)");
const darkModeQuery = window.matchMedia("(prefers-color-scheme: dark)");
const [toggleType, setToggleType] = useRecoilState(displayStyleState);
const initialDisplayStyle = getInitialDisplayStyle(
toggleType as DisplayStyleToggleType,
Expand All @@ -42,18 +42,22 @@ const DisplayStyleCardContainer = () => {
const handleDisplayStyleButtonClick = (displayStyleToggleType: string) => {
const castedToggleType = displayStyleToggleType as DisplayStyleToggleType;
if (toggleType === castedToggleType) return;
setToggleType(castedToggleType);
setColorsAndLocalStorage(castedToggleType);
};

useEffect(() => {
darkModeQuery.addEventListener("change", (event) => {
setDarkMode(
event.matches ? DisplayStyleType.DARK : DisplayStyleType.LIGHT
);
});
return () => darkModeQuery.removeEventListener("change", (event) => {});
}, []);
const handleSystemThemeChange = (event: MediaQueryListEvent) => {
if (toggleType === DisplayStyleToggleType.DEVICE) {
setDarkMode(
event.matches ? DisplayStyleType.DARK : DisplayStyleType.LIGHT
);
}
};
darkModeQuery.addEventListener("change", handleSystemThemeChange);
return () => {
darkModeQuery.removeEventListener("change", handleSystemThemeChange);
};
}, [toggleType]);

useEffect(() => {
document.body.setAttribute("display-style", darkMode);
Expand All @@ -69,7 +73,7 @@ const DisplayStyleCardContainer = () => {
darkModeQuery.matches ? DisplayStyleType.DARK : DisplayStyleType.LIGHT
);
}
}, [toggleType]);
}, [toggleType, darkModeQuery.matches]);

return (
<DisplayStyleCard
Expand Down

0 comments on commit 8a64ec8

Please sign in to comment.