From 3c56cb6b55ac9a2a68b2d2edf4b52410620c58a0 Mon Sep 17 00:00:00 2001 From: SameerJS6 Date: Fri, 26 Sep 2025 08:57:46 +0530 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20useHasPrimaryTouch=20hook=20t?= =?UTF-8?q?o=20detect=20primary=20touch=20capability?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/use-has-primary-touch.ts | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/hooks/use-has-primary-touch.ts diff --git a/src/hooks/use-has-primary-touch.ts b/src/hooks/use-has-primary-touch.ts new file mode 100644 index 0000000..80a5e56 --- /dev/null +++ b/src/hooks/use-has-primary-touch.ts @@ -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(), + }; +}