BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/guarahooks/use-orientation

UseOrientation

guarahooks/use-orientation
FreeHook

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.json

Source

registry/hooks/use-orientation.tsxguarahooks.com/r/use-orientation.json
1'use client';
2
3import { useEffect, useState } from 'react';
4
5export type Orientation = 'portrait' | 'landscape';
6
7function getOrientation(win?: typeof window): Orientation {
8 if (!win) return 'portrait'; // Default for SSR
9 return win.matchMedia('(orientation: portrait)').matches
10 ? 'portrait'
11 : 'landscape';
12}
13
14export function useOrientation(): Orientation {
15 const [orientation, setOrientation] = useState<Orientation>(() =>
16 getOrientation(typeof window !== 'undefined' ? window : undefined),
17 );
18
19 useEffect(() => {
20 if (typeof window === 'undefined') return;
21 const mql = window.matchMedia('(orientation: portrait)');
22
23 // Handler for orientation changes
24 const handler = () => setOrientation(getOrientation(window));
25
26 // Prefer addEventListener, fallback to addListener for older browsers
27 if (typeof mql.addEventListener === 'function') {
28 mql.addEventListener('change', handler);
29 } else {
30 mql.addListener(handler);
31 }
32
33 return () => {
34 if (typeof mql.removeEventListener === 'function') {
35 mql.removeEventListener('change', handler);
36 } else {
37 mql.removeListener(handler);
38 }
39 };
40 }, []);
41
42 return orientation;
43}

Files it writes

  • registry/hooks/use-orientation.tsx

Facts

Registry
guarahooks
Type
registry:hook
Access
Free
Files written
1
Licence
MIT
First indexed
2026-08-03
Last confirmed
yesterday

Go to the source

Docs on guarahooks guarahooks.com h3rmel/guarahooks on GitHub Raw registry item This item as JSON

More from guarahooks

All 100 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
UseAbortControlleruse-abort-controllerguarahooksHooksFreeno deps
UseArrayStateuse-array-stateguarahooksHooksFreeno deps
UseAxiosuse-axiosguarahooksHooksFreeno deps
UseBatteryStatususe-battery-statusguarahooksHooksFreeno deps
UseBetterAuthuse-better-authguarahooksHooksFreeno deps
UseClickOutsideuse-click-outsideguarahooksHooksFreeno deps
UseConfirmuse-confirmguarahooksHooksFreeno deps
UseCookieuse-cookieguarahooksHooksFreeno deps
BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex