Usage with Next.js

Mantine is fully compatible with Next.js, follow following guide to setup your application

Get started with template

If you are starting new project you can skip all configuration steps and use Next.js template. To get started follow this link and create new repository. Alternatively you can download or clone repository to get started on your machine.

Adding to existing Next.js project

  1. Install @mantine/next
yarn add @mantine/next
npm install @mantine/next
  1. Create pages/_document.tsx file:
import Document from 'next/document';
import { createGetInitialProps } from '@mantine/next';
const getInitialProps = createGetInitialProps();
export default class _Document extends Document {
static getInitialProps = getInitialProps;
}
  1. (Optional) Replace your pages/_app.tsx with
import { AppProps } from 'next/app';
import Head from 'next/head';
import { MantineProvider } from '@mantine/core';
export default function App(props: AppProps) {
const { Component, pageProps } = props;
return (
<>
<Head>
<title>Page title</title>
<meta name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width" />
</Head>
<MantineProvider
withGlobalStyles
withNormalizeCSS
theme={{
/** Put your mantine theme override here */
colorScheme: 'light',
}}
>
<Component {...pageProps} />
</MantineProvider>
</>
);
}

Create your own getInitialProps

In some cases you would want to have access to getInitialProps in Next Document, to do so you will need to setup Mantine ssr styles extraction on your own:

import Document, { DocumentContext } from 'next/document';
import { ServerStyles, createStylesServer } from '@mantine/next';
const stylesServer = createStylesServer();
export default class _Document extends Document {
static async getInitialProps(ctx: DocumentContext) {
const initialProps = await Document.getInitialProps(ctx);
// Add your app specific logic here
return {
...initialProps,
styles: (
<>
{initialProps.styles}
<ServerStyles html={initialProps.html} server={stylesServer} />
</>
),
};
}
}

Mantine components with Next Link

You can use polymorphic components (Button, ActionIcon, Anchor, etc.) with Next Link, to do so:

  • set component prop to a
  • wrap it with Link
import Link from 'next/link';
import { Button } from '@mantine/core';
function Demo() {
return (
<Link href="/hello" passHref>
<Button component="a">Next link button</Button>
</Link>
);
}
Build fully functional accessible web applications faster than ever
Feedback
Your feedback is most valuable contribution to the project, please share how you use Mantine, what features are missing and what is done good
Leave feedback