use-mobile
octane/use-mobileFreeHook
octane publishes no description for this item.
Live preview
live · rendered by the registry
Rendered by octane on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://raw.githubusercontent.com/octanejs/octane/main/packages/shadcn/registry/use-mobile.jsonSource
1// Ported from shadcn-ui/ui@4baadbc6517070ae8f8feb2c97037adc2b3055442// apps/v4/registry/bases/radix/hooks/use-mobile.ts. React hooks map onto3// octane's; the explicit `[]` dependency array is retained from upstream4// (subscribe once on mount). SSR-safe: `window` is only touched in the effect.5import { useEffect, useState } from 'octane';67const MOBILE_BREAKPOINT = 768;89export function useIsMobile() {10 const [isMobile, setIsMobile] = useState<boolean | undefined>(undefined);1112 useEffect(() => {13 const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);14 const onChange = () => {15 setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);16 };17 mql.addEventListener('change', onChange);18 setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);19 return () => mql.removeEventListener('change', onChange);20 }, []);2122 return !!isMobile;23}
