常见UI模式实现


常见 UI 模式实现

按钮

按钮是最基础的 UI 元素,不同场景需要不同变体:

按钮

按钮代码示例

按钮的通用规范:主按钮用品牌色,次要按钮用描边或浅灰,危险操作用红色。所有按钮都要有 hover、active、focus、disabled 四态,缺一个都会在某个交互场景下显得粗糙。

<!-- 主要按钮 --> <button class="bg-blue-500 hover:bg-blue-600 active:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg focus:ring-2 focus:ring-blue-300 focus:ring-offset-2 disabled:opacity-50 disabled:cursor-not-allowed transition-colors duration-150"> 主要操作 </button> <!-- 次要按钮 --> <button class="border border-gray-300 hover:bg-gray-50 text-gray-700 font-medium px-4 py-2 rounded-lg focus:ring-2 focus:ring-gray-200 transition-colors"> 次要操作 </button> <!-- 危险操作 --> <button class="bg-red-500 hover:bg-red-600 text-white px-4 py-2 rounded-lg transition-colors"> 删除 </button> <!-- 图标按钮 --> <button class="p-2 rounded-lg hover:bg-gray-100 focus:ring-2 focus:ring-gray-200" aria-label="搜索"> 搜索图标 </button> ​

标签页

<div class="border-b border-gray-200"> <nav class="flex gap-6"> <a class="border-b-2 border-blue-500 text-blue-600 font-medium pb-2">概览</a> <a class="border-b-2 border-transparent text-gray-500 hover:text-gray-700 pb-2">详情</a> <a class="border-b-2 border-transparent text-gray-500 hover:text-gray-700 pb-2">设置</a> </nav> </div> ​

激活标签用 border-b-2 加品牌色,未激活标签用透明边框占位,避免文字跳动。hover 时文字变深,提示可点击。

表格

表格的样式重点在表头、行分隔和悬停反馈:

<table class="w-full text-sm"> <thead> <tr class="border-b-2 border-gray-200 text-left text-gray-500"> <th class="py-2 px-3">名称</th> <th class="py-2 px-3">状态</th> <th class="py-2 px-3">更新时间</th> </tr> </thead> <tbody class="divide-y divide-gray-100"> <tr class="hover:bg-gray-50"> <td class="py-3 px-3">项目一</td> <td class="py-3 px-3"> <span class="inline-flex items-center px-2 py-0.5 rounded-full bg-green-100 text-green-700 text-xs">已上线</span> </td> <td class="py-3 px-3 text-gray-500">2025-01-15</td> </tr> </tbody> </table> ​

分页

<nav class="flex items-center gap-1"> <button class="px-3 py-1 rounded border border-gray-200 hover:bg-gray-50">上一页</button> <button class="px-3 py-1 rounded bg-blue-500 text-white">1</button> <button class="px-3 py-1 rounded border border-gray-200 hover:bg-gray-50">2</button> <button class="px-3 py-1 rounded border border-gray-200 hover:bg-gray-50">3</button> <button class="px-3 py-1 rounded border border-gray-200 hover:bg-gray-50">下一页</button> </nav> ​

空状态

空状态要告诉用户"这里为什么没有内容"以及"接下来能做什么":

<div class="text-center py-16"> <div class="text-4xl mb-4">空</div> <h3 class="text-lg font-semibold text-gray-900 mb-2">还没有数据</h3> <p class="text-sm text-gray-500 mb-6">创建第一个项目后,这里会显示统计信息</p> <button class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded-lg text-sm"> 创建项目 </button> </div> ​

模式组合的思路

这些模式的共同规律:先拆解视觉构成(颜色、间距、圆角、边框),再映射到工具类,最后用变体补齐交互状态。建议把高频模式沉淀为项目组件库,用 props 控制变体,避免在每个页面重复书写。

卡片

