ナビゲーション

v2.1.0
はじめに

インストール

パッケージのバージョンを固定し、ソースとコンパイル済み CSS のどちらかを選んで、最初のコンポーネントの表示を確認します。

このページの内容

@master/ui をインストールし、スタイルシートを読み込んで、コンポーネントクラスを付けたネイティブ要素を表示します。Master CSS を使うプロジェクトでは、パッケージのソースをアプリのスタイルと一緒にコンパイルします。それ以外のプロジェクトでは、コンパイル済みスタイルシートでコンポーネントのスタイルを利用できます。Master CSS のユーティリティは含まれません。

スタイルシートの選び方

ページごとに、どちらか一方の読み込み方法を選びます。 ソースをインポートすると、アプリの Master CSS コンパイラーが、ユーティリティの生成に使うクラスとあわせて処理します。パッケージの standalone.css はコンパイル済みなので、そのまま読み込めます。どちらの方法でもコンポーネントのスタイルを利用できます。コンパイル済み CSS を選んだ場合は、レイアウトをアプリ側の CSS で指定します。

#AI エージェントでセットアップする

セットアップをコーディングエージェントに任せることもできます。アプリのルートディレクトリで、下のプロンプトをエージェントに貼り付けます。エージェントはプロジェクトを調べ、ファイルを変更する前に、スタイルシートの読み込み方法、インポート範囲、エージェントの設定、アイコン、既存のコンポーネントライブラリの扱いについてユーザーに質問します。続いてバージョンを固定してパッケージをインストールし、スタイルシートをインポートして、最初のコンポーネントをブラウザーと master-ui check で確認します。

Master UI のセットアップ

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 以降と、利用するフレームワーク向けの連携機能を設定します。Vite、Astro、Next.js、Svelte などでの手順は Master CSS のインストールガイドを参照してください。

#パッケージをインストールする

npm install @master/ui --save-exact

使い慣れたパッケージマネージャーを使えます。たとえば bun add @master/ui --exact や pnpm add @master/ui --save-exact を実行します。マイナーリリースとパッチリリースではすべての公開名が維持されますが、コンポーネントの見た目が変わることはあります。バージョンを固定して必要なときに更新し、更新後は安定性ガイドに沿って確認します。

#スタイルシートをインポートする

アプリのスタイルシートで、Master CSS プリセットの後に Master UI をインポートします。

app.css
@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>

フレームワークがすでに書き出しているビューポートのタグはそのまま残し、スクリプトをその後ろ、たとえば <head> の末尾か <body> の先頭に置きます。読み手がフィールドにフォーカスできるようになる前に実行されれば十分です。スクリプトがなくてもすべて動作し、iOS がフォーカス時にページを拡大するだけです。代わりにタッチスクリーンでフィールドの文字サイズを 16px に上げることはしないでください。同じ行にあるボタンやセレクトと揃わなくなります。

#インストールを確認する

アプリで app.css が読み込まれていることを確認し、表示されるページにボタンを追加します。JSX では class の代わりに className を使います。

<button type="button" class="btn btn-primary">Save changes</button>

普段の開発コマンドでアプリを起動します。ライトモードでは濃色、ダークモードでは淡色のボタンが表示され、キーボードでフォーカスを移すとリングが見えます。見た目はビヘイビアのスクリプトなしで再現できます。実際の保存処理はアプリのイベントハンドラーで実装します。

ブラウザー既定のボタンに見える場合は、ページが app.css を読み込み、コンパイル結果に .btn が含まれているか確認します。ソースをインポートしている場合は、Master CSS の連携機能がそのスタイルシートを処理しているかも確認します。その後、マークアップを検査します。

#必要なスタイルだけをインポートする

スタイルシートは個別にもインポートできます。これは、スタイルを必要とするモジュールと一緒に読み込むアプリのための方法です。共通のテーマはアプリのスタイルシートに置き、各コンポーネントはそれを描画するモジュールからインポートします。

app.css
@import '@master/css';@import '@master/ui/theme.css';
save-button.js
import '@master/ui/components/button.css'import '@master/ui/components/spinner.css'

文書の <head> で、どのスタイルシートよりも先にカスケードレイヤーの順序を宣言します。ルートのコンポーネント CSS は app.css より先に読み込まれることがあります。components が先に宣言されると、プリセットの base が後になり、リセットがコンポーネントのスタイルを上書きする場合があります。

app.html
<style>@layer theme, base, defaults, components, utilities;</style>

