cobalt/web/src/lib/device.ts

67 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-09-03 09:52:41 +02:00
import { browser } from "$app/environment";
2024-08-31 19:46:10 +02:00
const app = {
2024-09-03 09:52:41 +02:00
is: {
installed: false,
}
}
const device = {
is: {
iPhone: false,
iPad: false,
iOS: false,
android: false,
mobile: false,
},
prefers: {
language: "en",
reducedMotion: false,
reducedTransparency: false,
},
supports: {
share: false,
directDownload: false,
},
userAgent: "sveltekit server",
2024-08-31 19:46:10 +02:00
}
2024-08-31 19:46:10 +02:00
if (browser) {
const ua = navigator.userAgent.toLowerCase();
2024-08-31 19:46:10 +02:00
const iPhone = ua.includes("iphone os");
const iPad = !iPhone && ua.includes("mac os") && navigator.maxTouchPoints > 0;
2024-08-31 19:46:10 +02:00
const iOS = iPhone || iPad;
const android = ua.includes("android") || ua.includes("diordna");
2024-08-31 19:46:10 +02:00
const installed = window.matchMedia('(display-mode: standalone)').matches;
2024-09-03 09:52:41 +02:00
app.is = {
installed,
};
2024-08-31 19:46:10 +02:00
device.is = {
iPhone,
2024-07-10 19:28:23 +02:00
iPad,
iOS,
android,
2024-09-03 09:52:41 +02:00
mobile: iOS || android,
2024-08-31 19:46:10 +02:00
};
device.prefers = {
2024-09-03 09:52:41 +02:00
language: navigator.language.toLowerCase().slice(0, 2) || "en",
reducedMotion: window.matchMedia('(prefers-reduced-motion: reduce)').matches,
reducedTransparency: window.matchMedia('(prefers-reduced-transparency: reduce)').matches,
2024-08-31 19:46:10 +02:00
};
device.supports = {
share: navigator.share !== undefined,
directDownload: !(installed && iOS),
2024-08-31 19:46:10 +02:00
};
2024-08-31 19:46:10 +02:00
device.userAgent = navigator.userAgent;
}
export { device, app };