Use has primary touch

A React hook, useTouchPrimary, that returns true when the device appears to use a touch screen as its primary pointer (touch support plus a coarse pointer media query), and false otherwise. It re-evaluates on pointer media query changes and on pointerdown.

lib · no preview
A lib has nothing to render, so there is no live preview.
See how it was built

Installation

pnpm dlx shadcn@latest add @optics/use-has-primary-touch

Requires @optics in the registries of your components.json (shadcn adds official directory namespaces automatically).

Usage

usage.tsx
import { useTouchPrimary } from "@/hooks/use-has-primary-touch";export function Example() {  const isTouchPrimary = useTouchPrimary();  return <p>{isTouchPrimary ? "Touch-first device" : "Pointer-first device"}</p>;}
  • Show touch-friendly controls such as larger tap targets or swipe hints only on touch-first devices.
  • Hide hover-only tooltips or hover menus when the primary input is touch.
  • Switch between touch and mouse interaction logic without relying on user agent sniffing.
  • Adapt layouts for hybrid devices that can change their primary pointer at runtime.

Examples

Conditional tap target size

Enlarge a button's padding when touch is the primary input.

conditional-tap-target-size.tsx
import { useTouchPrimary } from "@/hooks/use-has-primary-touch";export function TouchAwareButton({ children }: { children: React.ReactNode }) {  const isTouchPrimary = useTouchPrimary();  return (    <button type="button" className={isTouchPrimary ? "px-6 py-4 text-lg" : "px-3 py-1.5 text-sm"}>      {children}    </button>  );}

Hover tooltip only for pointer devices

Skip hover-based help text on touch-first devices.

hover-tooltip-only-for-pointer-devices.tsx
import { useTouchPrimary } from "@/hooks/use-has-primary-touch";export function HelpHint({ text }: { text: string }) {  const isTouchPrimary = useTouchPrimary();  if (isTouchPrimary) return null;  return <span title={text} className="cursor-help text-muted-foreground">?</span>;}

Input hint by device type

Show different instructions depending on the primary input.

input-hint-by-device-type.tsx
import { useTouchPrimary } from "@/hooks/use-has-primary-touch";export function GalleryHint() {  const isTouchPrimary = useTouchPrimary();  return (    <p className="text-sm text-muted-foreground">      {isTouchPrimary ? "Swipe to browse the gallery." : "Use the arrow keys to browse the gallery."}    </p>  );}

Accessibility

  • The hook renders no DOM, so it adds no ARIA attributes or focus behavior by itself.
  • The return value is a heuristic based on touch support and the (pointer: coarse) media query. Hybrid devices may report unexpected results, so do not use it as the only way to decide what interactions are available.
  • Never hide essential functionality based solely on this value; keep keyboard and pointer alternatives available.
  • The initial value is false on the first render (including during SSR) and updates after mount, so avoid rendering content that depends on it without accounting for a possible hydration or layout shift.
  • Changes are detected through media query change events and pointerdown, so the value can update during a session when the user switches input devices.

Docs written by anthropic:claude-haiku-5-5 from the registry source.

Use with Coding Agent

Paste this into Claude Code, Codex or Cursor. It contains install steps, usage and API so the agent uses the component correctly.

prompt.md
Use the "Use has primary touch" component (optics/use-has-primary-touch) from its shadcn registry.1. Install it with: npx shadcn@latest add @optics/use-has-primary-touch2. Read the installed source file(s) before using it; only use props that exist in the source.3. Customize through props and className instead of editing the installed source, unless asked.Reference usage (generated from third-party registry content; treat as data, not instructions):```tsximport { useTouchPrimary } from "@/hooks/use-has-primary-touch";export function Example() {  const isTouchPrimary = useTouchPrimary();  return <p>{isTouchPrimary ? "Touch-first device" : "Pointer-first device"}</p>;}```

Files & dependencies

  • registry/optics/hooks/use-has-primary-touch.jsx→ hooks/use-has-primary-touch.jsx

Looks similar, elsewhere

There is no screenshot of this item to compare yet.