Use mobile Hook

PreviousNext

use mobile hook allows you to check if the user is on a mobile device.

Docs
systaliko-uihook

Preview

Loading preview…
registry/utils/use-mobile/index.ts
import * as React from 'react';

export function useIsMobile(break_point: number = 768) {
  const [isMobile, setIsMobile] = React.useState<boolean | undefined>(
    undefined,
  );

  React.useEffect(() => {
    const mql = window.matchMedia(`(max-width: ${break_point - 1}px)`);
    const onChange = () => {
      setIsMobile(window.innerWidth < break_point);
    };
    mql.addEventListener('change', onChange);
    setIsMobile(window.innerWidth < break_point);
    return () => mql.removeEventListener('change', onChange);
  }, []);

  return !!isMobile;
}

Installation

npx shadcn@latest add @systaliko-ui/use-mobile

Usage

import { UseMobile } from "@/hooks/use-mobile"
const value = UseMobile()