use-is-touch-device

PreviousNext
Docs
platehook

Preview

Loading preview…
registry/hooks/use-is-touch-device.ts
'use client';

import * as React from 'react';

export function useIsTouchDevice() {
  const [isTouchDevice, setIsTouchDevice] = React.useState(false);

  React.useEffect(() => {
    function onResize() {
      setIsTouchDevice(
        'ontouchstart' in window ||
          navigator.maxTouchPoints > 0 ||
          navigator.maxTouchPoints > 0
      );
    }

    window.addEventListener('resize', onResize);
    onResize();

    return () => {
      window.removeEventListener('resize', onResize);
    };
  }, []);

  return isTouchDevice;
}

Installation

npx shadcn@latest add @plate/use-is-touch-device

Usage

import { UseIsTouchDevice } from "@/hooks/use-is-touch-device"
const value = UseIsTouchDevice()