安裝
安裝鎖定版本的套件,選擇原始或預先編譯的 CSS,並驗證第一個元件。
本頁內容
安裝 @master/ui,載入樣式表,再算繪一個套用元件類別的原生元素。如果專案使用 Master CSS,請將套件的原始樣式與應用程式的樣式一併編譯。否則,請使用預先編譯的樣式表,其中包含元件樣式,但不含 Master CSS utility。
載入元件樣式的兩種方式
每個頁面只選一種載入方式。 匯入原始樣式時,應用程式的 Master CSS 編譯器會一併處理這些樣式與用來產生 utility 的類別。套件中的 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 層,因此標記中的 utility 樣式仍然優先,你寫在 @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 標籤,把 script 放在它之後,例如 <head> 的結尾或 <body> 的開頭:只要在讀者能聚焦欄位之前執行即可。沒有這段 script 一切仍正常運作,只是 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。
編譯後的樣式表與從原始碼編譯的版本宣告相同的層級與 token。樣式表會依照使用者偏好的配色,並可透過 data-theme="light" 或 data-theme="dark" 為整個頁面或 DOM 子樹固定配色。這份樣式表不含 Master CSS utility,請自行撰寫版面配置的 CSS,並在複製版面配置、區塊或頁面時替換其中的 utility 類別。
#檢查標記
每份樣式表都有一份契約,說明有哪些類別、各類別應套用在哪個元素上,以及需要哪些屬性。check 會讀取範本並回報違反契約的地方:
npx master-ui check安裝套件後,請從專案根目錄執行指令。它會直接檢查 HTML 和 JSX、Svelte、Vue、Astro 等範本,無須先算繪這些範本。修正回報的問題後,請在瀏覽器中確認畫面呈現和鍵盤操作。通過靜態檢查並不能證明符合無障礙要求或受到瀏覽器支援。與 AI 協作說明如何處理檢查訊息,以及如何在 CI 中執行檢查。
#自訂主題
依照主題設定調整品牌色、圓角半徑或焦點色彩等共用值。預設主題說明這些值的用途與預設值。如果變更只影響單一實例或單一元件,請先參考自訂,選擇適合的層級。