BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/efferd/use-media-query

use-media-query

efferd-/use-media-query
FreeHook

Use media query hook.

Live preview

live · rendered by the registry
Rendered by efferd on their own page — this is the component running, not a screenshot of it.

Install it

npx shadcn@latest add http://efferd.com/r/use-media-query.json

Source

hooks/use-media-query.tsefferd.com/r/use-media-query.json
1"use client";
2import { useEffect, useState } from "react";
3
4function getDevice(): "mobile" | "tablet" | "desktop" | null {
5 if (typeof window === "undefined") {
6 return null;
7 }
8
9 if (window.matchMedia("(min-width: 1024px)").matches) {
10 return "desktop";
11 }
12
13 if (window.matchMedia("(min-width: 640px)").matches) {
14 return "tablet";
15 }
16
17 return "mobile";
18}
19
20function getDimensions() {
21 if (typeof window === "undefined") {
22 return null;
23 }
24
25 return { width: window.innerWidth, height: window.innerHeight };
26}
27
28export function useMediaQuery() {
29 const [device, setDevice] = useState<"mobile" | "tablet" | "desktop" | null>(
30 null
31 );
32 const [dimensions, setDimensions] = useState<{
33 width: number;
34 height: number;
35 } | null>(null);
36
37 useEffect(() => {
38 const checkDevice = () => {
39 setDevice(getDevice());
40 setDimensions(getDimensions());
41 };
42
43 // Initial detection
44 checkDevice();
45
46 // Listener for windows resize
47 window.addEventListener("resize", checkDevice);
48
49 // Cleanup listener
50 return () => {
51 window.removeEventListener("resize", checkDevice);
52 };
53 }, []);
54
55 return {
56 device,
57 width: dimensions?.width,
58 height: dimensions?.height,
59 isMobile: device === "mobile",
60 isTablet: device === "tablet",
61 isDesktop: device === "desktop",
62 };
63}

Files it writes

  • hooks/use-media-query.ts

Facts

Registry
efferd
Type
registry:hook
Access
Free
Files written
1
Licence
the repository states none
First indexed
2026-08-01
Last confirmed
yesterday

Go to the source

Docs on efferd efferd.com Raw registry item This item as JSON

More from efferd

All 201 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
use-copy-to-clipboarduse-copy-to-clipboardefferdHooksFreeno deps
use-keypressuse-keypressefferdHooksFreeno deps
use-scrolluse-scrollefferdHooksFreeno 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