web/device: add default values & types

This commit is contained in:
wukko 2024-09-03 13:52:41 +06:00
parent 2deb8aa53b
commit 30c4c1ad20
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2

View file

@ -1,14 +1,29 @@
import { browser } from '$app/environment' import { browser } from "$app/environment";
const device = {
is: {},
prefers: {},
supports: {},
userAgent: 'sveltekit server'
};
const app = { const app = {
is: {} 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",
} }
if (browser) { if (browser) {
@ -20,28 +35,24 @@ if (browser) {
const iOS = iPhone || iPad; const iOS = iPhone || iPad;
const android = ua.includes("android") || ua.includes("diordna"); const android = ua.includes("android") || ua.includes("diordna");
const mobile = iOS || android;
const language = navigator.language.toLowerCase().slice(0, 2);
const installed = window.matchMedia('(display-mode: standalone)').matches; const installed = window.matchMedia('(display-mode: standalone)').matches;
const reducedMotion = window.matchMedia(`(prefers-reduced-motion: reduce)`).matches; app.is = {
const reducedTransparency = window.matchMedia(`(prefers-reduced-transparency: reduce)`).matches; installed,
};
app.is = { installed };
device.is = { device.is = {
iPhone, iPhone,
iPad, iPad,
iOS, iOS,
android, android,
mobile, mobile: iOS || android,
}; };
device.prefers = { device.prefers = {
language, language: navigator.language.toLowerCase().slice(0, 2) || "en",
reducedMotion, reducedMotion: window.matchMedia('(prefers-reduced-motion: reduce)').matches,
reducedTransparency, reducedTransparency: window.matchMedia('(prefers-reduced-transparency: reduce)').matches,
}; };
device.supports = { device.supports = {