8. GlobalStyle&Theme

GlobalStyle κ³Ό Theme

🎲 CSS Reset

> CSS Tools: Reset CSS

λͺ¨λ“  HTML νƒœκ·Έμ—λŠ” κΈ°λ³Έ μŠ€νƒ€μΌμ΄ μ μš©λ˜μ–΄μžˆλŠ”λ°, μ΄λŸ¬ν•œ 속성듀은 μŠ€νƒ€μΌμ„ μž‘μ„ λ•Œ 였히렀 λΆˆνŽΈν•˜κΈ° 떄문에 μŠ€νƒ€μΌμ„ μ΄ˆκΈ°ν™” μ‹œμΌœμ€€λ‹€.

λ‹€μ‹œ 말해, 이것은 μ‹œμž‘μ μ΄μ§€ μ†λŒˆ 수 μ—†λŠ” λ…λ¦½λœ λΈ”λž™λ°•μŠ€κ°€ μ•„λ‹™λ‹ˆλ‹€.

npm styled-reset

> styled-reset github

npm i styled-reset

곡식 κΉƒν—™μ—μ„œ 보면 Styled Components 3.x or 2.x 버전인 κ²½μš°μ—λŠ”, injectGlobal api λ₯Ό μ‚¬μš©ν•˜λΌκ³  ꢌμž₯ν•˜κ³ , styled-reset@1.7.1 λ₯Ό μ„€μΉ˜ν•˜λΌκ³  ꢌμž₯ν•œλ‹€.

ν˜„μ‹œμ (2024λ…„ 3μ›”) μ‚¬μš©ν•˜λŠ” Styled Components 의 버전은 6.x 이닀.

μ‚¬μš©

import { Reset } from 'styled-reset';

export default function App() {
  return (
    <div>
      {/* μ΅œμƒμœ„μ— μ„ΈνŒ…ν•΄μ€€λ‹€. */}
      <Reset />
    </div>
  );
}

Global Style 적용

λ¨Όμ € src/styles 폴더λ₯Ό 생성 ν›„ GlobalStyle.ts νŒŒμΌμ„ λ§Œλ“ λ‹€.

import { createGlobalStyle } from 'styled-components';

const GlobalStyle = createGlobalStyle`
  html {
    box-sizing: border-box;
  }

  *,
  *::before,
  *::after {
    box-sizing: inherit;
  }

  html {
    font-size: 62.5%; // 1rem = 10px
  }

  body {
    font-size: 1.6rem; // 1.6rem = 16px
  }

  :lang(ko) {
    h1, h2, h3 {
      word-break: keep-all;
    }
  }
`;

export default GlobalStyle;

App.tsx μ—μ„œ GlobalStyle 을 μ‚¬μš©ν•œλ‹€. (단, λ°˜λ“œμ‹œ App.tsx 일 ν•„μš”λŠ” μ—†μŒ)

import { Reset } from 'styled-reset';

import GlobalStyle from './styles/GlobalStyle';

export default function App() {
  return (
    <div>
      {/* μ΅œμƒμœ„μ— μ„ΈνŒ…ν•΄μ€€λ‹€. */}
      <Reset />
      <GlobalStyle />
    </div>
  );
}

Theme

Design System 에 λ”°λΌμ„œ Theme λ₯Ό μ§€μ •ν•΄μ„œ μ‚¬μš©ν•œλ‹€.

defaultTheme

src/styles/defaultTheme.ts νŒŒμΌμ„ λ§Œλ“ λ‹€.

const defaultTheme = {
  colors: {
    background: '#FFF',
    text: '#000',
    primary: '#F00',
    secondary: '#00F',
  },
};

export default defaultTheme;

src/styles/darkTheme.ts νŒŒμΌμ„ λ§Œλ“ λ‹€.

import Theme from './Theme';

const darkTheme: Theme = {
  colors: {
    background: '#000',
    text: '#FFF',
    primary: '#F00',
    secondary: '#00F',
  },
};

export default darkTheme;

같은 μœ„μΉ˜μ— Theme.ts νŒŒμΌμ„ μƒμ„±ν•˜λŠ”λ°, νƒ€μž…μ„ μ§€μ •ν•˜κΈ° μœ„ν•¨μ΄λ‹€.

import defaultTheme from './defaultTheme';

type Theme = typeof defaultTheme;

export default Theme;

같은 μœ„μΉ˜μ— styled.d.ts 파일 μƒμ„±ν•œλ‹€.

μ•žμ„œ μƒμ„±ν•œ GlobalStyle.ts μ—μ„œ νƒ€μž…μ„ μž‘μ§€ λͺ»ν•˜λŠ” 이슈λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•¨μ΄λ‹€.

import 'styled-components';

import Theme from './Theme';

declare module 'styled-components' {
  export interface DefaultTheme extends Theme {
    // colors: {
    //   background: string;
    //   text: string;
    //   primary: string;
    //   secondary: string;
    // };
  }
}

GlobalStyle.ts 에 Theme 속성 μ μš©ν•˜κΈ°

import { createGlobalStyle } from 'styled-components';

const GlobalStyle = createGlobalStyle`
  html {
    box-sizing: border-box;
  }

  *,
  *::before,
  *::after {
    box-sizing: inherit;
  }

  html {
    font-size: 62.5%; // 1rem = 10px
  }

  body {
    font-size: 1.6rem; // 1.6rem = 16px
    background: ${(props) => props.theme.colors.background};
    color: ${(props) => props.theme.colors.text};
  }

  :lang(ko) {
    h1, h2, h3 {
      word-break: keep-all;
    }
  }
`;

export default GlobalStyle;

Last updated