Add useHasPrimaryTouch hook to detect primary touch capability

This commit is contained in:
SameerJS6
2025-09-26 08:57:46 +05:30
parent 8ce308ed36
commit 3c56cb6b55
+31
View File
@@ -0,0 +1,31 @@
import { browser } from "$app/environment";
import { writable } from "svelte/store";
export function useHasPrimaryTouch() {
const { subscribe, set } = writable(false);
if (!browser) {
return {
subscribe,
destroy: () => {},
};
}
const controller = new AbortController();
const { signal } = controller;
const handleTouch = () => {
const hasTouch = "ontouchstart" in window || navigator.maxTouchPoints > 0;
const prefersTouch = window.matchMedia("(pointer: coarse)").matches;
set(hasTouch && prefersTouch);
};
const mq = window.matchMedia("(pointer: coarse)");
mq.addEventListener("change", handleTouch, { signal });
window.addEventListener("pointerdown", handleTouch, { signal });
handleTouch();
return {
subscribe,
destroy: () => controller.abort(),
};
}