mirror of
https://github.com/pheralb/svgl.git
synced 2025-12-29 08:01:36 +08:00
✨ Add useHasPrimaryTouch hook to detect primary touch capability
This commit is contained in:
@@ -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(),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user