前言 本文要安裝的 Flowbite 是基於 Tailwind CSS UI 框架的元件庫。另外還有 Flowbite React 元件庫可以使用,但是元件較少。
建立專案 建立專案,同時安裝 Tailwind CSS 套件。
1 2 3 4 5 6 7 8 9 npx create-next-app@latest ✔ What is your project named? … example ✔ Would you like to use TypeScript? … No ✔ Would you like to use ESLint? … Yes ✔ Would you like to use Tailwind CSS? … Yes ✔ Would you like to use `src/` directory? … No ✔ Would you like to use App Router? (recommended) … Yes ✔ Would you like to customize the default import alias ? … No
安裝 Flowbite 元件庫。
修改 tailwind.config.js
檔。
1 2 3 4 5 6 7 8 9 10 11 module .exports = { content : [ 'node_modules/flowbite-react/**/*.{js,jsx,ts,tsx}' , ], plugins : [ require ('flowbite/plugin' ), ], };
修改 app/layout.js
檔,引入 Flowbite 的互動腳本。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import './globals.css' ;import { Inter } from 'next/font/google' ;import Script from 'next/script' ;const inter = Inter ({ subsets : ['latin' ] });export const metadata = { title : 'Create Next App' , description : 'Generated by create next app' , }; export default function RootLayout ({ children } ) { return ( <html lang ="en" > <body className ={inter.className} > {children} <Script src ="https://cdnjs.cloudflare.com/ajax/libs/flowbite/1.7.0/flowbite.min.js" /> </body > </html > ); }
實作 在根目錄新增 components
資料夾,建立一個 header.js
檔。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 export default function Header ( ) { return ( <nav class ="bg-white border-gray-200 dark:bg-gray-900" > <div class ="max-w-screen-xl flex flex-wrap items-center justify-between mx-auto p-4" > <a href ="https://flowbite.com/" class ="flex items-center" > <img src ="https://flowbite.com/docs/images/logo.svg" class ="h-8 mr-3" alt ="Flowbite Logo" /> <span class ="self-center text-2xl font-semibold whitespace-nowrap dark:text-white" > Flowbite</span > </a > <button data-collapse-toggle ="navbar-default" type ="button" class ="inline-flex items-center p-2 w-10 h-10 justify-center text-sm text-gray-500 rounded-lg md:hidden hover:bg-gray-100 focus:outline-none focus:ring-2 focus:ring-gray-200 dark:text-gray-400 dark:hover:bg-gray-700 dark:focus:ring-gray-600" aria-controls ="navbar-default" aria-expanded ="false" > <span class ="sr-only" > Open main menu</span > <svg class ="w-5 h-5" aria-hidden ="true" xmlns ="http://www.w3.org/2000/svg" fill ="none" viewBox ="0 0 17 14" > <path stroke ="currentColor" stroke-linecap ="round" stroke-linejoin ="round" stroke-width ="2" d ="M1 1h15M1 7h15M1 13h15" /> </svg > </button > <div class ="hidden w-full md:block md:w-auto" id ="navbar-default" > <ul class ="font-medium flex flex-col p-4 md:p-0 mt-4 border border-gray-100 rounded-lg bg-gray-50 md:flex-row md:space-x-8 md:mt-0 md:border-0 md:bg-white dark:bg-gray-800 md:dark:bg-gray-900 dark:border-gray-700" > <li > <a href ="#" class ="block py-2 pl-3 pr-4 text-white bg-blue-700 rounded md:bg-transparent md:text-blue-700 md:p-0 dark:text-white md:dark:text-blue-500" aria-current ="page" > Home</a > </li > <li > <a href ="#" class ="block py-2 pl-3 pr-4 text-gray-900 rounded hover:bg-gray-100 md:hover:bg-transparent md:border-0 md:hover:text-blue-700 md:p-0 dark:text-white md:dark:hover:text-blue-500 dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent" > About</a > </li > <li > <a href ="#" class ="block py-2 pl-3 pr-4 text-gray-900 rounded hover:bg-gray-100 md:hover:bg-transparent md:border-0 md:hover:text-blue-700 md:p-0 dark:text-white md:dark:hover:text-blue-500 dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent" > Services</a > </li > <li > <a href ="#" class ="block py-2 pl-3 pr-4 text-gray-900 rounded hover:bg-gray-100 md:hover:bg-transparent md:border-0 md:hover:text-blue-700 md:p-0 dark:text-white md:dark:hover:text-blue-500 dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent" > Pricing</a > </li > <li > <a href ="#" class ="block py-2 pl-3 pr-4 text-gray-900 rounded hover:bg-gray-100 md:hover:bg-transparent md:border-0 md:hover:text-blue-700 md:p-0 dark:text-white md:dark:hover:text-blue-500 dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent" > Contact</a > </li > </ul > </div > </div > </nav > ); }
修改 app/layout.js
檔,在裡面使用 Header
元件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import Header from '@/components/header' ;import './globals.css' ;import { Inter } from 'next/font/google' ;const inter = Inter ({ subsets : ['latin' ] });export const metadata = { title : 'Create Next App' , description : 'Generated by create next app' , }; export default function RootLayout ({ children } ) { return ( <html lang ="en" > <body className ={inter.className} > <Header /> {children} <script src ="https://cdnjs.cloudflare.com/ajax/libs/flowbite/1.7.0/flowbite.min.js" async /> </body > </html > ); }
啟動服務。
參考資料