Navigation

v2.1.0
Typography

Snippet

A command to copy: the line, its prompt and a button that copies it.

On this page

A snippet is one command to copy: the line a reader runs in a terminal, its prompt, and a button that copies it. It is a well in the mono face, as a code block is, and as tall as a button, so it can stand beside one. The prompt cannot be selected, the command never wraps, and the button sits 4px in from the end, quiet until it is pointed at.

Snippet

<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>
Copy the command: the icon turns into a check for two seconds.

#The line

snippet holds three things in three columns. snippet-prompt is the glyph a terminal shows before a command, $ or >, in a quieter ink; it cannot be selected, so a reader who selects the line by hand takes the command alone, and it is marked aria-hidden so it is not read aloud. snippet-command is the command, on a <code>. Then the button, a quiet square button whose height and corner the snippet sets: the line less 4px on each side.

A command longer than the line does not wrap: it scrolls sideways, without a scrollbar, and the edge where it goes on fades. The fade is read from the scroll position, so the start is clean until the line has been scrolled. Give the command tabindex="0", so the arrow keys can scroll it; the whole line then takes the focus ring.

A long command

<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>
Scroll the command sideways: each edge fades while there is more past it.

#Copying

Copying needs a script: hand the command's text to navigator.clipboard. The state it leaves is not a class. Beside the button is an empty <output>, snippet-status, which the reader never sees: the script writes "Copied" into it, which a screen reader announces, and empties it two seconds later. While it is not empty, the stylesheet turns the copy icon into a check, so the eye is told the same thing as the ear.

The script in the previews is the whole of it: find the command that is shown, write it to the clipboard, then fill and empty the status. The clipboard is there only on pages served over HTTPS or from localhost. Where scripting is off the button is not drawn, and the command is still text to select and copy by hand.

#Package managers

To give the same command for npm, pnpm, bun and yarn, put a segmented control of native radios first in the snippet and write one snippet-command for each option, in the same order. The control gives up its own well and lies along the top of the snippet, over a hairline; the chosen option rises as a sheet that glides to the next. The command in the chosen option's place is the one shown, with no script, for up to six options.

Package managers

<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>
Choose a package manager: its command takes the line.

#Sizes

The line is 38px, the height of a button. snippet-sm is 32px in 12px type, for a dense page or a table; snippet-lg is 44px in 14px type, for the head of a page, where it stands level with a large button. On a touch screen every size keeps its height; the copy button's target reaches 44px around it.

Sizes

<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>

#Snippet or code

A snippet is one line to run. Several lines, a file or output to read belong in a code block, and a command named inside a sentence is inline code. A key to press is a kbd.

#Accessibility

  • Mark the prompt aria-hidden="true": it is decoration, and the command is what is read.
  • Give the copy button an aria-label, such as "Copy command". Its icons are aria-hidden.
  • Leave snippet-status empty in the markup, with no white space inside it. The script's "Copied" is announced from it; an <output> is a polite live region of its own.
  • Give the command tabindex="0", so a command longer than the line can be scrolled from the keyboard.
  • In forced colors the fade is removed, the well is drawn with an outline and the prompt takes the system's gray text.

#Classes

ClassDescription
snippetThe line: a well 38px tall, in the mono face, with the prompt, the command and the button in three columns
snippet-smA 32px line in 12px type; the default is 38pxApply with snippet
snippet-lgA 44px line in 14px type, for the head of a pageApply with snippet
snippet-promptThe prompt glyph, $ or >, in a quieter ink. It cannot be selected; mark it aria-hidden="true"
snippet-commandThe command, on a <code>: one line that never wraps and scrolls sideways, fading at the edge where it goes on. Give it tabindex="0". With a segmented control, write one per option, in the same order; up to six
snippet-iconThe copy icon in the button, with icon. It steps aside once the status says the command is copied
snippet-checkThe check in the button, with icon, after the copy icon. It takes the copy icon's place once copied
snippet-statusAn empty <output>, not seen, which announces what the script writes into it. Written to, it turns the copy icon into the check; emptied, it turns it back

#Custom properties

PropertyDefaultDescription
--snippet-height2.375remHeight of the line; the button is 8px less, 4px inSet on snippet
--snippet-radiusvar(--radius-control)Circular radius of the well before the corner scale; the button and the options take it less their 4px insetSet on snippet
--snippet-fade1.5remLength of the fade at an edge where a long command goes onSet on snippet

#Source

