aboutsummaryrefslogtreecommitdiffstats
path: root/static/hold.js
diff options
context:
space:
mode:
Diffstat (limited to 'static/hold.js')
-rw-r--r--static/hold.js46
1 files changed, 46 insertions, 0 deletions
diff --git a/static/hold.js b/static/hold.js
new file mode 100644
index 0000000..e937a25
--- /dev/null
+++ b/static/hold.js
@@ -0,0 +1,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);
+}