Use with Next.js - Flowbite React
Learn how to install Flowbite React for your Next.js project and start developing with the most popular React-based framework built by Vercel
#
Using the CLIRun the following command to scaffold a new Flowbite React
project using Next.js
:
# npm
npm create flowbite-react@latest -- --template nextjs
# yarn
yarn create flowbite-react --template nextjs
# pnpm
pnpm create flowbite-react@latest --template nextjs
# bun
bun create flowbite-react@latest --template nextjs
Manual Installation
#
New projectAdd Flowbite React to a new Next.js project:
#
Create projectRun the following command to create a new Next.js project using tailwind
:
npx create-next-app@latest --tailwind
#
Install Flowbite ReactRun the following command to install flowbite-react
:
npm i flowbite-react
#
Configure Tailwind CSSOpen tailwind.config.js
and update content
and plugins
to include Flowbite React:
/** @type {import('tailwindcss').Config} */
export default {
content: [
// ...
"node_modules/flowbite-react/lib/esm/**/*.js",
],
plugins: [
// ...
require("flowbite/plugin"),
],
};
#
Existing projectAdd Flowbite React to an existing Next.js project:
#
Setup Tailwind CSS- Install
tailwindcss
and its peer dependencies:
npm i -D tailwindcss postcss autoprefixer
- Generate
tailwind.config.js
andpostcss.config.js
files:
npx tailwindcss init -p
- Add the paths to all of your template files in your
tailwind.config.js
file:
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./app/**/*.{js,ts,jsx,tsx,mdx}",
"./pages/**/*.{js,ts,jsx,tsx,mdx}",
"./components/**/*.{js,ts,jsx,tsx,mdx}",
// Or if using `src` directory:
"./src/**/*.{js,ts,jsx,tsx,mdx}",
],
theme: {
extend: {},
},
plugins: [],
};
- Add the
@tailwind
directives for each of Tailwind's layers to yourglobals.css
file:
@tailwind base;
@tailwind components;
@tailwind utilities;
#
Install Flowbite React- Run the following command to install
flowbite-react
:
npm i flowbite-react
- Add the Flowbite plugin to
tailwind.config.js
and include content fromflowbite-react
:
/** @type {import('tailwindcss').Config} */
export default {
content: [
// ...
"node_modules/flowbite-react/lib/esm/**/*.js",
],
plugins: [
// ...
require("flowbite/plugin"),
],
};
#
Dark modeIn server side rendered applications, such as Next.js, to avoid page flicker (if dark
mode is set) before Next.js hydrates the content, ThemeModeScript
component must be rendered in Head
component (see implementation below).
ThemeModeScript
renders a script tag that sets dark
or removes dark
from <html>
element before hydration occurs.
#
App router// app/layout.tsx
import { ThemeModeScript } from "flowbite-react";
export default function RootLayout({ children }) {
return (
<html>
<head>
<ThemeModeScript />
</head>
<body>{children}</body>
</html>
);
}
#
Pages router// pages/_document.tsx
import { ThemeModeScript } from "flowbite-react";
export default function Document() {
return (
<Html>
<Head>
<ThemeModeScript />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
#
Try it outNow that you have successfully installed Flowbite React you can start using the components from the library.
import { Button } from "flowbite-react";
export default function MyPage() {
return (
<div>
<Button>Click me</Button>
</div>
);
}