isMobile
Detects whether the current device is a mobile device (phone or tablet).
User-agent resolution order:
- Explicit
userAgentargument — useful for SSR (pass the request header) or unit tests. navigator.userAgentData.mobilefrom User-Agent Client Hints, when the browser exposes it.- Regex against
navigator.userAgent.
Pass maxWidth to additionally treat narrow viewports as mobile. The function returns true when either the user agent matches or the viewport width is <= maxWidth.
SSR-safe: returns false when no navigator / innerWidth is available and no explicit userAgent / width was passed.
Try it
Section titled “Try it”Import
Section titled “Import”import { isMobile } from "1o1-utils";import { isMobile } from "1o1-utils/is-mobile";Signature
Section titled “Signature”function isMobile(params?: IsMobileParams): booleanParameters
Section titled “Parameters”| Name | Type | Required | Description |
|---|---|---|---|
userAgent | string | No | Explicit user agent string. When provided, takes precedence over globalThis.navigator. |
maxWidth | number | No | Enables viewport-based detection. When set, a viewport width <= maxWidth is also treated as mobile (OR’d with the user-agent check). |
width | number | No | Explicit viewport width in pixels. Falls back to globalThis.innerWidth when omitted. Only used when maxWidth is set. |
Returns
Section titled “Returns”boolean — true on mobile devices, false otherwise.
Examples
Section titled “Examples”import { isMobile } from "1o1-utils/is-mobile";
if (isMobile()) { showInstallAppBanner();}// SSR — pass the request user agent headerimport { isMobile } from "1o1-utils/is-mobile";
export function loader({ request }: { request: Request }) { const mobile = isMobile({ userAgent: request.headers.get("user-agent") ?? undefined }); return { mobile };}// Viewport-aware: catch a desktop browser resized to phone widthimport { isMobile } from "1o1-utils/is-mobile";
if (isMobile({ maxWidth: 768 })) { renderMobileNav();} else { renderDesktopNav();}Edge Cases
Section titled “Edge Cases”- Returns
falsewhen called outside a browser without an explicituserAgent(Node, SSR). - Modern iPadOS “request desktop site” reports a macOS user agent and returns
falsefor the UA check — setmaxWidthto catch narrow viewports anyway. - Empty
userAgentstrings returnfalse. navigator.userAgentData.mobileis authoritative when exposed and skips the regex.- The viewport check is skipped silently when
maxWidthis unset, or when nowidth/globalThis.innerWidthis available (SSR without explicitwidth).
Also known as
Section titled “Also known as”is mobile, mobile, device, user agent, phone, tablet, iphone, ipad, android, responsive
Prompt suggestion
Section titled “Prompt suggestion”I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use isMobile to render different copy on mobile vs desktop, including the SSR case.