Interaction · Complete runnable example

Keyboard action menu

A compact menu for commands with deliberate opening, dismissal, and focus behavior.

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 a menu button for a compact set of application actions. This sample changes a fictional card's local readiness label and highlight. Ordinary site links should use normal navigation instead.

Try the important details

  1. Open with Enter, Space, or Down Arrow. Up Arrow opens on the last item.
  2. Use Up and Down arrows, Home, End, or an item's initial letter. Escape closes the menu and restores the opener.
  3. Mark the card ready, toggle its highlight, and reset it. Tab closes toward the project note; Shift+Tab returns to the button.

How it works

  • The button's aria-expanded state follows actual visibility. Its aria-controls identifies a menu containing menuitem buttons.
  • Programmatic focus moves among menu items. Pointer clicks outside and focus moving outside close the menu without stealing focus.
  • Commands change only existing DOM text and classes, announce the local outcome, and return focus to the menu button.

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: blob:; connect-src 'none'; form-action 'none'; object-src 'none'; base-uri 'none'"
    />
    <title>MRHTML | A few useful actions.</title>
    <style>
      :root {
        font-family: Arial, Helvetica, sans-serif;
        color-scheme: light;
        --ink: #192f34;
        --muted: #56666a;
        --paper: #f6f5f0;
        --line: #cdd5d0;
        --accent: #186354;
        --soft: #e7efe5;
        --mono: Consolas, 'Liberation Mono', monospace;
      }
      * {
        box-sizing: border-box;
      }
      html {
        scroll-padding-top: 90px;
      }
      body {
        margin: 0;
        background: var(--paper);
        color: var(--ink);
        line-height: 1.6;
      }
      h1,
      h2,
      h3,
      p {
        margin: 0;
      }
      h1,
      h2,
      h3 {
        font-weight: 500;
        letter-spacing: -0.045em;
        line-height: 1.1;
      }
      h1 {
        font-size: clamp(38px, 5.5vw, 65px);
      }
      h2 {
        font-size: 29px;
      }
      h3 {
        font-size: 21px;
      }
      p {
        overflow-wrap: anywhere;
      }
      a {
        color: var(--accent);
        text-underline-offset: 4px;
      }
      button,
      select {
        font: inherit;
        color: inherit;
      }
      button {
        cursor: pointer;
      }
      button,
      a,
      summary {
        touch-action: manipulation;
      }
      button:focus-visible,
      a:focus-visible,
      select:focus-visible,
      [tabindex]:focus-visible,
      summary:focus-visible {
        outline: 3px solid #a94823;
        outline-offset: 4px;
      }
      [hidden] {
        display: none !important;
      }
      .wrap {
        width: min(1050px, calc(100% - 64px));
        margin-inline: auto;
      }
      .header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 20px;
        padding: 28px 0;
        border-bottom: 1px solid var(--line);
      }
      .brand {
        font-size: 15px;
        font-weight: 700;
        display: flex;
        align-items: center;
        gap: 10px;
        letter-spacing: -0.5px;
      }
      .mark {
        display: grid;
        place-items: center;
        width: 28px;
        height: 28px;
        background: var(--accent);
        color: #fff;
        font-weight: bold;
        font-size: 18px;
      }
      .header > p,
      .eyebrow,
      .small {
        font: 10px/1.7 var(--mono);
        letter-spacing: 0.03em;
      }
      .header > p {
        color: var(--muted);
      }
      main {
        padding-block: 43px 50px;
      }
      .intro {
        margin-bottom: 30px;
        max-width: 720px;
      }
      .eyebrow {
        color: var(--accent);
        margin-bottom: 13px;
      }
      .intro > p:last-child {
        font-size: 15px;
        max-width: 570px;
        color: var(--muted);
        margin-top: 18px;
      }
      .surface {
        background: #fff;
        border: 1px solid var(--line);
        border-radius: 14px;
        box-shadow: 0 12px 32px #263e3010;
      }
      .button {
        border: 1px solid var(--accent);
        background: var(--accent);
        color: #fff;
        padding: 10px 16px;
        min-height: 43px;
        border-radius: 7px;
        font-size: 13px;
        font-weight: 600;
      }
      .button.secondary {
        background: #fff;
        color: var(--accent);
        border-color: #a9bbb0;
      }
      .button[aria-disabled='true'],
      button:disabled {
        background: #edf0e9;
        color: #5d695f;
        border-color: #c9d1c4;
        cursor: default;
      }
      .status {
        font-size: 12px;
        color: var(--muted);
        min-height: 22px;
      }
      .note {
        font-size: 12px;
        color: var(--muted);
        margin-top: 20px;
        max-width: 75ch;
      }
      .footer {
        display: flex;
        justify-content: space-between;
        gap: 20px;
        padding-block: 22px;
        border-top: 1px solid var(--line);
        font-size: 10px;
        color: var(--muted);
      }
      .sr-only {
        position: absolute;
        width: 1px;
        height: 1px;
        padding: 0;
        margin: -1px;
        overflow: hidden;
        clip: rect(0, 0, 0, 0);
        white-space: nowrap;
        border: 0;
      }
      noscript p {
        padding: 15px;
        background: #e8ece2;
        border-left: 3px solid var(--accent);
        font-size: 13px;
        margin-bottom: 20px;
      }
      code,
      kbd {
        font: 12px var(--mono);
      }
      kbd {
        border: 1px solid #bcc7bd;
        border-radius: 4px;
        padding: 2px 5px;
        background: #fff;
      }
      button:disabled:focus {
        outline: none;
      }
      @media (max-width: 600px) {
        .wrap {
          width: calc(100% - 36px);
        }
        .header {
          padding-block: 22px;
        }
        .header > p {
          font-size: 8px;
          max-width: 105px;
          text-align: right;
        }
        .brand {
          font-size: 13px;
          gap: 8px;
        }
        main {
          padding-block: 30px 35px;
        }
        .intro {
          margin-bottom: 26px;
        }
        .intro > p:last-child {
          font-size: 14px;
        }
        .eyebrow {
          font-size: 9px;
        }
        h1 {
          font-size: 40px;
        }
        .footer {
          flex-wrap: wrap;
          font-size: 9px;
        }
      }
      @media (max-width: 350px) {
        h1 {
          font-size: 35px;
        }
        .button {
          padding: 10px 12px;
          font-size: 12px;
        }
      }
      @media (prefers-reduced-motion: reduce) {
        *,
        *::before,
        *::after {
          animation: none !important;
          transition: none !important;
          scroll-behavior: auto !important;
        }
      }

      .project-card {
        max-width: 720px;
        margin-inline: auto;
        overflow: visible;
      }
      .project-top {
        display: flex;
        justify-content: space-between;
        gap: 20px;
        padding: 25px;
      }
      .project-top .small {
        font-size: 9px;
        color: var(--muted);
      }
      .state {
        display: inline-block;
        margin-top: 9px;
        padding: 3px 9px;
        border: 1px solid #adbda4;
        border-radius: 20px;
        font-size: 10px;
        color: #47613c;
        background: #f0f4eb;
      }
      .menu-wrap {
        position: relative;
      }
      #actions {
        position: absolute;
        z-index: 5;
        top: 52px;
        right: 0;
        min-width: 205px;
        padding: 7px;
        background: #fff;
        border: 1px solid #acbbae;
        border-radius: 9px;
        box-shadow: 0 12px 35px #16291f20;
      }
      #actions button {
        display: block;
        width: 100%;
        border: 0;
        border-radius: 4px;
        background: #fff;
        color: var(--ink);
        text-align: left;
        padding: 12px;
        font-size: 13px;
      }
      #actions button:focus {
        background: #e8efe5;
        outline: 2px solid #a94823;
        outline-offset: -2px;
      }
      .project-art {
        height: 255px;
        background: #dce7d2;
        position: relative;
        overflow: hidden;
        display: flex;
        align-items: center;
        padding-left: 38px;
      }
      .project-art > span {
        font-size: 57px;
        line-height: 0.92;
        letter-spacing: -3px;
        color: #244633;
        z-index: 1;
      }
      .art-line {
        position: absolute;
        width: 280px;
        height: 280px;
        right: 10px;
        top: 15px;
        border: 1px solid #677957;
        border-radius: 50%;
        transform: rotate(-30deg);
        box-shadow: inset 0 0 0 30px #e8e2ba;
      }
      .art-disc {
        width: 120px;
        height: 120px;
        border-radius: 50%;
        background: #b0be86;
        position: absolute;
        right: 78px;
        top: 81px;
      }
      .project-body {
        padding: 28px;
      }
      .project-body h2 {
        font-size: 34px;
      }
      .project-body > p {
        color: var(--muted);
        font-size: 14px;
        margin-top: 13px;
      }
      details {
        border-top: 1px solid var(--line);
        margin-top: 22px;
        padding-top: 13px;
      }
      summary {
        cursor: pointer;
        font-size: 13px;
        padding: 9px 0;
      }
      details p {
        font-size: 13px;
        color: var(--muted);
        margin-top: 9px;
      }
      .project-card.highlighted {
        outline: 3px solid #966521;
        outline-offset: 5px;
      }
      .action-status {
        margin-top: 25px;
      }
      .state.ready {
        background: #186354;
        color: #fff;
        border-color: #186354;
      }
      @media (max-width: 500px) {
        .project-top {
          padding: 20px;
          gap: 12px;
        }
        .project-top .small {
          font-size: 7px;
        }
        #menu-button {
          font-size: 11px;
          padding-inline: 12px;
        }
        #actions {
          min-width: 185px;
        }
        .project-art {
          height: 230px;
          padding-left: 23px;
        }
        .project-art > span {
          font-size: 49px;
        }
        .art-line {
          right: -70px;
        }
        .art-disc {
          right: -3px;
        }
        .project-body {
          padding: 22px;
        }
        .project-body h2 {
          font-size: 30px;
        }
      }
    </style>
  </head>
  <body>
    <header class="header wrap">
      <span class="brand"
        ><span class="mark" aria-hidden="true">m.</span>MRHTML / Workshop</span
      >
      <p>SMALL PATTERNS.<br />CONSIDERED DETAILS.</p>
    </header>
    <main class="wrap">
      <div class="intro">
        <p class="eyebrow">03 / ACTION MENU</p>
        <h1>A few useful actions.</h1>
        <p>
          A compact command menu for a fictional project card. Each action
          updates this local example and explains what changed.
        </p>
      </div>
      <noscript
        ><p>
          The sample card and its project note remain readable. JavaScript
          enables the local action menu.
        </p></noscript
      >
      <article class="surface project-card" id="project-card">
        <div class="project-top">
          <div>
            <span class="small">WORKSHOP / CONCEPT 04</span>
            <p id="project-state" class="state">Draft</p>
          </div>
          <div class="menu-wrap">
            <button
              class="button secondary"
              type="button"
              id="menu-button"
              aria-haspopup="menu"
              aria-expanded="false"
              aria-controls="actions"
              hidden
            >
              Actions <span aria-hidden="true">⌄</span>
            </button>
            <div id="actions" role="menu" aria-labelledby="menu-button" hidden>
              <button
                type="button"
                role="menuitem"
                tabindex="-1"
                data-action="ready"
              >
                Mark as ready</button
              ><button
                type="button"
                role="menuitem"
                tabindex="-1"
                data-action="highlight"
              >
                Toggle highlight</button
              ><button
                type="button"
                role="menuitem"
                tabindex="-1"
                data-action="reset"
              >
                Reset this card
              </button>
            </div>
          </div>
        </div>
        <div class="project-art" aria-hidden="true">
          <div class="art-line"></div>
          <div class="art-disc"></div>
          <span>room<br />for ideas.</span>
        </div>
        <div class="project-body">
          <h2>The idea shelf</h2>
          <p>A place to collect questions before turning them into plans.</p>
          <details>
            <summary id="review-note">Read the project note</summary>
            <p>
              This fictional card demonstrates local actions. “Ready” is a
              visual label, not a submission, approval, or external workflow.
            </p>
          </details>
        </div>
      </article>
      <p
        id="action-status"
        class="status action-status"
        role="status"
        aria-live="polite"
        aria-atomic="true"
      >
        The card is a draft. No changes have been made.
      </p>
      <p class="note">
        Use Arrow keys, Home, and End inside the menu. Escape closes it and
        returns focus. These are commands, not navigation links.
      </p>
    </main>
    <footer class="footer wrap">
      <span>MRHTML / Original runnable example</span
      ><span>Local content. No network required.</span>
    </footer>
    <script>
      (() => {
        'use strict';

        const trigger = document.querySelector('#menu-button'),
          menu = document.querySelector('#actions');
        const items = [...menu.querySelectorAll('[role="menuitem"]')];
        const card = document.querySelector('#project-card'),
          state = document.querySelector('#project-state'),
          status = document.querySelector('#action-status');
        function close(restore = false) {
          menu.hidden = true;
          trigger.setAttribute('aria-expanded', 'false');
          if (restore) trigger.focus();
        }
        function open(index = 0) {
          menu.hidden = false;
          trigger.setAttribute('aria-expanded', 'true');
          items[index].focus();
        }
        trigger.addEventListener('click', () =>
          menu.hidden ? open() : close(true),
        );
        trigger.addEventListener('keydown', (event) => {
          if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
            event.preventDefault();
            open(event.key === 'ArrowUp' ? items.length - 1 : 0);
          }
        });
        menu.addEventListener('keydown', (event) => {
          const index = items.indexOf(document.activeElement);
          let next;
          if (event.key === 'ArrowDown') next = (index + 1) % items.length;
          if (event.key === 'ArrowUp')
            next = (index - 1 + items.length) % items.length;
          if (event.key === 'Home') next = 0;
          if (event.key === 'End') next = items.length - 1;
          if (next !== undefined) {
            event.preventDefault();
            items[next].focus();
            return;
          }
          if (event.key === 'Escape') {
            event.preventDefault();
            close(true);
          }
          if (event.key === 'Tab') {
            event.preventDefault();
            close();
            (event.shiftKey
              ? trigger
              : document.querySelector('#review-note')
            ).focus();
          }
          if (
            event.key.length === 1 &&
            !event.ctrlKey &&
            !event.metaKey &&
            !event.altKey
          ) {
            const match = items.find((item) =>
              item.textContent
                .trim()
                .toLowerCase()
                .startsWith(event.key.toLowerCase()),
            );
            if (match) {
              event.preventDefault();
              match.focus();
            }
          }
        });
        items.forEach((item) =>
          item.addEventListener('click', () => {
            if (item.dataset.action === 'ready') {
              state.textContent = 'Ready';
              state.classList.add('ready');
              status.textContent =
                'Card marked as ready in this demo only. Nothing was submitted.';
            }
            if (item.dataset.action === 'highlight') {
              const active = card.classList.toggle('highlighted');
              status.textContent = active
                ? 'Highlight added to this card.'
                : 'Highlight removed from this card.';
            }
            if (item.dataset.action === 'reset') {
              state.textContent = 'Draft';
              state.classList.remove('ready');
              card.classList.remove('highlighted');
              status.textContent = 'Card reset to its original draft state.';
            }
            close(true);
          }),
        );
        document.addEventListener('pointerdown', (event) => {
          if (
            !menu.hidden &&
            !menu.contains(event.target) &&
            !trigger.contains(event.target)
          )
            close();
        });
        document.addEventListener('focusin', (event) => {
          if (
            !menu.hidden &&
            !menu.contains(event.target) &&
            event.target !== trigger
          )
            close();
        });
        trigger.hidden = false;
      })();
    </script>
  </body>
</html>