Files
meshcore-analyzer/public/gesture-hints.js
T
Kpa-clawbot 03b5d3fe28 fix(#1065): first-visit gesture discoverability hints (#1186)
Red commit: 4e0a168bc0 (CI run: see Checks
tab — branch pushes don't trigger CI on this repo; first CI is on this
PR)

Fixes #1065. Parent: #1052.

## What
First-visit gesture discoverability hints. Brief animated balloons
appear 800ms after page settle on first visit, announcing each gesture:
swipe-row-action, swipe-between-tabs, edge-swipe-drawer,
pull-to-refresh. Each hint dismisses individually via "Got it";
dismissed hints persist across sessions; "Reset gesture hints" in
Customize → Display restores them.

## Decisions
- **localStorage namespace:** `meshcore-gesture-hints-<id>` with keys
`row-swipe`, `tab-swipe`, `edge-drawer`, `pull-refresh`. Value:
`"seen"`.
- **Hint timing:** 800ms post-settle delay (lets page render); no
auto-mark — hints fade after 8s but only "Got it" sets the flag (so
users who miss the fade still see them next visit). Conservative
interpretation of AC.
- **Settings reset location:** Customize → Display tab → "Gesture Hints"
subsection → `↺ Reset gesture hints` button. Calls
`window.GestureHints.reset()` which clears all four keys + removes any
visible balloons.
- **Pull-to-refresh fallback:** hint only shown if `.pull-to-reconnect`
element exists in DOM (per #1063). If absent, the hint is silently
skipped — other 3 still show.
- **prefers-reduced-motion:** `animation-name: none !important` under
the media query; only opacity transition remains.
- **No focus stealing:** no `autofocus`, no `.focus()` calls. Wrapper
has `pointer-events: none`; only the inner balloon + dismiss button
capture pointer, so the row underneath stays interactive (no conflict
with #1185 row-swipe).
- **Singleton + cleanup:** module-scoped `window.__gestureHints1065Init`
counter; `hashchange` listener bound exactly once across SPA mounts;
dismissed hints don't re-show on route change (gated by `localStorage`).
- **Relevance gating:** row-swipe hint only on `/#/packets|nodes|live`;
edge-drawer only at viewport > 768px (matches #1064 drawer scope).

## E2E
`test-gesture-hints-1065-e2e.js` — Playwright covering first-visit show,
"Got it" dismiss + flag persistence, reload-no-show, Settings reset →
reload → re-show, edge-drawer at 1024x800, prefers-reduced-motion →
animation-name: none, focus not stolen, singleton across 5 SPA
round-trips.

E2E assertion added: test-gesture-hints-1065-e2e.js:90

Browser verified: pending CI run.

---------

Co-authored-by: openclaw-bot <bot@openclaw.local>
Co-authored-by: corescope-bot <bot@corescope.local>
2026-05-09 20:03:54 -07:00

209 lines
6.4 KiB
JavaScript

/* gesture-hints.js — Issue #1065
* First-visit gesture discoverability hints.
*
* - localStorage namespace: meshcore-gesture-hints-<hint>
* keys: row-swipe, tab-swipe, edge-drawer, pull-refresh
* value: "seen"
* - Show hint 800ms after page settle; auto-fade 8s; "Got it" dismisses.
* - aria-live=polite, role=status, no focus stealing, pointer-events:none.
* - prefers-reduced-motion: animation-name: none (style.css handles via media query).
* - Singleton + cleanup: module-scoped guard; SPA re-mount must not re-show dismissed.
* - Pull-to-refresh hint only when .pull-to-reconnect element exists in DOM.
* - Edge-drawer hint only at viewport > 768px (where edge-swipe drawer applies).
* - Row-swipe hint only on table pages: /#/packets, /#/nodes, etc.
*/
(function () {
'use strict';
if (window.__gestureHints1065Init) {
window.__gestureHints1065Init++;
return;
}
window.__gestureHints1065Init = 1;
var NS = 'meshcore-gesture-hints-';
var HINTS = {
'row-swipe': {
key: NS + 'row-swipe',
text: 'Tip: swipe a row left for quick actions.',
relevant: function () {
var h = location.hash || '';
return /^#\/(packets|nodes|live)/.test(h);
},
position: 'bottom',
},
'tab-swipe': {
key: NS + 'tab-swipe',
text: 'Tip: swipe left or right to switch tabs.',
relevant: function () {
return !!document.querySelector('[data-bottom-nav]');
},
position: 'bottom',
},
'edge-drawer': {
key: NS + 'edge-drawer',
text: 'Tip: swipe in from the left edge to open navigation.',
relevant: function () {
return window.innerWidth > 768 && !!document.querySelector('.nav-drawer, [data-nav-drawer]');
},
position: 'top-left',
},
'pull-refresh': {
key: NS + 'pull-refresh',
text: 'Tip: pull down to refresh the connection.',
relevant: function () {
return !!document.querySelector('.pull-to-reconnect');
},
position: 'top',
},
};
var SHOW_DELAY_MS = 800;
var AUTO_FADE_MS = 8000;
var _shown = Object.create(null); // hint id → element (currently rendered)
var _scheduledTimer = null;
var _routeChangeBound = false;
function isSeen(id) {
try { return localStorage.getItem(HINTS[id].key) === 'seen'; }
catch (_e) { return false; }
}
function markSeen(id) {
try { localStorage.setItem(HINTS[id].key, 'seen'); } catch (_e) {}
}
function clearAll() {
try {
Object.keys(HINTS).forEach(function (id) { localStorage.removeItem(HINTS[id].key); });
} catch (_e) {}
}
function buildHintEl(id) {
var def = HINTS[id];
var wrap = document.createElement('div');
wrap.className = 'gesture-hint gesture-hint-' + def.position;
// Belt-and-suspenders: inline style guarantees pointer-events:none
// regardless of CSS load order or cascade collisions. The hint must
// never capture clicks; only the inner button does (via .gesture-hint-inner).
wrap.style.pointerEvents = 'none';
wrap.setAttribute('data-gesture-hint', id);
wrap.setAttribute('role', 'status');
wrap.setAttribute('aria-live', 'polite');
wrap.setAttribute('aria-atomic', 'true');
var inner = document.createElement('div');
inner.className = 'gesture-hint-inner';
var msg = document.createElement('span');
msg.className = 'gesture-hint-text';
msg.textContent = def.text;
inner.appendChild(msg);
var btn = document.createElement('button');
btn.type = 'button';
btn.className = 'gesture-hint-dismiss';
btn.setAttribute('data-gesture-hint-dismiss', '');
btn.setAttribute('aria-label', 'Dismiss hint');
btn.textContent = 'Got it';
btn.addEventListener('click', function (e) {
e.preventDefault();
e.stopPropagation();
dismiss(id);
});
inner.appendChild(btn);
wrap.appendChild(inner);
return wrap;
}
function show(id) {
if (_shown[id]) return;
if (isSeen(id)) return;
var def = HINTS[id];
if (!def || !def.relevant()) return;
var el = buildHintEl(id);
document.body.appendChild(el);
_shown[id] = el;
// Auto-fade after AUTO_FADE_MS — does NOT mark seen; user must explicitly dismiss
// (per AC: "Got it" button clears the flag).
var fadeTimer = setTimeout(function () {
if (_shown[id] === el) {
el.classList.add('gesture-hint-fading');
setTimeout(function () {
if (el.parentNode) el.parentNode.removeChild(el);
if (_shown[id] === el) delete _shown[id];
}, 350);
}
}, AUTO_FADE_MS);
el._gestureHintFadeTimer = fadeTimer;
}
function dismiss(id) {
var el = _shown[id];
markSeen(id);
if (el) {
if (el._gestureHintFadeTimer) clearTimeout(el._gestureHintFadeTimer);
if (el.parentNode) el.parentNode.removeChild(el);
delete _shown[id];
}
}
function scheduleHints() {
if (_scheduledTimer) clearTimeout(_scheduledTimer);
_scheduledTimer = setTimeout(function () {
_scheduledTimer = null;
Object.keys(HINTS).forEach(function (id) {
if (!isSeen(id)) show(id);
});
}, SHOW_DELAY_MS);
}
function onRouteChange() {
// Remove hints that are no longer relevant for the new route.
Object.keys(_shown).slice().forEach(function (id) {
var def = HINTS[id];
if (!def || !def.relevant()) {
var el = _shown[id];
if (el && el._gestureHintFadeTimer) clearTimeout(el._gestureHintFadeTimer);
if (el && el.parentNode) el.parentNode.removeChild(el);
delete _shown[id];
}
});
// Re-evaluate: show any not-yet-seen relevant hints.
scheduleHints();
}
function init() {
if (!_routeChangeBound) {
_routeChangeBound = true;
window.addEventListener('hashchange', onRouteChange);
}
scheduleHints();
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init, { once: true });
} else {
init();
}
window.GestureHints = {
show: show,
dismiss: dismiss,
reset: function () {
clearAll();
// Remove any visible.
Object.keys(_shown).slice().forEach(function (id) {
var el = _shown[id];
if (el && el._gestureHintFadeTimer) clearTimeout(el._gestureHintFadeTimer);
if (el && el.parentNode) el.parentNode.removeChild(el);
delete _shown[id];
});
},
_keys: function () {
return Object.keys(HINTS).map(function (id) { return HINTS[id].key; });
},
};
})();