docs: 重写 README,补充三种添加 item 的方式
- 项目简介、技术栈、快速开始、目录结构 - 新增 Item:首页导航卡片 / More 应用卡片 / 独立页面路由三种方式及字段说明 - 补充路由原理、样式约定与部署说明
This commit is contained in:
1 parent
9ce0c5704a
commit
7d0854127a
1 file changed
+193
-62
@@ -1,73 +1,204 @@
|
||||
# React + TypeScript + Vite
|
||||
# 筱周の网站 (XiaoZhou's Website)
|
||||
|
||||
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
|
||||
个人主页 —— 终端 / 黑客风格,黑底 + emerald 主题,等宽字体 Geist。
|
||||
|
||||
Currently, two official plugins are available:
|
||||
- 首页 `/`:视差 Hero、打字机标题、导航卡片,以及「关于我 / 项目展示 / 联系我」三个区块。
|
||||
- 更多 `/more`:应用聚合页,把自建的小工具集中到一个入口。
|
||||
|
||||
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react) uses [Oxc](https://oxc.rs)
|
||||
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/)
|
||||
## 技术栈
|
||||
|
||||
## React Compiler
|
||||
| 类别 | 选型 |
|
||||
| --- | --- |
|
||||
| 框架 | React 19 + TypeScript |
|
||||
| 构建 | Vite 8 |
|
||||
| 样式 | Tailwind CSS 4 |
|
||||
| 图标 | lucide-react |
|
||||
| 字体 | Geist Variable (`@fontsource-variable/geist`) |
|
||||
| 路由 | 自研轻量路由(零依赖,见 `src/router/`) |
|
||||
|
||||
The React Compiler is not enabled on this template because of its impact on dev & build performances. To add it, see [this documentation](https://react.dev/learn/react-compiler/installation).
|
||||
## 快速开始
|
||||
|
||||
## Expanding the ESLint configuration
|
||||
|
||||
If you are developing a production application, we recommend updating the configuration to enable type-aware lint rules:
|
||||
|
||||
```js
|
||||
export default defineConfig([
|
||||
globalIgnores(['dist']),
|
||||
{
|
||||
files: ['**/*.{ts,tsx}'],
|
||||
extends: [
|
||||
// Other configs...
|
||||
|
||||
// Remove tseslint.configs.recommended and replace with this
|
||||
tseslint.configs.recommendedTypeChecked,
|
||||
// Alternatively, use this for stricter rules
|
||||
tseslint.configs.strictTypeChecked,
|
||||
// Optionally, add this for stylistic rules
|
||||
tseslint.configs.stylisticTypeChecked,
|
||||
|
||||
// Other configs...
|
||||
],
|
||||
languageOptions: {
|
||||
parserOptions: {
|
||||
project: ['./tsconfig.node.json', './tsconfig.app.json'],
|
||||
tsconfigRootDir: import.meta.dirname,
|
||||
},
|
||||
// other options...
|
||||
},
|
||||
},
|
||||
])
|
||||
```bash
|
||||
npm install # 安装依赖
|
||||
npm run dev # 启动开发服务器(HMR)
|
||||
npm run build # 生产构建 + 后处理
|
||||
npm run preview # 本地预览构建产物
|
||||
npm run lint # ESLint 检查
|
||||
```
|
||||
|
||||
You can also install [eslint-plugin-react-x](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x) and [eslint-plugin-react-dom](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-dom) for React-specific lint rules:
|
||||
> 构建脚本为 `tsc -b && vite build && node scripts/postbuild.mjs`,最后一步会把
|
||||
> `dist/index.html` 复制成 `dist/more/index.html`,用于静态托管。
|
||||
|
||||
## 目录结构
|
||||
|
||||
```
|
||||
src/
|
||||
├── App.tsx 根组件:解析当前路由并渲染对应页面
|
||||
├── main.tsx React 挂载入口
|
||||
├── index.css Tailwind 入口 + 全局基础样式
|
||||
├── router/ 轻量路由
|
||||
│ ├── routes.ts 路由表(页面的唯一数据源)
|
||||
│ ├── history.ts normalizePath / navigate / subscribe
|
||||
│ ├── useRoute.ts 基于 useSyncExternalStore 的路由 hook
|
||||
│ ├── Link.tsx 站内跳转链接组件
|
||||
│ └── index.ts 统一导出
|
||||
├── components/
|
||||
│ ├── Banner.tsx 首页 Hero(背景 + 打字机 + 导航卡片)
|
||||
│ ├── NavCard.tsx 导航卡片
|
||||
│ ├── layout/ Header / Footer
|
||||
│ └── sections/ About / Projects / Contact 区块
|
||||
├── pages/
|
||||
│ ├── HomePage.tsx 首页
|
||||
│ └── MorePage.tsx /more 应用聚合页
|
||||
├── data/navigation.ts 首页导航卡片数据
|
||||
├── hooks/ useTypewriter / useParallax
|
||||
├── types/index.ts NavItem 类型
|
||||
└── assets/banner.png 全站背景图
|
||||
scripts/postbuild.mjs 构建后处理(生成 dist/more/index.html)
|
||||
```
|
||||
|
||||
## 添加 Item
|
||||
|
||||
有 3 种情况,按你想加的东西选择。
|
||||
|
||||
### 方式一:首页 Banner 上的导航卡片
|
||||
|
||||
编辑 `src/data/navigation.ts`,往 `NAV_ITEMS` 数组追加一项:
|
||||
|
||||
```ts
|
||||
import { LayoutDashboard, ListCollapse, BookOpen } from "lucide-react"; // 1. 引入图标
|
||||
import type { NavItem } from "@/types";
|
||||
|
||||
export const NAV_ITEMS: NavItem[] = [
|
||||
// ...已有项
|
||||
{
|
||||
name: "Blog", // 卡片标题
|
||||
desc: "我的博客", // 卡片副标题
|
||||
path: "https://blog.xiaozhou233.cn", // 链接(见下方说明)
|
||||
Icon: BookOpen, // lucide 图标组件
|
||||
color: "text-sky-400", // 图标颜色
|
||||
borderHover: // hover 时的边框 + 光晕
|
||||
"hover:border-sky-500/50 hover:shadow-[0_0_20px_rgba(56,189,248,0.2)]",
|
||||
},
|
||||
];
|
||||
```
|
||||
|
||||
字段说明:
|
||||
|
||||
| 字段 | 说明 |
|
||||
| --- | --- |
|
||||
| `name` | 卡片标题,同时作为 React `key`,**不要重复** |
|
||||
| `desc` | 卡片副标题 |
|
||||
| `path` | 以 `/` 开头 → 站内路由、当前页 SPA 跳转;其他(`http...`、`#`)→ 新标签页打开。由 `NavCard.tsx` 自动判断 |
|
||||
| `Icon` | 任意 `lucide-react` 图标组件 |
|
||||
| `color` | 图标 Tailwind 颜色类 |
|
||||
| `borderHover` | hover 的边框色 + 阴影类 |
|
||||
|
||||
> 卡片网格会自动排版,无需改动布局。
|
||||
|
||||
### 方式二:`/more` 页的应用卡片
|
||||
|
||||
编辑 `src/pages/MorePage.tsx`,需要动两个地方:图标定义和列表。
|
||||
|
||||
```ts
|
||||
// 1. 在 appIcons 里加图标(SVG 内容,通常从 lucide 复制 path)
|
||||
const appIcons = {
|
||||
water: <path d="M12 2.69l5.66 5.66a8 8 0 1 1-11.31 0z" />,
|
||||
atom: (<>...</>),
|
||||
book: <path d="M4 19.5A2.5 2.5 0 0 1 6.5 17H20" />, // 新增
|
||||
};
|
||||
|
||||
// 2. 在 apps 数组里追加一项
|
||||
const apps = [
|
||||
// ...已有项
|
||||
{
|
||||
name: "新应用",
|
||||
desc: "一句话描述",
|
||||
color: "#10b981", // 图标底色,渐变自动生成
|
||||
link: "https://example.com", // 跳转地址
|
||||
icon: appIcons.book, // 指向上面定义的图标
|
||||
},
|
||||
];
|
||||
```
|
||||
|
||||
| 字段 | 说明 |
|
||||
| --- | --- |
|
||||
| `name` | 应用名,同时作为 React `key`,**不要重复** |
|
||||
| `desc` | 一句话描述 |
|
||||
| `color` | 图标十六进制底色,卡片会自动生成同色渐变与光晕 |
|
||||
| `link` | 目标地址(站内相对路径或完整 URL) |
|
||||
| `icon` | 引用 `appIcons` 里的 SVG 片段 |
|
||||
|
||||
### 方式三:新增一个独立页面(路由)
|
||||
|
||||
如果卡片要指向站内新页面(例如 `/blog`):
|
||||
|
||||
**1. 新建页面组件**,如 `src/pages/BlogPage.tsx`。
|
||||
|
||||
**2. 注册路由**,在 `src/router/routes.ts` 的 `ROUTES` 追加:
|
||||
|
||||
```ts
|
||||
import BlogPage from "@/pages/BlogPage";
|
||||
|
||||
export const ROUTES: RouteDefinition[] = [
|
||||
// ...已有路由
|
||||
{
|
||||
name: "blog",
|
||||
path: "/blog",
|
||||
pattern: /^\/blog$/, // 路径匹配正则
|
||||
title: "博客 - 筱周の网站", // 会自动设置 document.title
|
||||
Component: BlogPage,
|
||||
},
|
||||
];
|
||||
```
|
||||
|
||||
**3.(可选)让该路径能被直接访问。** 目前 `scripts/postbuild.mjs` 只为 `/more`
|
||||
生成了静态入口。若希望直接打开 `https://你的域名/blog` 也能命中(而不是只从首页点进去),
|
||||
照抄一份:
|
||||
|
||||
```js
|
||||
// eslint.config.js
|
||||
import reactX from 'eslint-plugin-react-x'
|
||||
import reactDom from 'eslint-plugin-react-dom'
|
||||
|
||||
export default defineConfig([
|
||||
globalIgnores(['dist']),
|
||||
{
|
||||
files: ['**/*.{ts,tsx}'],
|
||||
extends: [
|
||||
// Other configs...
|
||||
// Enable lint rules for React
|
||||
reactX.configs['recommended-typescript'],
|
||||
// Enable lint rules for React DOM
|
||||
reactDom.configs.recommended,
|
||||
],
|
||||
languageOptions: {
|
||||
parserOptions: {
|
||||
project: ['./tsconfig.node.json', './tsconfig.app.json'],
|
||||
tsconfigRootDir: import.meta.dirname,
|
||||
},
|
||||
// other options...
|
||||
},
|
||||
},
|
||||
])
|
||||
const blogDir = resolve(root, "dist", "blog");
|
||||
mkdirSync(blogDir, { recursive: true });
|
||||
writeFileSync(resolve(blogDir, "index.html"), indexHtml);
|
||||
```
|
||||
|
||||
> 如果托管平台已配置 SPA history fallback(如 Cloudflare Pages / Netlify 的 rewrite),
|
||||
> 这一步可以省略。
|
||||
|
||||
## 路由
|
||||
|
||||
路由是自研的零依赖实现,核心思路:
|
||||
|
||||
- **路由表唯一数据源**:`src/router/routes.ts` 里的 `ROUTES`,每条包含 `path`、
|
||||
`pattern`、`title`、`Component`。加页面只改这里。
|
||||
- **路径归一化**:`normalizePath()` 把 `/more`、`/more/`、`/more/index.html`
|
||||
统一成 `/more`。
|
||||
- **响应式**:`useRoute()` 基于 `useSyncExternalStore`,监听 `popstate`,
|
||||
浏览器前进/后退正常工作。
|
||||
- **站内跳转**:用 `src/router/Link.tsx` 的 `<Link to="/more">` 做客户端跳转;
|
||||
中键 / Ctrl 点击等仍会走浏览器默认行为(新标签页)。
|
||||
- **兜底**:未知路径回落到首页(`FALLBACK_ROUTE`)。
|
||||
|
||||
## 样式约定
|
||||
|
||||
- 主色 `emerald`,背景 `black`,卡片统一使用玻璃拟态:
|
||||
`bg-white/5 border border-white/5 backdrop-blur-sm rounded-2xl`。
|
||||
- 强调/交互用 emerald 光晕:`hover:border-emerald-500/30 hover:shadow-[0_0_25px_rgba(52,211,153,0.1)]`。
|
||||
- 标题、终端提示符用 `font-mono`;正文描述用 `font-sans`(Geist)。
|
||||
- Tailwind 4 语法:渐变用 `bg-linear-to-r`,字体族通过 `@theme` 覆盖。
|
||||
|
||||
## 部署
|
||||
|
||||
```bash
|
||||
npm run build
|
||||
```
|
||||
|
||||
产物在 `dist/`,可直接托管到任意静态平台。注意构建会自动把
|
||||
`dist/index.html` 复制成 `dist/more/index.html`,保证 `/more` 可直达。
|
||||
|
||||
## 备注
|
||||
|
||||
- 全站背景图为 `src/assets/banner.png`,由 Vite 打包(带 hash)。
|
||||
- `index.html` 中接入了 umami 访问统计(`umami.xiaozhou233.cn`),如不需要可移除该 `<script>`。
|
||||
- 首页导航目前包含一个「Maintenance」占位项,原来的 Blog / Git / Jenkins / Workstation
|
||||
入口已临时下线。
|
||||
Reference in new issue
Block a user