Back to the library

Field — format as you type

Variant: Format as you type

A phone number formatting itself as it is typed, in three bursts with a 450ms breath between them, and then backspaced through a separator in four distinct taps. The idea is in when the punctuation arrives: each separator is inserted by the digit that follows it, never by the one before. If “(415) ” appears after the third digit, the caret ends up after a space the formatter made. Backspace deletes the space, the formatter puts it straight back, and the key seems dead, which is the most common bug in hand-rolled masks. Inserted late, “) ” arrives with the fourth digit and leaves with it, so every backspace removes a number. The loop proves it: deleting “0” takes the hyphen with it, and the caret lands right after “555”. Digits land on the keystroke frame. The mask inks in over 120ms at 42% grey, so it reads as the field’s handwriting rather than the user’s. The 450ms pause between groups is long enough to see “(415” sitting without its bracket, which is the whole idea in one frame. Use it for phone, card, date and IBAN fields.

textkeyboardcontrolcharacterloop

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>Field — format as you type</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 — 5.2s. Ten digits in three bursts — 90ms apart inside a group,
           450ms between groups, long enough to see "(415" sit without its
           ")" — from 400ms; a 970ms read; four separate backspaces 160ms
           apart from 2.9s; the loop rewinds inside a fade at 4.3–4.8s. */
        --cycle: 5.2s;
        --t: calc(var(--cycle) / var(--speed));

        --_quiet: color-mix(in srgb, var(--slate) 55%, transparent);
        --_mask: color-mix(in srgb, var(--slate) 42%, transparent);
        --_halo: color-mix(in srgb, var(--clay) 16%, transparent);
      }

      /* Curves are written out inside @keyframes, not as tokens: a var() in a
         keyframe's animation-timing-function is dropped and falls back to
         ease, which would quietly flatten every curve below. */

      * {
        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;
      }

      .stack {
        display: grid;
        gap: 8px;
        width: min(300px, 86vw);
        animation: seam var(--t) infinite;
      }

      .title,
      .foot {
        font-size: 12.5px;
        color: var(--_quiet);
      }

      .title {
        font-weight: 600;
        letter-spacing: 0.01em;
      }

      .field {
        position: relative;
        height: 50px;
        border: 1.5px solid var(--oat);
        border-radius: calc(12px * var(--round));
        background: var(--paper);
        font-size: 16px;
        line-height: 22px;
      }

      .ring {
        position: absolute;
        inset: -1.5px;
        border: 1.5px solid var(--clay);
        border-radius: calc(12px * var(--round));
        box-shadow: 0 0 0 3px var(--_halo);
      }

      /* Tabular figures: a number is read back in groups, and even digit
         widths keep "555" and "0132" the same length per digit. */
      .value {
        position: absolute;
        top: 12.5px;
        left: 15px;
        white-space: pre;
        font-variant-numeric: tabular-nums;
        letter-spacing: 0.02em;
      }

      .value span {
        color: transparent;
        animation-duration: var(--t);
        animation-timing-function: step-end;
        animation-iteration-count: infinite;
      }

      /* The separator is inserted by the digit that follows it, never by the
         one before. "(415) " with the caret after a mask-made space is a
         trap: Backspace deletes the space, the formatter puts it straight
         back, and the key appears dead. Arriving late, ") " leaves with the
         digit that brought it, so a backspace always removes a number.
         Digits land on the keystroke's frame; the mask inks in over 120ms
         at 42% — it is the field's handwriting, not the user's. */
      .value span:nth-child(1) {
        animation-name: k0;
      }

      .value span:nth-child(2) {
        animation-name: k1;
      }

      .value span:nth-child(3) {
        animation-name: k2;
      }

      .value span:nth-child(4) {
        animation-name: k3;
      }

      .value span:nth-child(5) {
        animation-name: k4;
      }

      .value span:nth-child(6) {
        animation-name: k5;
      }

      .value span:nth-child(7) {
        animation-name: k6;
      }

      .value span:nth-child(8) {
        animation-name: k7;
      }

      .value span:nth-child(9) {
        animation-name: k8;
      }

      .value span:nth-child(10) {
        animation-name: k9;
      }

      .value span:nth-child(11) {
        animation-name: k10;
      }

      .value span:nth-child(12) {
        animation-name: k11;
      }

      .value span:nth-child(13) {
        animation-name: k12;
      }

      .value span:nth-child(14) {
        animation-name: k13;
      }

      .lead {
        position: absolute;
        top: 14px;
        left: 15px;
        width: 2px;
        height: 19px;
        margin-left: -1px;
        border-radius: 999px;
        background: var(--clay);
        opacity: 0;
        animation: lead var(--t) step-end infinite;
      }

      @keyframes lead {
        0% {
          opacity: 1;
        }
        7.692% {
          opacity: 0;
        }
        86.538% {
          opacity: 1;
        }
        100% {
          opacity: 1;
        }
      }

      @keyframes seam {
        0% {
          opacity: 1;
        }
        82.692% {
          opacity: 1;
          animation-timing-function: ease;
        }
        86.538% {
          opacity: 0;
        }
        88.462% {
          opacity: 0;
          animation-timing-function: ease;
        }
        92.308% {
          opacity: 1;
        }
        100% {
          opacity: 1;
        }
      }

      @keyframes k0 {
        0% {
          color: transparent;
        }
        7.692% {
          color: transparent;
          animation-timing-function: cubic-bezier(0.23, 1, 0.32, 1);
        }
        10% {
          color: var(--_mask);
          animation-timing-function: step-end;
        }
        86.538% {
          color: transparent;
          animation-timing-function: step-end;
        }
        100% {
          color: transparent;
        }
      }

      @keyframes k1 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        7.692% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        9.423% {
          color: var(--slate);
          box-shadow: none;
        }
        86.538% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k2 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        9.423% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        11.154% {
          color: var(--slate);
          box-shadow: none;
        }
        86.538% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k3 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        11.154% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        19.808% {
          color: var(--slate);
          box-shadow: none;
        }
        86.538% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k4 {
        0% {
          color: transparent;
        }
        19.808% {
          color: transparent;
          animation-timing-function: cubic-bezier(0.23, 1, 0.32, 1);
        }
        22.115% {
          color: var(--_mask);
          animation-timing-function: step-end;
        }
        86.538% {
          color: transparent;
          animation-timing-function: step-end;
        }
        100% {
          color: transparent;
        }
      }

      @keyframes k5 {
        0% {
          color: transparent;
        }
        19.808% {
          color: transparent;
          animation-timing-function: cubic-bezier(0.23, 1, 0.32, 1);
        }
        22.115% {
          color: var(--_mask);
          animation-timing-function: step-end;
        }
        86.538% {
          color: transparent;
          animation-timing-function: step-end;
        }
        100% {
          color: transparent;
        }
      }

      @keyframes k6 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        19.808% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        21.538% {
          color: var(--slate);
          box-shadow: none;
        }
        86.538% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k7 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        21.538% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        23.269% {
          color: var(--slate);
          box-shadow: none;
        }
        86.538% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k8 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        23.269% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        31.923% {
          color: var(--slate);
          box-shadow: none;
        }
        65% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        74.615% {
          color: var(--slate);
          box-shadow: none;
        }
        84.231% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        86.538% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k9 {
        0% {
          color: transparent;
        }
        31.923% {
          color: transparent;
          animation-timing-function: cubic-bezier(0.23, 1, 0.32, 1);
        }
        34.231% {
          color: var(--_mask);
          animation-timing-function: step-end;
        }
        65% {
          color: transparent;
          animation-timing-function: step-end;
        }
        100% {
          color: transparent;
        }
      }

      @keyframes k10 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        31.923% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        33.654% {
          color: var(--slate);
          box-shadow: none;
        }
        61.923% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        65% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k11 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        33.654% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        35.385% {
          color: var(--slate);
          box-shadow: none;
        }
        58.846% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        61.923% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k12 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        35.385% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        37.115% {
          color: var(--slate);
          box-shadow: none;
        }
        55.769% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        58.846% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k13 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        37.115% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        46.731% {
          color: var(--slate);
          box-shadow: none;
        }
        55.769% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @media (prefers-reduced-motion: reduce) {
        /* Nothing travels, so nothing is removed: the whole clock, the mask's
           120ms ink included, runs 1.6x slower. */
        .stack,
        .value span,
        .lead {
          animation-duration: calc(var(--t) * 1.6);
        }
      }
    </style>
  </head>
  <body>
    <div
      class="stack"
      role="img"
      aria-label="A phone field formatting as it is typed. Each separator appears, in a lighter grey, only with the digit that follows it; backspacing the last four digits removes the hyphen together with the first of them, so the caret is never left behind punctuation."
    >
      <p class="title">Phone</p>
      <div class="field">
        <div class="ring"></div>
        <span class="value"
          ><span>(</span><span>4</span><span>1</span><span>5</span><span>)</span><span> </span
          ><span>5</span><span>5</span><span>5</span><span>-</span><span>0</span><span>1</span
          ><span>3</span><span>2</span></span
        >
        <span class="lead"></span>
      </div>
      <p class="foot">We’ll text a code to confirm</p>
    </div>
  </body>
</html>

The variant family

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