npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

nice-react-hook-device-detector

v1.1.0

Published

A React hook for detecting mobile devices with enhanced debugging capabilities

Readme

nice-react-hook-device-detector

npm version License: MIT

A React hook for detecting mobile devices with enhanced debugging capabilities and Chrome DevTools support.

Features

Smart Detection: Multi-layered approach combining user agent, touch capability, and viewport width 🔧 DevTools Support: Enhanced detection for Chrome DevTools mobile emulation 🐛 Debug Mode: Easy testing with localStorage or URL parameter flags 📱 Responsive: Automatically updates on window resize events ⚡ Lightweight: Zero dependencies (except React peer dependency) 🎯 TypeScript: Full TypeScript support with type definitions

Installation

npm install nice-react-hook-device-detector

Quick Start

Using the Hook Directly

import { useDeviceDetector } from 'nice-react-hook-device-detector';

function MyComponent() {
  const isMobile = useDeviceDetector();

  return (
    <div>
      {isMobile ? (
        <MobileView />
      ) : (
        <DesktopView />
      )}
    </div>
  );
}

Using Context Provider (Optional)

For applications that need to share device detection state across multiple components, you can use the Context Provider pattern:

import { DeviceProvider, useDevice } from 'nice-react-hook-device-detector';

// Wrap your app with the provider
function App() {
  return (
    <DeviceProvider>
      <YourAppComponents />
    </DeviceProvider>
  );
}

// Use the hook in any child component
function ChildComponent() {
  const { isMobile } = useDevice();

  return (
    <div>
      {isMobile ? 'Mobile View' : 'Desktop View'}
    </div>
  );
}

API Reference

useDeviceDetector()

A React hook that returns a boolean indicating whether the current device is detected as mobile.

Returns:

  • boolean - true if mobile device is detected, false otherwise

Example:

const isMobile = useDeviceDetector();

DeviceProvider

A React Context Provider that shares device detection state across components.

Props:

  • children: React.ReactNode - Child components that will have access to the device context

Example:

<DeviceProvider>
  <App />
</DeviceProvider>

useDevice()

A React hook that accesses the device context. Must be used within a DeviceProvider.

Returns:

  • { isMobile: boolean } - Object containing the mobile detection state

Throws:

  • Error if used outside of DeviceProvider

Example:

const { isMobile } = useDevice();

Detection Logic

The hook uses a comprehensive approach to detect mobile devices:

  1. User Agent Detection: Checks for mobile device indicators in the user agent string
  2. Touch Capability: Detects if the device supports touch events (ontouchstart or maxTouchPoints)
  3. Viewport Width: Considers devices with width ≤ 480px as mobile
  4. DevTools Emulation: Special handling for Chrome DevTools mobile emulation (combines touch + small screen)

Debug Mode

For testing and development, you can force mobile detection:

Method 1: localStorage

localStorage.setItem('debug-mobile', 'true');
// Refresh the page or re-render the component

Method 2: URL Parameter

https://yoursite.com?mobile=true

Disable Debug Mode

localStorage.removeItem('debug-mobile');
// Or visit URL without the mobile parameter

Use Cases

  • Responsive Components: Show different UI components based on device type
  • Touch Interactions: Enable/disable touch-specific features
  • Performance Optimization: Load different assets for mobile vs desktop
  • User Experience: Adapt navigation, modals, and interactions for mobile users

Examples

Conditional Rendering

import { useDeviceDetector } from 'nice-react-hook-device-detector';

function Navigation() {
  const isMobile = useDeviceDetector();

  return (
    <nav>
      {isMobile ? (
        <MobileMenu />
      ) : (
        <DesktopMenu />
      )}
    </nav>
  );
}

Conditional Styling

import { useDeviceDetector } from 'nice-react-hook-device-detector';
import styled from 'styled-components';

const Container = styled.div<{ isMobile: boolean }>`
  padding: ${props => props.isMobile ? '10px' : '20px'};
  font-size: ${props => props.isMobile ? '14px' : '16px'};
`;

function MyComponent() {
  const isMobile = useDeviceDetector();

  return (
    <Container isMobile={isMobile}>
      Content adapts to device type
    </Container>
  );
}

Feature Toggling

import { useDeviceDetector } from 'nice-react-hook-device-detector';

function App() {
  const isMobile = useDeviceDetector();

  return (
    <div>
      {isMobile && <TouchGestures />}
      {!isMobile && <MouseInteractions />}
      <MainContent />
    </div>
  );
}

Browser Support

  • ✅ Chrome/Chromium
  • ✅ Firefox
  • ✅ Safari
  • ✅ Edge
  • ✅ Mobile browsers (iOS Safari, Chrome Mobile, etc.)

Requirements

  • React 16.8.0 or higher (hooks support)

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT © Nice Prototypes