Add styled components - team-wedev/wedev GitHub Wiki
styled-components๋ wrapper component์ธ <ThemeProvider>
๋ฅผ export ํจ์ผ๋ก์จ full theming ์ ์ง์ํฉ๋๋ค. ์ด ์ปดํฌ๋ํธ ํ๋ก๋ฐ์ด๋๋ ์ปจํ
์คํธ API๋ฅผ ํตํด ์์ ์ ์๋์ ์กด์ฌํ๋ ๋ชจ๋ React ์ปดํฌ๋ํธ์ ํ
๋ง๋ฅผ ์ ๊ณตํฉ๋๋ค. ๋ ๋ ํธ๋ฆฌ์์ ์คํ์ผ์ด ์ง์ ๋ ๋ชจ๋ ์ปดํฌ๋ํธ๋ ๊น์ด๊ฐ ์ฌ๋ฌ ์์ค์ธ ๊ฒฝ์ฐ์๋ ์ ๊ณต๋ ํ
๋ง์ ์ก์ธ์ค ํ ์ ์์ต๋๋ค.
Note:
<ThemeProvider>
returns its children when rendering, so it must only wrap a single child node as it may be used as the root of therender()
method.
theme prop์ผ๋ก ํจ์ ๋ํ ๋๊ฒจ์ค ์ ์์ต๋๋ค. ์ด ํจ์๋ ๋ถ๋ชจ ํ
๋ง๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๋ฐ๊ฒ๋ฉ๋๋ค. ์ด ๋ ๋ถ๋ชจ ํ
๋ง๋ ํธ๋ฆฌ ์์์ ์๋ <ThemeProvider>
๋ฅผ ์๋ฏธํฉ๋๋ค.
// Define our button, but with the use of props.theme this time
const Button = styled.button`
color: ${props => props.theme.fg};
border: 2px solid ${props => props.theme.fg};
background: ${props => props.theme.bg};
font-size: 1em;
margin: 1em;
padding: 0.25em 1em;
border-radius: 3px;
`;
// Define our `fg` and `bg` on the theme
const theme = {
fg: "palevioletred",
bg: "white"
};
// This theme swaps `fg` and `bg`
const invertTheme = ({ fg, bg }) => ({
fg: bg,
bg: fg
});
render(
<ThemeProvider theme={theme}>
<div>
<Button>Default Theme</Button>
<ThemeProvider theme={invertTheme}>
<Button>Inverted Theme</Button>
</ThemeProvider>
</div>
</ThemeProvider>
);
import { renderToString } from "react-dom/server";
import { ServerStyleSheet } from "styled-components";
const sheet = new ServerStyleSheet();
try {
const html = renderToString(sheet.collectStyles(<YourApp />));
const styleTags = sheet.getStyleTags(); // or sheet.getStyleElement();
} catch (error) {
// handle error
console.error(error);
} finally {
sheet.seal();
}
collectStyles
๋ ์ฌ์ฉ์์ element๋ฅผ provider ๋ด๋ถ๋ก ๊ฐ์๋๋ค.
import { renderToString } from "react-dom/server";
import { ServerStyleSheet, StyleSheetManager } from "styled-components";
const sheet = new ServerStyleSheet();
try {
const html = renderToString(
<StyleSheetManager sheet={sheet.instance}>
<YourApp />
</StyleSheetManager>
);
const styleTags = sheet.getStyleTags(); // or sheet.getStyleElement();
} catch (error) {
// handle error
console.error(error);
} finally {
sheet.seal();
}
- commit
- _app.js
import App from "next/app";
import React from "react";
import { ThemeProvider } from "styled-components";
const theme = {
colors: {
primary: "red"
}
};
export default class MyApp extends App {
render() {
const { Component, pageProps } = this.props;
return (
<ThemeProvider theme={theme}>
<Component {...pageProps} />
</ThemeProvider>
);
}
}
- _document.js
import Document, { Html, Head, Main, NextScript } from "next/document";
import { ServerStyleSheet as StyledComponentsSheet } from "styled-components";
import { ServerStyleSheets as MaterialSheet } from "@material-ui/styles";
import CssBaseline from "@material-ui/core/CssBaseline";
export default class MyDocument extends Document {
static async getInitialProps(ctx) {
const styledComponentsSheet = new StyledComponentsSheet();
const materialSheet = new MaterialSheet();
const originalRenderPage = ctx.renderPage;
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: App => props =>
materialSheet.collect(
styledComponentsSheet.collectStyles(
<>
<CssBaseline />
<App {...props} />
</>
)
)
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{materialSheet.getStyleElement()}
{styledComponentsSheet.getStyleElement()}
</>
)
};
} finally {
styledComponentsSheet.seal();
}
}
}