Next.js (App)
A guide for installing and using Yamada UI with Next.js app directory.
Installation
- 1
Create application
Create Next.js application.
pnpm create next-app my-app --typescriptnpx create-next-app my-app --typescriptyarn create next-app my-app --typescriptbun create next-app my-app --typescript - 2
Setup
Running the command will create the necessary files and folders in your project.
pnpm dlx @yamada-ui/cli initnpx @yamada-ui/cli inityarn dlx @yamada-ui/cli initbunx @yamada-ui/cli init - 3
Install the package
Install
@workspaces/uito your application.pnpm add "@workspaces/ui@workspace:*"npm install "@workspaces/ui@workspace:*"yarn add "@workspaces/ui@workspace:*"bun add "@workspaces/ui@workspace:*" - 4
Add provider
After installing, add
UIProviderto the root of your application. To suppress hydration errors, addsuppressHydrationWarningto thehtmlandbodytags.layout.tsx
import { UIProvider } from "@yamada-ui/react" export default function RootLayout({ children, }: { children: React.ReactNode }) { return ( <html lang="en" suppressHydrationWarning> <body suppressHydrationWarning> <UIProvider>{children}</UIProvider> </body> </html> ) } - 5
Use components
After adding
UIProvider, you can use the components in your application.page.tsx
import { Button } from "@workspaces/ui" export default function Home() { return <Button>Click me!</Button> }That's it! You've successfully set up Yamada UI.
Scripts
ColorModeScript
To use Color Mode, you need to add ColorModeScript to the body to ensure it works correctly.
This is because color mode is implemented using localStorage or cookies, and adding the script ensures proper synchronization when the page loads.
layout.tsx
import { UIProvider, ColorModeScript } from "@workspaces/ui"
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en" suppressHydrationWarning>
<body suppressHydrationWarning>
<ColorModeScript />
<UIProvider>{children}</UIProvider>
</body>
</html>
)
}
If you change the defaultColorMode in your config, set the defaultValue prop on ColorModeScript.
layout.tsx
import { UIProvider, ColorModeScript } from "@workspaces/ui"
import { config } from "@workspaces/theme"
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en" suppressHydrationWarning>
<body suppressHydrationWarning>
<ColorModeScript defaultValue={config.defaultColorMode} />
<UIProvider config={config}>{children}</UIProvider>
</body>
</html>
)
}
ThemeSchemeScript
To use theme switching, you need to add ThemeSchemeScript to the body to ensure it works correctly.
This is because theme switching is implemented using localStorage or cookies, and adding the script ensures proper synchronization when the page loads.
layout.tsx
import { UIProvider, ThemeSchemeScript } from "@workspaces/ui"
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en" suppressHydrationWarning>
<body suppressHydrationWarning>
<ThemeSchemeScript />
<UIProvider>{children}</UIProvider>
</body>
</html>
)
}
If you change the defaultThemeScheme in your config, set the defaultValue prop on ThemeSchemeScript.
layout.tsx
import { UIProvider, ThemeSchemeScript } from "@workspaces/ui"
import { config } from "@workspaces/theme"
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en" suppressHydrationWarning>
<body suppressHydrationWarning>
<ThemeSchemeScript defaultValue={config.defaultThemeScheme} />
<UIProvider config={config}>{children}</UIProvider>
</body>
</html>
)
}
Use cookies
Using cookies allows you to ensure proper operation even on server-side rendering.
layout.tsx
import { UIProvider, ColorModeScript, ThemeSchemeScript } from "@workspaces/ui"
import { cookies } from "next/headers"
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
const cookieStore = await cookies()
return (
<html lang="en" suppressHydrationWarning>
<body suppressHydrationWarning>
<ColorModeScript type="cookie" />
<ThemeSchemeScript type="cookie" />
<UIProvider cookie={cookieStore.toString()}>{children}</UIProvider>
</body>
</html>
)
}