MIGRATION_TO_STYLED_COMPONENTS.md 6.5 KB

迁移到 Styled Components 完成报告

✅ 迁移完成

项目已成功从 SCSS 迁移到 Styled Components(功能最强大的 CSS 方案)。


📦 已安装的依赖

{
  "styled-components": "^6.x.x"
}

🎨 新的文件结构

主题文件

src/styles/
└── theme.js          # 主题配置(替代 _variables.scss)

组件文件(已转换)

src/page/
├── home.jsx          # ✅ 已转换为 Styled Components
├── device/
│   └── device.jsx    # ✅ 已转换为 Styled Components
├── ai-chat/
│   ├── ai-chat.jsx   # ✅ 已转换为 Styled Components
│   ├── input/
│   │   └── input.jsx # ✅ 已转换为 Styled Components
│   └── dialog/
│       └── dialog.jsx # ✅ 已转换为 Styled Components
├── process/
│   └── process.jsx   # ✅ 已转换为 Styled Components
├── screenshot/
│   └── screenshot.jsx # ✅ 已转换为 Styled Components
└── public/
    └── alert-view/
        └── alert-view.jsx # ✅ 已转换为 Styled Components

已删除的文件

  • ❌ 所有 .scss 文件已删除
  • src/styles/_variables.scss 已删除

🔄 主要变化

1. 主题系统

之前(SCSS)

// _variables.scss
$primary-color: #0769fb;
// component.scss
@import '../../styles/_variables';
.container {
  background-color: $primary-color;
}

现在(Styled Components)

// theme.js
export const theme = {
  colors: {
    primary: '#0769fb',
  }
}
// component.jsx
import styled from 'styled-components'

const Container = styled.div`
  background-color: ${props => props.theme.colors.primary};
`

2. 组件样式

之前(SCSS)

import './component.scss'

<div className="container">

现在(Styled Components)

import styled from 'styled-components'

const Container = styled.div`
  /* 样式 */
`

<Container>

3. 入口文件

之前

// index.jsx
<App />

现在

// index.jsx
<ThemeProvider theme={theme}>
  <App />
</ThemeProvider>

🚀 新功能优势

1. 动态样式(SCSS 无法实现)

现在可以根据 props 动态改变样式:

const Device = styled.div`
  background-color: ${props => 
    props.isActive ? '#28a745' : '#0769fb'
  };
`

<Device isActive={true}>设备</Device>

2. 主题切换(内置支持)

可以轻松切换主题:

const lightTheme = { colors: { primary: '#0769fb' } }
const darkTheme = { colors: { primary: '#4a9eff' } }

<ThemeProvider theme={isDark ? darkTheme : lightTheme}>
  <App />
</ThemeProvider>

3. 组件绑定(自动隔离)

样式和组件绑定,不会产生冲突:

const Button = styled.button`
  /* 样式自动隔离 */
`

4. TypeScript 支持(完美)

如果使用 TypeScript,可以获得完整的类型支持:

interface ButtonProps {
  variant?: 'primary' | 'danger';
}

const Button = styled.button<ButtonProps>`
  background: ${props => 
    props.variant === 'primary' ? '#0769fb' : '#dc3545'
  };
`

📝 使用指南

基本用法

import styled from 'styled-components'

const Container = styled.div`
  width: 100%;
  background-color: ${props => props.theme.colors.primary};
  padding: 16px;
  
  &:hover {
    background-color: ${props => props.theme.colors.secondary};
  }
`

function Component() {
  return <Container>内容</Container>
}

使用主题

所有组件自动可以访问主题:

const Button = styled.button`
  color: ${props => props.theme.colors.white};
  background: ${props => props.theme.colors.primary};
`

动态样式

const Card = styled.div`
  background: ${props => 
    props.isActive ? props.theme.colors.primary : '#f5f5f5'
  };
  transform: ${props => props.isActive ? 'scale(1.05)' : 'scale(1)'};
  transition: all 0.3s;
`

<Card isActive={isActive}>卡片</Card>

🎯 主题配置

当前主题配置在 src/styles/theme.js

export const theme = {
  colors: {
    primary: '#0769fb',
    aiChatBg: '#e0e8f4',
    processBg: '#07fb40',
    screenshotBg: 'rgba(251, 7, 7, 0.9)',
    white: '#fff',
    black: '#000',
  },
  sizes: {
    full: '100%',
    spacingBase: 0,
  },
  breakpoints: {
    sm: '576px',
    md: '768px',
    lg: '992px',
    xl: '1200px',
  },
}

添加新颜色

theme.js 中添加:

colors: {
  // ... 现有颜色
  success: '#28a745',
  danger: '#dc3545',
}

使用新颜色

const Button = styled.button`
  background: ${props => props.theme.colors.success};
`

⚠️ 注意事项

1. 性能

  • Styled Components 有运行时开销,但对于大多数应用可以忽略
  • 如果性能是首要考虑,可以考虑使用 CSS Modules + SCSS

2. 学习曲线

  • 需要学习 Styled Components 语法
  • 但功能更强大,值得学习

3. 调试

  • 浏览器中会看到生成的类名(如 sc-bdVaJa
  • 可以使用 babel-plugin-styled-components 获得更好的调试体验

🔧 可选优化

1. 安装 Babel 插件(更好的调试)

npm install -D babel-plugin-styled-components

2. 创建全局样式

import { createGlobalStyle } from 'styled-components'

const GlobalStyle = createGlobalStyle`
  * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
  }
  
  body {
    font-family: Arial, sans-serif;
  }
`

function App() {
  return (
    <>
      <GlobalStyle />
      <App />
    </>
  )
}

3. 创建混入(Mixin)

// styles/mixins.js
export const flexCenter = `
  display: flex;
  align-items: center;
  justify-content: center;
`

// 使用
const Container = styled.div`
  ${flexCenter}
`

📚 相关文档


✅ 迁移检查清单

  • 安装 styled-components
  • 创建主题文件(theme.js)
  • 转换所有组件为 Styled Components
  • 更新入口文件添加 ThemeProvider
  • 删除所有旧的 SCSS 文件
  • 测试项目运行正常

🎉 完成

项目已成功迁移到 Styled Components,现在可以使用:

  • ✅ 动态样式
  • ✅ 主题系统
  • ✅ 组件绑定
  • ✅ TypeScript 支持
  • ✅ 条件样式

享受功能最强大的 CSS 方案带来的便利!


迁移日期: 2025-01-30