Skip to content

Commit

Permalink
[FE] FIX: 초기 깜빡임 문제 해결을 위한 getDisplayStyleFromLocalStorage 함수 도입 #1649
Browse files Browse the repository at this point in the history
  • Loading branch information
gykoh42 committed Dec 19, 2024
1 parent 9c57158 commit 35de4ca
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ import {
DisplayStyleType,
} from "@/Cabinet/types/enum/displayStyle.type.enum";

export const savedDisplayStyleToggle =
(localStorage.getItem("display-style-toggle") as DisplayStyleToggleType) ||
DisplayStyleToggleType.DEVICE;
export const getDisplayStyleFromLocalStorage: () => DisplayStyleToggleType =
() => {
return (
(localStorage.getItem(
"display-style-toggle"
) as DisplayStyleToggleType) || DisplayStyleToggleType.DEVICE
);
};

(function () {
const isClient = typeof window !== "undefined";
if (isClient) {
const darkModeQuery = window.matchMedia("(prefers-color-scheme: dark)");

const colorMode = getInitialDisplayStyle(
savedDisplayStyleToggle,
getDisplayStyleFromLocalStorage(),
darkModeQuery
);

Expand Down
12 changes: 6 additions & 6 deletions frontend/src/Cabinet/components/Common/DarkModeToggleSwitch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useRecoilState } from "recoil";
import styled from "styled-components";
import { displayStyleState } from "@/Cabinet/recoil/atoms";
import { getInitialDisplayStyle } from "@/Cabinet/components/Card/DisplayStyleCard/DisplayStyleCard.container";
import { savedDisplayStyleToggle } from "@/Cabinet/components/Card/DisplayStyleCard/displayStyleInitializer";
import { getDisplayStyleFromLocalStorage } from "@/Cabinet/components/Card/DisplayStyleCard/displayStyleInitializer";
import { ReactComponent as MoonIcon } from "@/Cabinet/assets/images/moonIcon.svg";
import { ReactComponent as SunIcon } from "@/Cabinet/assets/images/sunIcon.svg";
import {
Expand All @@ -17,17 +17,17 @@ const DarkModeToggleSwitch = ({ id }: { id: string }) => {
const darkModeQuery = window.matchMedia("(prefers-color-scheme: dark)");
const [displayStyleType, setDisplayStyleType] = useState<DisplayStyleType>(
() => {
return getInitialDisplayStyle(savedDisplayStyleToggle, darkModeQuery);
return getInitialDisplayStyle(
getDisplayStyleFromLocalStorage(),
darkModeQuery
);
}
);

const isDarkMode = displayStyleType === DisplayStyleType.DARK;
const toggleType =
(localStorage.getItem("display-style-toggle") as DisplayStyleToggleType) ||
DisplayStyleToggleType.DEVICE;

useEffect(() => {
setDisplayStyleToggle(toggleType);
setDisplayStyleToggle(getDisplayStyleFromLocalStorage());
}, []);

useEffect(() => {
Expand Down

0 comments on commit 35de4ca

Please sign in to comment.