Yevhen Klymentiev
dark
light
console
darkness
y.klymentiev@gmail.com
Reusable Snippets|Practical utility code for everyday use — custom-built and ready to share

isTablet

Detects if the current device is a tablet.

TypeScript
Copied!
1/**
2 * Detects if the current device is a tablet.
3 *
4 * @returns True if the device is likely a tablet, false otherwise.
5 */
6export function isTablet(): boolean {
7  if (typeof navigator === 'undefined' || typeof navigator.userAgent !== 'string') {
8    return false;
9  }
10
11  const ua = navigator.userAgent.toLowerCase();
12
13  return (
14    /ipad/.test(ua) ||
15    (/(android)/.test(ua) && !/mobile/.test(ua)) || // Android tablets typically lack "mobile"
16    /tablet/.test(ua) ||
17    /kindle/.test(ua) ||
18    /silk/.test(ua) ||
19    /playbook/.test(ua)
20  );
21}
  • Broad Detection Logic

    Covers a wide range of tablet user agents, including iPad, Android tablets, Kindle, and PlayBook devices.

  • Mobile vs Tablet Disambiguation

    Smartly distinguishes Android tablets from phones by checking for absence of the "mobile" keyword.

  • Graceful Fallback for Non-Browser Contexts

    Safeguards against environments where navigator is undefined, preventing runtime errors.

  • Lowercase Normalization for Robust Matching

    Ensures consistent behavior regardless of user agent case formatting.

Tests | Examples

TypeScript
Copied!
1const originalNavigator = global.navigator;
2
3function mockUserAgent(ua: string) {
4  Object.defineProperty(global, 'navigator', {
5    value: { userAgent: ua },
6    configurable: true,
7  });
8}
9
10afterEach(() => {
11  global.navigator = originalNavigator;
12});
13
14test('detects iPad as tablet', () => {
15  mockUserAgent('Mozilla/5.0 (iPad; CPU OS 13_2 like Mac OS X)');
16  expect(isTablet()).toBe(true);
17});
18
19test('detects Android tablet', () => {
20  mockUserAgent('Mozilla/5.0 (Linux; Android 9; Nexus 7) AppleWebKit/537.36');
21  expect(isTablet()).toBe(true);
22});
23
24test('does not detect Android phone as tablet', () => {
25  mockUserAgent('Mozilla/5.0 (Linux; Android 9; Pixel 3) Mobile');
26  expect(isTablet()).toBe(false);
27});
28
29test('detects Kindle as tablet', () => {
30  mockUserAgent('Mozilla/5.0 (Linux; U; en-US) AppleWebKit/533.3 (KHTML, like Gecko) Kindle/3.0');
31  expect(isTablet()).toBe(true);
32});
33
34test('returns false if navigator is undefined', () => {
35  // @ts-expect-error intentionally breaking navigator
36  delete global.navigator;
37  expect(isTablet()).toBe(false);
38});

Common Use Cases

  • Responsive UI Adjustments

    Tailor layouts, font sizes, or navigation behavior specifically for tablet-sized screens.

  • Device-Specific Feature Flags

    Enable or disable features such as drag gestures, hover effects, or multi-column views on tablets.

  • Analytics and Device Classification

    Collect telemetry data for user segmentation based on device type.

  • Custom Touch Optimization

    Improve touch interactions and layout padding for larger touch surfaces typical of tablets.

Codebase: Utilities -> Platforms -> isTablet | Yevhen Klymentiev