UseOrientation
guarahooks/use-orientationFreeHook
Tracks the current orientation of the device.
Live preview
live · rendered by the registry
Rendered by guarahooks on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://guarahooks.com/r/use-orientation.jsonSource
1'use client';23import { useEffect, useState } from 'react';45export type Orientation = 'portrait' | 'landscape';67function getOrientation(win?: typeof window): Orientation {8 if (!win) return 'portrait'; // Default for SSR9 return win.matchMedia('(orientation: portrait)').matches10 ? 'portrait'11 : 'landscape';12}1314export function useOrientation(): Orientation {15 const [orientation, setOrientation] = useState<Orientation>(() =>16 getOrientation(typeof window !== 'undefined' ? window : undefined),17 );1819 useEffect(() => {20 if (typeof window === 'undefined') return;21 const mql = window.matchMedia('(orientation: portrait)');2223 // Handler for orientation changes24 const handler = () => setOrientation(getOrientation(window));2526 // Prefer addEventListener, fallback to addListener for older browsers27 if (typeof mql.addEventListener === 'function') {28 mql.addEventListener('change', handler);29 } else {30 mql.addListener(handler);31 }3233 return () => {34 if (typeof mql.removeEventListener === 'function') {35 mql.removeEventListener('change', handler);36 } else {37 mql.removeListener(handler);38 }39 };40 }, []);4142 return orientation;43}
Files it writes
More from guarahooks
All 100 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| UseAbortControlleruse-abort-controller | guarahooks | Hooks | Free | no deps | |
| UseArrayStateuse-array-state | guarahooks | Hooks | Free | no deps | |
| UseAxiosuse-axios | guarahooks | Hooks | Free | no deps | |
| UseBatteryStatususe-battery-status | guarahooks | Hooks | Free | no deps | |
| UseBetterAuthuse-better-auth | guarahooks | Hooks | Free | no deps | |
| UseClickOutsideuse-click-outside | guarahooks | Hooks | Free | no deps | |
| UseConfirmuse-confirm | guarahooks | Hooks | Free | no deps | |
| UseCookieuse-cookie | guarahooks | Hooks | Free | no deps |
