apps/www/content/docs/get-started/frameworks/next-app.mdx
Chakra UI works with Next.js 15 and 16.
Chakra UI doesn't lock you to a specific Next.js major version. If your project uses supported React and Emotion versions, this guide applies.
The templates in this repo may pin an older Next.js major for stability. You can
upgrade next to the latest major in your app.
Use one of the following templates to get started quickly. The templates are configured correctly to use Chakra UI.
:::card-group
<ResourceCard type="github" title="Next.js app template" url="https://github.com/chakra-ui/chakra-ui/tree/main/sandbox/next-app" />
<ResourceCard type="github" title="Next.js pages template" url="https://github.com/chakra-ui/chakra-ui/tree/main/sandbox/next-pages" />
:::
The minimum node version required is Node.20.x
:::steps
npm i @chakra-ui/react @emotion/react
Snippets are pre-built components that you can use to build your UI faster.
Using the @chakra-ui/cli you can add snippets to your project.
npx @chakra-ui/cli snippet add
If you're using TypeScript, you need to update the compilerOptions in the
tsconfig file to include the following options:
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Bundler",
"skipLibCheck": true,
"paths": {
"@/*": ["./src/*"]
}
}
}
If you're using JavaScript, create a
jsconfig.jsonfile and add the above code to the file.
Wrap your application with the Provider component generated in the
components/ui/provider component at the root of your application.
This provider composes the following:
ChakraProvider from @chakra-ui/react for the styling systemThemeProvider from next-themes for color modeimport { Provider } from "@/components/ui/provider"
export default function RootLayout(props: { children: React.ReactNode }) {
const { children } = props
return (
<html suppressHydrationWarning>
<body>
<Provider>{children}</Provider>
</body>
</html>
)
}
Adding the
suppressHydrationWarningprop to thehtmlelement is required to prevent the warning about thenext-themeslibrary.
We recommend using the experimental.optimizePackageImports feature in Next.js
to optimize your bundle size by loading only the modules that you are actually
using.
export default {
experimental: {
optimizePackageImports: ["@chakra-ui/react"],
},
}
This also helps to resolve warnings like:
[webpack.cache.PackFileCacheStrategy] Serializing big strings (xxxkiB)
If you see an error like this: Hydration failed because the initial server rendered HTML did not match the client, and the error looks similar to:
+<div className="chakra-xxx">
-<style data-emotion="css-global xxx" data-s="">
This is caused by how Next.js hydrates Emotion CSS when running with Turbopack.
Please add the --webpack flag to your dev and build scripts in your
package.json file instead.
- "dev": "next dev"
- "build": "next build"
+ "dev": "next dev --webpack"
+ "build": "next build --webpack"
When this is fixed by the Next.js team, we'll update this guide.
With the power of the snippets and the primitive components from Chakra UI, you can build your UI faster.
import { Button, HStack } from "@chakra-ui/react"
const Demo = () => {
return (
<HStack>
<Button>Click me</Button>
<Button>Click me</Button>
</HStack>
)
}
:::