スニペット
コピー用の1行のコマンドを、プロンプトとコピーボタン付きで表示します。
このページの内容
スニペットは、ターミナルで実行する1行のコマンドを、プロンプトとコピーボタン付きで表示します。コードブロックと同じように等幅フォントで表示するくぼんだ面で、ボタンと同じ高さなので横に並べられます。プロンプトは選択できず、コマンドは折り返しません。コピーボタンは終端から4px内側にあり、ポインターを重ねるまでは控えめに表示されます。
スニペット
<div class="snippet max-w(360) mx-auto"> <span class="snippet-prompt" aria-hidden="true">$</span> <code class="snippet-command" tabindex="0">npm install @master/ui</code> <button type="button" class="btn btn-quiet btn-square" aria-label="Copy command"> <svg class="icon snippet-icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M3 1.5H9A1.4999 1.4999 0 0 1 10.5 3V4.25H9.5V3A0.5002 0.5002 0 0 0 9 2.5H3A0.5002 0.5002 0 0 0 2.5 3V9A0.5002 0.5002 0 0 0 3 9.5H4.25V10.5H3A1.4999 1.4999 0 0 1 1.5 9V3A1.4999 1.4999 0 0 1 3 1.5Z" fill="currentColor" stroke="none" fill-rule="evenodd" /><rect x="5.5" y="5.5" width="8.5" height="8.5" rx="1" /> </svg> <svg class="icon snippet-check" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M3 8L6.3532 11.3532A0.2 0.2 0 0 0 6.6412 11.3479L13 4.5" /> </svg> </button> <output class="snippet-status"></output></div><script>document.querySelectorAll('.snippet').forEach(function (snippet) {var status = snippet.querySelector('.snippet-status')var timer = 0snippet.querySelector('.btn').addEventListener('click', function () { var command = Array.prototype.find.call(snippet.querySelectorAll('.snippet-command'), function (line) { return line.checkVisibility() }) navigator.clipboard.writeText(command.textContent).then(function () { status.textContent = 'Copied' clearTimeout(timer) timer = setTimeout(function () { status.textContent = '' }, 2000) })})})</script>#コマンド行
snippetは、プロンプト、コマンド、コピーボタンを3列に並べます。snippet-promptは、端末でコマンドの前に表示される$や>などの記号で、控えめな色で表示します。選択できないため、行を手動で選択してもコマンドだけが選択されます。読み上げの対象から外すため、aria-hiddenを指定します。snippet-commandは<code>に記述したコマンドです。最後に、控えめな正方形のボタンを置きます。ボタンの高さと角丸はスニペットが設定します。ボタンの一辺は、行の高さから上下4pxずつを引いた寸法です。
行に収まらないコマンドは折り返さず、スクロールバーを表示せずに横スクロールできます。続きがある端には、スクロール位置に応じてフェードがかかります。スクロールするまでは先頭側にフェードはかかりません。コマンドにtabindex="0"を指定すると、矢印キーでスクロールでき、フォーカスリングは行全体を囲みます。
長いコマンド
<div class="snippet max-w(360) mx-auto"> <span class="snippet-prompt" aria-hidden="true">$</span> <code class="snippet-command" tabindex="0"> curl -fsSL https://ui.master.co/install.sh | sh -s -- --yes --prefix ~/.local </code> <button type="button" class="btn btn-quiet btn-square" aria-label="Copy command"> <svg class="icon snippet-icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M3 1.5H9A1.4999 1.4999 0 0 1 10.5 3V4.25H9.5V3A0.5002 0.5002 0 0 0 9 2.5H3A0.5002 0.5002 0 0 0 2.5 3V9A0.5002 0.5002 0 0 0 3 9.5H4.25V10.5H3A1.4999 1.4999 0 0 1 1.5 9V3A1.4999 1.4999 0 0 1 3 1.5Z" fill="currentColor" stroke="none" fill-rule="evenodd" /><rect x="5.5" y="5.5" width="8.5" height="8.5" rx="1" /> </svg> <svg class="icon snippet-check" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M3 8L6.3532 11.3532A0.2 0.2 0 0 0 6.6412 11.3479L13 4.5" /> </svg> </button> <output class="snippet-status"></output></div><script>document.querySelectorAll('.snippet').forEach(function (snippet) {var status = snippet.querySelector('.snippet-status')var timer = 0snippet.querySelector('.btn').addEventListener('click', function () { var command = Array.prototype.find.call(snippet.querySelectorAll('.snippet-command'), function (line) { return line.checkVisibility() }) navigator.clipboard.writeText(command.textContent).then(function () { status.textContent = 'Copied' clearTimeout(timer) timer = setTimeout(function () { status.textContent = '' }, 2000) })})})</script>#コピー
コピーにはスクリプトが必要です。コマンドのテキストをnavigator.clipboardに渡します。コピー済みの状態は、クラスではなくボタンの隣にある空の<output>(snippet-status)で表します。この要素は画面には表示されません。スクリプトが「コピーしました」と書き込むとスクリーンリーダーが読み上げ、2秒後にスクリプトが内容を消去します。内容がある間は、スタイルシートがコピーアイコンをチェックマークに切り替え、画面上でもコピーの完了を伝えます。
プレビューのスクリプトは、表示中のコマンドを取得してクリップボードに書き込み、ステータスにメッセージを入れてから消去します。クリップボードを利用できるのは、HTTPSまたはlocalhostで配信されるページに限られます。スクリプトが無効な場合はボタンが表示されず、コマンドを手動で選択してコピーできます。
#パッケージマネージャー
同じ操作を行うコマンドをnpm、pnpm、bun、yarnごとに用意するには、ネイティブのラジオボタンで構成したセグメントコントロールをスニペットの先頭に置き、各選択肢に対応するsnippet-commandを、選択肢と同じ順序で1つずつ記述します。コントロール自体の背景はなくなり、スニペット上部の細い罫線の上に並びます。選択された項目は一段浮いた面として表示され、選択を変えるとその面が次の項目へ滑らかに移動します。選択された項目と同じ順番にあるコマンドが表示されます。スクリプトなしで最大6項目に対応します。
パッケージマネージャー
<div class="snippet max-w(360) mx-auto"> <div class="segmented" role="radiogroup" aria-label="Package manager"> <label class="segmented-item"> <input class="segmented-input" type="radio" name="manager" checked>npm </label> <label class="segmented-item"> <input class="segmented-input" type="radio" name="manager">pnpm </label> <label class="segmented-item"> <input class="segmented-input" type="radio" name="manager">bun </label> <label class="segmented-item"> <input class="segmented-input" type="radio" name="manager">yarn </label> </div> <span class="snippet-prompt" aria-hidden="true">$</span> <code class="snippet-command" tabindex="0">npm install @master/ui</code> <code class="snippet-command" tabindex="0">pnpm add @master/ui</code> <code class="snippet-command" tabindex="0">bun add @master/ui</code> <code class="snippet-command" tabindex="0">yarn add @master/ui</code> <button type="button" class="btn btn-quiet btn-square" aria-label="Copy command"> <svg class="icon snippet-icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M3 1.5H9A1.4999 1.4999 0 0 1 10.5 3V4.25H9.5V3A0.5002 0.5002 0 0 0 9 2.5H3A0.5002 0.5002 0 0 0 2.5 3V9A0.5002 0.5002 0 0 0 3 9.5H4.25V10.5H3A1.4999 1.4999 0 0 1 1.5 9V3A1.4999 1.4999 0 0 1 3 1.5Z" fill="currentColor" stroke="none" fill-rule="evenodd" /><rect x="5.5" y="5.5" width="8.5" height="8.5" rx="1" /> </svg> <svg class="icon snippet-check" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M3 8L6.3532 11.3532A0.2 0.2 0 0 0 6.6412 11.3479L13 4.5" /> </svg> </button> <output class="snippet-status"></output></div><script>document.querySelectorAll('.snippet').forEach(function (snippet) {var status = snippet.querySelector('.snippet-status')var timer = 0snippet.querySelector('.btn').addEventListener('click', function () { var command = Array.prototype.find.call(snippet.querySelectorAll('.snippet-command'), function (line) { return line.checkVisibility() }) navigator.clipboard.writeText(command.textContent).then(function () { status.textContent = 'Copied' clearTimeout(timer) timer = setTimeout(function () { status.textContent = '' }, 2000) })})})</script>#サイズ
行の高さはボタンと同じ38pxです。snippet-smは高さ32px、文字サイズ12pxで、情報量の多いページやテーブルに適しています。snippet-lgは高さ44px、文字サイズ14pxで、ページ上部で大きいボタンと高さをそろえられます。タッチスクリーンでも、各サイズの行の高さは変わりません。コピーボタンの操作領域は、ボタンを中心に44pxまで広がります。
サイズ
<div class="flex flex-col items-center gap-lg"> <div class="snippet snippet-sm"> <span class="snippet-prompt" aria-hidden="true">$</span> <code class="snippet-command" tabindex="0">npx master-ui add button</code> <button type="button" class="btn btn-quiet btn-square" aria-label="Copy command"> <svg class="icon snippet-icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M3 1.5H9A1.4999 1.4999 0 0 1 10.5 3V4.25H9.5V3A0.5002 0.5002 0 0 0 9 2.5H3A0.5002 0.5002 0 0 0 2.5 3V9A0.5002 0.5002 0 0 0 3 9.5H4.25V10.5H3A1.4999 1.4999 0 0 1 1.5 9V3A1.4999 1.4999 0 0 1 3 1.5Z" fill="currentColor" stroke="none" fill-rule="evenodd" /><rect x="5.5" y="5.5" width="8.5" height="8.5" rx="1" /> </svg> <svg class="icon snippet-check" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M3 8L6.3532 11.3532A0.2 0.2 0 0 0 6.6412 11.3479L13 4.5" /> </svg> </button> <output class="snippet-status"></output> </div> <div class="snippet"> <span class="snippet-prompt" aria-hidden="true">$</span> <code class="snippet-command" tabindex="0">npx master-ui add button</code> <button type="button" class="btn btn-quiet btn-square" aria-label="Copy command"> <svg class="icon snippet-icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M3 1.5H9A1.4999 1.4999 0 0 1 10.5 3V4.25H9.5V3A0.5002 0.5002 0 0 0 9 2.5H3A0.5002 0.5002 0 0 0 2.5 3V9A0.5002 0.5002 0 0 0 3 9.5H4.25V10.5H3A1.4999 1.4999 0 0 1 1.5 9V3A1.4999 1.4999 0 0 1 3 1.5Z" fill="currentColor" stroke="none" fill-rule="evenodd" /><rect x="5.5" y="5.5" width="8.5" height="8.5" rx="1" /> </svg> <svg class="icon snippet-check" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M3 8L6.3532 11.3532A0.2 0.2 0 0 0 6.6412 11.3479L13 4.5" /> </svg> </button> <output class="snippet-status"></output> </div> <div class="flex flex-wrap items-center justify-center gap-sm"> <a class="btn btn-primary btn-lg" href="#">Get started</a> <div class="snippet snippet-lg"> <span class="snippet-prompt" aria-hidden="true">$</span> <code class="snippet-command" tabindex="0">npm install @master/ui</code> <button type="button" class="btn btn-quiet btn-square" aria-label="Copy command"> <svg class="icon snippet-icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M3 1.5H9A1.4999 1.4999 0 0 1 10.5 3V4.25H9.5V3A0.5002 0.5002 0 0 0 9 2.5H3A0.5002 0.5002 0 0 0 2.5 3V9A0.5002 0.5002 0 0 0 3 9.5H4.25V10.5H3A1.4999 1.4999 0 0 1 1.5 9V3A1.4999 1.4999 0 0 1 3 1.5Z" fill="currentColor" stroke="none" fill-rule="evenodd" /><rect x="5.5" y="5.5" width="8.5" height="8.5" rx="1" /> </svg> <svg class="icon snippet-check" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M3 8L6.3532 11.3532A0.2 0.2 0 0 0 6.6412 11.3479L13 4.5" /> </svg> </button> <output class="snippet-status"></output> </div> </div></div><script>document.querySelectorAll('.snippet').forEach(function (snippet) {var status = snippet.querySelector('.snippet-status')var timer = 0snippet.querySelector('.btn').addEventListener('click', function () { var command = Array.prototype.find.call(snippet.querySelectorAll('.snippet-command'), function (line) { return line.checkVisibility() }) navigator.clipboard.writeText(command.textContent).then(function () { status.textContent = 'Copied' clearTimeout(timer) timer = setTimeout(function () { status.textContent = '' }, 2000) })})})</script>#スニペットとコードの使い分け
スニペットは、実行する1行のコマンドに使います。複数行のコード、ファイルの内容、出力結果を読ませる場合はコードブロック、文中でコマンドに言及する場合はインラインのコードを使います。押すキーにはキーボード入力を使います。
#アクセシビリティ
- 装飾用のプロンプトには
aria-hidden="true"を指定し、コマンドだけが読み上げられるようにします。 - コピーボタンには「コマンドをコピー」などの
aria-labelを指定します。アイコンにはaria-hiddenを指定します。 - マークアップでは
snippet-statusを空にし、空白文字も入れないでください。スクリプトが書き込んだ「コピーしました」がここから読み上げられます。<output>はそれ自体が優先度 polite のライブリージョンです。 - コマンドに
tabindex="0"を指定し、行に収まらない場合にキーボードでスクロールできるようにします。 - 強制カラーモードでは、フェードがなくなり、コマンド欄に輪郭が付き、プロンプトにはシステムのグレーのテキスト色が適用されます。
#クラス
| クラス | 説明 |
|---|---|
snippet | 高さ 38px のくぼみ。等幅書体を使い、プロンプト、コマンド、ボタンを 3 列に配置 |
snippet-sm | 高さ 32px、文字サイズ 12px の行です。既定の高さは 38px です。snippet と併用します |
snippet-lg | ページ上部向けの、高さ 44px、文字サイズ 14px の行です。snippet と併用します |
snippet-prompt | プロンプト記号($ または >)は控えめな文字色で表示され、選択できません。aria-hidden="true" を指定します。 |
snippet-command | <code> に置くコマンド。折り返さない 1 行で横スクロールでき、続きがある側の端でフェードします。tabindex="0" を付けます。セグメントコントロールと併用する場合は、各選択肢に 1 つずつ、同じ順序で最大 6 つ記述します。 |
snippet-icon | ボタン内のコピーアイコン。icon を指定します。ステータスがコマンドのコピー完了を示すと非表示になります。 |
snippet-check | ボタン内でコピーアイコンの後に置くチェックマーク。icon を付けます。コピー後はコピーアイコンに代わって表示されます。 |
snippet-status | 画面には表示されない空の <output> です。スクリプトが書き込んだ内容を読み上げます。書き込むとコピーアイコンがチェックマークに変わり、空にすると元に戻ります。 |
#カスタムプロパティ
| プロパティ | 既定値 | 説明 |
|---|---|---|
--snippet-height | 2.375rem | 行の高さです。ボタンは行より 8px 低く、上下に 4px ずつ内側に配置されます。指定先:snippet |
--snippet-radius | var(--radius-control) | 角のスケールを適用する前の、くぼみの角の半径です。ボタンと選択肢の半径は、内側に寄せる分だけ 4px 小さくなります。指定先:snippet |
--snippet-fade | 1.5rem | 長いコマンドの続きがある側の端で、フェードをかける範囲の長さです。指定先:snippet |