如何使用 React 和 Flowbite 安装 Tailwind CSS
·
React 是世界上最受欢迎的前端库之一,被 Facebook、Instagram、Yahoo Mail、Dropbox 等网站使用。
结合 Tailwind CSS 和 Flowbite 的组件,您将能够比以往更快地开发应用程序。
使用 React 安装 Tailwind CSS
按照接下来的步骤使用 Create React App安装 Tailwind CSS 和 Flowbite。
- 如果您还没有 React 应用程序,请在您的终端中运行以下命令来创建一个 React 应用程序:
npx create-react-app my-project
cd my-project
进入全屏模式 退出全屏模式
- 通过运行以下两个命令安装 Tailwind CSS:
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
进入全屏模式 退出全屏模式
1.配置tailwind.config.js文件里面的模板路径:
module.exports = {
content: [
"./src/**/*.{js,jsx,ts,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}
进入全屏模式 退出全屏模式
- 在
./src/index.css文件中设置 Tailwind 指令:
@tailwind base;
@tailwind components;
@tailwind utilities;
进入全屏模式 退出全屏模式
- 通过在终端中运行以下命令来安装 Flowbite:
npm install flowbite
进入全屏模式 退出全屏模式
- 要求 Flowbite 作为
tailwind.config.js文件中的插件:
module.exports = {
plugins: [
require('flowbite/plugin')
]
}
进入全屏模式 退出全屏模式
- 在主文件
index.js中导入 Flowbite JavaScript 文件:
import 'flowbite';
进入全屏模式 退出全屏模式
现在您可以通过运行npm run start启动服务器或使用npm run build构建项目。
React 中的 Flowbite 组件
只要您的项目中包含 Tailwind CSS,Flowbite 的大多数组件都可以无缝运行。下拉菜单、工具提示和模式等交互式元素将根据您可以直接在视图文件中编码的数据属性工作。
您只需将class属性更改为className即可开始在您的 React 项目中使用 Flowbite 中的组件。
这是您可以在App.js文件中添加的模态组件示例:
import './App.css';
function App() {
return (
<div className="App">
<button className="block text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800" type="button" data-modal-toggle="default-modal">
Toggle modal
</button>
<div id="default-modal" aria-hidden="true" className="hidden overflow-y-auto overflow-x-hidden fixed right-0 left-0 top-4 z-50 justify-center items-center h-modal md:h-full md:inset-0">
<div className="relative px-4 w-full max-w-2xl h-full md:h-auto">
<div className="relative bg-white rounded-lg shadow dark:bg-gray-700">
<div className="flex justify-between items-start p-5 rounded-t border-b dark:border-gray-600">
<h3 className="text-xl font-semibold text-gray-900 lg:text-2xl dark:text-white">
Terms of Service
</h3>
<button type="button" className="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm p-1.5 ml-auto inline-flex items-center dark:hover:bg-gray-600 dark:hover:text-white" data-modal-toggle="default-modal">
<svg className="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path></svg>
</button>
</div>
<div className="p-6 space-y-6">
<p className="text-base leading-relaxed text-gray-500 dark:text-gray-400">
With less than a month to go before the European Union enacts new consumer privacy laws for its citizens, companies around the world are updating their terms of service agreements to comply.
</p>
<p className="text-base leading-relaxed text-gray-500 dark:text-gray-400">
The European Union’s General Data Protection Regulation (G.D.P.R.) goes into effect on May 25 and is meant to ensure a common set of data rights in the European Union. It requires organizations to notify users as soon as possible of high-risk data breaches that could personally affect them.
</p>
</div>
<div className="flex items-center p-6 space-x-2 rounded-b border-t border-gray-200 dark:border-gray-600">
<button data-modal-toggle="default-modal" type="button" className="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">I accept</button>
<button data-modal-toggle="default-modal" type="button" className="text-gray-500 bg-white hover:bg-gray-100 focus:ring-4 focus:ring-gray-300 rounded-lg border border-gray-200 text-sm font-medium px-5 py-2.5 hover:text-gray-900 focus:z-10 dark:bg-gray-700 dark:text-gray-300 dark:border-gray-500 dark:hover:text-white dark:hover:bg-gray-600">Decline</button>
</div>
</div>
</div>
</div>
</div>
);
}
export default App;
进入全屏模式 退出全屏模式
我们目前正在开发一个独立的 React 项目,您将能够直接将这些交互式元素作为 React 组件包含在内,并以编程方式使用它们,而不仅仅是使用数据属性。
更多推荐


所有评论(0)