react-server675fbba4
react-serverfilesdocssrcpagesja(pages)integrationsmui.mdx
docs/src/pages/ja/(pages)/integrations/mui.mdxmdx4.9 KiBa45af160

title: Material UI category: Integrations order: 6

import Link from "../../../../components/Link.jsx";

Material UI

@lazarv/react-serverは、GoogleのMaterial Designを実装する人気のReactコンポーネントライブラリであるMaterial UI (MUI)と互換性があります。MUIはCSS-in-JSスタイリングにEmotionを使用しており、@lazarv/react-serverでそのまま動作します。

<Link name="installation"> ## インストール </Link>

Material UIと必要な依存関係をインストールします:

pnpm add @mui/material @emotion/react @emotion/styled

オプションで、RobotoフォントとMaterial Iconsをインストールします:

pnpm add @fontsource/roboto @mui/icons-material
<Link name="theme-setup"> ## テーマのセットアップ </Link>

Material UIテーマを設定するテーマファイルを作成します:

import { createTheme } from "@mui/material/styles";

const theme = createTheme({
  palette: {
    mode: "light",
  },
  typography: {
    fontFamily: "Roboto",
  },
});

export default theme;
<Link name="provider-setup"> ## プロバイダーのセットアップ </Link>

Material UIには、適切なテーマとベースラインスタイルのためにThemeProviderCssBaselineが必要です。アプリをラップするクライアントコンポーネントを作成します:

"use client";

import Container from "@mui/material/Container";
import CssBaseline from "@mui/material/CssBaseline";
import { ThemeProvider } from "@mui/material/styles";

import theme from "../theme";

export default function Layout({ children }) {
  return (
    <ThemeProvider theme={theme}>
      <CssBaseline />
      <Container maxWidth="lg">
        {children}
      </Container>
    </ThemeProvider>
  );
}

次に、フォントをインポートし、Layoutプロバイダーコンポーネントを使用するルートレイアウトを設定します:

import "@fontsource/roboto/300.css";
import "@fontsource/roboto/400.css";
import "@fontsource/roboto/500.css";
import "@fontsource/roboto/700.css";

import Layout from "./components/Layout";

export default function RootLayout({ children }) {
  return (
    <html lang="en" suppressHydrationWarning>
      <head>
        <meta charSet="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>My App</title>
      </head>
      <body suppressHydrationWarning>
        <Layout>{children}</Layout>
      </body>
    </html>
  );
}
<Link name="navigation"> ## ナビゲーション </Link>

MUIのリンクやボタンコンポーネントを@lazarv/react-serverのクライアントサイドナビゲーションで使用するには、componentプロップを介してランタイムのLinkコンポーネントを渡します:

"use client";

import { Link as ReactServerLink } from "@lazarv/react-server/navigation";
import Link from "@mui/material/Link";

export default function Home() {
  return (
    <Link to="/about" color="secondary" component={ReactServerLink}>
      Go to the about page
    </Link>
  );
}

同じパターンでMUIのButtonをナビゲーションに使用できます:

"use client";

import { Link as ReactServerLink } from "@lazarv/react-server/navigation";
import Button from "@mui/material/Button";

export default function About() {
  return (
    <Button to="/" component={ReactServerLink}>
      Go to the home page
    </Button>
  );
}
<Link name="server-components"> ## サーバーコンポーネント </Link>

ブラウザAPIやReactフックに依存しない一部のMUIコンポーネントは、サーバーコンポーネントで直接使用できます。TypographyLink(ナビゲーションなし)などのコンポーネントはサーバーコンポーネントで動作します:

import Typography from "@mui/material/Typography";
import Link from "@mui/material/Link";

export default function Copyright() {
  return (
    <Typography variant="body2" color="text.secondary" align="center">
      {"Copyright © "}
      <Link color="inherit" href="https://mui.com/">
        Your Website
      </Link>{" "}
      {new Date().getFullYear()}.
    </Typography>
  );
}
<Link name="icons"> ## アイコン </Link>

@mui/icons-materialを使用する場合、適切なモジュール解決のためにESMパスからアイコンをインポートします:

import LightbulbOutlined from "@mui/icons-material/esm/LightbulbOutlined";

@lazarv/react-serverでMUIを使用する完全な例については、Material UI exampleを確認してください。