use-breakpoint-agent
Version:
A React hook to detect device type based on screen width
21 lines (20 loc) • 909 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDeviceTypeFromHeaders = getDeviceTypeFromHeaders;
const ua_parser_js_1 = require("ua-parser-js");
const types_1 = require("../types/index.js");
function getDeviceTypeFromHeaders(headers) {
var _a;
if (typeof window !== "undefined") {
throw new Error("use-breakpoint-agent/server cannot be used on the client side");
}
let agent = headers instanceof Headers ? headers.get("user-agent") : headers["user-agent"];
agent = Array.isArray(agent) ? agent[0] : agent;
const data = (0, ua_parser_js_1.UAParser)(agent || undefined);
const device = (_a = data.device) === null || _a === void 0 ? void 0 : _a.type;
if (device === "mobile")
return types_1.DeviceEnum.MOBILE;
if (device === "tablet")
return types_1.DeviceEnum.TABLET;
return types_1.DeviceEnum.DESKTOP;
}