1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
// deck-it - press and hold, for buttons a stray touch must not fire
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
export const HOLD_MS = 800;
// The tablet buzzes when a hold fires (or a long press opens a menu), so
// the finger knows it can let go.
export const buzz = () => navigator.vibrate?.(40);
// fire runs after the button is held ms; releasing early cancels, and runs
// tap instead when one is given. The button gets .holding meanwhile and
// --hold for the CSS fill.
export function hold(btn, fire, { ms = HOLD_MS, tap } = {}) {
let timer = null;
const cancel = () => {
clearTimeout(timer);
timer = null;
btn.classList.remove("holding");
};
btn.style.setProperty("--hold", `${ms}ms`);
btn.addEventListener("pointerdown", (e) => {
e.preventDefault();
e.stopPropagation();
btn.setPointerCapture(e.pointerId);
btn.classList.add("holding");
timer = setTimeout(() => { cancel(); buzz(); fire(); }, ms);
});
btn.addEventListener("pointerup", () => {
if (timer && tap) tap();
cancel();
});
btn.addEventListener("pointercancel", cancel);
}
|