統合このページを編集.md

Mantine UI

@lazarv/react-serverは、フル機能のReactコンポーネントライブラリであるMantineと互換性があります。クライアントコンポーネントでMantineコンポーネントを使用し、サーバーサイドレンダリングのサポート、MantineのスタイリングシステムのためのPostCSS設定、テーマのためのMantineProviderを利用できます。

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

pnpm add @mantine/core @mantine/hooks pnpm add -D postcss postcss-preset-mantine postcss-simple-vars

Mantineにはpostcss-preset-mantineプラグインとブレークポイント変数を含むPostCSS設定が必要です:

postcss.config.cjs
module.exports = { plugins: { "postcss-preset-mantine": {}, "postcss-simple-vars": { variables: { "mantine-breakpoint-xs": "36em", "mantine-breakpoint-sm": "48em", "mantine-breakpoint-md": "62em", "mantine-breakpoint-lg": "75em", "mantine-breakpoint-xl": "88em", }, }, }, };

コアMantineスタイルをインポートし、ルートレイアウトでMantineProviderでアプリをラップします。スタイルなしコンテンツのフラッシュを防ぐために、<head>ColorSchemeScriptを含めます:

src/pages/layout.tsx
import "@mantine/core/styles.css"; import { ColorSchemeScript, createTheme, MantineProvider } from "@mantine/core"; const theme = createTheme({ /** Mantineのテーマオーバーライドをここに記述 */ }); export default function Layout({ children }: { children: React.ReactNode }) { return ( <html lang="en" data-mantine-color-scheme="light" suppressHydrationWarning> <head> <ColorSchemeScript /> </head> <body suppressHydrationWarning> <MantineProvider theme={theme}> {children} </MantineProvider> </body> </html> ); }

ReactフックやブラウザAPIを使用するMantineコンポーネントは、クライアントコンポーネントで使用する必要があります。クライアントコンポーネントを作成し、必要なMantineコンポーネントをインポートします:

src/components/Counter.tsx
"use client"; import { useState } from "react"; import { Button, Group, Text } from "@mantine/core"; export function Counter() { const [count, setCount] = useState(0); return ( <Group> <Button onClick={() => setCount((c) => c - 1)}>-</Button> <Text>{count}</Text> <Button onClick={() => setCount((c) => c + 1)}>+</Button> </Group> ); }

次に、サーバーコンポーネントページからクライアントコンポーネントを使用します:

src/pages/index.tsx
import { Counter } from "../components/Counter"; export default function HomePage() { return <Counter />; }

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

src/components/MainNavigation.tsx
"use client"; import { NavLink } from "@mantine/core"; import { Link, usePathname } from "@lazarv/react-server/navigation"; export function MainNavigation() { const pathname = usePathname(); return ( <> <NavLink component={Link} to="/" label="Home" active={pathname === "/"} /> <NavLink component={Link} to="/about" label="About" active={pathname === "/about"} /> </> ); }

Mantineは追加機能のための多くの拡張パッケージを提供しています。使用するページやレイアウトでスタイルをインポートします:

src/pages/dates.tsx
import "@mantine/dates/styles.css"; import { MyDates } from "../components/MyDates"; export default function DatesPage() { return <MyDates />; }

よく使われるMantine拡張パッケージとそのスタイルインポート:

パッケージスタイルインポート
@mantine/dates@mantine/dates/styles.css
@mantine/charts@mantine/charts/styles.css
@mantine/notifications@mantine/notifications/styles.css
@mantine/code-highlight@mantine/code-highlight/styles.css
@mantine/carousel@mantine/carousel/styles.css
@mantine/spotlight@mantine/spotlight/styles.css
@mantine/nprogress@mantine/nprogress/styles.css
@mantine/tiptap@mantine/tiptap/styles.css
@mantine/dropzone@mantine/dropzone/styles.css

一部のコンポーネント(ブラウザAPIに依存するチャートなど)はサーバーサイドレンダリングができません。@lazarv/react-server/clientClientOnlyを使用して、クライアントでのみレンダリングします:

src/pages/charts.tsx
import "@mantine/charts/styles.css"; import { ClientOnly } from "@lazarv/react-server/client"; import { MyAreaChart } from "../components/MyAreaChart"; export default function ChartsPage() { return ( <ClientOnly> <MyAreaChart /> </ClientOnly> ); }

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