程式碼
以等寬字型呈現原樣文字,可放在行內或獨立成區塊。
本頁內容
程式碼以等寬字型原樣呈現指令、值或檔名等文字。行內用法是在 <code> 上套用 .code,讓它在行內顯示為小型膠囊標籤。區塊用法是在 <pre> 上套用 code-block,形成一個可獨立捲動的凹槽,即使有程式碼行超出區塊寬度,也能捲動查看。
程式碼
<p class="m(0) text-sm fg-text-body leading-md"> Install the package with <code class="code">npm install @master/ui</code>, then import it after <code class="code">@master/css</code>.</p><p className="m(0) text-sm fg-text-body leading-md"> Install the package with <code className="code">npm install @master/ui</code>, then import it after <code className="code">@master/css</code>.</p><p class="m(0) text-sm fg-text-body leading-md"> Install the package with <code class="code">npm install @master/ui</code>, then import it after <code class="code">@master/css</code>.</p><template> <p class="m(0) text-sm fg-text-body leading-md"> Install the package with <code class="code">npm install @master/ui</code>, then import it after <code class="code">@master/css</code>. </p></template>#區塊
在 <pre> 上套用 code-block,並在裡面保留 <code> 以維持語意。區塊保留原本的換行,長行則以水平捲動呈現。若有任何一行可能超出區塊寬度,請加上 tabindex="0",讓使用者能以鍵盤捲動。
區塊
<pre class="code-block" tabindex="0"><code>@import '@master/css';@import '@master/ui';@theme {--color-brand: var(--color-indigo-50);}</code></pre><pre class="code-block" tabindex="0"><code>@import '@master/css';@import '@master/ui';@theme {'{'}--color-brand: var(--color-indigo-50);{'}'}</code></pre><template> <pre class="code-block" tabindex="0"><code>@import '@master/css';@import '@master/ui';@theme {--color-brand: var(--color-indigo-50);}</code></pre></template>#程式碼行
若要針對特定行加上標示,請將每行設為 code-line。區塊會以網格排列各行,每行都與最長的一行同寬,因此行底色會涵蓋完整的可捲動寬度。網格項目之間的空白不會顯示,所以元素間的換行不會多占一行。每一行程式碼各用一個元素。
code-line-highlight 以淡色底和行首的前景色細線標示要說明的那一行。在區塊上套用 code-numbered,即可用 CSS 計數器在程式碼旁的行號區顯示行號。選取或複製程式碼時,行號不會一併選取或複製。
行號與醒目標示
<pre class="code-block code-numbered" tabindex="0"><code class="code-line">.btn {</code><code class="code-line"> --btn-height: 2.375rem;</code><code class="code-line code-line-highlight"> --btn-radius: var(--radius-control);</code><code class="code-line"> border-radius: calc(var(--btn-radius) * var(--corner-scale));</code><code class="code-line"> corner-shape: var(--corner-shape);</code><code class="code-line">}</code></pre><pre class="code-block code-numbered" tabindex="0"><code class="code-line">.btn {'{'}</code><code class="code-line"> --btn-height: 2.375rem;</code><code class="code-line code-line-highlight"> --btn-radius: var(--radius-control);</code><code class="code-line"> border-radius: calc(var(--btn-radius) * var(--corner-scale));</code><code class="code-line"> corner-shape: var(--corner-shape);</code><code class="code-line">{'}'}</code></pre><template> <pre class="code-block code-numbered" tabindex="0"><code class="code-line">.btn {</code><code class="code-line"> --btn-height: 2.375rem;</code><code class="code-line code-line-highlight"> --btn-radius: var(--radius-control);</code><code class="code-line"> border-radius: calc(var(--btn-radius) * var(--corner-scale));</code><code class="code-line"> corner-shape: var(--corner-shape);</code><code class="code-line">}</code></pre></template>#差異
code-line-added 和 code-line-removed 標示新增與移除的行。色調對應各行原有的行首符號。請像修補檔一樣,在文字中保留 + 和 -,讓使用者單憑文字也能辨識變更,並能將內容直接複製為修補檔。
差異
<pre class="code-block" tabindex="0"><code class="code-line"> @theme {</code><code class="code-line code-line-removed">- --radius-control: .5rem;</code><code class="code-line code-line-added">+ --radius-control: .625rem;</code><code class="code-line"> --radius-surface: 1rem;</code><code class="code-line"> }</code></pre><pre class="code-block" tabindex="0"><code class="code-line"> @theme {'{'}</code><code class="code-line code-line-removed">- --radius-control: .5rem;</code><code class="code-line code-line-added">+ --radius-control: .625rem;</code><code class="code-line"> --radius-surface: 1rem;</code><code class="code-line"> {'}'}</code></pre><template> <pre class="code-block" tabindex="0"><code class="code-line"> @theme {</code><code class="code-line code-line-removed">- --radius-control: .5rem;</code><code class="code-line code-line-added">+ --radius-control: .625rem;</code><code class="code-line"> --radius-surface: 1rem;</code><code class="code-line"> }</code></pre></template>#面板
code-panel 將標頭放在程式碼區塊上方,兩者共用同一個凹槽。檔名放在 code-title 中,控制項位於標頭末端,向內縮 4px,圓角與外框同心。內部的程式碼區塊會省去自己的凹槽樣式。
可在獨立的 code-block 或包住它的 code-panel 上設定 --code-radius。預設值為 var(--radius-control)。若根元素是托盤的直接子元素,則使用 var(--radius-surface),讓圓角配合托盤的內距。面板內的區塊維持直角與透明背景,整組只呈現一道外輪廓。
複製功能只需三行腳本。讀取區塊的文字,再寫入剪貼簿。
面板
<div class="code-panel"> <div class="code-header"> <span class="code-title">app.css</span> <button type="button" class="btn btn-quiet btn-sm" id="copy-app-css"> <svg class="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>Copy </button> </div> <pre class="code-block" tabindex="0"><code>@import '@master/css';@import '@master/ui';@theme {--color-brand: var(--color-indigo-50);}</code></pre> <output class="sr-only"></output></div><script>document.getElementById('copy-app-css').addEventListener('click', function (event) {var button = event.currentTargetvar status = button.closest('.code-panel').querySelector('output')navigator.clipboard.writeText(button.closest('.code-panel').querySelector('.code-block').textContent).then(function () { button.lastChild.textContent = 'Copied' status.textContent = 'Copied' setTimeout(function () { button.lastChild.textContent = 'Copy' status.textContent = '' }, 1600)})})</script>#程式碼或 kbd
用 code 標記讀者要輸入的文字或從程式讀到的內容,用 kbd 標記要按的按鍵。樣式表不提供語法醒目提示,語法醒目提示工具產生的 span 元素在區塊內會保留各自的顏色。
#類別
| 類別 | 說明 |
|---|---|
code | 套用於 <code>,將行內程式碼呈現為文字行內的小標籤 |
code-block | 套用於 <pre> 的程式碼區塊會保留原有換行,並可橫向捲動。若有任一行可能超出區塊寬度,請加上 tabindex="0"。 |
code-line | 程式碼區塊中的一行,使用 <code> 或 <span>。標記各行後,區塊會以格線排列,每行都與最長的一行同寬。 |
code-line-highlight | 以淡色背景標示正在說明的那一行,並在行首加上 2px 的前景色線條。搭配 code-line 使用 |
code-line-added | 差異比對中新增的一行,使用成功色,並在文字中保留 +。搭配 code-line 使用 |
code-line-removed | 差異比對中刪除的一行,使用危險色,並保留文字中的 -。搭配 code-line 使用 |
code-numbered | 使用 CSS 計數器為區塊內的 code-line 元素編號。行號顯示在專用欄位,選取或複製程式碼時不會包含行號。搭配 code-block 使用 |
code-panel | 標頭與程式碼區塊共用同一個容器,內部的程式碼區塊會取消自己的容器樣式。 |
code-header | 區塊上方高度為 40px 的橫列,起始側放名稱,結束側放控制項。若放置複製按鈕,需用腳本將區塊內的文字複製到剪貼簿。 |
code-title | 標頭中的檔案名稱或語言名稱。 |
#自訂屬性
| 屬性 | 預設值 | 說明 |
|---|---|---|
--code-radius | var(--radius-control) | 獨立程式碼區塊在縮放前的圓角半徑。設定於 code-block |
--code-radius | var(--radius-control) | 面板在縮放前的圓角半徑,內嵌控制項也沿用此值。面板直接置於托盤內時,使用 var(--radius-surface)。設定於 code-panel |
--code-gutter | 2ch | 行號所在欄位的寬度:預設容納兩位數;程式碼達一百行以上時設為 3ch,讓行號保持對齊。設定於 code-numbered |
#無障礙
- 若有任何一行可能超出區塊寬度,請加上
tabindex="0",讓使用者能以鍵盤捲動。 - 行號是產生的內容,朗讀、選取或複製程式碼時都不會包含行號。
- 醒目標示是視覺效果。周圍文字若要指涉某一行,請直接說明該行的內容,避免以顏色或行號指稱。