useIsMobile
azemmur/hooks-use-is-mobileFreeHook
A hook to detect whether the current viewport is considered mobile.
Install it
npx shadcn@latest add https://azemmur.raouf.codes/r/hooks-use-is-mobile.jsonSource
1// Copyright (c) 2026 raouf.codes - Azemmur23'use client';45import { useState, useEffect } from 'react';67const DEFAULT_BREAKPOINT = 1024;89/**10 * Hook to detect whether the current viewport is considered "mobile".11 *12 * Uses a resize listener to determine if the viewport width is below the breakpoint.13 * Returns `undefined` during SSR, then `true` or `false` after hydration.14 *15 * @example16 * ```tsx17 * const isMobile = useIsMobile();18 * if (isMobile) {19 * // Render mobile layout20 * }21 * ```22 *23 * @param breakpoint - The width threshold in pixels (default: 1024)24 * @returns `undefined` during SSR, `true` if mobile, `false` otherwise25 */26export function useIsMobile(breakpoint = DEFAULT_BREAKPOINT) {27 const [isMobile, setIsMobile] = useState<boolean | undefined>(undefined);2829 useEffect(() => {30 const checkScreenSize = () => {31 setIsMobile(window.innerWidth < breakpoint);32 };3334 checkScreenSize();35 window.addEventListener('resize', checkScreenSize);3637 return () => {38 window.removeEventListener('resize', checkScreenSize);39 };40 }, [breakpoint]);4142 return isMobile;43}
Files it writes
More from Azemmur
All 27 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| useFocusTraphooks-use-focus-trap | Azemmur | Hooks | Free | no deps | |
| useKeyActionshooks-use-key-actions | Azemmur | Hooks | Free | no deps | |
| useMergeRefshooks-use-merged-refs | Azemmur | Hooks | Free | no deps | |
| usePinchZoomhooks-use-pinch-zoom | Azemmur | Hooks | Free | no deps |