バンドラーは、いくつのモジュールからインポートされてもモジュールを 1 回だけ読み込みます。そのため、2 つのモジュールがどちらも button.css をインポートしても、出力されるのは 1 回です。マークアップに含まれるクラスすべてに対応するスタイルシートを読み込みます。たとえばボタンの中に icon や spinner を置く場合は、それぞれのスタイルシートも必要です。一括インポートを使えば、一覧を個別に管理する手間を省けます。

これらは必ずモジュールのインポートとして書き、CSS の @import ルールにはしないでください。また、ライブラリ全体を一括インポートしているプロジェクトには追加しないでください。理由は読み込み方を 1 つに決めるで説明します。

#スタイルシートを 1 行ずつインポートする

パッケージには小さな CLI が付属します。init は、テーマと個別のスタイルシートを 1 行ずつインポートする ui/index.css と、プロジェクトで管理するソースを記録する master-ui.json を生成します。

npx master-ui init
app.css
@import '@master/css';@import './ui/index.css';

このエントリーファイルは、@master/ui の一括インポートの代わりに使えます。その場合、このファイルがプロジェクトにおけるライブラリの唯一の読み込み元になり、ライブラリのほかのスタイルシートをインポートする唯一のスタイルシートになります。この方法は任意です。インポート一覧を管理したい場合や、一部のコンポーネントのソースをプロジェクトで管理する場合に選びます。マークアップで該当クラスを使わなくなったときだけ、その行を削除してください。

#読み込み方を 1 つに決める

CSS の @import はファイルの内容を取り込むもので、モジュールを読み込むものではありません。同じスタイルシートを 2 つのファイルがインポートした場合や、ライブラリ全体と一緒に読み込んだうえで個別にもインポートした場合、そのスタイルシートは出力に 2 回書き込まれます。バイトが重複し、どちらが後に来るかはページによって変わります。モジュールとしてインポートしたスタイルシートは 1 回だけ読み込まれます。そのため、プロジェクトでは読み込み方を 1 つに決め、それを守ります。

プロジェクト読み込み方
アプリケーション1 つのスタイルシートに @import '@master/ui'; を書くか、その代わりに ui/index.css を使います。プロジェクト内のほかの場所では、ライブラリのスタイルシートをインポートしません
スタイルを必要に応じて読み込むアプリケーションスタイルシートごとにモジュールのインポートを 1 つ書きます:import '@master/ui/components/<name>.css'
Master UI を基にしたライブラリスタイルシートごとにモジュールのインポートを 1 つ、ライブラリ自身のモジュールに書きます
そのようなライブラリを使うアプリケーション同じく、スタイルシートごとにモジュールのインポートを 1 つ書きます。ライブラリのインポートはすでにバンドルに含まれているため、一括インポートを使うとそれらのスタイルシートがもう一度出力されます
  • 1 つのプロジェクトで 2 つの読み込み方を併用しないでください。 一括インポートと、個別のスタイルシートのモジュールインポートが併存すると、そのスタイルシートは 2 回出力されます。
  • 個別のコンポーネントを 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 のいずれかを報告します。各コードはコントラクトで説明しています。

#ビヘイビア

ほとんどのコンポーネントはスクリプトを必要としません。コンボボックス、コマンドパレット、カレンダーなど、対応するネイティブ要素がないパターンには、リファレンス実装のスクリプトが付属します。

app.js
import { attach } from '@master/ui/behaviors'const detach = attach()

マークアップが DOM に入った後、ブラウザー側で attach() を呼び出します。その時点で認識したパターンにビヘイビアを取り付け、解除用の関数を返します。マークアップを取り除く前に detach() を呼び出します。後から挿入した要素の監視や、コンテキストメニューを開く対象の設定は行いません。適用範囲の指定と個別の関数はビヘイビアを参照してください。React、Svelte、Vue では、フレームワークコンポーネントでライフサイクルを管理します。

#Master CSS を使わずに導入する

パッケージには通常の CSS にコンパイルしたコンポーネントが含まれます。CSS のインポートに対応するバンドラーでは、バージョンを固定してインストールしたパッケージから読み込めます。

app.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 などのテンプレートを、レンダリングせずに検査します。検査で指摘された問題を修正したら、ブラウザーで表示とキーボード操作を確認してください。静的検査を通過した後も、アクセシビリティと対象ブラウザーでの動作を確認する必要があります。診断の読み方や CI での実行については、AI との開発を参照してください。

#テーマを調整する

ブランドカラー、角の半径、フォーカス色など、共有する値の変更方法はテーマの変更を参照してください。既定のテーマには、各トークンの役割と既定値が載っています。変更が一つのインスタンスまたは一つのコンポーネントだけに及ぶ場合は、まずカスタマイズを参照し、変更に適したレイヤーを選びます。