useTouchPrimary
fluid-functionalism/use-touch-primaryFreeHook
Detects touch-primary devices (coarse pointer + touch points), updating live on pointer-mode and media-query changes. Adapted from Lina (https://lina.sameer.sh).
Install it
npx shadcn@latest add https://fluidfunctionalism.com/r/use-touch-primary.jsonSource
1"use client";23// Adapted from Lina by SameerJS6 (https://lina.sameer.sh) — use-has-primary-touch.4// Detects touch-primary devices (coarse pointer + touch points), updating live5// on pointer-mode and media-query changes. Returns false on the server and the6// first client render, so the non-touch branch is the hydration-stable default.78import { useEffect, useState } from "react";910export function useTouchPrimary() {11 const [isTouchPrimary, setIsTouchPrimary] = useState(false);1213 useEffect(() => {14 if (typeof window === "undefined") return;1516 const controller = new AbortController();17 const { signal } = controller;1819 const handleTouch = () => {20 const hasTouch = "ontouchstart" in window || navigator.maxTouchPoints > 0;21 const prefersTouch = window.matchMedia("(pointer: coarse)").matches;22 setIsTouchPrimary(hasTouch && prefersTouch);23 };2425 const mq = window.matchMedia("(pointer: coarse)");26 mq.addEventListener("change", handleTouch, { signal });27 window.addEventListener("pointerdown", handleTouch, { signal });2829 handleTouch();3031 return () => controller.abort();32 }, []);3334 return isTouchPrimary;35}
Files it writes
More from fluid-functionalism
All 53 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| useMergeSplitBlocksuse-merge-split | fluid-functionalism | Hooks | Free | no deps | |
| useProximityHoveruse-proximity-hover | fluid-functionalism | Hooks | Free | no deps |
