Open in a new tab

Swipe a card left or right to change it and double-tap to like it, with mouse, pen or touch; buttons and arrow keys do the same without a gesture.

Uses @awacloud/fw (Apache-2.0). This example's code is Apache-2.0.

When to use it

Touch-first surfaces: a carousel, a photo viewer, cards to sort. For moving an item between containers, see fw/dom-dnd.

Copy-paste steps

  1. npm install @awacloud/fw
  2. Serve node_modules/@awacloud/fw/src/ at /vendor/fw/ (node serve.js from the examples root does this; any static server works).
  3. Copy index.html, main.js and style.css next to each other and open index.html through that server. The ../../_style/examples.css link is optional: copy that file along, or delete the link.

What it shows

  • gesture.attach(el) returns a controller; controller.on('swipe', …) receives { direction, distance, velocityPxPerMs }, and on('doubletap', …) fires on the second quick tap. The same detector also emits tap, longpress, pan and pinch.
  • A tap fires on each tap, the first of a double tap included: the page listens only to doubletap, so a single tap does nothing.
  • touch-action: pan-y on the card: the page still scrolls vertically, and horizontal moves reach the detector instead of the browser.
  • A gesture is never the only way (WCAG 2.5.1): Previous, Like and Next buttons, and the arrow keys on the focused card, call the same functions.

Code

main.js

javascriptApache-2.046 lines

// SPDX-License-Identifier: Apache-2.0
import fw from '@awacloud/fw';
import { gesture } from '@awacloud/fw/dom/query/gesture.js';

fw.runtime.registerAllDeep([gesture]);
const GESTURE = fw.runtime.resolve('gesture');

/** @param {string} id */
const el = (id) => /** @type {HTMLElement} */ (document.getElementById(id));
const CARDS = ['Lisbon', 'Kyoto', 'Montréal', 'Nairobi'];
/** Indexes of the liked cards. */
const liked = new Set();
let index = 0;

/** Show a card (wrapping around), with its position and like state. @param {number} to */
function show(to) {
    index = (to + CARDS.length) % CARDS.length;
    el('title').textContent = CARDS[index];
    el('position').textContent = `${index + 1} of ${CARDS.length}`;
    el('like').setAttribute('aria-pressed', String(liked.has(index)));
    el('card').classList.toggle('liked', liked.has(index));
}

function toggleLike() {
    if (!liked.delete(index)) liked.add(index);
    show(index);
}

// Gestures on the card: one pointer for mouse, pen or touch. Swipe left = next, as on a phone.
const card = GESTURE.attach(el('card'));
card.on('swipe', (/** @type {{ direction: string }} */ { direction }) => {
    if (direction === 'left') show(index + 1);
    if (direction === 'right') show(index - 1);
});
card.on('doubletap', toggleLike);

// Every gesture has a single-click and a keyboard path (WCAG 2.5.1).
el('prev').addEventListener('click', () => show(index - 1));
el('next').addEventListener('click', () => show(index + 1));
el('like').addEventListener('click', toggleLike);
el('card').addEventListener('keydown', (event) => {
    if (event.key === 'ArrowLeft') show(index - 1);
    if (event.key === 'ArrowRight') show(index + 1);
});

show(0);

index.html

htmlApache-2.031 lines

<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>fw · Swipe and double-tap a card</title>
  <link rel="stylesheet" href="../../_style/examples.css">
  <link rel="stylesheet" href="./style.css">
  <script type="importmap">
  { "imports": {
    "@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/" } }
  </script>
  <script type="module" src="./main.js"></script>
</head>
<body>
  <main>
    <h1>Swipe and double-tap a card</h1>
    <p>Swipe the card left or right, double-tap it to like it, or use the buttons and the arrow keys.</p>
    <section id="card" class="card" tabindex="0" aria-roledescription="carousel" aria-label="Cities">
      <h2 id="title" aria-live="polite"></h2>
      <p id="position"></p>
    </section>
    <p class="controls">
      <button id="prev" type="button">Previous</button>
      <button id="like" type="button" aria-pressed="false">Like</button>
      <button id="next" type="button">Next</button>
    </p>
  </main>
</body>
</html>

style.css

cssApache-2.011 lines

/* SPDX-License-Identifier: Apache-2.0 */
/* pan-y: the page still scrolls vertically; horizontal moves reach the gesture detector. */
.card {
  touch-action: pan-y; user-select: none; cursor: grab;
  display: grid; place-content: center; text-align: center; min-height: 12rem; margin-block: 1rem;
  border: 1px solid var(--ex-border, #ccc); border-radius: 12px;
}
.card.liked { outline: 3px solid var(--ex-accent, #2563eb); }
.card h2 { margin: 0; }
.controls { display: flex; gap: 0.5rem; justify-content: center; }
#like[aria-pressed="true"] { font-weight: bold; }