MUI
Material Design 风格的 React 组件库,全球最受欢迎的 React UI 框架之一,提供从基础组件到高级数据表格的完整解决方案。
实现 Google Material Design 的 React 组件库,GitHub Stars 达 98,500+,npm 周下载量超 600 万次。多包架构涵盖核心组件、Data Grid、Charts 等,支持 sx prop 样式系统和完整的 TypeScript 类型定义。
MUI
Material Design 风格的 React 组件库,全球最受欢迎的 React UI 框架之一,提供从基础组件到高级数据表格的完整解决方案。
技术简介说明
MUI(原名 Material-UI)是一个实现了 Google Material Design 设计系统的 React 组件库。自 2014 年创建以来,它一直是 React 生态中最成熟的组件库之一,GitHub Stars 达到 98,500+,npm 周下载量超过 600 万次。MUI 不仅是对 Material Design 的忠实实现,更是一套可高度定制的通用组件系统,广泛应用于各类 Web 应用。
MUI 的核心优势在于其丰富的生态系统和灵活度。核心包 @mui/material 提供基础组件,@mui/x 提供高级功能(如 Data Grid、Charts、Date Picker),@mui/base 提供无样式的 headless 组件,@mui/joy 提供 Joy Design 风格组件。此外,MUI 还提供了零运行时的样式方案实验、与 Tailwind CSS v4 的集成支持,以及企业级的商业组件。
2025 年发布的 v7 带来了全面的 ESM 支持和 CSS Layer 集成,2026 年的 v9 则进一步优化了可访问性和性能。MUI 的持续演进使其在国际化产品、SaaS 应用、设计系统驱动的项目中始终保持强大的竞争力。
基本信息
- 官网:https://mui.com/
- GitHub:https://github.com/mui/material-ui
- License:MIT(核心包)/ 商业许可(MUI X Pro & Premium)
- 最新版本:v9.2.0(2026 年)
- 主要维护者:MUI Org(原 Material-UI 团队),商业公司运营
- npm 周下载量:约 6,000,000+
- TypeScript 支持:原生支持,完全类型化
快速上手
安装
# 核心包(Material UI)
npm install @mui/material @emotion/react @emotion/styled
# 使用 pnpm
pnpm add @mui/material @emotion/react @emotion/styled
# 可选:图标库
npm install @mui/icons-material
# 可选:字体(Roboto)
# 在 index.html 中引入:
# <link rel="preconnect" href="https://fonts.googleapis.com" />
# <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
# <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap" rel="stylesheet" />基础配置
// src/theme.ts
import { createTheme } from '@mui/material/styles';
const theme = createTheme({
palette: {
primary: {
main: '#1976d2',
},
secondary: {
main: '#dc004e',
},
mode: 'light', // 或 'dark'
},
typography: {
fontFamily: '"Roboto", "Helvetica", "Arial", sans-serif',
},
shape: {
borderRadius: 8,
},
});
export default theme;// src/App.tsx
import { ThemeProvider } from '@mui/material/styles';
import { CssBaseline } from '@mui/material';
import theme from './theme';
function App() {
return (
<ThemeProvider theme={theme}>
<CssBaseline />
<MyApplication />
</ThemeProvider>
);
}
export default App;最小示例
import React from 'react';
import Button from '@mui/material/Button';
import TextField from '@mui/material/TextField';
import { Stack } from '@mui/material';
const App: React.FC = () => (
<Stack spacing={2} sx={{ p: 4 }}>
<TextField label="用户名" variant="outlined" />
<Button variant="contained" color="primary">
登录
</Button>
</Stack>
);
export default App;核心概念与架构
多包架构
MUI 采用多包架构,各包职责清晰:
┌────────────────────────────────────────────────────────┐
│ MUI 生态系统 │
├────────────────┬───────────────┬────────────────────────┤
│ @mui/material │ @mui/x │ @mui/joy │
│ 核心 Material │ 高级组件 │ Joy Design 风格 │
│ Design 组件 │ Data Grid │ 组件(轻量友好) │
│ │ Charts │ │
│ │ Date Picker │ │
├────────────────┼───────────────┼────────────────────────┤
│ @mui/base │ @mui/system │ @mui/lab │
│ Headless 组件 │ 样式引擎 │ 实验性组件 │
│ 无样式无行为 │ sx prop │ 尚未稳定的组件 │
│ │ Theme │ │
├────────────────┼───────────────┼────────────────────────┤
│ @mui/utils │ @mui/material│ Toolpad │
│ 工具函数 │ -next │ 低代码平台 │
│ │ (零运行时实验) │ │
└────────────────┴───────────────┴────────────────────────┘
sx prop 与样式系统
sx prop 是 MUI 的核心样式能力,直接将主题令牌映射为 CSS:
<Box
sx={{
p: 2, // padding: theme.spacing(2)
bgcolor: 'primary.main', // backgroundColor: theme.palette.primary.main
borderRadius: 2, // borderRadius: theme.shape.borderRadius * 2
'&:hover': {
bgcolor: 'primary.dark',
},
// 响应式
width: { xs: '100%', sm: '50%', md: '33%' },
}}
>
内容
</Box>主题系统
const theme = createTheme({
// 调色板
palette: {
primary: { main: '#1976d2', light: '#42a5f5', dark: '#1565c0' },
secondary: { main: '#9c27b0' },
background: { default: '#fafafa', paper: '#fff' },
},
// 排版
typography: {
h1: { fontSize: '2.5rem', fontWeight: 700 },
body1: { fontSize: '1rem' },
},
// 间距
spacing: 8, // 基准间距
// 断点
breakpoints: {
values: { xs: 0, sm: 600, md: 900, lg: 1200, xl: 1536 },
},
// 组件级别覆盖
components: {
MuiButton: {
styleOverrides: {
root: { textTransform: 'none', borderRadius: 8 },
},
defaultProps: { variant: 'contained' },
},
},
});Slot 模式(v7+)
v7 完成了 Slot 模式的全面实现,提供标准化的组件内部结构修改能力:
// 使用 slots 自定义组件内部结构
<Tabs
slots={{
root: CustomTabsRoot,
indicator: CustomIndicator,
}}
slotProps={{
root: { className: 'my-tabs' },
indicator: { className: 'my-indicator' },
}}
>
{/* ... */}
</Tabs>核心特性
- Material Design 实现:Google Material Design 3 的完整 React 实现,支持最新的 Material You 设计语言
- 全面的 ESM 支持(v7+):完全支持 ES Module,与 Vite、Turbopack 等现代构建工具完美兼容
- CSS Layer 支持:通过
enableCssLayer配置支持 CSS 级联层,方便与 Tailwind CSS v4 等工具集成 - sx prop 样式系统:强大的样式 API,直接消费主题令牌,支持响应式和伪类
- 丰富的组件生态:核心组件 60+,加上 MUI X 提供 Data Grid、Charts 等高级组件
- TypeScript 全类型覆盖:所有组件和 API 均有完整类型定义
- 无障碍优先:组件遵循 WAI-ARIA 规范,内置键盘导航和焦点管理
- 服务端渲染支持:完整的 SSR 支持,内置 Next.js 集成指南
- 主题继承与嵌套:支持主题层级继承,可在应用不同区域应用不同主题
- Headless 基础组件(@mui/base):无样式的可访问组件,提供最大灵活性
- 暗色模式:内置明暗主题切换,配合
CssBaseline一键启用
生态图
关键包
| 包名 | 说明 | License |
|---|---|---|
@mui/material | 核心 Material Design 组件 | MIT |
@mui/x-data-grid | 高级数据表格 | MIT (Pro/Premium 商业) |
@mui/x-charts | 图表组件 | MIT (Pro 商业) |
@mui/x-date-pickers | 日期选择器 | MIT (Pro 商业) |
@mui/x-tree-view | 树形组件 | MIT (Pro 商业) |
@mui/base | Headless 无样式组件 | MIT |
@mui/joy | Joy Design 风格组件 | MIT |
@mui/system | 样式系统引擎 | MIT |
@mui/icons-material | Material Design 图标 | MIT |
@mui/lab | 实验性组件 | MIT |
@mui/utils | 工具函数 | MIT |
关键依赖
{
"@emotion/react": "^11.x", // CSS-in-JS 运行时
"@emotion/styled": "^11.x", // 组件样式化
"react": "^18.0 || ^19.0", // React 运行时
"@mui/material": "^9.x" // 核心组件
}社区生态
- Material-UI-dropzone:文件拖拽上传组件
- mui-tiptap:基于 MUI 的富文本编辑器
- notistack:Snackbar 通知管理
- react-admin:基于 MUI 的管理后台框架
- react-hook-form + MUI:表单集成方案
适用场景
- Material Design 风格应用:需要严格遵循 Material Design 规范的产品,特别是 Google 生态产品
- 国际化 SaaS 产品:Material Design 是全球认知度最高的设计系统之一,适合面向国际市场的产品
- 数据密集型应用:MUI X Data Grid 提供企业级的数据展示能力(排序、筛选、分组、虚拟滚动、行编辑)
- 需要丰富图表的仪表盘:MUI X Charts 提供折线图、柱状图、饼图等可视化组件
- 设计系统项目:强大的主题定制能力适合构建企业级设计系统
- Next.js 应用:完善的 SSR 支持和官方集成指南
- 低代码平台:Toolpad 提供基于 MUI 组件的低代码搭建能力
开发与工程化
开发流程
# 使用 create-react-app(不推荐,仅示例)
npx create-react-app my-app --template typescript
cd my-app
npm install @mui/material @emotion/react @emotion/styled
# 使用 Vite(推荐)
npm create vite@latest my-app -- --template react-ts
cd my-app
npm install @mui/material @emotion/react @emotion/styled
# 使用 Next.js
npx create-next-app@latest my-app --typescript --eslint
cd my-app
npm install @mui/material @emotion/react @emotion/styledNext.js 集成
// app/layout.tsx (Next.js App Router)
import { AppRouterCacheProvider } from '@mui/material-nextjs/v15-appRouter';
import { ThemeProvider } from '@mui/material/styles';
import theme from '../theme';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="zh-CN">
<body>
<AppRouterCacheProvider>
<ThemeProvider theme={theme}>
{children}
</ThemeProvider>
</AppRouterCacheProvider>
</body>
</html>
);
}TypeScript 配置
{
"compilerOptions": {
"strict": true,
"module": "ESNext",
"moduleResolution": "bundler"
}
}CI/CD 集成
# GitHub Actions 示例
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
- run: npm ci
- run: npm run typecheck
- run: npm run lint
- run: npm run build
- run: npm run test性能与安全
性能特点
- Tree Shaking:ES Module 架构确保未使用的组件不被打包
- v7 ESM 优化:完整的 ESM 支持消除了 CommonJS/ESM 混合加载的开销
- v9 sx prop 性能提升:样式计算引擎优化,sx prop 性能显著改善
- 懒加载组件:可通过
React.lazy按需加载重型组件 - Data Grid 虚拟滚动:大数据量表格默认支持行/列虚拟化
性能优化建议
// 1. 避免在渲染中创建样式对象
// ❌
<Box sx={{ mt: useMemo(() => spacing(2), [spacing]) }} />
// ✅
const boxSx = { mt: 2 };
<Box sx={boxSx} />
// 2. 使用 styled 代替内联 sx(频繁使用的组件)
const StyledCard = styled(Card)(({ theme }) => ({
padding: theme.spacing(2),
marginBottom: theme.spacing(2),
}));
// 3. Data Grid 虚拟滚动
<DataGrid
rows={largeDataSet}
columns={columns}
initialState={{ pagination: { paginationModel: { pageSize: 100 } } }}
/>
// 4. 使用 CSS Layer 避免样式优先级冲突
// mui.config.js
module.exports = {
enableCssLayer: true,
};安全注意事项
- XSS 防护:MUI 组件默认转义文本内容,避免使用
dangerouslySetInnerHTML - CSP 兼容:使用 Emotion 的 nonce 属性支持严格的 Content Security Policy
- 依赖审计:定期审计
@emotion/*等依赖版本
已知性能瓶颈
- Emotion CSS-in-JS 运行时开销:v9 的 sx prop 已优化,但大量动态样式仍有开销
- 首次加载体积:核心包 + Emotion 运行时约 150-250KB(gzipped)
- Data Grid 大数据量:10,000+ 行时需启用虚拟滚动
技术对比
| 特性 | MUI v9 | Ant Design v6 | Chakra UI v3 | Mantine v9 |
|---|---|---|---|---|
| 设计风格 | Material Design | 自研企业级 | 灵活无风格 | 现代简洁 |
| 样式方案 | Emotion CSS-in-JS | 内置 CSS-in-JS | Panda CSS(构建时) | CSS Modules |
| 运行时开销 | Emotion 运行时 | 内置 CSS-in-JS | 零运行时 | 零运行时 |
| 商业组件 | Data Grid / Charts(付费) | ProComponents(免费) | 无 | 无 |
| Headless 方案 | @mui/base | 无 | Ark UI | 无 |
| ESM 支持 | 完整(v7+) | 完整 | 完整 | 完整 |
| CSS Layer | 支持(opt-in) | 支持 | 原生支持 | 支持 |
| React Server Components | 部分支持 | 部分支持 | 部分支持 | 部分支持 |
| 低代码平台 | Toolpad | Ant Design Pro | 无 | 无 |
| 国际化 | 社区方案 | 70+ 语言包内置 | 社区方案 | 社区方案 |
| 适合场景 | 国际化产品/SaaS | 中后台系统 | 灵活项目 | 通用 Web |
| Bundle Size | 较大(含 Emotion) | 中等 | 较小 | 中等 |
选型建议
- 选 MUI:需要 Material Design 风格、国际市场产品、需要 Data Grid 等商业高级组件
- 选 Ant Design:中文市场、企业级中后台、需要免费的高级业务组件
- 选 Chakra UI:重视可访问性、零运行时性能、多框架需求
- 选 Mantine:丰富 hooks、轻量灵活、需要日程调度组件
最佳实践
生产环境建议
- 主题集中管理:将
createTheme配置独立为文件,通过ThemeProvider注入
// theme/index.ts - 分模块管理主题
import { createTheme } from '@mui/material/styles';
import { palette } from './palette';
import { typography } from './typography';
import { components } from './components';
export const theme = createTheme({
palette,
typography,
components,
spacing: 8,
});- 组件覆盖而非重写:利用主题
components配置全局修改组件默认样式
// theme/components.ts
export const components = {
MuiButton: {
styleOverrides: {
root: {
textTransform: 'none',
borderRadius: 8,
fontWeight: 600,
},
containedPrimary: {
boxShadow: '0 2px 8px rgba(25, 118, 210, 0.3)',
},
},
defaultProps: {
disableElevation: true,
},
},
};- 响应式断点使用:始终使用主题定义的断点值
// ✅ 使用断点 key
<Box sx={{ display: { xs: 'none', md: 'block' } }} />
// ❌ 硬编码像素值
<Box sx={{ display: 'none', '@media (min-width: 900px)': { display: 'block' } }} />- Data Grid 性能:大列表启用虚拟滚动,合理使用列 memo
常见陷阱
// ❌ 错误:在 ThemeProvider 外使用 useTheme
const MyComponent = () => {
const theme = useTheme(); // 如果没有 ThemeProvider 包裹,得到默认主题
return <Box sx={{ color: theme.palette.primary.main }} />;
};
// ✅ 正确:确保 ThemeProvider 在组件树顶层
<ThemeProvider theme={theme}>
<CssBaseline />
<App />
</ThemeProvider>
// ❌ 错误:每次渲染创建新主题
function App() {
return (
<ThemeProvider theme={createTheme({ palette: { primary: { main: '#1976d2' } } })}>
{/* 每次渲染都创建新主题,触发全量样式重算 */}
</ThemeProvider>
);
}
// ✅ 正确:主题提取到模块级
const theme = createTheme({ /* ... */ });
function App() {
return <ThemeProvider theme={theme}>{/* ... */}</ThemeProvider>;
}推荐模式
// 响应式布局模式
import { Grid, Card, CardContent, Typography } from '@mui/material';
<Grid container spacing={{ xs: 2, md: 3 }}>
{items.map(item => (
<Grid size={{ xs: 12, sm: 6, md: 4, lg: 3 }} key={item.id}>
<Card>
<CardContent>
<Typography variant="h6">{item.title}</Typography>
<Typography variant="body2" color="text.secondary">
{item.description}
</Typography>
</CardContent>
</Card>
</Grid>
))}
</Grid>技术局限与边界
已知限制
- Emotion 运行时依赖:核心方案依赖 Emotion 作为 CSS-in-JS 运行时,增加 Bundle 体积和运行时开销
- Bundle 体积较大:核心包 + Emotion 打包后约 150-250KB(gzipped),对于轻量应用偏重
- 商业组件收费:Data Grid Pro/Premium、Charts Pro 需要商业许可,小团队成本较高
- Material Design 风格限制:虽然可高度定制,但默认风格偏向 Material Design,偏离该风格需要较多覆盖工作
- 样式覆盖复杂度:深度自定义组件时,需要理解
classes覆盖、styled、sx多种方式的交互 - v7 → v9 升级频繁:版本迭代快,迁移成本需要考虑
迁移注意事项
- v5 → v7:移除 v5 中废弃的 API,ESM 导入路径可能需要调整
- v7 → v9:sx prop 行为微调,部分废弃 API 移除,建议参考官方迁移指南
- Emotion 迁移:如考虑从零运行时迁移,需评估替代方案
学习资源
官方文档
- MUI 官网 - 完整文档与示例
- Material UI 组件文档
- MUI X 文档 - Data Grid、Charts 等
- 主题定制指南
- 迁移指南
- MUI Base(Headless)文档
教程
- MUI 官方示例仓库 - 各种框架集成示例
- MUI YouTube 频道 - 视频教程
- Next.js + MUI 集成指南
- MUI Blog - 最新发布与更新
社区资源
- GitHub Discussions - 社区讨论
- Stack Overflow (material-ui tag) - 技术问答
- Discord - 实时交流
- MUI Store - 付费模板市场
2026 年现状
最新版本
- Material UI v9.2.0:当前最新稳定版,优化可访问性和 sx prop 性能
- MUI X v8:2025 年 4 月发布,兼容 Material UI v7+
- MUI Base:Headless 组件持续完善
发展趋势
- 零运行时样式实验:
@mui/material-nextjs包探索零运行时样式方案,减少对 Emotion 的依赖 - React Server Components 集成:逐步支持 RSC,减少客户端 JavaScript 体积
- CSS Layer 深度集成:与 Tailwind CSS v4 等现代 CSS 工具链协同
- AI 驱动开发:Toolpad 持续演进,降低应用开发门槛
- 可访问性持续优化:v9 专注于无障碍改进,包括新的
nativeButtonprop
社区活跃度
- GitHub Stars:98,500+
- npm 周下载量:6,000,000+
- GitHub Contributors:3,000+
- 商业公司运营模式,有稳定的全职团队维护
- 活跃的 Discord 社区
- 每周发布稳定更新
未来路线图
- 零运行时样式方案成熟并推广到核心包
- React Server Components 全面支持
- MUI X 组件持续增强(AI 数据表格、更多图表类型)
- Toolpad 低代码平台正式版
- 性能优化:减小 Bundle 体积,提升渲染效率
- 更好的 Tailwind CSS v4 集成