is-touch-device
Detects whether the current JavaScript environment supports touch input in browsers.
Repository Health
Technical Analysis
is-touch-device is a tiny JavaScript utility from Airbnb that answers one focused question: does the current environment support touch input? It checks for ontouchstart support, the legacy DocumentTouch interface, and the standards-based navigator.maxTouchPoints/msMaxTouchPoints properties, returning a single boolean.
The library ships as a single default export with no configuration, dependencies, or DOM manipulation of its own. It’s built with Babel, tested with Mocha and Chai using mocha-wrap for stubbing window/navigator globals, and linted against Airbnb’s ESLint config, making it a small, well-tested primitive for conditionally rendering touch-specific UI or skipping unnecessary event listeners on non-touch devices.
What You Get
- A single default-exported
isTouchDevice()function with no arguments and no configuration - Detection via
ontouchstartpresence, the legacyDocumentTouchinterface, andnavigator.maxTouchPoints/msMaxTouchPoints - Safe behavior in non-browser environments, returning
falsewhenwindow/navigatorare undefined instead of throwing - A Babel-compiled
build/output published to npm, keeping the runtime footprint to a few lines of code
Common Use Cases
- Conditional UI rendering - show larger tap targets or touch-specific controls only on touch-capable devices
- Event listener selection - attach
touchstart/touchendhandlers only when touch is actually supported, falling back to mouse events otherwise - Feature gating in component libraries - internal check used by higher-level UI kits to decide whether to enable drag/swipe gestures
- Analytics segmentation - tag sessions as touch vs. non-touch to understand input method distribution
Under The Hood
Architecture
The library is a single-file module (src/index.js) exporting one default function with no internal layering, state, or dependencies; the entire implementation is a short-circuited boolean expression that checks window.ontouchstart, window.DocumentTouch/document instanceof window.DocumentTouch, and navigator.maxTouchPoints/navigator.msMaxTouchPoints in sequence, guarding each branch with typeof checks so it never throws when window, document, or navigator are absent (e.g. during server-side rendering). There is no build-time or runtime configuration surface, and nothing here would meaningfully change if a new touch API were added beyond appending another guarded clause.
Tech Stack
The package targets plain JavaScript compiled with babel-cli and babel-preset-airbnb from src/ into a published build/ directory, with babel-plugin-add-module-exports normalizing the default export for CommonJS consumers. Linting runs through ESLint with eslint-config-airbnb-base, tests run via Mocha with Chai assertions and mocha-wrap for stubbing global window/navigator/document, coverage is collected with nyc and babel-plugin-istanbul, and CI is wired through Travis (.travis.yml). Publish safety is handled by safe-publish-latest and in-publish, and rimraf/mkdirp manage the build directory between runs.
Code Quality
The single test file (test/index.js) exercises every branch of the detection logic, no-window/no-navigator behavior, ontouchstart presence, DocumentTouch instance checks, and both maxTouchPoints and msMaxTouchPoints truthy/falsy cases, using mocha-wrap’s withGlobal/withOverride helpers to stub browser globals cleanly rather than relying on a real DOM. pretest runs ESLint before Mocha, enforcing style on every test run, though the code has no TypeScript types or JSDoc annotations, and there is no CI badge activity visible beyond the historical Travis config.
What Makes It Unique
The library isn’t structurally novel, feature detection for touch is a well-established pattern, but it does consolidate three separate browser signals (event support, the legacy DocumentTouch interface, and both standard and Microsoft-prefixed maxTouchPoints) into one safe, dependency-free check, saving consumers from re-deriving the same cross-browser compatibility logic themselves.