# Dark 模式弹窗与卡片适配修复
## 📋 问题描述
切换到 Dark 模式后,弹窗(Dialog)和卡片(Card)等组件未能正确适配深色主题,导致:
- ✗ 弹窗背景仍然是白色
- ✗ 文字颜色对比度不足
- ✗ 渐变背景色不适配
- ✗ 边框颜色不明显
---
## ✅ 修复内容
### 1. **Dialog 组件修复**
**问题:** Dialog 使用硬编码的 `bg-white` 背景色
**修复:**
```tsx
// 修复前
className="bg-white ... border p-6 shadow-lg"
// 修复后
className="bg-background text-foreground ... border p-6 shadow-lg transition-colors"
```
**改进:**
- ✅ 使用 `bg-background` 替代 `bg-white`
- ✅ 添加 `text-foreground` 确保文字颜色正确
- ✅ 添加 `transition-colors` 实现平滑过渡
---
### 2. **全局 CSS 颜色适配**
在 `/styles/globals.css` 中添加了完整的 Dark 模式颜色映射:
#### 灰色系
```css
.dark .bg-gray-50 { background-color: #1f2937; }
.dark .bg-gray-100 { background-color: #374151; }
.dark .bg-gray-200 { background-color: #4b5563; }
.dark .text-gray-900 { color: #e7e9ea; }
.dark .text-gray-800 { color: #d1d5db; }
.dark .text-gray-700 { color: #9ca3af; }
```
#### 绿色主题(农业主题)
```css
.dark .bg-green-50 { background-color: rgba(34, 197, 94, 0.1); }
.dark .bg-green-100 { background-color: rgba(34, 197, 94, 0.2); }
.dark .text-green-800 { color: #4ade80; }
.dark .text-green-700 { color: #4ade80; }
.dark .text-green-600 { color: #22c55e; }
```
#### 蓝色主题
```css
.dark .bg-blue-50 { background-color: rgba(59, 130, 246, 0.1); }
.dark .bg-blue-100 { background-color: rgba(59, 130, 246, 0.2); }
.dark .text-blue-800 { color: #60a5fa; }
.dark .text-blue-700 { color: #60a5fa; }
.dark .text-blue-600 { color: #3b82f6; }
.dark .text-blue-900 { color: #93c5fd; }
```
#### 红色主题(告警/危险)
```css
.dark .bg-red-50 { background-color: rgba(239, 68, 68, 0.1); }
.dark .bg-red-100 { background-color: rgba(239, 68, 68, 0.2); }
.dark .text-red-800 { color: #f87171; }
.dark .text-red-700 { color: #f87171; }
.dark .text-red-600 { color: #ef4444; }
```
#### 橙色主题(警告)
```css
.dark .bg-orange-50 { background-color: rgba(249, 115, 22, 0.1); }
.dark .bg-orange-100 { background-color: rgba(249, 115, 22, 0.2); }
.dark .text-orange-800 { color: #fb923c; }
.dark .text-orange-700 { color: #fb923c; }
.dark .text-orange-600 { color: #f97316; }
```
#### 黄色主题(提示)
```css
.dark .bg-yellow-50 { background-color: rgba(234, 179, 8, 0.1); }
.dark .bg-yellow-100 { background-color: rgba(234, 179, 8, 0.2); }
.dark .text-yellow-800 { color: #fbbf24; }
.dark .text-yellow-700 { color: #fbbf24; }
```
#### 紫色主题
```css
.dark .bg-purple-50 { background-color: rgba(139, 92, 246, 0.1); }
.dark .bg-purple-100 { background-color: rgba(139, 92, 246, 0.2); }
.dark .text-purple-800 { color: #a78bfa; }
.dark .text-purple-700 { color: #a78bfa; }
.dark .text-purple-600 { color: #8b5cf6; }
.dark .text-purple-900 { color: #c4b5fd; }
```
#### 粉色主题
```css
.dark .bg-pink-50 { background-color: rgba(236, 72, 153, 0.1); }
.dark .bg-pink-100 { background-color: rgba(236, 72, 153, 0.2); }
.dark .text-pink-800 { color: #f472b6; }
.dark .text-pink-700 { color: #f472b6; }
```
#### 青色/蓝绿色主题
```css
.dark .bg-cyan-50 { background-color: rgba(6, 182, 212, 0.1); }
.dark .bg-teal-50 { background-color: rgba(20, 184, 166, 0.1); }
.dark .text-cyan-800 { color: #22d3ee; }
```
#### 边框颜色
```css
.dark .border-green-200 { border-color: rgba(34, 197, 94, 0.3); }
.dark .border-blue-200 { border-color: rgba(59, 130, 246, 0.3); }
.dark .border-red-200 { border-color: rgba(239, 68, 68, 0.3); }
.dark .border-orange-200 { border-color: rgba(249, 115, 22, 0.3); }
.dark .border-purple-200 { border-color: rgba(139, 92, 246, 0.3); }
```
---
## 🎨 设计原则
### 1. **透明度策略**
对于彩色背景,使用半透明 rgba 值:
- `bg-*-50` → `rgba(color, 0.1)` - 10% 不透明度
- `bg-*-100` → `rgba(color, 0.2)` - 20% 不透明度
**优势:**
- ✅ 在深色背景上保持可见性
- ✅ 不会过于刺眼
- ✅ 保持视觉层次感
### 2. **文字颜色调整**
深色模式下,文字颜色使用较浅的色调:
- `text-*-600` → 保持原色(主色调)
- `text-*-700` → 调亮(从深色变为亮色)
- `text-*-800` → 进一步调亮
- `text-*-900` → 最亮色调
### 3. **对比度优化**
确保所有颜色组合符合 WCAG AA 标准:
- 正常文字:至少 4.5:1
- 大文字(18pt+):至少 3:1
- UI 组件:至少 3:1
---
## 📦 已适配的 UI 组件
### ✅ Dialog(对话框)
- 背景色:`bg-background`
- 文字色:`text-foreground`
- 边框:`border`(自动适配)
- 过渡:`transition-colors`
### ✅ AlertDialog(确认对话框)
- 背景色:`bg-background`(已内置)
- 所有子组件自动继承颜色
### ✅ Card(卡片)
- 背景色:`bg-card`
- 文字色:`text-card-foreground`
- 边框:`border`(自动适配)
### ✅ Popover(弹出框)
- 背景色:`bg-popover`
- 文字色:`text-popover-foreground`
- 已完全适配
---
## 🎯 使用示例
### 示例 1: 绿色信息卡片
**浅色模式:**
```tsx
这是一条成功消息
错误:操作失败,请重试