Hooks
No hooks found in any category.
useIsTablet
sensorsInstallation
npx usehooks-cli@latest add use-is-tablet
Description
A React hook for detecting tablet devices using media queries, user agent detection, and touch capability checks. Provides real-time updates when the device orientation or window size changes.
Return Type
UseIsTabletReturn
Property | Type | Description |
---|---|---|
isTablet | boolean | Whether the current device is detected as a tablet |
isLoading | boolean | Whether the hook is still determining the device type |
Examples
Basic Tablet Detection
Simple tablet device detection
1const { isTablet, isLoading } = useIsTablet();
2
3if (isLoading) {
4 return <div>Detecting device...</div>;
5}
6
7return (
8 <div>
9 <p>Device type: {isTablet ? 'Tablet' : 'Not Tablet'}</p>
10 {isTablet ? (
11 <TabletOptimizedLayout />
12 ) : (
13 <StandardLayout />
14 )}
15 </div>
16);
Responsive Grid
Adjust grid layout based on tablet detection
1const { isTablet } = useIsTablet();
2
3return (
4 <div
5 className={`grid ${
6 isTablet ? 'grid-cols-2 gap-4' : 'grid-cols-1 gap-2'
7 }`}
8 >
9 {items.map(item => (
10 <Card key={item.id} item={item} />
11 ))}
12 </div>
13);
Dependencies
react
Notes
- •Uses media query (769px - 1024px) as primary detection method
- •Includes user agent detection for tablet-specific devices
- •Special handling for iPad detection (including newer iPads that report as desktop)
- •Considers touch capability as additional detection factor
- •Automatically updates when window is resized or orientation changes
Implementation
1'use client';
2
3import { useState, useEffect } from "react";
4
5interface UseIsTabletReturn {
6 isTablet: boolean;
7 isLoading: boolean;
8}
9
10export const useIsTablet = (): UseIsTabletReturn => {
11 const [isTablet, setIsTablet] = useState(false);
12 const [isLoading, setIsLoading] = useState(true);
13
14 useEffect(() => {
15 const checkIsTablet = () => {
16 // Check using media query for tablet range
17 const tabletQuery = window.matchMedia(
18 "(min-width: 769px) and (max-width: 1024px)"
19 );
20
21 // Check using user agent for tablet-specific devices
22 const userAgent = navigator.userAgent.toLowerCase();
23 const tabletKeywords = [
24 'ipad', 'tablet', 'kindle', 'silk', 'playbook', 'nexus 7',
25 'nexus 9', 'nexus 10', 'galaxy tab', 'sm-t', 'gt-p', 'gt-n'
26 ];
27
28 const isTabletUA = tabletKeywords.some(keyword =>
29 userAgent.includes(keyword)
30 );
31
32 // Additional check for iPad (which might report as desktop in some cases)
33 const isIPad = /ipad|macintosh/.test(userAgent) && 'ontouchend' in document;
34
35 // Combine checks - media query + user agent + touch capability
36 const isTabletDevice = tabletQuery.matches ||
37 isTabletUA ||
38 isIPad ||
39 (window.innerWidth >= 769 && window.innerWidth <= 1024 && 'ontouchstart' in window);
40
41 setIsTablet(isTabletDevice);
42 setIsLoading(false);
43 };
44
45 // Initial check
46 checkIsTablet();
47
48 // Listen for media query changes
49 const tabletQuery = window.matchMedia(
50 "(min-width: 769px) and (max-width: 1024px)"
51 );
52 const handleChange = () => checkIsTablet();
53
54 if (tabletQuery.addEventListener) {
55 tabletQuery.addEventListener('change', handleChange);
56 } else {
57 // Fallback for older browsers
58 tabletQuery.addListener(handleChange);
59 }
60
61 // Listen for window resize
62 window.addEventListener('resize', checkIsTablet);
63
64 return () => {
65 if (tabletQuery.removeEventListener) {
66 tabletQuery.removeEventListener('change', handleChange);
67 } else {
68 tabletQuery.removeListener(handleChange);
69 }
70 window.removeEventListener('resize', checkIsTablet);
71 };
72 }, []);
73
74 return {
75 isTablet,
76 isLoading,
77 };
78};
79
80export default useIsTablet;