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, NormalizeCSS, GlobalStyles } 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
theme={{
/** Put your mantine theme override here */
colorScheme: 'light',
}}
>
<NormalizeCSS />
<GlobalStyles />
<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 from 'next/document';
import NextDocument, { DocumentContext } from 'next/document';
import { ServerStyles, createStylesServer } from '@mantine/next';
const stylesServer = createStylesServer();
export default class _Document extends Document {
static async function getInitialProps(ctx: DocumentContext) {
const page = await ctx.renderPage();
const initialProps = await NextDocument.getInitialProps(ctx);
// Add your app specific logic here
return {
...initialProps,
styles: (
<>
{initialProps.styles}
<ServerStyles html={page.html} server={stylesServer} />
</>
),
};
};
}
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