use-breakpoint-agent
Version:
A React hook to detect device type based on screen width
16 lines (15 loc) • 587 B
JavaScript
import { DeviceEnum } from "../types/index.js";
import { UAParser } from "ua-parser-js";
export function getDeviceTypeFromString(input) {
var _a;
if (typeof window === "undefined") {
throw new Error("use-breakpoint-agent/client cannot be used on the server side");
}
const data = UAParser(input || navigator.userAgent);
const device = (_a = data.device) === null || _a === void 0 ? void 0 : _a.type;
if (device === "mobile")
return DeviceEnum.MOBILE;
if (device === "tablet")
return DeviceEnum.TABLET;
return DeviceEnum.DESKTOP;
}