npx create-next-app@latest --typescript ํ๋ก์ ํธ๋ช
global.css
: _app.tsx
์์๋ง ์ฌ์ฉ ๊ฐ๋ฅ์ปดํฌ๋ํธ๋ช
.module.css
: ๋ค๋ฅธ ์ปดํฌ๋ํธ์์ import ํด์ ์ฌ์ฉ (Home.module.css
๋ฑ)๐๐ฝ styled-components์ ์ฌ์ฉํ๋ฉด ์ ์ญ ์คํ์ผ์
global-style.ts
์ฌ์ฉํ๋ฏ๋กglobal.css
๋ ์ดํ ์ญ์
index.tsx
_app.tsx
_app.tsx
์ดํ _document.tsx
๊ฐ ์คํ๋จ_document.tsx
yarn add styled-components @types/styled-components
yarn add -dev babel-plugin-styled-components
yarn add styled-reset
.barbelrc
ํ์ผ{ "presets" : ["next/babel"], "plugins": [ [ "styled-components", { "ssr": true, "displayName": true, "preprocess": false } ] ] }
_document.tsx
ํ์ผimport Document, { Html, Head, Main, NextScript, DocumentContext, } from 'next/document'; import { ServerStyleSheet } from 'styled-components'; // 0) export default class MyDocument extends Document { static async getInitialProps(ctx: DocumentContext) { const sheet = new ServerStyleSheet(); // 1) const originalRenderPage = ctx.renderPage; try { ctx.renderPage = () => originalRenderPage({ enhanceApp: (App) => (props) => sheet.collectStyles(<App {...props} />), //2) }); const initialProps = await Document.getInitialProps(ctx); return { ...initialProps, styles: [initialProps.styles, sheet.getStyleElement()], // 2) }; } finally { sheet.seal(); } } render() { return ( <Html lang="en"> <Head /> <body> <Main /> <NextScript /> </body> </Html> ); } }
1)
ServerStyleSheet
๋ฅผ ์ด์ฉํ์ฌsheets
๋ผ๋ ์ธ์คํด์ค๋ฅผ ์์ฑ.
2)sheets
๋ฅผ ์ด์ฉํ์ฌ ์ง์ ํ ์ปดํฌ๋ํธ(์ฌ๊ธฐ์๋<App />
)์ ์คํ์ผ ์์๋ฅผ ๊ฒ์ํ๊ณ ๊ทธ ์คํ์ผ์<style>
ํ๊ทธ๋ก ์ถ์ถ.
3) ์ถ์ถํ ๊ฒฐ๊ณผ๋ฌผ์ Document์ ์ ๋ฌ.
๐๐ฝ ์ด์ ์๋ฒ์์ ๋ ๋๋ง๋๊ณ ์์ค ํ์ด์ง์์๋ ์คํ์ผ์ด ํ์๋๋ค
styles
ํด๋ : global-styles.ts
์ theme.ts
โโโ styles โ โโโ global-styles.ts โ โโโ theme.ts
global-styles.ts
import reset from 'styled-reset'; // ๋ ์ด์์ ๋ฆฌ์ import { createGlobalStyle } from "styled-components"; export const GlobalStyle = createGlobalStyle` html, body { padding: 0; margin: 0; letter-spacing: -1px; font-size: 15px; font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif; } .txt-c { text-align: center; } .txt-r { text-align: right; } .txt-l { text-align: left; } p { margin: 0; } `;
theme.ts
_app.tsx
: ์คํ์ผ ์ ์ฉํ๊ธฐglobal-styles.ts
์ theme.ts
) ์ ์ฉํ๊ธฐimport type { AppProps } from 'next/app'; import { ThemeProvider } from 'styled-components'; // 1) import GlobalStyle from '../styles/global-style'; // 2) import { theme } from '../styles/theme'; // 1) export default function App({ Component, pageProps }: AppProps) { return ( <> <ThemeProvider theme={theme}> // 1) <GlobalStyle /> // 2) <Component {...pageProps} /> </ThemeProvider> </> ); }
1) ThemeProvider๋ฅผ ์ฃผ์ ํ๊ณ theme๋ฅผ ์ฌ์ฉํ๋๋ก ์ธํ
2) GlobalStyle ์ ์ฉ
๋ ํผ๋ฐ์ค