-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1716 from innovationacademy-kr/dev
DEV TO MAIN
- Loading branch information
Showing
15 changed files
with
331 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule config
updated
from 36f81f to af2375
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 31 additions & 62 deletions
93
frontend/src/Cabinet/components/Card/DisplayStyleCard/DisplayStyleCard.container.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,55 @@ | ||
import { useEffect, useState } from "react"; | ||
import { useEffect } from "react"; | ||
import { useRecoilValue } from "recoil"; | ||
import { displayStyleState } from "@/Cabinet/recoil/atoms"; | ||
import DisplayStyleCard from "@/Cabinet/components/Card/DisplayStyleCard/DisplayStyleCard"; | ||
import { | ||
DisplayStyleToggleType, | ||
DisplayStyleType, | ||
} from "@/Cabinet/types/enum/displayStyle.type.enum"; | ||
import useDisplayStyleToggle from "@/Cabinet/hooks/useDisplayStyleToggle"; | ||
import { | ||
isDeviceDarkMode, | ||
updateBodyDisplayStyle, | ||
} from "@/Cabinet/utils/displayStyleUtils"; | ||
|
||
// 로컬스토리지의 display-style-toggle 값에 따라 DisplayStyleType 반환 | ||
export const getInitialDisplayStyle = ( | ||
savedDisplayStyleToggle: DisplayStyleToggleType, | ||
displayStyleToggle: DisplayStyleToggleType, | ||
darkModeQuery: MediaQueryList | ||
) => { | ||
): DisplayStyleType => { | ||
// 라이트 / 다크 버튼 | ||
if (savedDisplayStyleToggle === DisplayStyleToggleType.LIGHT) | ||
if (displayStyleToggle === DisplayStyleToggleType.LIGHT) { | ||
return DisplayStyleType.LIGHT; | ||
else if (savedDisplayStyleToggle === DisplayStyleToggleType.DARK) | ||
return DisplayStyleType.DARK; | ||
// 디바이스 버튼 | ||
if (darkModeQuery.matches) { | ||
} else if (displayStyleToggle === DisplayStyleToggleType.DARK) { | ||
return DisplayStyleType.DARK; | ||
// 디바이스 버튼 | ||
} else { | ||
return darkModeQuery.matches | ||
? DisplayStyleType.DARK | ||
: DisplayStyleType.LIGHT; | ||
} | ||
return DisplayStyleType.LIGHT; | ||
}; | ||
|
||
const DisplayStyleCardContainer = () => { | ||
const savedDisplayStyleToggle = | ||
(localStorage.getItem("display-style-toggle") as DisplayStyleToggleType) || | ||
DisplayStyleToggleType.DEVICE; | ||
var darkModeQuery = window.matchMedia("(prefers-color-scheme: dark)"); | ||
const initialDisplayStyle = getInitialDisplayStyle( | ||
savedDisplayStyleToggle, | ||
darkModeQuery | ||
); | ||
const [darkMode, setDarkMode] = useState<DisplayStyleType>( | ||
initialDisplayStyle as DisplayStyleType | ||
); | ||
const [toggleType, setToggleType] = useState<DisplayStyleToggleType>( | ||
savedDisplayStyleToggle | ||
); | ||
|
||
const setColorsAndLocalStorage = (toggleType: DisplayStyleToggleType) => { | ||
setToggleType(toggleType); | ||
localStorage.setItem("display-style-toggle", toggleType); | ||
}; | ||
|
||
const handleDisplayStyleButtonClick = (displayStyleToggleType: string) => { | ||
if (toggleType === displayStyleToggleType) return; | ||
setToggleType( | ||
displayStyleToggleType as React.SetStateAction<DisplayStyleToggleType> | ||
); | ||
setColorsAndLocalStorage(displayStyleToggleType as DisplayStyleToggleType); | ||
}; | ||
const darkModeQuery = isDeviceDarkMode(); | ||
const toggleType = useRecoilValue(displayStyleState); | ||
const { addDarkModeListener } = useDisplayStyleToggle(); | ||
|
||
useEffect(() => { | ||
darkModeQuery.addEventListener("change", (event) => | ||
setDarkMode( | ||
event.matches ? DisplayStyleType.DARK : DisplayStyleType.LIGHT | ||
) | ||
const applyDisplayStyle = () => { | ||
const newDarkMode = getInitialDisplayStyle(toggleType, darkModeQuery); | ||
updateBodyDisplayStyle(newDarkMode); | ||
}; | ||
|
||
applyDisplayStyle(); | ||
const removeListener = addDarkModeListener( | ||
darkModeQuery, | ||
applyDisplayStyle | ||
); | ||
}, []); | ||
|
||
useEffect(() => { | ||
document.body.setAttribute("display-style", darkMode); | ||
}, [darkMode]); | ||
|
||
useEffect(() => { | ||
if (toggleType === DisplayStyleToggleType.LIGHT) { | ||
setDarkMode(DisplayStyleType.LIGHT); | ||
} else if (toggleType === DisplayStyleToggleType.DARK) { | ||
setDarkMode(DisplayStyleType.DARK); | ||
} else { | ||
setDarkMode( | ||
darkModeQuery.matches ? DisplayStyleType.DARK : DisplayStyleType.LIGHT | ||
); | ||
} | ||
return removeListener; | ||
}, [toggleType]); | ||
|
||
return ( | ||
<DisplayStyleCard | ||
displayStyleToggle={toggleType} | ||
handleDisplayStyleButtonClick={handleDisplayStyleButtonClick} | ||
/> | ||
); | ||
return <DisplayStyleCard />; | ||
}; | ||
|
||
export default DisplayStyleCardContainer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.