cobalt/web/src/lib/device.ts

56 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-08-31 19:46:10 +02:00
import { browser } from '$app/environment'
2024-08-31 19:46:10 +02:00
const device = {
is: {},
prefers: {},
supports: {},
userAgent: 'sveltekit server'
};
2024-08-31 19:46:10 +02:00
const app = {
is: {}
}
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 mobile = iOS || android;
2024-08-31 19:46:10 +02:00
const language = navigator.language.toLowerCase().slice(0, 2);
2024-08-31 19:46:10 +02:00
const installed = window.matchMedia('(display-mode: standalone)').matches;
const reducedMotion = window.matchMedia(`(prefers-reduced-motion: reduce)`).matches;
const reducedTransparency = window.matchMedia(`(prefers-reduced-transparency: reduce)`).matches;
app.is = { installed };
device.is = {
iPhone,
2024-07-10 19:28:23 +02:00
iPad,
iOS,
android,
mobile,
2024-08-31 19:46:10 +02:00
};
device.prefers = {
language,
reducedMotion,
reducedTransparency,
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 };