<!-- 基础卡片 --> <div class="bg-white rounded-xl border border-gray-200 shadow-sm overflow-hidden"> <img src="/cover.jpg" class="w-full h-48 object-cover"> <div class="p-5"> <div class="flex items-center gap-2 mb-2"> <span class="bg-blue-100 text-blue-700 text-xs font-medium px-2 py-0.5 rounded"> 标签 </span> </div> <h3 class="text-lg font-semibold text-gray-900 mb-2">卡片标题</h3> <p class="text-sm text-gray-500 line-clamp-2"> 卡片描述文字,最多显示两行,超出部分截断省略号。 </p> <div class="mt-4 flex items-center justify-between"> <span class="text-xs text-gray-400">2025-01-15</span> <button class="text-sm text-blue-500 hover:text-blue-700 font-medium"> 阅读更多 → </button> </div> </div> </div> ​

表单

<form class="max-w-md space-y-4"> <!-- 文本输入 --> <div> <label class="block text-sm font-medium text-gray-700 mb-1">邮箱</label> <input type="email" placeholder="you@example.com" class="w-full px-3 py-2 border border-gray-300 rounded-lg placeholder:text-gray-400 focus:border-blue-500 focus:ring-2 focus:ring-blue-200 outline-none transition-colors"> </div> <!-- 选择框 --> <div> <label class="block text-sm font-medium text-gray-700 mb-1">角色</label> <select class="w-full px-3 py-2 border border-gray-300 rounded-lg bg-white focus:border-blue-500 focus:ring-2 focus:ring-blue-200 outline-none transition-colors"> <option>开发者</option> <option>设计师</option> <option>产品经理</option> </select> </div> <!-- 文本域 --> <div> <label class="block text-sm font-medium text-gray-700 mb-1">备注</label> <textarea rows="3" class="w-full px-3 py-2 border border-gray-300 rounded-lg placeholder:text-gray-400 focus:border-blue-500 focus:ring-2 focus:ring-blue-200 outline-none transition-colors resize-none"></textarea> </div> <!-- 复选框 --> <label class="flex items-center gap-2"> <input type="checkbox" class="w-4 h-4 rounded border-gray-300 text-blue-500 focus:ring-blue-200"> <span class="text-sm text-gray-600">我同意服务条款</span> </label> <!-- 错误状态 --> <div> <label class="block text-sm font-medium text-gray-700 mb-1">密码</label> <input type="password" class="w-full px-3 py-2 border border-red-500 rounded-lg focus:ring-2 focus:ring-red-200 outline-none"> <p class="mt-1 text-sm text-red-500">密码不能少于 8 个字符</p> </div> <!-- 提交按钮 --> <button type="submit" class="w-full bg-blue-500 hover:bg-blue-600 text-white py-2.5 rounded-lg font-medium transition-colors"> 提交 </button> </form> ​

导航栏

导航栏

提示消息(Toast)

提示消息(Toast)

模态框

<!-- 遮罩层 --> <div class="fixed inset-0 z-50 flex items-center justify-center"> <!-- 半透明背景 --> <div class="fixed inset-0 bg-black/30" /> <!-- 弹窗 --> <div class="relative bg-white rounded-xl shadow-xl max-w-md w-full mx-4 p-6"> <h2 class="text-lg font-semibold text-gray-900 mb-2">确认操作</h2> <p class="text-sm text-gray-500 mb-6">确定要删除这个项目吗?此操作不可撤销。</p> <div class="flex justify-end gap-3"> <button class="px-4 py-2 text-sm text-gray-600 hover:bg-gray-100 rounded-lg transition-colors"> 取消 </button> <button class="px-4 py-2 text-sm bg-red-500 hover:bg-red-600 text-white rounded-lg transition-colors"> 确认删除 </button> </div> </div> </div> ​

这些 UI 模式展示了 Tailwind 工具类在实际场景中的组合方式。核心思路:先确定视觉设计(颜色、间距、圆角),然后翻译为对应的工具类。所有状态(hover、focus、disabled、error)都通过变体前缀声明式地处理。

掌握这些常见模式后,你可以举一反三地构建更复杂的 UI 组件。建议将常用模式封装为项目内部的组件库(Button、Card、Input 等),避免在每个页面重复写相同的工具类组合。

作者与出处
原作者:灏天文库
来源:平台策划编纂
整理人: 灏天文库整理
由灏天文库平台收录,内容或由平台用户上传,仅供学习交流
发布者: 作者: 灏天文库 转发
评论区 (0)
U