與 AI 協作
提供與專案版本相符的契約給代理程式,檢查代理程式變更的標記,並在接受變更前驗證實際呈現的介面。
本頁內容
將專案所用版本的契約提供給程式開發代理程式,再請它檢查自己變更的標記。契約列出類別名稱、必要元素、狀態屬性與公開自訂屬性。檢查工具無須呈現頁面,就能找出常見的結構錯誤。
若要請代理程式安裝這套程式庫,請將設定提示詞貼給它。這個提示詞也會讓它安裝技能和下方的指引。
變更前,代理程式應確認專案如何匯入 Master UI、使用哪個主題,以及 master-ui.json 是否記錄由專案自行維護的元件。這些本機契約優先於套件的預設契約。變更後,靜態檢查與瀏覽器驗證各有用途。即使標記有效,仍可能有版面配置錯誤、互動不符無障礙需求,或缺少行為腳本。
契約檢查與瀏覽器驗證
檢查原始碼與實際呈現結果。 請依據已安裝的契約或專案自行維護的契約撰寫標記。CLI 檢查可讀取的元素、類別和屬性;瀏覽器則可驗證版面配置、鍵盤操作和狀態行為。無論哪一項檢查發現問題,都應在接受變更前修正。
#技能
master-ui 技能適用於在使用這套程式庫的專案中工作的代理程式。請執行以下指令安裝:
npx skills add master-co/ui --skill master-ui| 檔案 | 內容 |
|---|---|
SKILL.md | 如何確認專案取得程式庫的方式及自行維護的元件、如何依契約撰寫標記、程式庫缺少所需功能時如何處理,以及如何驗證結果 |
references/choosing.md | 當多種元件都可能適用時,如何依用途挑選,例如在頁面上顯示浮層、傳達訊息、從多個選項中挑選、處理數值、導覽或呈現一組記錄 |
references/customizing.md | 自訂的各個層級與自行維護元件的條件 |
references/frameworks.md | 在 React、Svelte 和 Vue 中哪些部分仍以標記撰寫、哪些模式提供可複製的元件,以及如何實作元件 |
這項技能會指示代理程式在已匯入 Master UI 的專案中使用它。若專案已採用其他程式庫,這項技能不會引入 Master UI。
#網站提供給程式讀取的內容
網站提供結構化資料和可攜式 Markdown。元件的 Markdown 由契約產生,指南、版面配置、區塊和頁面則從原始頁面匯出。這些匯出內容提供文字與程式碼,不包含原網站的互動功能。
| URL | 內容 |
|---|---|
https://ui.master.co/llms.txt | 索引,包含 Master UI 標記的撰寫規則,以及每個頁面的 Markdown 連結 |
https://ui.master.co/<section>/<slug>.md | 各頁面的 Markdown 版本。區段可以是 guide、components、charts、blocks 或 pages。元件頁面依契約撰寫,包含標記、類別、狀態、屬性、腳本、鍵盤操作與平台功能。歸在元件區段的版面配置會保留原始頁面,並在後面附上契約 |
https://ui.master.co/contracts.json | 所有契約的資料 |
https://ui.master.co/contract.schema.json | 所有契約遵循的 JSON Schema |
https://ui.master.co/theme.json | 主題中的所有 token 及其值 |
https://ui.master.co/r/registry.json | 登錄檔,提供可複製到專案的原始碼 |
無法使用 shell 的代理程式可以從 llms.txt 開始,讀取相關元件的頁面,例如 https://ui.master.co/components/dialog.md。公開網站記載的是目前部署的程式庫版本,可能與專案安裝的版本不同。若能存取套件,請優先讀取套件內的 @master/ui/contracts.json 和 @master/ui/theme.json。如果專案有本機契約,請使用下方的 CLI。
#查詢契約
安裝套件後,請從專案根目錄執行 CLI。CLI 會從已安裝的套件與專案讀取契約。請避免在尚未安裝 CLI 的情況下執行查詢,以免指令下載與專案無關的版本。
npx master-ui list # every contract, one line eachnpx master-ui contract dialog # one contract as JSON可以用類別名稱查詢,例如 npx master-ui contract btn-primary 會輸出按鈕的契約。npx master-ui list --json 會以資料格式輸出清單。
這兩個指令也會讀取 master-ui.json 中設定的目錄和其他契約路徑(預設目錄為 ui/)。由專案自行維護的元件會以專案內的副本為準,專案自行撰寫的元件則會與程式庫的元件一併列出。
#檢查標記
npx master-ui check 會讀取標記,並回報違反契約的位置。例如,以下檔案有三個錯誤和一個警告:
<button type="button" class="btn-primary">Save changes</button><button type="button" class="btn btn-outline">Cancel</button><button type="button" class="btn btn-square"> <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 4L12 12"/><path d="M4 12L12 4"/></svg></button><div class="dialog"> <h2 class="dialog-title">Delete this project?</h2></div>npx master-ui check page.htmlpage.html:1:1 error missing-base "btn-primary" modifies "btn": write class="btn btn-primary".page.html:2:1 warning unknown-class "btn-outline" is not a class of btn. It has: btn, btn-primary, btn-quiet, btn-brand, btn-danger, btn-sm, btn-lg, btn-square, btn-pill, btn-group.page.html:3:1 error label "btn-square" needs an accessible name: text of its own, aria-label, aria-labelledby, or a <label> that wraps or points at it.page.html:6:1 error element "dialog" goes on <dialog>, not <div>.1 files checked: 3 errors, 1 warnings.每一列都會列出位置、嚴重程度、代碼和建議寫法,讓代理程式能修正標記並重新執行指令,直到沒有任何問題。契約列出了所有代碼。
- 路徑。 未指定引數時,會檢查工作目錄下所有的
.html、.htm、.astro、.svelte、.vue、.jsx、.tsx、.mdx、.erb、.php、.njk、.liquid和.hbs檔案,並略過node_modules和建置輸出。也會讀取其中的樣式表以及 JavaScript 和 TypeScript 模組,檢查它們匯入函式庫的方式。可指定檔案、目錄或 glob 模式來縮小檢查範圍。 - 略過的情況。 檢查工具直接讀取標籤、類別和屬性,無須執行瀏覽器。遇到無法判定的情況,例如由運算式產生的類別,或根元素位於另一個檔案的元件部位,就不會回報。
--json會以資料格式輸出結果,包含檔案數量,以及每個發現問題的檔案的診斷資訊。每筆診斷都包含code、severity、className、contract、message、line和column。--strict會將unknown-class回報為錯誤。如果類別名稱以元件的根類別接上連字號開頭,但未出現在任何契約中,預設只會發出警告,因為它可能是 utility。若專案已安裝 Master CSS,檢查工具就能辨識預設的 utility。也請檢視與專案自訂類別相關的診斷。即使檢查沒有回報問題,也無法保證每個自訂 utility 或動態計算出的類別都有效。
指令找到錯誤時會以狀態碼 1 結束,因此可直接用來讓 CI 步驟在有錯誤時失敗:
{ "scripts": { "check:ui": "master-ui check --strict" }}若要在程式中執行檢查,請使用 @master/ui/check。契約說明了檢查工具會讀取的資料。請將瀏覽器驗證列入代理程式的完成條件,測試受影響的鍵盤操作、各種狀態、窄寬度下的版面配置與配色模式,並確認所需的行為均已掛載。
#提供指引給代理程式
代理程式會優先遵循專案本身的指示。請將以下內容貼到 AGENTS.md,或代理程式會讀取的檔案中:
## Master UIThis project builds its interface with Master UI: classes on native elements, each described by a contract.- Read the contract before writing a component's markup: `npx master-ui contract <name>`, and `npx master-ui list` to find one. Do not write markup from memory.- Never invent a Master UI component class. Use the installed or locally owned contract, and keep project-defined classes outside the library's class families.- Mark a state with the attribute the contract lists (`disabled`, `aria-pressed="true"`, `open`). Never add a class for a state.- A component is a class on a native element in any framework. Do not wrap one in a component to rename it. The patterns that hold state have a component to copy: `npx master-ui add` lists them, and `ui/` may already hold one.- To change how something looks, take the lowest step that works: a utility on the element, a theme token, a public custom property, a theme file, and only then `npx master-ui own <name>`. A project rule may select a library class to set its documented custom properties; change its implementation through owned source.- Icons are Master Icons unless this project names another set. Look a name up before importing it (`node node_modules/@master/icons/skills/master-icons/scripts/search.mjs "<words>"`), pick the 16, 20 or 24 drawing by the size the component gives the icon, put `icon` on it and never set `stroke-width` in CSS.- Run `npx master-ui check` after changing markup. Fix every error and read each warning. Check the affected interactions in a browser, including the keyboard, a narrow viewport and both color schemes.#MCP
Master UI 套件提供上述 CLI 和契約檔案。如果代理程式也連線到 Master CSS MCP 伺服器,請先確認該連線提供哪些工具,再用它查詢元件契約。可用工具取決於連線的伺服器。已安裝的 CLI 可直接讀取專案實際採用的契約。
#在聊天沙箱或聊天產出物中使用
若聊天產出物或靜態 HTML 原型沒有 CSS 編譯流程,請連結網站已編譯的樣式表。如果環境沒有提供基礎樣式重設,也請一併載入:
<link rel="stylesheet" href="https://ui.master.co/base.css"><link rel="stylesheet" href="https://ui.master.co/standalone.css"><button type="button" class="btn btn-primary">Save changes</button>這些網址會隨網站的部署版本更新,適合用於一次性的原型。若專案會持續維護,請從固定版本的套件提供已編譯的檔案,並讀取同一版本的契約。載入 CSS 只會套用樣式。需要行為腳本的模式仍須透過瀏覽器模組或框架元件掛載行為。
這類環境無法直接使用 utility。flex 或 gap-md 等類別需要由 Master CSS 產生,因此請在 style 屬性或簡短的樣式區塊中自行撰寫 CSS,設定頁面的版面配置。主題的 token 是自訂屬性,因此仍可使用,例如 color: var(--color-text-muted) 會讀取主題中的值。