指令片段
一行可複製的指令,附有終端機提示符號和複製按鈕。
本頁內容
指令片段包含一行可在終端機執行的指令、終端機提示符號,以及複製指令的按鈕。它和程式碼區塊一樣採用等寬字型和凹槽樣式,高度則與按鈕相同,可與按鈕並排。提示符號無法選取,指令維持單行,複製按鈕距尾端 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 分成三欄。第一欄的 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 表示。使用者看不到這個元素。腳本寫入「已複製」後,螢幕閱讀器會朗讀,兩秒後腳本再清空文字。只要狀態有文字,樣式表就會把複製圖示換成勾號,讓畫面和朗讀傳達相同的訊息。
預覽中的腳本包含完整流程:找出目前顯示的指令、寫入剪貼簿,再填入並清空狀態訊息。只有透過 HTTPS 提供的頁面或來自 localhost 的頁面可存取剪貼簿。腳本停用時,複製按鈕會隱藏,仍可手動選取並複製指令文字。
#套件管理工具
若要為 npm、pnpm、bun 和 yarn 提供同一項操作的指令,請在片段開頭放入由原生單選按鈕組成的分段控制項,再依照選項順序為每個選項寫一個 snippet-command。分段控制項會省略自己的凹槽,沿片段頂部排列,下方以細線分隔。選取的選項會凸起,切換時凸起面板會滑到新選項。元件只顯示與選取位置對應的指令,無需腳本,最多支援六個選項。
套件管理工具
<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>#指令片段與程式碼的用途
指令片段用於可直接執行的單行指令。多行內容、檔案或供閱讀的輸出適合放在程式碼區塊中。句子中提到指令時,請使用行內程式碼。要標示使用者應按下的按鍵,請使用按鍵元件。
#無障礙
- 終端機提示符號僅供裝飾,請加上
aria-hidden="true",讓螢幕閱讀器只朗讀指令。 - 為複製按鈕加上
aria-label,例如「複製指令」。按鈕內的圖示使用aria-hidden。 - 在標記中讓
snippet-status保持完全空白,連空白字元也不要放入。<output>本身就是會在適當時機朗讀的即時區域,因此腳本填入的「已複製」會由螢幕閱讀器朗讀。 - 為指令加上
tabindex="0",讓使用者能以鍵盤捲動超出可用寬度的指令。 - 在強制色彩模式下,淡出效果會移除,凹槽會加上外框,提示符號則使用系統的灰色文字色。
#類別
| 類別 | 說明 |
|---|---|
snippet | 高 38px 的凹槽,採用等寬字型,提示符號、指令和按鈕各占一欄 |
snippet-sm | 高度 32px 的列,字級為 12px。預設列高為 38px。搭配 snippet 使用 |
snippet-lg | 高度 44px 的列,字級為 14px,適用於頁首搭配 snippet 使用 |
snippet-prompt | 提示符號 $ 或 > 使用較低調的前景色,且無法選取。請加上 aria-hidden="true"。 |
snippet-command | 指令寫在 <code> 元素內,維持單行並可水平捲動。內容超出時,相應邊緣會淡出。請設定 tabindex="0"。搭配分段控制項時,依選項順序各寫一條指令,最多六條 |
snippet-icon | 按鈕內使用 icon 的複製圖示。狀態顯示指令已複製時,會由勾號取代 |
snippet-check | 按鈕內使用 icon 的勾號,放在複製圖示後方。複製完成後會取代複製圖示 |
snippet-status | 初始為空且不在畫面上顯示的 <output>,會向輔助技術播報腳本寫入的內容。寫入內容時,複製圖示會換成勾號,清空後則恢復原狀。 |
#自訂屬性
| 屬性 | 預設值 | 說明 |
|---|---|---|
--snippet-height | 2.375rem | 指令列的高度。按鈕比指令列矮 8px,上下各留 4px。設定於 snippet |
--snippet-radius | var(--radius-control) | 指令列套用圓角縮放前的圓角半徑。按鈕與選項向內縮 4px,因此各自的半徑比此值小 4px。設定於 snippet |
--snippet-fade | 1.5rem | 長指令超出顯示範圍時,邊緣淡出的長度設定於 snippet |