Interaction · Complete runnable example

Clipboard button with honest feedback

Only report a successful copy after the clipboard promise resolves.

Live example

This preview runs in an isolated document. Clipboard writes and saved preferences may be blocked, and printing requires the standalone file. A dialog affects only the example. Reset reloads the original example state.

When to use it

Use this pattern for small, explicit copy actions. It gives readers useful feedback whether the browser accepts the clipboard request or blocks it because of permissions, context, or policy.

Try the important details

  1. Use the example copy button and read its status message.
  2. When copying is blocked, use the selected text for a manual copy with your keyboard.
  3. Try it again after changing the sample text. The copied value should preserve its exact characters.

How it works

  • The click handler awaits writeText before announcing a successful copy.
  • Failure leaves the source visible and offers manual selection instead of displaying a false success state.
  • The status region communicates the outcome without replacing the button’s accessible name.

The complete source

The preview, copied text, and downloaded file use this same document.

HTML

<!doctype html>
<!--
MIT License
Copyright (c) 2026 MRHTML

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta
      http-equiv="Content-Security-Policy"
      content="default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; img-src data:; connect-src 'none'; form-action 'none'; object-src 'none'; base-uri 'none'"
    />
    <title>Honest clipboard button | MRHTML</title>
    <style>
      /* MRHTML standalone example. No external styles, fonts, or assets. */
      :root {
        color-scheme: light;
        --paper: #f5f3ee;
        --surface: #fff;
        --ink: #1e2b42;
        --muted: #59657a;
        --line: #d9dee7;
        --soft: #eef1fb;
        --blue: #344ed5;
        --blue-hover: #263caf;
        --button-text: #fff;
        --focus: #b65716;
        --error: #ad253d;
        font:
          16px/1.6 system-ui,
          -apple-system,
          BlinkMacSystemFont,
          'Segoe UI',
          sans-serif;
        color: var(--ink);
        background: var(--paper);
      }
      * {
        box-sizing: border-box;
      }
      body {
        margin: 0;
        min-height: 100svh;
        display: grid;
        place-items: center;
        padding: 40px 24px;
      }
      main {
        width: min(680px, 100%);
        min-width: 0;
      }
      h1,
      h2,
      h3,
      p {
        margin: 0;
      }
      h1,
      h2,
      h3 {
        line-height: 1.15;
        letter-spacing: -0.045em;
      }
      h1 {
        font-size: clamp(1.9rem, 5vw, 2.65rem);
        font-weight: 650;
        max-width: 570px;
      }
      h2 {
        font-size: 1.35rem;
        font-weight: 650;
      }
      p {
        color: var(--muted);
      }
      button,
      input,
      textarea {
        font: inherit;
      }
      button {
        cursor: pointer;
      }
      button:disabled {
        cursor: not-allowed;
        opacity: 0.65;
      }
      button,
      a,
      input,
      textarea {
        -webkit-tap-highlight-color: transparent;
      }
      :focus-visible {
        outline: 3px solid var(--focus);
        outline-offset: 4px;
      }
      .eyebrow {
        display: flex;
        align-items: center;
        gap: 10px;
        color: var(--muted);
        font-size: 0.65rem;
        font-weight: 650;
        letter-spacing: 0.13em;
        text-transform: uppercase;
        margin-bottom: 22px;
      }
      .eyebrow::before {
        content: '';
        width: 8px;
        height: 8px;
        background: var(--blue);
        border-radius: 50%;
      }
      .intro {
        font-size: 0.94rem;
        max-width: 540px;
        margin-top: 17px;
      }
      .card {
        margin-top: 28px;
        padding: 28px;
        background: var(--surface);
        border: 1px solid var(--line);
        border-radius: 16px;
        box-shadow: 0 8px 28px #14264a05;
      }
      .card-label {
        color: var(--muted);
        font-size: 0.62rem;
        font-weight: 650;
        letter-spacing: 0.11em;
        text-transform: uppercase;
        margin-bottom: 11px;
      }
      .button {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 13px;
        padding: 12px 19px;
        min-height: 46px;
        border: 1px solid transparent;
        border-radius: 8px;
        background: var(--blue);
        color: var(--button-text);
        font-size: 0.85rem;
        font-weight: 600;
        line-height: 1.45;
        transition:
          background 0.15s,
          transform 0.15s;
      }
      .button:hover:not(:disabled) {
        background: var(--blue-hover);
        transform: translateY(-1px);
      }
      .button.secondary {
        background: var(--surface);
        border-color: var(--line);
        color: var(--ink);
      }
      .button.secondary:hover:not(:disabled) {
        background: var(--soft);
      }
      .button svg {
        display: block;
        width: 18px;
        height: 18px;
        flex: none;
      }
      .hint {
        font-size: 0.74rem;
        line-height: 1.75;
        margin-top: 20px;
        max-width: 570px;
      }
      .status {
        min-height: 1.6em;
        margin-top: 17px;
        font-size: 0.8rem;
        color: var(--ink);
        overflow-wrap: anywhere;
      }
      .actions {
        display: flex;
        align-items: center;
        flex-wrap: wrap;
        gap: 12px;
      }
      [hidden] {
        display: none !important;
      }
      @media (max-width: 430px) {
        body {
          padding: 30px 18px;
        }
        .card {
          padding: 23px 20px;
        }
        .eyebrow {
          font-size: 0.6rem;
          letter-spacing: 0.11em;
        }
        .intro {
          font-size: 0.88rem;
        }
      }
      @media (prefers-reduced-motion: reduce) {
        *,
        *::before,
        *::after {
          animation: none !important;
          transition: none !important;
        }
        .button:hover:not(:disabled) {
          transform: none;
        }
      }

      .copy-label {
        display: flex;
        align-items: baseline;
        justify-content: space-between;
        gap: 16px;
        margin-bottom: 13px;
      }
      .copy-label label {
        font-size: 0.86rem;
        font-weight: 600;
      }
      .copy-label span {
        color: var(--muted);
        font-size: 0.66rem;
      }
      .copy-source {
        display: block;
        width: 100%;
        min-height: 180px;
        resize: vertical;
        border: 1px solid #b8c3d7;
        border-radius: 9px;
        background: #f8f9fd;
        color: var(--ink);
        padding: 18px;
        font:
          16px/1.75 ui-monospace,
          SFMono-Regular,
          Consolas,
          monospace;
      }
      .copy-actions {
        display: flex;
        align-items: center;
        gap: 14px;
        flex-wrap: wrap;
        margin-top: 18px;
      }
      .copy-actions > p {
        font-size: 0.68rem;
      }
      .manual-copy {
        border-top: 1px solid var(--line);
        margin-top: 22px;
        padding-top: 20px;
      }
      .manual-copy h2 {
        font-size: 1rem;
        letter-spacing: -0.02em;
      }
      .manual-copy > p {
        font-size: 0.78rem;
        margin-block: 10px 16px;
      }
      .manual-copy .button {
        font-size: 0.76rem;
      }
      @media (max-width: 430px) {
        .copy-label {
          flex-wrap: wrap;
          gap: 3px 12px;
        }
        .copy-source {
          padding: 14px;
        }
      }
    </style>
  </head>
  <body>
    <main>
      <p class="eyebrow">MRHTML / Clipboard pattern</p>
      <h1>A small button.<br />A clear result.</h1>
      <p class="intro">
        Copy the exact text below, including its line breaks. If the browser
        blocks clipboard access, select it and copy it yourself.
      </p>
      <section class="card" aria-label="Copy example">
        <div class="copy-label">
          <label for="copy-source">Text to copy</label
          ><span>Edit it, then try the button.</span>
        </div>
        <textarea
          class="copy-source"
          id="copy-source"
          rows="5"
          spellcheck="false"
        >
