项目已成功从 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 已删除// _variables.scss
$primary-color: #0769fb;
// component.scss
@import '../../styles/_variables';
.container {
background-color: $primary-color;
}
// 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};
`
import './component.scss'
<div className="container">
import styled from 'styled-components'
const Container = styled.div`
/* 样式 */
`
<Container>
// index.jsx
<App />
// index.jsx
<ThemeProvider theme={theme}>
<App />
</ThemeProvider>
现在可以根据 props 动态改变样式:
const Device = styled.div`
background-color: ${props =>
props.isActive ? '#28a745' : '#0769fb'
};
`
<Device isActive={true}>设备</Device>
可以轻松切换主题:
const lightTheme = { colors: { primary: '#0769fb' } }
const darkTheme = { colors: { primary: '#4a9eff' } }
<ThemeProvider theme={isDark ? darkTheme : lightTheme}>
<App />
</ThemeProvider>
样式和组件绑定,不会产生冲突:
const Button = styled.button`
/* 样式自动隔离 */
`
如果使用 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};
`
sc-bdVaJa)babel-plugin-styled-components 获得更好的调试体验npm install -D babel-plugin-styled-components
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 />
</>
)
}
// styles/mixins.js
export const flexCenter = `
display: flex;
align-items: center;
justify-content: center;
`
// 使用
const Container = styled.div`
${flexCenter}
`
项目已成功迁移到 Styled Components,现在可以使用:
享受功能最强大的 CSS 方案带来的便利!
迁移日期: 2025-01-30