Next.js App Router 与 Tailwind v4
使用 Node 22、pnpm 9.15.0,并将 @ancrel 指向提供 0.2.0 的组织包源。以下命令从空目录建立 React 19 应用。
mkdir ac-next && cd ac-next
pnpm init
pnpm add -E next@16.2.6 react@19.1.1 react-dom@19.1.1
pnpm add -E @ancrel/tokens@0.2.0 @ancrel/ui@0.2.0 @ancrel/workspace@0.2.0 @ancrel/client-core@0.2.0
pnpm add -D tailwindcss@4.1.17 @tailwindcss/postcss@4.1.17 postcss@8.5.6
mkdir app
启用静态导出和 PostCSS
在根目录创建 next.config.mjs。
export default { output: 'export' }
同目录创建 postcss.config.mjs。
export default { plugins: { '@tailwindcss/postcss': {} } }
确定级联顺序
创建 app/globals.css。包内已有 ac.reset 和 ac.components,导入时直接使用原层名;额外添加 layer(ac) 会产生 ac.ac.*。v4 适配器的 @theme inline 让工具类在所在元素上解析变量。
@layer theme, base, ac, components, utilities;
@import "tailwindcss";
@import "@ancrel/tokens/css";
@import "@ancrel/tokens/tailwind-v4.css";
@import "@ancrel/ui/styles.css";
@import "@ancrel/workspace/styles.css";
在 app/layout.jsx 引入一次全局样式。服务端首帧使用暗色舒适密度。
import './globals.css'
export default function Layout({ children }) {
return <html lang="zh-CN" data-theme="dark" data-density="comfortable"><body>{children}</body></html>
}
添加可交互页面
创建 app/page.jsx。切换按钮需要客户端事件,预览区通过自己的 data-theme 保持浅色。
'use client'
import * as React from 'react'
import { Button, StatusBadge, Table } from '@ancrel/ui'
import { ActivityRail, WorkspaceShell } from '@ancrel/workspace'
export default function App() {
const rail = <ActivityRail aria-label="工作区" selectedId="build"
items={[{ id: 'build', label: '构建', icon: '◇', href: '#build' }]} />
return (
<WorkspaceShell rail={rail} titleBar="构建工作台">
<div id="build" className="bg-ac-panel p-ac-inset text-ac-ink-primary">
<Table caption="最近构建">
<tbody><tr><th scope="row">文档</th><td><StatusBadge status="success" label="通过" /></td></tr></tbody>
</Table>
<Button variant="primary" className="mt-4" onClick={() => {
const root = document.documentElement
root.dataset.theme = root.dataset.theme === 'dark' ? 'light' : 'dark'
}}>切换主题</Button>
<section data-theme="light" aria-label="浅色预览" className="bg-ac-panel p-ac-inset text-ac-ink-primary">
<Button variant="primary">浅色预览</Button>
</section>
</div>
</WorkspaceShell>
)
}
pnpm exec next build
pnpm dlx serve@14 out
打开终端显示的本地地址,刷新后应再次看到暗色工作台和浅色预览。
检查文件变更
在构建结果旁显示服务器返回的统一差异。组件样式来自前面引入的 workspace 样式表。HTTP 或 SSE 数据可由宿主通过 @ancrel/client-core 取得。
import { UnifiedDiffBlock } from '@ancrel/workspace/diff'
export function ChangedFile({ patch }) {
return <UnifiedDiffBlock label="构建配置变更" diff={patch} />
}
验收
WorkspaceShell API reference
Props
| Prop | Type | Required | Default |
|---|---|---|---|
children | ReactNode | Yes | — |
className | string | No | — |
defaultNarrowPane | ShellPane | No | — |
density | Density | No | — |
inspector | ReactNode | No | — |
key | React.Key | null | undefined | No | — |
labels | Partial<{
showSidebar: string;
showInspector: string;
backToMain: string;
}> | No | — |
mainAs | 'main' | 'div' | No | — |
narrowBreakpoint | number | No | — |
narrowPane | ShellPane | No | — |
onNarrowPaneChange | ((pane: ShellPane) => void) | undefined | No | — |
rail | ReactNode | No | — |
ref | React.Ref<HTMLDivElement> | undefined | No | — |
sidebar | ReactNode | No | — |
split | Pick<ResizableSplitProps, 'leftConfig' | 'rightConfig' | 'minCenterWidth' | 'persistKey' | 'storage' | 'labels' | 'onWidthsChange'> | No | — |
statusBar | ReactNode | No | — |
theme | Theme | No | — |
titleBar | ReactNode | No | — |