spartan
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesespartan/ui
spartan/ui
spartan/ui is an Angular UI library. It uses a two-layer architecture:
- Brain () - accessible, unstyled primitives (Angular directives/components), installed from npm. This is the behavior and accessibility layer.
@spartan-ng/brain - Helm () - the styled layer (Tailwind + class-variance-authority). Helm code is copied into the user's project by the CLI so they own and can customize it.
@spartan-ng/helm
You compose Helm directives/components onto host elements; Helm wires up the matching Brain
primitive under the hood. Always prefer existing components over hand-written markup.
All CLI commands run through the workspace's runner. Detect it from the project:
- Nx workspace (has ):
nx.json(ornpx nx g @spartan-ng/cli:<generator>).pnpm nx g ... - Angular CLI workspace (has , no
angular.json):nx.json.ng g @spartan-ng/cli:<generator>
spartan/ui是一款Angular UI库,采用双层架构:
- Brain ()——可访问、无样式的原语(Angular指令/组件),从npm安装。这是负责行为与可访问性的层。
@spartan-ng/brain - Helm ()——样式化层(基于Tailwind + class-variance-authority)。Helm代码会通过CLI复制到用户项目中,以便用户拥有并自定义它。
@spartan-ng/helm
你可以将Helm指令/组件组合到宿主元素上;Helm会在底层关联匹配的Brain原语。优先使用现有组件而非手写标记。
所有CLI命令通过工作区的运行器执行,可从项目中检测运行器类型:
- Nx工作区(包含):
nx.json(或npx nx g @spartan-ng/cli:<generator>)。pnpm nx g ... - Angular CLI工作区(包含,无
angular.json):nx.json。ng g @spartan-ng/cli:<generator>
Current project context
当前项目上下文
Before generating any code, gather the project context:
bash
npx nx g @spartan-ng/cli:info --json # Nx
ng g @spartan-ng/cli:info --json # Angular CLIThis is read-only and prints JSON with:
- -
workspaceType|nx(decides which runner to use above).angular-cli - - where Helm components are copied (e.g.
config.componentsPath).libs/ui - - the import prefix for Helm, default
config.importAlias.@spartan-ng/helm - -
config.generateAs|library(Nx layout choice).entrypoint - - Angular, Angular CDK, Tailwind,
versions,@spartan-ng/brain.@spartan-ng/cli - -
iconLibrarywhen present.@ng-icons - - the global stylesheet that imports the preset.
tailwindCssFile - - components already present (do not re-add these).
installedComponents - - everything the CLI can generate.
availableComponents
If does not exist, the project is not set up yet - run
first (it installs dependencies and the theme). itself is created when you add the
first component with (see ).
components.json@spartan-ng/cli:initcomponents.jsonuicli.md在生成任何代码之前,先收集项目上下文:
bash
npx nx g @spartan-ng/cli:info --json # Nx
ng g @spartan-ng/cli:info --json # Angular CLI这是只读操作,会输出包含以下内容的JSON:
- ——
workspaceType|nx(决定使用上述哪种运行器)。angular-cli - ——Helm组件的复制路径(例如
config.componentsPath)。libs/ui - ——Helm的导入前缀,默认为
config.importAlias。@spartan-ng/helm - ——
config.generateAs|library(Nx布局选项)。entrypoint - ——Angular、Angular CDK、Tailwind、
versions、@spartan-ng/brain的版本。@spartan-ng/cli - ——当存在时为
iconLibrary。@ng-icons - ——导入预设的全局样式表。
tailwindCssFile - ——已存在的组件(请勿重复添加)。
installedComponents - ——CLI可生成的所有组件。
availableComponents
如果不存在,说明项目尚未设置——先运行(它会安装依赖项和主题)。当你首次使用添加组件时,会创建(详见)。
components.json@spartan-ng/cli:inituicomponents.jsoncli.mdPrinciples
原则
- Use existing components first. Check , then
installedComponents. Find docs via the MCP server (availableComponents/spartan_components_list) or the live docs atspartan_components_get. Seehttps://www.spartan.ng/components/<name>.mcp.md - Compose, do not reinvent. Build dashboards, forms, and dialogs from existing Helm + Brain pieces rather than custom markup.
- Use built-in variants before custom styles. Buttons, badges, alerts, etc. ship and
variantinputs - use them instead of overriding classes.size - Use semantic colors, never raw values. , not
bg-primary text-primary-foreground. Seebg-blue-500.rules/styling.md
- 优先使用现有组件。先检查,再查看
installedComponents。可通过MCP服务器(availableComponents/spartan_components_list)或在线文档spartan_components_get查找文档。详见https://www.spartan.ng/components/<name>。mcp.md - 组合而非重造。使用现有Helm + Brain组件构建仪表板、表单和对话框,而非自定义标记。
- 优先使用内置变体而非自定义样式。按钮、徽章、警告等组件提供和
variant输入——优先使用这些,而非覆盖类。size - 使用语义化颜色,绝不使用原始值。例如使用,而非
bg-primary text-primary-foreground。详见bg-blue-500。rules/styling.md
Critical rules
关键规则
Read the rule file before doing the related work:
- - the
rules/styling.mdutil, semantic color tokens, layout-only classes,hlm()overgap-*,space-*, dark mode, no manual z-index on overlays.size-* - - compose forms with
rules/forms.md(label, control, error, description) andhlmField/hlmFieldSeton nativehlmFieldLegend/<fieldset>; option sets (2-7 choices) use<legend>.hlm-toggle-group - - items belong inside their group; dialogs/sheets need a title; full Card composition; tabs triggers inside
rules/composition.md; avatar always has a fallback; usehlm-tabs-list/Alert/Empty/Skeleton/Badge/Separatorinstead of custom markup.Spinner - - icons are
rules/icons.md; register with<ng-icon name="lucide...">; no manual sizing classes inside components - use theprovideIconsinput.size - - the two-layer model (one headless library, Brain, plus the styled Helm layer); when to reach for Brain directly vs Helm, and how composition works via directives.
rules/brain-vs-helm.md - - every generator (
cli.md,init,ui,ui-theme,healthcheck,info), with Nx and Angular CLI invocations.migrate-* - - the Brain-npm + Helm-copy-in distribution model,
registry.md, and the fixed component catalog (the shipped CLI uses no remote or custom registry).components.json - - theming via the
customization.md, CSS variables, thehlm-tailwind-preset.cssgenerator, and extending copied Helm components.ui-theme - - using the
mcp.mdtools, resources, and prompts for discovery.@spartan-ng/mcp
在进行相关工作前,请阅读规则文件:
- ——
rules/styling.md工具、语义化颜色令牌、仅布局类、优先使用hlm()而非gap-*、space-*、深色模式、不在覆盖层上手动设置z-index。size-* - ——使用
rules/forms.md(标签、控件、错误信息、描述)和hlmField/hlmFieldSet组合表单,原生hlmFieldLegend/<fieldset>需搭配使用;选项集(2-7个选项)使用<legend>。hlm-toggle-group - ——元素需放在对应组内;对话框/侧边栏需要标题;完整的Card组合;标签触发器需放在
rules/composition.md内;头像必须有回退方案;使用hlm-tabs-list/Alert/Empty/Skeleton/Badge/Separator而非自定义标记。Spinner - ——图标使用
rules/icons.md;通过<ng-icon name="lucide...">注册;组件内不使用手动尺寸类——使用provideIcons输入。size - ——双层模型(一个无头库Brain,加上样式化的Helm层);何时直接使用Brain而非Helm,以及通过指令实现组合的方式。
rules/brain-vs-helm.md - ——所有生成器(
cli.md、init、ui、ui-theme、healthcheck、info),包含Nx和Angular CLI的调用方式。migrate-* - ——Brain(npm安装)+ Helm(复制到项目)的分发模型、
registry.md,以及固定的组件目录(发布的CLI不使用远程或自定义注册表)。components.json - ——通过
customization.md、CSS变量、hlm-tailwind-preset.css生成器进行主题定制,以及扩展复制的Helm组件。ui-theme - ——使用
mcp.md工具、资源和提示进行组件探索。@spartan-ng/mcp
Key patterns
核心模式
html
<!-- Buttons: use variant/size inputs, not custom classes -->
<button hlmBtn variant="destructive" size="lg">Delete</button>
<!-- Icon in a button: ng-icon -->
<button hlmBtn size="icon" variant="ghost">
<ng-icon name="lucideTrash" />
</button>
<!-- Loading state: compose a spinner, there is no isLoading input -->
<button hlmBtn [disabled]="loading()">
@if (loading()) {
<hlm-spinner />
} Save
</button>
<!-- Layout: gap, not space-* ; size-* when width == height -->
<div class="flex items-center gap-2">
<span hlmBadge variant="secondary">beta</span>
</div>html
<!-- 按钮:使用variant/size输入,而非自定义类 -->
<button hlmBtn variant="destructive" size="lg">删除</button>
<!-- 按钮内的图标:使用ng-icon -->
<button hlmBtn size="icon" variant="ghost">
<ng-icon name="lucideTrash" />
</button>
<!-- 加载状态:组合spinner,没有isLoading输入 -->
<button hlmBtn [disabled]="loading()">
@if (loading()) {
<hlm-spinner />
} 保存
</button>
<!-- 布局:使用gap,而非space-* ; 宽高相等时使用size-* -->
<div class="flex items-center gap-2">
<span hlmBadge variant="secondary">测试版</span>
</div>Component selection
组件选择
| Need | Component(s) |
|---|---|
| Action / button | |
| Text/number input | |
| Choice input | |
| Toggle 2-7 options | |
| Form layout/validation | |
| Data display | |
| Navigation | |
| Overlays | |
| Menus | |
| Feedback | |
| Layout/containers | |
| Empty states | |
| Dates | |
| Icons | |
| Typography | |
| 需求 | 组件 |
|---|---|
| 操作 / 按钮 | |
| 文本/数字输入 | |
| 选择输入 | |
| 切换2-7个选项 | |
| 表单布局/验证 | |
| 数据展示 | |
| 导航 | |
| 覆盖层 | |
| 菜单 | |
| 反馈 | |
| 布局/容器 | |
| 空状态 | |
| 日期相关 | |
| 图标 | |
| 排版 | |
Workflow
工作流程
- Get context. Run . If the project is not set up, run
@spartan-ng/cli:info --json, then add components with:init(the first:uirun creates:ui).components.json - Check what is installed. Do not re-add anything in .
installedComponents - Find the component. Use the MCP tools or for the API and examples (
https://www.spartan.ng/components/<name>). Never guess selectors - confirm them.mcp.md - Add it. (Nx) or
npx nx g @spartan-ng/cli:ui --name=<component>(Angular CLI). This installs the Brain dependency and copies the Helm code. Omitng g @spartan-ng/cli:ui --name=<component>to get an interactive multiselect.--name - Compose correctly. Import the const (e.g.
*Imports) or the individual classes from the import alias, add them to the standalone component'sHlmDialogImports, and follow the composition rules.imports - Register icons. Any you use must be passed to
<ng-icon>(seeprovideIcons(...)).rules/icons.md - Verify. After bigger changes or upgrades, run to catch deprecated patterns and fix imports.
@spartan-ng/cli:healthcheck - Theme/customize. Edit the copied Helm files and the CSS variables; do not fork Brain.
- 获取上下文。运行。如果项目未设置,运行
@spartan-ng/cli:info --json,然后使用:init添加组件(首次运行:ui会创建:ui)。components.json - 检查已安装组件。请勿重复添加中的任何组件。
installedComponents - 查找组件。使用MCP工具或查看API和示例(详见
https://www.spartan.ng/components/<name>)。切勿猜测选择器——务必确认。mcp.md - 添加组件。Nx项目使用,Angular CLI项目使用
npx nx g @spartan-ng/cli:ui --name=<component>。这会安装Brain依赖并复制Helm代码。省略ng g @spartan-ng/cli:ui --name=<component>可进入交互式多选模式。--name - 正确组合。导入常量(例如
*Imports)或从导入别名导入单个类,将其添加到独立组件的HlmDialogImports中,并遵循组合规则。imports - 注册图标。所有使用的必须传入
<ng-icon>(详见provideIcons(...))。rules/icons.md - 验证。在较大改动或升级后,运行以检测已弃用的模式并修复导入。
@spartan-ng/cli:healthcheck - 主题/自定义。编辑复制的Helm文件和CSS变量;请勿fork Brain。
Quick reference
快速参考
bash
undefinedbash
undefinedInitialize (creates components.json, wires Tailwind + preset)
初始化(创建components.json,配置Tailwind + 预设)
npx nx g @spartan-ng/cli:init
ng g @spartan-ng/cli:init
npx nx g @spartan-ng/cli:init
ng g @spartan-ng/cli:init
Project context as JSON
以JSON格式获取项目上下文
npx nx g @spartan-ng/cli:info --json
npx nx g @spartan-ng/cli:info --json
Add components (interactive, or pass --name)
添加组件(交互式,或传入--name指定)
npx nx g @spartan-ng/cli:ui
npx nx g @spartan-ng/cli:ui --name=dialog
npx nx g @spartan-ng/cli:ui
npx nx g @spartan-ng/cli:ui --name=dialog
Generate theme variables
生成主题变量
npx nx g @spartan-ng/cli:ui-theme
npx nx g @spartan-ng/cli:ui-theme
Scan + auto-fix deprecated APIs/imports after an upgrade
升级后扫描并自动修复已弃用的API/导入
npx nx g @spartan-ng/cli:healthcheck --autoFix
undefinednpx nx g @spartan-ng/cli:healthcheck --autoFix
undefined