Carbon设计系统 + Next.js 13 集成教程
这是一个完整的入门教程项目,旨在帮助开发者将IBM Carbon Design System与Next.js 13框架无缝集成。通过本教程,您将学习如何使用Carbon React组件构建现代化、企业级的Web应用程序。
功能特性
- Next.js 13 集成:完全兼容Next.js 13的App Router架构
- Carbon设计系统组件:使用IBM企业级React组件库
- 开发最佳实践:涵盖TypeScript配置、路径别名、ESLint代码规范
- 现代化开发体验:支持热重载、快速刷新等开发特性
- 灵活的项目结构:支持
src/目录结构和自定义路径映射 - 开箱即用的配置:提供完整的项目初始化和配置文件示例
安装指南
系统要求
- Node.js 16.8 或更高版本
- Yarn 或 npm 包管理器
创建新项目
使用Yarn创建Next.js 13应用程序:
yarn create next-app
按照提示进行配置:
- 项目名称:
next-base - TypeScript:根据需要选择
- ESLint:建议启用
- Tailwind CSS:不需要(将使用Carbon组件)
src/目录:建议启用- App Router:建议启用(使用最新路由系统)
- 默认导入别名:使用默认配置
配置路径别名
在项目根目录创建或修改 jsconfig.json 文件:
{
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"@/components/*": ["components/*"],
"@/app/*": ["app/*"]
}
}
}
启动开发服务器
cd carbon-tutorial-nextjs
yarn dev
使用说明
项目结构
配置完成后的标准项目结构:
carbon-tutorial-nextjs/
├── src/
│ ├── app/
│ │ ├── globals.css # 全局样式
│ │ ├── layout.js # 根布局组件
│ │ └── page.js # 首页组件
│ └── components/ # 自定义组件目录
├── public/ # 静态资源
├── jsconfig.json # 路径别名配置
└── next.config.js # Next.js配置文件
基础布局示例
根布局组件 (src/app/layout.js):
import './globals.css';
export const metadata = {
title: 'Carbon + Next13',
description: 'IBM Carbon Tutorial with NextJS 13',
};
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
首页组件示例
首页组件 (src/app/page.js):
import Image from 'next/image';
import styles from './page.module.css';
export default function Home() {
return (
<main className={styles.main}>
<div className={styles.description}>
<p>
Get started by editing
<code className={styles.code}>src/app/page.js</code>
</p>
<div>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
By{' '}
<Image
src="/vercel.svg"
alt="Vercel Logo"
className={styles.vercelLogo}
width={100}
height={24}
priority
/>
</a>
</div>
</div>
<div className={styles.center}>
<Image
className={styles.logo}
src="/next.svg"
alt="Next.js Logo"
width={180}
height={37}
priority
/>
</div>
<div className={styles.grid}>
<a
href="https://nextjs.org/docs"
className={styles.card}
target="_blank"
rel="noopener noreferrer"
>
<h2>
Docs <span>-></span>
</h2>
<p>Find in-depth information about Next.js features and API.</p>
</a>
<a
href="https://nextjs.org/learn"
className={styles.card}
target="_blank"
rel="noopener noreferrer"
>
<h2>
Learn <span>-></span>
</h2>
<p>Learn about Next.js in an interactive course with quizzes!</p>
</a>
<a
href="https://vercel.com/templates"
className={styles.card}
target="_blank"
rel="noopener noreferrer"
>
<h2>
Templates <span>-></span>
</h2>
<p>Explore the Next.js 13 playground.</p>
</a>
<a
href="https://vercel.com/new"
className={styles.card}
target="_blank"
rel="noopener noreferrer"
>
<h2>
Deploy <span>-></span>
</h2>
<p>
Instantly deploy your Next.js site to a shareable URL with Vercel.
</p>
</a>
</div>
</main>
);
}
核心代码
Next.js配置文件
next.config.js - Next.js核心配置文件,用于定义构建和运行时配置:
/** @type {import('next').NextConfig} */
const nextConfig = {};
module.exports = nextConfig;
路径别名配置
jsconfig.json - JavaScript项目的路径映射配置,简化模块导入路径:
{
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"@/components/*": ["components/*"],
"@/app/*": ["app/*"]
}
}
}
元数据配置
src/app/layout.js - 根布局组件中的元数据配置,用于SEO和页面信息管理:
export const metadata = {
title: 'Carbon + Next13',
description: 'IBM Carbon Tutorial with NextJS 13',
};
样式模块化
项目使用CSS Modules技术,每个组件的样式通过 page.module.css 文件进行模块化管理,避免样式冲突,同时保持组件样式的封装性。
EK0Gb+keL7bPh/cg/4npFgp2cZNSUXvlgjtWmsVLM7ywYE1fis7BegRvbfPi/ksH