ナビゲーション

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にします。ブロックは各行をグリッドに配置し、すべての行の幅を最長行に揃えます。これにより、行の帯はスクロールできる範囲全体に伸びます。グリッド項目間の空白は描画されないため、要素間で改行しても余分な空白は生じません。コード1行につき1要素を記述します。

注目させたい行には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-radiusは単独のcode-blockか、それを内包するcode-panelに指定します。既定値はvar(--radius-control)です。トレイの直下に置くルート要素にはvar(--radius-surface)を使い、親要素の内側の余白に角丸を合わせます。パネル内のブロックは角丸なしの透明な面になり、全体をひとつの輪郭で囲みます。

コピーにはスクリプトが必要です。ブロックのテキストを読み取り、クリップボードに渡すだけなので、3行で実装できます。

パネル

<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ブロック内の 1 行を表す <code> または <span> に適用します。各行に適用すると、ブロックは各行をグリッドに配置し、幅を最も長い行に揃えます。
code-line-highlight説明の対象となる行を、淡い背景色と行頭の 2px のインク色の罫線で強調します。code-line と併用します
code-line-added差分の追加行を成功を示す色で表示します。テキスト中の + は残します。code-line と併用します
code-line-removed差分の削除行を危険を示す色で表示します。テキスト中の - は残します。code-line と併用します
code-numberedCSS カウンターでブロック内の code-line 要素に番号を付けます。行番号はコード脇の専用欄に表示され、選択やコピーの対象から外れます。code-block と併用します
code-panelヘッダーとコードブロックを 1 つのくぼみで囲みます。内側のコードブロック自体のくぼみは省きます。
code-headerブロック上部の高さ40pxの帯。先頭に名前、末尾にコントロールを配置します。コピーボタンを置く場合は、ブロックのテキストをクリップボードに書き込むスクリプトが必要です。
code-titleヘッダーに表示するファイル名または言語名

#カスタムプロパティ

プロパティ既定値説明
--code-radiusvar(--radius-control)角のスケール適用前の、単独のブロックの円弧の半径指定先:code-block
--code-radiusvar(--radius-control)角のスケール適用前のパネルの円弧の半径です。パネル内のコントロールにも使われます。パネルをトレイに直接収める場合は var(--radius-surface) を使います。指定先:code-panel
--code-gutter2ch行番号を表示する余白の幅です。既定では2桁分です。100行以上のコードでは 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;  }}