typescript, material-ui

min0729·2021년 7월 11일
1
post-thumbnail

material-ui 설치

node i @material-ui/core @material-ui/icons

dev모드 실행 속도 향상

Minimizing-bundle-size
이 단계를 건너뛰면 개발 번들에 전체 라이브러리가 포함되어 프로젝트 실행 시간이 무척 느려짐

npm i -D babel-plugin-import

.babelrc

{
    "presets": ["next/babel"],
    "plugins": [
        [
          "babel-plugin-import",
          {
            "libraryName": "@material-ui/core",
            // Use ""libraryDirectory": ""," if your bundler does not support ES modules
            "libraryDirectory": "",
            "camel2DashComponentName": false
          },
          "core"
        ],
        [
          "babel-plugin-import",
          {
            "libraryName": "@material-ui/icons",
            // Use ""libraryDirectory": ""," if your bundler does not support ES modules
            "libraryDirectory": "",
            "camel2DashComponentName": false
          },
          "icons"
        ]
      ]
}

해당 설정 후 다음과 같이 material-ui 사용 가능

import { Button, TextField } from '@material-ui/core';

테마 설정

Theming

touch styles/theme.ts
// styles/theme.ts

import { createMuiTheme } from '@material-ui/core/styles';

// Create a theme instance.
const theme = createMuiTheme({
  palette: {
    common: {
      black: '#19192B',
      white: '#ffffff',
    },
    primary: {
      light: '#B3E5FC',
      main: '#03A9F4',
      dark: '#0288D1',
      contrastText: '#212121',
    },
    secondary: {
      main: '#607D8B', // omitting light and dark will calculate from main
      contrastText: '#757575',
    },
    grey: {
      '500': '#bcbcbc',
      '700': '#79797a',
    },
    info: {
      main: '#1bb2f1',
    },
    success: {
      main: '#00d589',
    },
    error: {
      main: '#832838',
    },
    background: {
      default: '#fff',
    },
  },
  typography: {
    fontFamily: 'Roboto',
  },
});

export default theme;

_app 설정

css-baseline

// pages/_app.tsx
/* eslint-disable react/jsx-props-no-spreading */
import { FC} from 'react';
import CssBaseline from '@material-ui/core/CssBaseline';
import { ThemeProvider } from '@material-ui/core/styles';
import { AppProps } from 'next/app';
import Head from 'next/head';
import theme from '@styles/theme';

const MyApp: FC<AppProps> = ({ Component, pageProps }: AppProps) => {

  return (
    <>
      <Head>
        <title>My App</title>
        <meta name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width" />
				<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" />
      </Head>
      <ThemeProvider theme={theme}>
        {/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
        <CssBaseline />
        <Component {...pageProps} />
      </ThemeProvider>
    </>
  );
};

export default MyApp;

page 생성 및 적용

touch pages/sup.tsx
// pages/sup.tsx
import { FC } from 'react';
import { Button } from '@material-ui/core';
import { makeStyles, createStyles } from '@material-ui/core/styles';
import styles from '../styles/Home.module.css';

interface SupProps {
  message: string;
}

const useStyles = makeStyles(() =>
  createStyles({
    root: {
      backgroundColor: 'red',
    },
  })
);

const Sup: FC<SupProps> = ({ message }: SupProps) => {
  const classes = useStyles();
  return (
    <div className={styles.container}>
      <button type="button">I am so fucking boring!</button>
      <Button className={classes.root} variant="contained">
        {message}
      </Button>
    </div>
  );
};

export const getServerSideProps = () => {
  return {
    props: {
      message: 'sup',
    },
  };
};

export default Sup;

SSR에서 material-ui 사용하기

_document 생성

Custom document

touch pages/_document.tsx
/* eslint-disable react/jsx-props-no-spreading */
// eslint-disable-next-line no-use-before-define
import React from 'react';
import { ServerStyleSheets } from '@material-ui/core/styles';
import Document, { Html, Head, Main, NextScript } from 'next/document';
import theme from '@styles/theme';

export default class MyDocument extends Document {
  render() {
    return (
      <Html lang="en">
        <Head>
          <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" />
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

// `getInitialProps` belongs to `_document` (instead of `_app`),
// it's compatible with server-side generation (SSG).
MyDocument.getInitialProps = async (ctx) => {
  // Resolution order
  //
  // On the server:
  // 1. app.getInitialProps
  // 2. page.getInitialProps
  // 3. document.getInitialProps
  // 4. app.render
  // 5. page.render
  // 6. document.render
  //
  // On the server with error:
  // 1. document.getInitialProps
  // 2. app.render
  // 3. page.render
  // 4. document.render
  //
  // On the client
  // 1. app.getInitialProps
  // 2. page.getInitialProps
  // 3. app.render
  // 4. page.render

  // Render app and page and get the context of the page with collected side effects.
  const sheets = new ServerStyleSheets();
  const originalRenderPage = ctx.renderPage;

  ctx.renderPage = () =>
    originalRenderPage({
      enhanceApp: (App) => (props) => sheets.collect(<App {...props} />),
    });

  const initialProps = await Document.getInitialProps(ctx);

  return {
    ...initialProps,
    // Styles fragment is rendered after the app and page rendering finish.
    styles: [...React.Children.toArray(initialProps.styles), sheets.getStyleElement()],
  };
};

app 페이지

// pages/_app.tsx
/* eslint-disable react/jsx-props-no-spreading */
import { FC, useEffect} from 'react';
import CssBaseline from '@material-ui/core/CssBaseline';
import { ThemeProvider } from '@material-ui/core/styles';
import { AppProps } from 'next/app';
import Head from 'next/head';
import theme from '@styles/theme';

const MyApp: FC<AppProps> = ({ Component, pageProps }: AppProps) => {
	useEffect(() => {
	    // Remove the server-side injected CSS.
	    const jssStyles = document.querySelector('#jss-server-side');
	    if (jssStyles) {
	      jssStyles?.parentElement?.removeChild(jssStyles);
	    }
	  }, []);

  return (
    <>
      <Head>
        <title>My App</title>
				<meta name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width" />
      </Head>
      <ThemeProvider theme={theme}>
        {/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
        <CssBaseline />
        <Component {...pageProps} />
      </ThemeProvider>
    </>
  );
};

export default MyApp;

useEffect 사용

// pages/_app.tsx
/* eslint-disable react/jsx-props-no-spreading */
import { FC, useEffect} from 'react';
import CssBaseline from '@material-ui/core/CssBaseline';
import { ThemeProvider } from '@material-ui/core/styles';
import { AppProps } from 'next/app';
import Head from 'next/head';
import theme from '@styles/theme';

const MyApp: FC<AppProps> = ({ Component, pageProps }: AppProps) => {
	useEffect(() => {
	    // Remove the server-side injected CSS.
	    const jssStyles = document.querySelector('#jss-server-side');
	    if (jssStyles) {
	      jssStyles?.parentElement?.removeChild(jssStyles);
	    }
	  }, []);

  return (
    <>
      <Head>
        <title>My App</title>
				<meta name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width" />
      </Head>
      <ThemeProvider theme={theme}>
        {/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
        <CssBaseline />
        <Component {...pageProps} />
      </ThemeProvider>
    </>
  );
};

export default MyApp;
profile
Steady Web Developer

0개의 댓글