安装
安装锁定版本的软件包,选择源码或预编译 CSS,并验证第一个组件。
本页内容
安装 @master/ui,加载其样式表,再渲染带有组件类的原生元素。如果项目使用 Master CSS,请将软件包的源码与应用样式一起编译。其他项目可使用预编译样式表,其中只包含组件样式,不含 Master CSS 工具类。
加载组件样式的两种方式
任选一种方式在页面中加载样式。 导入源码时,应用的 Master CSS 编译器会同时处理样式源码和用于生成工具类的类名。包中的 standalone.css 已经编译完成,可以直接加载。两种方式都提供组件样式。使用独立样式表时,请自行编写布局 CSS。
#用 AI 编程智能体配置
编程智能体可以代为完成配置。在应用根目录启动编程智能体,并粘贴下方的提示词。智能体会先检查项目,并在修改文件前询问如何加载样式表、导入范围、如何配置智能体、使用哪套图标,以及如何处理现有组件库。随后,它会安装锁定版本的包,导入样式表,再在浏览器中验证第一个组件,并运行 master-ui check 检查其标记。
You are working in an existing web application. Set up Master UI end to end. Documentation: https://ui.master.co/guide/installation.md First inspect the project before editing: package manager, framework and build tool, whether Master CSS is set up (the installed @master/css version, and the app stylesheet that contains @import '@master/css' and where it is loaded), any component library or styling system already in use, React, Svelte or Vue, an existing master-ui.json, the dev/lint/build/test commands, and agent guidance files such as AGENTS.md. Before editing, ask me these and wait for the answers. Do not choose them yourself: - Stylesheet route: source (the default; @import '@master/ui' compiled by Master CSS 2.1 or later, with utilities for layout) or precompiled (@master/ui/standalone.css, no Master CSS and no utilities). If the project has no Master CSS and I choose source, set it up first by following https://css.master.co/guide/installation. - Import scope: the full library in one import (the default); npx master-ui init, which writes ui/index.css with one @import per stylesheet and master-ui.json for components the project may later own; or, for an app that loads styles on demand, one module import per stylesheet (import '@master/ui/components/<name>.css' in the module that renders the component). A project takes exactly one of these: never combine them, and never @import a single component stylesheet from CSS. If a dependency built on Master UI already imports its stylesheets from modules, the app must use module imports too. See https://ui.master.co/guide/installation#one-route. - Agent setup: the full setup (the master-ui skill, Master UI guidance in AGENTS.md and a check:ui script) or minimal (package and stylesheet only). - Existing components: if the project already uses another component library, whether to leave it in place (the default) and use Master UI only for new interface, or replace the components I name. Never replace components I have not named. - Icons: Master Icons (the default; the drawings Master UI's own components and examples use), the icon library the project already has, or none for now. Never replace icons I have not named. Then adopt Master UI conservatively: 1. Run every command from the app root, where its package.json lives, with the project's package manager. 2. Install the package at an exact version: npm install @master/ui --save-exact (bun add @master/ui --exact, pnpm add @master/ui --save-exact). A minor or patch release keeps every public name but can change how a component looks, so keep the version pinned and review each upgrade. 3. Load the stylesheet in the stylesheet the app already loads. Source route: add @import '@master/ui'; on the line after @import '@master/css';, or, with npx master-ui init, @import './ui/index.css'; in its place (adjust the path to where ui/ is), or, with module imports, @import '@master/ui/theme.css'; in its place and import '@master/ui/components/<name>.css' in each module that renders that component. Precompiled route: @import '@master/ui/base.css'; then @import '@master/ui/standalone.css';, and omit base.css if the project already has an equivalent reset. Installing the package alone does not load anything. Then set the page's viewport as https://ui.master.co/guide/installation#set-the-viewport shows: the viewport tag and the short script after it that stops iOS zooming onto a focused field. For module imports, put <style>@layer theme, base, defaults, components, utilities;</style> in the document head before any stylesheet. 4. Add no behavior script and no framework component yet. When a pattern that needs one is used later, follow https://ui.master.co/guide/behaviors, or https://ui.master.co/guide/frameworks with npx master-ui add <name> in React, Svelte or Vue. 5. If I chose Master Icons: install exactly one package at an exact version, for the framework the app uses: @master/icons-react, -vue, -svelte, -preact or -astro, or @master/icons for plain HTML. Never invent an icon name: look it up with node node_modules/@master/icons/skills/master-icons/scripts/search.mjs "<words>". Pick the drawing by the size the component gives the icon (16 up to 17px, 20 for 18 to 22px, 24 above), import that native drawing (@master/icons-react/native/16/soft/search) and put class "icon" on it. Never set stroke-width in CSS: choose a strokeWidth on a fixed entry instead. Keep the accessible name on the control, not the icon. See https://ui.master.co/components/icon.md#master-icons. 6. If I chose the full agent setup: install the skill with npx skills add master-co/ui --skill master-ui (and, with Master Icons, copy node_modules/@master/icons/skills/master-icons into the agent's skills folder), append the Master UI block from https://ui.master.co/guide/ai.md#agents-md to AGENTS.md with the project's actual validation commands below it, and add "check:ui": "master-ui check --strict" to the package.json scripts. 7. If I chose to replace named components, rewrite each one's markup from its contract (npx master-ui contract <name>), keep its behavior and accessible names, and remove the old dependency only after nothing uses it. 8. Preserve the existing architecture, styling and component library unless I chose otherwise, and avoid unrelated refactors. 9. Verify: start the dev server and render <button type="button" class="btn btn-primary">Save changes</button> on a visible page (className in JSX). Confirm an ink face in light mode, a pale face in dark mode and a visible focus ring when it is reached with Tab. If it looks like an unstyled browser button, confirm the page loads the stylesheet and the compiled CSS contains .btn. With Master Icons, put a 16px icon in the button and confirm its stroke is 1px. Run npx master-ui check on the file that holds the button and fix what it reports. Then remove the button unless I want to keep it, and run the project's lint, build and tests. When finished, summarize the changed files, the installed version, the route and options chosen, behavior intentionally left unchanged, the validation results, and anything not verified in a browser.
#配置 Master CSS
如果要使用源码样式表,请先配置 Master CSS 2.1 或更高版本,以及与所用框架对应的集成。请参阅 Master CSS 安装指南,按 Vite、Astro、Next.js、Svelte 等对应的步骤操作。
#安装包
npm install @master/ui --save-exact可以使用任意包管理器,例如 bun add @master/ui --exact 或 pnpm add @master/ui --save-exact。次版本和修订版本会保留所有公开名称,但仍可能改变组件的外观。请锁定版本,并在需要时主动升级。稳定性列出了升级后要运行的命令。
#导入样式表
在应用的样式表中,先导入 Master CSS 预设,再导入 Master UI:
@import '@master/css';@import '@master/ui';预设声明了层叠层顺序 theme, base, defaults, components, utilities。Master UI 将规则放入 components 层内的 components.ui 层,因此标记中的工具类仍然优先,你写在 @layer components 中的规则也是如此。
#设置视口
输入框在任何指针设备上都保持相同的字号:大多是 14px,与界面其余部分一致。在 iOS 上,字号小于 16px 的输入框获得焦点时,页面会放大到该输入框,之后也不会缩回。限制视口的最大缩放比例可以阻止这种放大,而且无论视口如何设置,iOS 仍允许读者双指缩放。Android 的浏览器会遵守这个限制,连双指缩放也一并禁用,因此页面只在支持 -webkit-touch-callout 的 iOS 上设置它:
<meta name="viewport" content="width=device-width, initial-scale=1"><script> if (CSS.supports('-webkit-touch-callout', 'none')) { const viewport = document.querySelector('meta[name="viewport"]') if (viewport) viewport.content += ', maximum-scale=1' }</script>保留框架已经写入的 viewport 标签,把脚本放在它之后,例如 <head> 的末尾或 <body> 的开头:只要在读者能够聚焦输入框之前运行即可。没有这段脚本一切照常工作,只是 iOS 会在输入框获得焦点时放大页面。不要改为在触摸屏上把输入框字号提高到 16px:那样它就会与同一行的按钮和下拉选择框不一致。
#验证安装
确认应用已加载 app.css,然后在实际渲染的页面中添加一个按钮。在 JSX 中,用 className 代替 class:
<button type="button" class="btn btn-primary">Save changes</button>使用项目现有的开发命令运行应用。按钮在浅色模式下应呈墨色,在深色模式下应呈浅色,通过键盘聚焦时应显示清晰可见的焦点环。按钮样式无需行为脚本,保存更改仍由应用的操作处理函数负责。
如果按钮看起来仍是浏览器的默认样式,请确认页面已加载 app.css,且编译产物中包含 .btn。如果导入的是源码,请确认 Master CSS 集成处理了这份样式表。然后检查标记。
#按需导入
每个样式表也可以单独导入,供随模块按需加载样式的应用使用。将共享主题保留在应用的样式表中,再从渲染组件的模块导入各个组件:
@import '@master/css';@import '@master/ui/theme.css';import '@master/ui/components/button.css'import '@master/ui/components/spinner.css'在文档的 <head> 中,在所有样式表之前声明层顺序。路由可能比 app.css 更早加载组件 CSS。如果先声明 components,预设样式表的 base 就会排在后面,让重置规则覆盖组件样式。
<style>@layer theme, base, defaults, components, utilities;</style>无论有多少模块导入同一个模块,打包工具都只加载一次,因此两个模块都导入 button.css 时,它只会输出一次。请导入标记中所有类名对应的样式表。例如,按钮中包含 icon 或 spinner 时,也需要导入对应组件的样式表。完整导入可以省去手动维护这份列表的工作。
这些导入一律写成模块导入,不要写成 CSS @import 规则,也不要加入已完整导入库的项目;原因见只选一种方式。
#每个样式表单独导入
包中附带一个小型命令行工具。init 会生成 ui/index.css 和 master-ui.json,前者逐行导入主题和各个样式表,后者记录项目自行维护的内容:
npx master-ui init@import '@master/css';@import './ui/index.css';可以用这个入口文件代替完整的 @master/ui 导入。此后它就是项目中库的唯一来源,也是唯一导入库中其他样式表的样式表。这种方式并非必需;如果想自行维护导入列表或自行维护部分组件源码,再选用即可。只有当标记不再使用某一行导入的类名时,才删除该行。
#只选一种方式
CSS 的 @import 是把文件内容包含进来,而不是加载模块。同一个样式表如果被两个文件导入,或者随完整的库加载后又被单独导入,就会在输出中出现两次:字节重复,而且哪一份排在后面会因页面而异。模块导入则只加载一次。因此,项目应选定一种方式并始终使用:
| 项目 | 方式 |
|---|---|
| 应用 | 在一个样式表中写 @import '@master/ui';,或用 ui/index.css 代替。项目中没有其他地方导入库的样式表 |
| 按需加载样式的应用 | 每个样式表使用一个模块导入:import '@master/ui/components/<name>.css' |
| 基于 Master UI 构建的库 | 每个样式表使用一个模块导入,由库自己的模块导入 |
| 使用这类库的应用 | 同样是每个样式表使用一个模块导入。库的导入已在打包结果中,完整导入会让那些样式表再输出一次 |
- 同一个项目不能混用两种方式。 完整导入与单个样式表的模块导入并存时,该样式表会输出两次。
- 不能用 CSS
@import导入单个组件。 项目中的样式表和<style>块都不导入@master/ui/components/<name>.css。只有入口文件可以这样做:包自身的入口,或init生成的ui/index.css。 - 你自己的规则不依赖打包结果的顺序。 构建不保证 CSS 的顺序与模块导入的顺序一致,也不需要保证:库的规则位于
components.ui,因此无论组件样式表落在何处,你写在@layer components中的规则都优先于组件,无需加长选择器。自定义说明了这个层带来的结果。
项目偏离所选方式时,npx master-ui check 会报告 stylesheet-import、mixed-imports 或 repeated-import;契约说明了每个代码。
#行为脚本
大多数组件无需脚本。组合框、命令面板和日历等少数交互模式没有对应的原生元素,包中为每种模式提供了参考脚本:
import { attach } from '@master/ui/behaviors'const detach = attach()等标记出现在 DOM 中后,在浏览器端代码中调用 attach()。它会为当时识别到的交互模式绑定行为,并返回清理函数。移除这些标记前,请调用 detach()。它不会监听之后插入的标记,也不会配置上下文菜单的触发目标。有关如何限定绑定范围和使用各个函数,请参阅行为。在 React、Svelte 或 Vue 中,请使用框架组件管理这些行为的生命周期。
#在未使用 Master CSS 的项目中安装
包中包含已编译为普通 CSS 的组件。只要打包工具支持导入 CSS,就可以使用已安装的锁定版本:
@import '@master/ui/base.css';@import '@master/ui/standalone.css';base.css 提供预设的重置样式,其中包括 box-sizing: border-box 和表单控件的字体设置。如果项目已提供等效的重置样式,可以省略这项导入。
对于静态页面,将已安装包中的编译产物复制到服务器对外提供的目录中。例如,如果 public/ 的访问地址是 /:
mkdir -p public/stylescp node_modules/@master/ui/dist/base.css public/styles/master-ui-base.csscp node_modules/@master/ui/dist/standalone.css public/styles/master-ui.css<link rel="stylesheet" href="/styles/master-ui-base.css"><link rel="stylesheet" href="/styles/master-ui.css"><button type="button" class="btn btn-primary">Save changes</button>复制的文件与已安装的包版本完全对应。主动升级版本后,请重新复制这些文件。采用这种方式,应用无需编译 CSS。
编译后的样式表声明的层和令牌与从源码构建时相同。它默认跟随读者的配色方案,也可以通过 data-theme="light" 或 data-theme="dark" 为整个页面或某个子树固定配色方案。样式表不包含 Master CSS 工具类,请自行编写布局所需的 CSS,并在复制布局、区块或页面时替换其中的工具类。
#检查标记
每个样式表都有一份契约,规定了类名、各类适用的元素及所需的属性。check 会读取模板并报告违反契约的地方:
npx master-ui check安装包后,从项目根目录运行该命令。它无需渲染即可检查 HTML 以及 JSX、Svelte、Vue 和 Astro 等模板。处理检查报告的问题后,在浏览器中核对显示效果和键盘交互。即使静态检查通过,也仍需验证无障碍性和浏览器支持情况。与 AI 协作介绍了如何处理诊断信息以及如何在 CI 中运行检查。
#自定义主题
参照主题配置修改品牌色、圆角半径或焦点颜色等共享值。默认主题说明了这些值的用途和默认值。如果改动只涉及单个实例或组件,请先阅读自定义,选择合适的修改层级。