본문 바로가기
부트캠프교육중/react

[React] Dark모드, Light모드 적용하기

by 뭉지야 2023. 8. 27.
728x90

index.tsx 에 있던 

<ThemeProvider theme={theme}>
</ThemeProvider>

이것을 app.tsx로 옮긴다.

 

theme를 darkTheme와 lightTheme로 나눠서 만들어준다.

 

//theme.ts


import { DefaultTheme } from "styled-components";

export const darkTheme:DefaultTheme = {
    bgColor: "#1e272e",
    textColor: "black",
    accentColor: "#f53b57",
}

export const lightTheme:DefaultTheme = {
    bgColor: "whitesmoke",
    textColor: "black",
    accentColor: "#f53b57",
}

 

버튼을 만들어서 적용한다.

//app.tsx


import React, { useState } from 'react';
import styled, { createGlobalStyle } from "styled-components";
import { ThemeProvider } from 'styled-components';
import { darkTheme, lightTheme } from './theme';
import Router from './Router';
import { ReactQueryDevtools } from "react-query/devtools";


const GlobalStyle = createGlobalStyle`
@import url('https://fonts.googleapis.com/css2?family=Hind&display=swap');
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, menu, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
main, menu, nav, output, ruby, section, summary,
time, mark, audio, video {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, main, menu, nav, section {
  display: block;
}
/* HTML5 hidden-attribute fix for newer browsers */
*[hidden] {
    display: none;
}
body {
  line-height: 1;
}
menu, ol, ul {
  list-style: none;
}
blockquote, q {
  quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
  content: '';
  content: none;
}
table {
  border-collapse: collapse;
  border-spacing: 0;
}
*{
  box-sizing: border-box;
}
body {
  font-family: 'Hind', sans-serif;
  background-color: ${(props)=> props.theme.bgColor};
  color: ${(props)=> props.theme.textColor}
}
a {
  text-decoration: none;
  color: inherit;
}
`;


function App() {
  const [isDark, setIsDark] = useState(false);
  const toggleDark = () => setIsDark(current => !current);

  return (
    <>
     <ThemeProvider theme={isDark ? darkTheme : lightTheme}>
      <button onClick={toggleDark}>Toggle Mode</button>
    <GlobalStyle />
    <Router/>
    <ReactQueryDevtools initialIsOpen={true} />
    </ThemeProvider>
    </>
  );
}

export default App;
728x90