导航

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用成功色标示 diff 中新增的行,保留文本中的 +。与 code-line 一同使用
code-line-removed用危险色标示 diff 中删除的行,保留文本中的 -。与 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;  }}