diff --git a/src/App.tsx b/src/App.tsx index 6a9e6c4..c71a8aa 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -7,20 +7,41 @@ import Board from './pages/Board'; import Post from './pages/Post'; import MyPage from './pages/MyPage'; import Sidebar from './components/layout/sideBar'; -import { AppContainer, ContentWrapper } from './components/layout/style'; +import { AppContainer, ContentWrapper } from './components/layout/style'; +import { useEffect, useState } from 'react'; function App() { + const [theme, setTheme] = useState(() => { + return localStorage.getItem('theme') || 'dark'; // 기본값은 'dark' + }); + + useEffect(() => { + document.body.className = theme; // body의 className 변경하여 테마 적용 + }, [theme]); + + const handleThemeChange = (newTheme: string) => { + setTheme(newTheme); + if (newTheme === 'light') { + localStorage.setItem('theme', 'light'); // light 모드 저장 + } else { + localStorage.removeItem('theme'); // dark 모드는 저장 X (기본값) + } + }; + return ( - + } /> } /> } /> } /> - } /> + } + /> diff --git a/src/components/myPage/ThemeSelection/index.tsx b/src/components/myPage/ThemeSelection/index.tsx index 28c11c5..8bfae08 100644 --- a/src/components/myPage/ThemeSelection/index.tsx +++ b/src/components/myPage/ThemeSelection/index.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import React from 'react'; import { RadioOption, ThemeSelectionForm, @@ -6,35 +6,21 @@ import { UncheckedCircle, } from './style'; import { BsFillCheckCircleFill } from 'react-icons/bs'; +import { ThemeSelectionProps } from '../../../models/MyPage'; -export const ThemeSelection: React.FC = () => { - const [selectedTheme, setSelectedTheme] = useState(() => { - return localStorage.getItem('theme') || 'dark'; - }); - - const handleThemeChange = (theme: string) => { - setSelectedTheme(theme); - - if (theme === 'light') { - localStorage.setItem('theme', theme); - } else { - localStorage.removeItem('theme'); - } - }; - - useEffect(() => { - document.body.className = selectedTheme; - }, [selectedTheme]); - +export const ThemeSelection: React.FC = ({ + theme, + setTheme, +}) => { return (

테마 선택

handleThemeChange('dark')} + isChecked={theme === 'dark'} + onClick={() => setTheme('dark')} > - {selectedTheme === 'dark' ? ( + {theme === 'dark' ? ( ) : ( @@ -42,10 +28,10 @@ export const ThemeSelection: React.FC = () => { handleThemeChange('light')} + isChecked={theme === 'light'} + onClick={() => setTheme('light')} > - {selectedTheme === 'light' ? ( + {theme === 'light' ? ( ) : ( diff --git a/src/models/MyPage.ts b/src/models/MyPage.ts index 95a4c87..d88164b 100644 --- a/src/models/MyPage.ts +++ b/src/models/MyPage.ts @@ -1,3 +1,8 @@ +export interface MyPageProps { + theme: string; + setTheme: (theme: string) => void; +} + export interface ProfileImageProps { profileImage: string; patchUserProfileImage: (file: File) => void; @@ -13,3 +18,8 @@ export interface ProfileDetailsProps { handleChange: () => void; detail: string; } + +export interface ThemeSelectionProps { + theme: string; + setTheme: (theme: string) => void; +} diff --git a/src/pages/MyPage/index.tsx b/src/pages/MyPage/index.tsx index fdfea6f..d210fe7 100644 --- a/src/pages/MyPage/index.tsx +++ b/src/pages/MyPage/index.tsx @@ -12,8 +12,9 @@ import { useUpdateProfile } from '../../hooks/Auth/useUpdateProfile'; import { ProfileImg } from '../../components/myPage/ProfileImage'; import { ProfileDetails } from '../../components/myPage/ProfileDetails'; import { ThemeSelection } from '../../components/myPage/ThemeSelection'; +import { MyPageProps } from '../../models/MyPage'; -const MyPage: React.FC = () => { +const MyPage: React.FC = ({ theme, setTheme }) => { const { data, isLoading, error, refetch } = useUserProfile(); const { patchUserNickName, patchUserLoginId, patchUserProfileImage } = useUpdateProfile(); @@ -96,7 +97,7 @@ const MyPage: React.FC = () => { - + );