Back to the library

Caret — undo goes to the edit first

Variant: Undo return

Undo when the caret has wandered off from the edit. A value is selected and deleted, and the user clicks away to another line. On ⌘Z the caret travels back to the edit site in 192ms on ease-in-out — it is already on screen moving from A to B — waits 120ms, and only then does the value return. It goes to the floor first and only then opens the doors: a change made where the user is not looking has to be found, while one made under the caret is simply seen. The travel and the dwell are paid only when the caret is somewhere else; a repeated ⌘Z at the same spot restores instantly, and an edit off screen would scroll into view first. The value and its selection fade back over 120ms, because something that pops back in one frame on undo can be missed, and the selection is restored too, so the user can delete again, retype or carry on. Edits move the comma and the caret instantly; only navigation glides. → collapses the selection into the loop’s opening state, so there is no seam. Under reduced motion the caret jumps instead of travelling and the value still waits before returning. Reach for it for any editor, form or canvas with undo.

textkeyboardtransitionfocusloop

The source

A complete HTML document. Paste it into an empty .html file and it plays — no stylesheet, script, font, or image to fetch.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Caret — undo goes to the edit first</title>
    <style>
      :root {
        /* Neutrals — fixed, never remixed. */
        --ivory: #faf9f5;
        --paper: #ffffff;
        --oat: #e3dacc;
        --slate: #141413;

        /* Knobs — the remix API. A knob exists iff declared here and used below. */
        --hue: 38.8;
        --lift: 0;
        --chroma: 1;
        --round: 1;
        --speed: 1;

        /* Accents — derived from the knobs with the family's offsets baked in,
           so one hue roll moves them together. Defaults reproduce the original hex. */
        --clay: oklch(calc(0.6724 + var(--lift)) calc(0.1308 * var(--chroma)) var(--hue));
        --clay-d: oklch(calc(0.5797 + var(--lift)) calc(0.127 * var(--chroma)) var(--hue));
        --olive: oklch(
          calc(0.6118 + var(--lift)) calc(0.0713 * var(--chroma)) calc(var(--hue) + 88.3)
        );
        --rust: oklch(
          calc(0.5408 + var(--lift)) calc(0.1357 * var(--chroma)) calc(var(--hue) - 10.3)
        );

        /* Clock — 6s. Double-click 12%, Delete 24%, click away 36% (96ms
           glide), ⌘Z 54%: the caret travels home in 192ms, dwells 120ms, and
           the text comes back at 59.2%. → collapses the selection at 78%,
           which is the state the loop began in — there is no seam. */
        --cycle: 6s;
        --t: calc(var(--cycle) / var(--speed));

        --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
        --ease-in-out: cubic-bezier(0.77, 0, 0.175, 1);

        --_read: color-mix(in srgb, var(--slate) 80%, transparent);
        --_quiet: color-mix(in srgb, var(--slate) 52%, transparent);
        --_sel: color-mix(in srgb, var(--clay) 24%, transparent);
      }

      * {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
      }

      body {
        min-height: 100vh;
        display: grid;
        place-items: center;
        padding: clamp(12px, 3vw, 28px);
        background: var(--ivory);
        color: var(--slate);
        font-family:
          system-ui,
          -apple-system,
          'Segoe UI',
          Roboto,
          sans-serif;
      }

      .card {
        width: min(360px, 92vw);
        padding: clamp(15px, 3.6vw, 24px) clamp(16px, 3.8vw, 26px);
        border: 1px solid var(--oat);
        border-radius: calc(14px * var(--round));
        background: var(--paper);
      }

      .eyebrow {
        font-size: clamp(11px, 2.4vw, 12.5px);
        font-weight: 600;
        letter-spacing: 0.09em;
        text-transform: uppercase;
        color: var(--_quiet);
      }

      .code {
        position: relative;
        margin-top: clamp(10px, 2vw, 15px);
        white-space: pre;
        font-family: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, monospace;
        font-size: clamp(14px, 4.4vw, 19px);
        line-height: 1.7;
        color: var(--_read);
        isolation: isolate;
      }

      .code .k {
        color: var(--clay-d);
      }

      .val,
      .tail {
        display: inline-block;
      }

      .val {
        animation: val var(--t) infinite;
      }

      /* The comma rides the edit, snapping 3ch on the same frame the value
         leaves and returns. Text reflow in an editor is not a layout
         animation; it is the document, and it is instant. */
      .tail {
        animation: tail var(--t) infinite;
      }

      /* Undo restores the selection as well as the text, so the user lands
         exactly where they were before the delete — able to delete again,
         retype, or carry on. */
      .sel {
        position: absolute;
        z-index: -1;
        top: calc(3.4em + 0.16em);
        left: 9ch;
        width: 3ch;
        height: 1.38em;
        border-radius: calc(3px * var(--round));
        background: var(--_sel);
        opacity: 0;
        animation: sel var(--t) infinite;
      }

      .caret {
        position: absolute;
        top: 0.22em;
        left: 0;
        width: max(2px, 0.09em);
        height: 1.26em;
        margin-left: -1px;
        border-radius: 999px;
        background: var(--clay);
        animation:
          caret var(--t) infinite,
          blink var(--t) infinite;
      }

      .via {
        display: flex;
        align-items: center;
        gap: 8px;
        margin-top: clamp(12px, 2.6vw, 18px);
        font-size: clamp(11px, 2.5vw, 12.5px);
        color: var(--_quiet);
      }

      .slot {
        position: relative;
        height: 2.6em;
        width: 8.4em;
        overflow: hidden;
      }

      .reel {
        display: flex;
        flex-direction: column;
        animation:
          reel var(--t) infinite,
          cue var(--t) infinite;
      }

      .key {
        display: inline-flex;
        align-items: center;
        height: 2.6em;
      }

      .key b {
        padding: 0.12em 0.6em;
        border: 1px solid var(--oat);
        border-bottom-width: 2px;
        border-radius: calc(5px * var(--round));
        background: var(--ivory);
        font-weight: 500;
        color: var(--slate);
      }

      /* Deleted on one frame; restored over 120ms on ease-out, the one fade
         in the piece — undo is the moment the user most needs to see what
         changed, and a value that blinks back in can be missed. */
      @keyframes val {
        0%,
        23.99% {
          opacity: 1;
        }
        24%,
        59.2% {
          opacity: 0;
          animation-timing-function: var(--ease-out);
        }
        61.2%,
        100% {
          opacity: 1;
        }
      }

      @keyframes tail {
        0%,
        23.99% {
          transform: translateX(0);
        }
        24%,
        59.19% {
          transform: translateX(-3ch);
        }
        59.2%,
        100% {
          transform: translateX(0);
        }
      }

      @keyframes sel {
        0%,
        11.99% {
          opacity: 0;
        }
        12%,
        23.99% {
          opacity: 1;
        }
        24%,
        59.2% {
          opacity: 0;
          animation-timing-function: var(--ease-out);
        }
        61.2%,
        77.99% {
          opacity: 1;
        }
        78%,
        100% {
          opacity: 0;
        }
      }

      /* Navigation glides; edits push. The delete moves the caret 3ch on the
         edit frame. The click away is the family's 96ms ease-out glide. The
         undo is the one long move — 192ms on ease-in-out, because the caret
         is already on screen travelling from A to B — and it finishes before
         anything changes. It goes to the floor first and only then opens the
         doors: a change made where the user is not looking has to be found,
         and one made under the caret is simply seen. */
      @keyframes caret {
        0%,
        23.99% {
          transform: translate(12ch, 3.4em);
        }
        24%,
        36% {
          transform: translate(9ch, 3.4em);
          animation-timing-function: var(--ease-out);
        }
        37.6%,
        54% {
          transform: translate(8ch, 0);
          animation-timing-function: var(--ease-in-out);
        }
        57.2%,
        59.19% {
          transform: translate(9ch, 3.4em);
        }
        59.2%,
        100% {
          transform: translate(12ch, 3.4em);
        }
      }

      /* 500ms on, 500ms off; every event restarts the phase on "on", and the
         caret holds solid through the whole undo, travel and dwell included. */
      @keyframes blink {
        0%,
        3% {
          opacity: 1;
        }
        3.01%,
        11.33% {
          opacity: 0;
        }
        11.34%,
        20.33% {
          opacity: 1;
        }
        20.34%,
        23.99% {
          opacity: 0;
        }
        24%,
        32.33% {
          opacity: 1;
        }
        32.34%,
        35.99% {
          opacity: 0;
        }
        36%,
        45.93% {
          opacity: 1;
        }
        45.94%,
        53.99% {
          opacity: 0;
        }
        54%,
        67.53% {
          opacity: 1;
        }
        67.54%,
        75.86% {
          opacity: 0;
        }
        75.87%,
        86.33% {
          opacity: 1;
        }
        86.34%,
        94.67% {
          opacity: 0;
        }
        94.68%,
        100% {
          opacity: 1;
        }
      }

      @keyframes reel {
        0%,
        11.99% {
          transform: translateY(-10.4em);
        }
        12%,
        23.99% {
          transform: translateY(0);
        }
        24%,
        35.99% {
          transform: translateY(-2.6em);
        }
        36%,
        53.99% {
          transform: translateY(-5.2em);
        }
        54%,
        77.99% {
          transform: translateY(-7.8em);
        }
        78%,
        100% {
          transform: translateY(-10.4em);
        }
      }

      @keyframes cue {
        0%,
        10% {
          opacity: 0.45;
        }
        11.99% {
          opacity: 0;
        }
        12% {
          opacity: 0.2;
          animation-timing-function: var(--ease-out);
        }
        14.5%,
        22.5% {
          opacity: 1;
        }
        23.99% {
          opacity: 0;
        }
        24% {
          opacity: 0.2;
          animation-timing-function: var(--ease-out);
        }
        26.5%,
        34.5% {
          opacity: 1;
        }
        35.99% {
          opacity: 0;
        }
        36% {
          opacity: 0.2;
          animation-timing-function: var(--ease-out);
        }
        38.5%,
        52% {
          opacity: 1;
        }
        53.99% {
          opacity: 0;
        }
        54% {
          opacity: 0.2;
          animation-timing-function: var(--ease-out);
        }
        56.5%,
        76% {
          opacity: 1;
        }
        77.99% {
          opacity: 0;
        }
        78% {
          opacity: 0.2;
          animation-timing-function: var(--ease-out);
        }
        80.5%,
        94% {
          opacity: 1;
        }
        100% {
          opacity: 0.45;
        }
      }

      @media (prefers-reduced-motion: reduce) {
        /* The travel goes; the order stays. The caret steps to the click and
           steps home on ⌘Z, and the value still waits its dwell before it
           returns — so "where, then what" survives without movement — on a
           clock 1.6x slower. */
        .val,
        .tail,
        .sel,
        .caret,
        .reel {
          animation-duration: calc(var(--t) * 1.6);
        }

        .caret {
          animation-name: caret-rm, blink;
        }

        @keyframes caret-rm {
          0%,
          23.99% {
            transform: translate(12ch, 3.4em);
          }
          24%,
          35.99% {
            transform: translate(9ch, 3.4em);
          }
          36%,
          53.99% {
            transform: translate(8ch, 0);
          }
          54%,
          59.19% {
            transform: translate(9ch, 3.4em);
          }
          59.2%,
          100% {
            transform: translate(12ch, 3.4em);
          }
        }
      }
    </style>
  </head>
  <body>
    <div class="card">
      <p class="eyebrow">policy.ts</p>
      <div
        class="code"
        role="img"
        aria-label="The value 250 is selected and deleted, the caret is clicked away to the first line, and on undo the caret travels back to where the value was before the value reappears, still selected."
      ><span class="sel"></span><div>retry: {</div><div>  limit: <span class="k">3</span>,</div><div>  delay: <span class="val k">250</span><span class="tail">,</span></div><div>}</div><span class="caret"></span></div>
      <div class="via" aria-hidden="true">
        <span>via</span>
        <span class="slot"
          ><span class="reel"
            ><span class="key"><b>double-click</b></span
            ><span class="key"><b>Delete</b></span
            ><span class="key"><b>click</b></span
            ><span class="key"><b>⌘Z</b></span
            ><span class="key"><b>→</b></span></span
          ></span
        >
      </div>
    </div>
  </body>
</html>

The variant family

Same idea, one axis moved. Compare them side by side before you commit.