Back to the library

Snap guide — size match

Variant: Size

A resize dragged inward, its width counting down from 128, caught 6px short of a neighbour’s 96 — and the neighbour answers: its outline takes the accent and a dimension rule and badge appear under it, lit on the same frame as the selection’s own. That second readout is the idea. A match is a relation between two objects, so it has to be visible on both, and the neighbour’s width is withheld until the match because until then it is not information. The neighbour sits more than 12px clear of every width the drag passes through, outside the family’s release band, so no edge can align on the way and the only thing the two can agree on is size. The edge leaves the pointer for the last 6px on the family’s 78ms snap while the pointer coasts on another 3 — the frame that shows the edge is being pulled rather than pushed. The neighbour’s badge drops 3px out of its rule in 160ms on an ease-out, the one entrance in the piece. Use it for match-width and match-height snapping in editors and column resizers.

gesturelayoutcounterdiagramloop

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>Snap guide — size match</title>
    <style>
      /* --_w is the selected object's width in px. The fill, the right edge,
         the handle, the pointer and the badge all derive from it. --_n is the
         same value as an integer for the readout. */
      @property --_w {
        syntax: '<number>';
        inherits: true;
        initial-value: 128;
      }
      @property --_n {
        syntax: '<integer>';
        inherits: true;
        initial-value: 128;
      }

      :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 — 3.6s: a resize inward, a catch 6px short of the
           neighbour's width, the drop. */
        --cycle: 3.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);
        /* The snap — the family's 78ms catch. */
        --ease-accent: cubic-bezier(0.32, 0.72, 0, 1);
        /* Approach: leaves rest but reaches the threshold still moving, so
           the snap carries the drag on instead of firing after a stop. */
        --_approach: cubic-bezier(0.55, 0, 0.75, 0.75);

        --_muted: color-mix(in srgb, var(--slate) 52%, transparent);
        --_bar: color-mix(in srgb, var(--oat) 80%, transparent);
        --_rule: color-mix(in srgb, var(--slate) 34%, transparent);
      }

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

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

      .rig {
        display: grid;
        gap: 12px;
        justify-items: center;
        animation: rig var(--t) linear infinite;
      }

      .cap,
      .foot {
        font-size: 11px;
        letter-spacing: 0.03em;
        color: var(--_muted);
      }

      .stage {
        position: relative;
        width: 260px;
        height: 150px;
        background: var(--paper);
        border: 1px solid var(--oat);
        border-radius: calc(10px * var(--round));
        overflow: hidden;
        animation: size var(--t) infinite;
      }

      /* The neighbour sits to the right of every width the selection passes
         through, so no edge ever lines up with it on the way — the only
         thing the two can agree on is size. */
      .ref {
        position: absolute;
        left: 156px;
        top: 22px;
        width: 96px;
        height: 34px;
        padding: 0 10px;
        display: grid;
        align-content: center;
        gap: 6px;
        background: var(--paper);
        border: 1px solid var(--oat);
        animation: ref-edge var(--t) linear infinite;
      }

      .ref i {
        display: block;
        height: 5px;
        border-radius: 999px;
        background: var(--_bar);
      }

      .ref i + i {
        width: 60%;
      }

      /* A resize must not animate width, so the box is built from parts:
         a fill that scales (only its top and bottom borders, which a
         horizontal scale leaves 1.5px thick), a fixed left edge and a right
         edge that translates. Square corners, as canvas objects have. */
      .fill {
        position: absolute;
        left: 14px;
        top: 76px;
        width: 100px;
        height: 34px;
        background: var(--paper);
        border-top: 1.5px solid var(--slate);
        border-bottom: 1.5px solid var(--slate);
        transform-origin: left center;
        transform: scaleX(calc(var(--_w) / 100));
      }

      .edge {
        position: absolute;
        left: 14px;
        top: 76px;
        width: 1.5px;
        height: 34px;
        background: var(--slate);
      }

      .edge.r {
        left: 12.5px;
        transform: translateX(calc(var(--_w) * 1px));
      }

      .handle {
        position: absolute;
        left: calc(14px - 4px);
        top: calc(93px - 4px);
        width: 8px;
        height: 8px;
        background: var(--paper);
        border: 1.5px solid var(--slate);
        transform: translateX(calc(var(--_w) * 1px - 0.75px));
      }

      /* Dimension rules: one under each object, lit together on the match.
         The selection's scales from its fixed left end; ticks are separate
         so the scale never fattens them. */
      .rule,
      .tick {
        position: absolute;
        background: var(--_rule);
        animation: agree-rule var(--t) linear infinite;
      }

      .rule {
        height: 1px;
      }

      .tick {
        width: 1px;
        height: 5px;
      }

      .rule.mine {
        left: 14px;
        top: 116px;
        width: 100px;
        transform-origin: left center;
        transform: scaleX(calc(var(--_w) / 100));
      }

      .tick.mine-l {
        left: 14px;
        top: 114px;
      }

      .tick.mine-r {
        left: 13px;
        top: 114px;
        transform: translateX(calc(var(--_w) * 1px));
      }

      .theirs-dim {
        position: absolute;
        left: 156px;
        top: 60px;
        width: 96px;
        height: 5px;
        opacity: 0;
        animation: theirs-rule var(--t) linear infinite;
      }

      .theirs-dim .rule {
        left: 0;
        top: 2px;
        width: 96px;
        background: var(--clay-d);
        animation: none;
      }

      .theirs-dim .tick {
        top: 0;
        background: var(--clay-d);
        animation: none;
      }

      .theirs-dim .tick + .tick {
        left: 95px;
      }

      .badge {
        position: absolute;
        padding: 1px 5px;
        border-radius: calc(4px * var(--round));
        font-size: 11px;
        font-weight: 600;
        line-height: 16px;
        font-variant-numeric: tabular-nums;
        white-space: nowrap;
      }

      .badge.mine {
        left: 14px;
        top: 122px;
        background: var(--oat);
        color: var(--slate);
        transform: translateX(calc(-50% + var(--_w) * 0.5px));
        animation: agree-badge var(--t) linear infinite;
      }

      .badge.mine::before {
        counter-reset: n var(--_n);
        content: 'W ' counter(n);
      }

      /* The neighbour's width is shown only at the match: it is not
         information until it is the answer. */
      .badge.theirs {
        left: 204px;
        top: 68px;
        background: var(--clay-d);
        color: var(--paper);
        opacity: 0;
        transform: translateX(-50%);
        animation: theirs var(--t) linear infinite;
      }

      .hand {
        position: absolute;
        inset: 0;
        animation: hand var(--t) infinite;
      }

      .ptr {
        position: absolute;
        left: 13px;
        top: 92px;
        width: 14px;
        height: 18px;
        opacity: 0;
        animation: ptr var(--t) linear infinite;
      }

      .arrow {
        display: block;
        width: 100%;
        height: 100%;
        fill: var(--slate);
        stroke: var(--paper);
        stroke-width: 1.4;
        stroke-linejoin: round;
      }

      .tip {
        position: absolute;
        left: -9px;
        top: -9px;
        width: 20px;
        height: 20px;
        border-radius: 50%;
        background: var(--clay-d);
        opacity: 0;
        transform: scale(0.5);
        animation: grip var(--t) infinite;
      }

      @keyframes size {
        0%,
        8% {
          --_w: 128;
          --_n: 128;
          animation-timing-function: var(--_approach);
        }
        40% {
          --_w: 102;
          --_n: 102;
          animation-timing-function: var(--ease-accent);
        }
        42.2%,
        95% {
          --_w: 96;
          --_n: 96;
        }
        95.1%,
        100% {
          --_w: 128;
          --_n: 128;
        }
      }

      /* The pointer coasts on to 99 while the edge leaves it for 96 — the
         frame that shows the edge is being pulled, not pushed. */
      @keyframes hand {
        0%,
        8% {
          transform: translate(128px, 0);
          animation-timing-function: var(--_approach);
        }
        40% {
          transform: translate(102px, 0);
          animation-timing-function: var(--ease-out);
        }
        46% {
          transform: translate(99px, 0);
          animation-timing-function: var(--ease-out);
        }
        58%,
        95% {
          transform: translate(114px, 20px);
        }
        95.1%,
        100% {
          transform: translate(128px, 0);
        }
      }

      @keyframes agree-badge {
        0%,
        40% {
          background: var(--oat);
          color: var(--slate);
        }
        40.5%,
        100% {
          background: var(--clay-d);
          color: var(--paper);
        }
      }

      /* The neighbour's own badge drops 3px out of its edge in 160ms — the
         one entrance in the piece, so it is the one that eases. */
      @keyframes theirs {
        0%,
        40% {
          opacity: 0;
          transform: translate(-50%, -3px);
          animation-timing-function: var(--ease-out);
        }
        44.4%,
        100% {
          opacity: 1;
          transform: translate(-50%, 0);
        }
      }

      @keyframes agree-rule {
        0%,
        40% {
          background: var(--_rule);
        }
        40.5%,
        100% {
          background: var(--clay-d);
        }
      }

      @keyframes theirs-rule {
        0%,
        40% {
          opacity: 0;
        }
        40.5%,
        100% {
          opacity: 1;
        }
      }

      @keyframes ref-edge {
        0%,
        40% {
          border-color: var(--oat);
        }
        40.5%,
        100% {
          border-color: var(--clay);
        }
      }

      @keyframes ptr {
        0% {
          opacity: 0;
        }
        6%,
        46% {
          opacity: 1;
        }
        58%,
        100% {
          opacity: 0;
        }
      }

      @keyframes grip {
        0%,
        7.5% {
          opacity: 0;
          transform: scale(0.5);
          animation-timing-function: var(--ease-out);
        }
        8%,
        45.5% {
          opacity: 0.4;
          transform: scale(1);
          animation-timing-function: var(--ease-out);
        }
        46.5%,
        100% {
          opacity: 0;
          transform: scale(0.5);
        }
      }

      @keyframes rig {
        0% {
          opacity: 0;
        }
        3%,
        89% {
          opacity: 1;
        }
        94%,
        100% {
          opacity: 0;
        }
      }

      @media (prefers-reduced-motion: reduce) {
        /* Parked matched: both widths stated, and the pair of badges
           breathes together as the one live thing on the card. */
        .rig,
        .stage,
        .hand,
        .ref {
          animation: none;
        }

        .stage {
          --_w: 96;
          --_n: 96;
        }

        .hand {
          opacity: 0;
        }

        .ref {
          border-color: var(--clay);
        }

        .rule,
        .tick {
          animation: none;
          background: var(--clay-d);
        }

        .theirs-dim {
          animation: none;
          opacity: 1;
        }

        .badge.mine,
        .badge.theirs {
          background: var(--clay-d);
          color: var(--paper);
          transform: translateX(calc(-50% + var(--_w) * 0.5px));
          animation: badge-rest calc(var(--t) * 1.2) ease-in-out infinite;
        }

        .badge.theirs {
          transform: translateX(-50%);
        }

        @keyframes badge-rest {
          0%,
          100% {
            opacity: 0.55;
          }
          50% {
            opacity: 1;
          }
        }
      }
    </style>
  </head>
  <body>
    <div
      class="rig"
      role="img"
      aria-label="A box's right edge is dragged inward, its width counting down from 128. Six pixels short of its neighbour's width it snaps to 96, and the neighbour shows its own width, 96, so both read the same."
    >
      <p class="cap">resize · catch · match</p>
      <div class="stage">
        <div class="ref"><i></i><i></i></div>
        <span class="badge theirs">W 96</span>
        <div class="fill"></div>
        <i class="edge"></i>
        <i class="edge r"></i>
        <i class="handle"></i>
        <i class="rule mine"></i>
        <i class="tick mine-l"></i>
        <i class="tick mine-r"></i>
        <span class="badge mine"></span>
        <div class="theirs-dim"><i class="rule"></i><i class="tick"></i><i class="tick"></i></div>
        <div class="hand">
          <span class="ptr">
            <span class="tip"></span>
            <svg class="arrow" viewBox="0 0 14 18">
              <path d="M1 1 L1 14.8 L4.5 11.6 L6.7 16.6 L9.3 15.4 L7.1 10.5 L11.8 10.3 Z" />
            </svg>
          </span>
        </div>
      </div>
      <p class="foot">the match shows on both objects</p>
    </div>
  </body>
</html>

The variant family

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