MRHTML release note
Ready when you are.

Made with care.</textarea>
        <div class="copy-actions">
          <button class="button" id="copy-button" type="button" disabled>
            <svg viewBox="0 0 20 20" fill="none" aria-hidden="true">
              <rect
                x="7"
                y="7"
                width="9"
                height="10"
                rx="2"
                stroke="currentColor"
                stroke-width="1.5"
              />
              <path
                d="M5 13H4a1 1 0 0 1-1-1V4a1 1 0 0 1 1-1h8a1 1 0 0 1 1 1v1"
                stroke="currentColor"
                stroke-width="1.5"
                stroke-linecap="round"
              /></svg
            ><span id="copy-label">Copy text</span>
          </button>
          <p>No trimming. No added text.</p>
        </div>
        <p class="status" id="copy-status" role="status"></p>
        <div class="manual-copy" id="manual-copy" hidden>
          <h2>Manual copy is available.</h2>
          <p>
            Select the text, then use your keyboard's copy shortcut or your
            device's selection menu.
          </p>
          <button class="button secondary" id="select-text" type="button">
            Select all text
          </button>
        </div>
      </section>
      <p class="hint">
        An embedded preview may block clipboard access. The manual selection
        fallback keeps the text usable without reporting a false success.
      </p>
      <noscript
        ><p class="hint">
          JavaScript is off. Select the text above and use your keyboard or
          device menu to copy it.
        </p></noscript
      >
    </main>

    <script>
      // MRHTML copy pattern: await the browser's result; never assume success.
      const source = document.getElementById('copy-source');
      const button = document.getElementById('copy-button');
      const label = document.getElementById('copy-label');
      const status = document.getElementById('copy-status');
      const manual = document.getElementById('manual-copy');
      button.disabled = false;
      function selectText() {
        source.focus();
        source.select();
        source.setSelectionRange(0, source.value.length);
      }
      document.getElementById('select-text').addEventListener('click', () => {
        selectText();
        status.textContent = source.value.length
          ? 'Text selected. Use your keyboard or device menu to copy.'
          : 'The text is empty. Add something to copy.';
      });
      source.addEventListener('input', () => {
        status.textContent = '';
      });
      button.addEventListener('click', async () => {
        // A snapshot is passed unchanged to writeText. Editing is paused until it settles.
        const text = source.value;
        button.disabled = true;
        source.readOnly = true;
        button.setAttribute('aria-busy', 'true');
        label.textContent = 'Copying...';
        status.textContent = '';
        try {
          if (
            !navigator.clipboard ||
            typeof navigator.clipboard.writeText !== 'function'
          )
            throw new Error('Clipboard unavailable');
          await navigator.clipboard.writeText(text);
          manual.hidden = true;
          status.textContent = 'Copied to clipboard.';
        } catch {
          manual.hidden = false;
          status.textContent =
            'Clipboard access is unavailable or was denied. Use the manual copy option below.';
          selectText();
        } finally {
          source.readOnly = false;
          button.disabled = false;
          button.removeAttribute('aria-busy');
          label.textContent = 'Copy text';
        }
      });
    </script>
  </body>
</html>