如何构建 Tailwind CSS 模态组件
由 Mux 赞助的 DEV 全球展示挑战赛:展示你的项目!
在本教程中,我将向您展示如何使用 Flowbite 构建Tailwind CSS 模态元素,并使其也能与深色模式配合使用。
上次我在 DEV 社区向大家展示了如何使用 Tailwind CSS 构建响应式导航栏,现在我很高兴能开始使用这个模态组件。
模态框是一种非常重要且交互性强的元素,可用于向网站用户展示信息和接收信息。
以下是我们将要构建的内容预览:
我们开始吧!
Tailwind CSS 模态组件
开始之前,请确保您的项目中已安装 Tailwind CSS 和 Flowbite。
我假设您已经有一个可以正常运行的 Tailwind CSS 项目,所以我们首先来安装 Flowbite:
npm i flowbite
现在,请在您的文件中将其作为插件引入tailwind.config.js:
module.exports = {
plugins: [
require('flowbite/plugin')
]
}
现在还可以通过 NPM 或 CDN 引入 Flowbite JavaScript 文件:
<script src="../path/to/flowbite/dist/flowbite.js"></script>
通过 CDN:
<link rel="stylesheet" href="https://unpkg.com/flowbite@1.2.0/dist/flowbite.min.css" />
太棒了!现在我们先来添加一个按钮,用来切换模态框的显示/隐藏状态:
<!-- Modal toggle -->
<button class="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>
如您所见,我们添加了一个data-modal-toggle数据属性,其中default-modal是我们现在要构建的模态元素的 id。
让我们使用 Flowbite 中的一个模态组件示例,并将其添加到按钮组件之后:
<!-- Main modal -->
<div id="default-modal" aria-hidden="true" class="hidden overflow-x-hidden overflow-y-auto fixed h-modal md:h-full top-4 left-0 right-0 md:inset-0 z-50 justify-center items-center">
<div class="relative w-full max-w-2xl px-4 h-full md:h-auto">
<!-- Modal content -->
<div class="bg-white rounded-lg shadow relative dark:bg-gray-700">
<!-- Modal header -->
<div class="flex items-start justify-between p-5 border-b rounded-t dark:border-gray-600">
<h3 class="text-gray-900 text-xl lg:text-2xl font-semibold dark:text-white">
Terms of Service
</h3>
<button type="button" class="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 class="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>
<!-- Modal body -->
<div class="p-6 space-y-6">
<p class="text-gray-500 text-base leading-relaxed 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 class="text-gray-500 text-base leading-relaxed 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>
<!-- Modal footer -->
<div class="flex space-x-2 items-center p-6 border-t border-gray-200 rounded-b dark:border-gray-600">
<button data-modal-toggle="default-modal" type="button" class="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" class="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>
干得好!现在你拥有了一个可以正常工作且响应迅速的模态框,可以在你的 Tailwind CSS 项目中使用。
此模态组件也支持深色模式,您可以查看Flowbite 的Tailwind CSS 深色模式切换指南来自行设置。
Flowbite 组件库中还有许多其他模态组件变体和尺寸,您可以查看并将其用于您的项目中。
文章来源:https://dev.to/themesberg/how-to-build-a-tailwind-css-modal-component-31ko

