網站導覽

v2.1.0
文字排版

程式碼

以等寬字型呈現原樣文字,可放在行內或獨立成區塊。

本頁內容

程式碼以等寬字型原樣呈現指令、值或檔名等文字。行內用法是在 <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-radiusvar(--radius-control)獨立程式碼區塊在縮放前的圓角半徑。設定於 code-block
--code-radiusvar(--radius-control)面板在縮放前的圓角半徑,內嵌控制項也沿用此值。面板直接置於托盤內時,使用 var(--radius-surface)。設定於 code-panel
--code-gutter2ch行號所在欄位的寬度:預設容納兩位數;程式碼達一百行以上時設為 3ch,讓行號保持對齊。設定於 code-numbered

#無障礙

  • 若有任何一行可能超出區塊寬度,請加上 tabindex="0",讓使用者能以鍵盤捲動。
  • 行號是產生的內容,朗讀、選取或複製程式碼時都不會包含行號。
  • 醒目標示是視覺效果。周圍文字若要指涉某一行,請直接說明該行的內容,避免以顏色或行號指稱。

#原始碼

@master/ui/components/code.css
/* * Code: literal text in the mono face. Inline, it is a small chip in the line; as a block, a well that * scrolls on its own when a line is long. * A block may mark its lines. Each line is then a code-line, and the block lays them out as a grid, so a * line's band runs the whole scrolling width and the line breaks between the spans cost nothing. Lines can * be numbered by a CSS counter, highlighted, or marked as added and removed in a diff. * A panel puts a header over the block: the file's name and a control, in one well. * Code is literal: the spacing a page may add between Han and Latin letters (text-autospace), the trimming of * CJK punctuation (text-spacing-trim) and a hyphen at a line break would paint a form that is not the source, * so the inline chip, the block and the title turn all three off. */@layer components.ui {  .code {    padding: .125em .375em;    border-radius: calc((var(--radius-chip) - 2px) * var(--corner-scale));    corner-shape: var(--corner-shape);    background: var(--code-tone, var(--color-surface-chip));    color: var(--code-ink, var(--color-text-strong));    box-shadow: inset 0 0 0 .5px var(--code-line, var(--color-line-contour));    font: 400 .875em/1.4 var(--font-family-mono);    font-variant-ligatures: none;    text-autospace: no-autospace;    text-spacing-trim: space-all;    hyphens: none;    overflow-wrap: anywhere;    /* Set in a right-to-left sentence, a run of code keeps the order it was written in, signs and slashes included. */    unicode-bidi: plaintext;    /* Broken across lines, each piece keeps the chip's padding, corner and edge. */    -webkit-box-decoration-break: clone;    box-decoration-break: clone;    @media (forced-colors: active) {      outline: 1px solid CanvasText;      outline-offset: -1px;    }  }  /* On a <pre>. Lines keep their breaks and the block scrolls sideways. */  .code-block {    --code-radius: var(--radius-control);    --code-padding-x: 1rem;    --code-padding-y: .875rem;    margin: 0;    padding: var(--code-padding-y) var(--code-padding-x);    border-radius: calc(var(--code-radius) * var(--corner-scale));    corner-shape: var(--corner-shape);    overflow: auto;    background: var(--color-surface-inset);    color: var(--color-text-strong);    box-shadow: var(--shadow-pressed);    font: 400 .8125rem/1.65 var(--font-family-mono);    font-variant-ligatures: none;    text-autospace: no-autospace;    text-spacing-trim: space-all;    tab-size: 2;    white-space: pre;    scrollbar-width: thin;    scrollbar-color: var(--color-line-strong) transparent;    overscroll-behavior-inline: contain;    &:focus-visible {      @apply --focus-ring;    }    /*     * Marked lines: the block becomes a grid of lines, each as wide as the longest. White space between the     * line elements is not rendered in a grid, so the markup may keep one line of source per line of code.     */    &:has(.code-line) {      display: grid;      align-content: start;      counter-reset: code-line;    }    @media (forced-colors: active) {      outline: 1px solid CanvasText;      outline-offset: -1px;    }  }  /* One line of a block. It bleeds to the block's edges, so a band on it meets the well's contour. */  .code-line {    display: block;    min-height: 1lh;    /* The 2px rule a marked line draws at its start: on the left, or on the right of a right-to-left line. */    --code-line-rule: 2px;    margin-inline: calc(-1 * var(--code-padding-x));    padding-inline: var(--code-padding-x);    counter-increment: code-line;    &:dir(rtl) {      --code-line-rule: -2px;    }    /* The line being talked about: a tint, and a 2px rule of ink at its start. */    &.code-line-highlight {      background: color-mix(in oklab, var(--color-text-strong) 6%, transparent);      box-shadow: inset var(--code-line-rule) 0 0 var(--color-text-strong);    }    /*     * A diff. The hue agrees with the sign the line already starts with, + or -, which is part of the text     * and is what a reader copies; a removed line also steps back in ink.     */    &.code-line-added {      background: color-mix(in oklab, var(--color-success) 11%, transparent);      box-shadow: inset var(--code-line-rule) 0 0 var(--color-success);    }    &.code-line-removed {      background: color-mix(in oklab, var(--color-danger) 10%, transparent);      color: var(--color-text-body);      box-shadow: inset var(--code-line-rule) 0 0 var(--color-danger);    }    @media (forced-colors: active) {      &:is(.code-line-highlight, .code-line-added, .code-line-removed) {        outline: 1px solid Highlight;        outline-offset: -1px;      }    }  }  /*   * Numbered: each line counts itself in a quiet gutter that is not selected with the code. The gutter holds two   * digits; a listing of a hundred lines or more sets --code-gutter: 3ch, so every number ends on one edge.   */  .code-numbered {    --code-gutter: 2ch;    .code-line::before {      content: counter(code-line);      display: inline-block;      width: var(--code-gutter);      margin-inline-end: 1.25rem;      color: var(--color-text-disabled);      text-align: end;      user-select: none;      -webkit-user-select: none;    }    .code-line:is(.code-line-highlight, .code-line-added, .code-line-removed)::before {      color: var(--color-text-muted);    }    /* The gutter stays quieter than the code in forced colors, as a snippet's prompt does. */    @media (forced-colors: active) {      .code-line::before {        color: GrayText;      }    }  }  /*   * Panel: one well around a header and a block. The block inside gives up its own well; the header is a   * 40px strip over a hairline, with the name at the start and a control 4px in from the end.   */  .code-panel {    --code-radius: var(--radius-control);    display: flex;    flex-direction: column;    min-width: 0;    margin: 0;    border-radius: calc(var(--code-radius) * var(--corner-scale));    corner-shape: var(--corner-shape);    overflow: clip;    background: var(--color-surface-inset);    box-shadow: var(--shadow-pressed);    .code-block {      border-radius: 0;      background: transparent;      box-shadow: none;      &:focus-visible {        outline-offset: -2px;      }    }    .btn {      --btn-height: 2rem;      --btn-radius: calc(var(--code-radius) - 4px / var(--corner-scale));      --btn-font-size: .78125rem;      --btn-padding-x: .625rem;      --icon-size: .875rem;      --icon-offset: -.0625rem;      min-height: var(--btn-height);    }    @media (forced-colors: active) {      outline: 1px solid CanvasText;      outline-offset: -1px;    }  }  .code-header {    display: flex;    flex: none;    align-items: center;    justify-content: space-between;    gap: .75rem;    min-height: 2.5rem;    padding-block: 0;    padding-inline: 1rem 4px;    border-block-end: 1px solid var(--color-line-hair);  }  /* The name of the file, or of the language. */  .code-title {    --icon-size: .875rem;    display: inline-flex;    align-items: center;    gap: .5rem;    min-width: 0;    margin: 0;    overflow: hidden;    color: var(--color-text-muted);    font: 400 .75rem/1.4 var(--font-family-mono);    font-variant-ligatures: none;    text-autospace: no-autospace;    text-spacing-trim: space-all;    text-overflow: ellipsis;    white-space: nowrap;  }}