@master/ui/components/snippet.css
/* * Snippet: a command to copy, on one line: the prompt, the command and a button that copies it. * The line is a well, as a code block is: literal text is cut into the page, and the button that takes it is a * quiet control 4px in from the end, which rises as a sheet under the pointer as it does in a code panel. The * well is as tall as a button (32, 38 and 44px), so a snippet can stand in a row beside one, as an install line * stands beside the main action of a hero. * The prompt is a glyph in the mono face, quieter than the command; it cannot be selected and is not read aloud, * so selecting the line by hand takes the command alone. The command never wraps: a long one scrolls sideways * and fades at the edge where it goes on, read from its own scroll position. Without scroll-driven animations * it scrolls the same, with clean edges. * Copied is not a class: the script writes "Copied" into an <output> beside the button, which announces it, and * the stylesheet reads that the output is no longer empty and turns the copy icon into a check. Emptying it turns * it back. Without scripting the button is not drawn, and the command is still text to select. * With a segmented control in it, the snippet holds one command per package manager: the control gives up its * own well and lies along the top over a hairline, and the command whose place matches the checked option is * the one shown, for up to six, with no script. */@property --snippet-fade-start {  syntax: '<length>';  inherits: false;  initial-value: 0px;}@property --snippet-fade-end {  syntax: '<length>';  inherits: false;  initial-value: 0px;}@layer components.ui {  .snippet {    --snippet-height: 2.375rem;    --snippet-radius: var(--radius-control);    --snippet-padding-x: .875rem;    --snippet-font-size: .8125rem;    --snippet-fade: 1.5rem;    position: relative;    /* Three columns: the prompt, the command and the button. A row of options takes a row of its own above. */    display: grid;    grid-template-columns: auto minmax(0, 1fr) auto;    grid-auto-rows: minmax(var(--snippet-height), auto);    align-items: center;    min-width: 0;    margin: 0;    padding-block: 0;    padding-inline: var(--snippet-padding-x) 4px;    border-radius: calc(var(--snippet-radius) * var(--corner-scale));    corner-shape: var(--corner-shape);    background: var(--color-surface-inset);    color: var(--color-text-strong);    box-shadow: var(--shadow-pressed);    font: 400 var(--snippet-font-size)/1.5 var(--font-family-mono);    font-variant-ligatures: none;    /* A command is literal: no space the page adds between scripts and no trimmed punctuation in it. */    text-autospace: no-autospace;    text-spacing-trim: space-all;    &.snippet-sm {      --snippet-height: 2rem;      --snippet-radius: var(--radius-control-sm);      --snippet-padding-x: .75rem;      --snippet-font-size: .75rem;    }    &.snippet-lg {      --snippet-height: 2.75rem;      --snippet-radius: var(--radius-control-lg);      --snippet-padding-x: 1rem;      --snippet-font-size: .875rem;      .btn {        --icon-size: 1rem;      }    }    /* Focus on the command, which scrolls with the arrow keys, rings the whole line. */    &:has(.snippet-command:focus-visible) {      @apply --focus-ring;    }    /*     * The button in the well sits 4px in, so its corner is the well's corner minus that inset. It does not lift:     * it rises out of the well instead.     */    .btn {      --btn-height: calc(var(--snippet-height) - 8px);      --btn-radius: calc(var(--snippet-radius) - 4px / var(--corner-scale));      --btn-lift: 0px;      --icon-size: .875rem;      --icon-offset: 0px;      grid-column: 3;      min-height: var(--btn-height);      margin-inline-start: .375rem;      /* A square one stays square on touch, where a button alone would widen to 44px. */      &.btn-square {        min-width: var(--btn-height);      }    }    /*     * Package managers: the segmented control lies along the top of the well, over a hairline, and gives up its     * own well; its options rise out of the snippet's, 4px in, as tall and as round as the button below.     */    .segmented {      --segmented-inset: 4px;      --segmented-item-height: calc(var(--snippet-height) - 8px);      --segmented-item-radius: calc(var(--snippet-radius) - 4px / var(--corner-scale));      --segmented-font-size: .75rem;      display: flex;      grid-column: 1 / -1;      align-self: stretch;      gap: 2px;      max-width: none;      margin-inline: calc(-1 * var(--snippet-padding-x)) -4px;      border-radius: 0;      border-block-end: 1px solid var(--color-line-hair);      background: none;      box-shadow: none;    }    .segmented-item {      flex: none;      min-width: 0;      padding-inline: .625rem;    }    /* Each option shows the command in its place; the others step aside. */    &:has(.segmented) .snippet-command {      display: none;    }    &:has(.segmented-item:nth-child(1 of .segmented-item) .segmented-input:checked) .snippet-command:nth-child(1 of .snippet-command),    &:has(.segmented-item:nth-child(2 of .segmented-item) .segmented-input:checked) .snippet-command:nth-child(2 of .snippet-command),    &:has(.segmented-item:nth-child(3 of .segmented-item) .segmented-input:checked) .snippet-command:nth-child(3 of .snippet-command),    &:has(.segmented-item:nth-child(4 of .segmented-item) .segmented-input:checked) .snippet-command:nth-child(4 of .snippet-command),    &:has(.segmented-item:nth-child(5 of .segmented-item) .segmented-input:checked) .snippet-command:nth-child(5 of .snippet-command),    &:has(.segmented-item:nth-child(6 of .segmented-item) .segmented-input:checked) .snippet-command:nth-child(6 of .snippet-command) {      display: block;    }    /* Copying needs a script; without one the button would do nothing, so it is not drawn. */    @media (scripting: none) {      .btn {        display: none;      }    }    @media (forced-colors: active) {      outline: 1px solid CanvasText;      outline-offset: -1px;      /* The control gave up its own well, so it gives up the outline that draws one too; the hairline stays. */      .segmented {        outline: none;      }      &:has(.snippet-command:focus-visible) {        outline: 2px solid Highlight;        outline-offset: 2px;      }    }  }  /* The prompt: a glyph such as $ or >, quieter than the command. Mark it aria-hidden. */  .snippet-prompt {    grid-column: 1;    margin: 0;    margin-inline-end: 1ch;    color: var(--color-text-muted);    font: inherit;    user-select: none;    -webkit-user-select: none;    @media (forced-colors: active) {      color: GrayText;    }  }  /*   * The command: one line that scrolls sideways and never wraps, with no scrollbar. Give it tabindex="0" so the   * keys can scroll it. Where it goes on past an edge, that edge fades.   */  .snippet-command {    display: block;    grid-column: 2;    min-width: 0;    margin: 0;    padding: 0;    overflow: auto hidden;    background: none;    color: inherit;    font: inherit;    white-space: pre;    outline: none;    scrollbar-width: none;    overscroll-behavior-inline: contain;    &::-webkit-scrollbar {      display: none;    }    @supports (animation-timeline: scroll()) {      /* From the start edge to the end edge: right to left, the start edge is on the right. */      --snippet-run: to right;      mask-image: linear-gradient(var(--snippet-run), transparent, black var(--snippet-fade-start), black calc(100% - var(--snippet-fade-end)), transparent);      /* Longhands: tools that fold the timeline into the shorthand write a value no browser reads. */      animation-name: snippet-fade-start, snippet-fade-end;      animation-timing-function: linear;      animation-fill-mode: both;      animation-timeline: scroll(self inline), scroll(self inline);      animation-range: 0 var(--snippet-fade), calc(100% - var(--snippet-fade)) 100%;      &:dir(rtl) {        --snippet-run: to left;      }    }    @media (forced-colors: active) {      mask-image: none;    }  }  /*   * The copy icon, and the check that takes its place once copied, in the strong ink. Both sit in the button, one   * over the other; the swap is a quick cross-fade, and with motion the check grows into place as the icon shrinks.   */  .snippet-icon, .snippet-check {    transition-property: opacity, scale;    transition-duration: var(--duration-quick);    transition-timing-function: var(--easing-glide);  }  .snippet-check {    position: absolute;    inset: 0;    margin: auto;    color: var(--color-text-strong);    opacity: 0;  }  .snippet:has(.snippet-status:not(:empty)) {    .snippet-icon {      opacity: 0;    }    .snippet-check {      opacity: 1;    }  }  @media (prefers-reduced-motion: no-preference) {    .snippet-check {      scale: .6;    }    .snippet:has(.snippet-status:not(:empty)) {      .snippet-icon {        scale: .6;      }      .snippet-check {        scale: 1;      }    }  }  /* An <output> the script writes "Copied" into: announced, never seen. The icon says it to the eye. */  .snippet-status {    position: absolute;    width: 1px;    height: 1px;    margin: 0;    overflow: hidden;    clip-path: inset(50%);    white-space: nowrap;  }}@keyframes snippet-fade-start {  from {    --snippet-fade-start: 0px;  }  to {    --snippet-fade-start: var(--snippet-fade);  }}@keyframes snippet-fade-end {  from {    --snippet-fade-end: var(--snippet-fade);  }  to {    --snippet-fade-end: 0px;  }}