Files
calendar/apps/web/pages/_applyThemeForDocument.ts
T
Peer RichelsenGitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>Eunjae Lee
01a0d43943 feat: add conditional todesktop class to html element (#22985)
- Add applyToDesktopClass function to detect window.todesktop
- Conditionally add 'todesktop' class to html element when ToDesktop environment is detected
- Follow existing pattern used by applyTheme function for client-side script injection
- Enable desktop-specific CSS styling that already exists in globals.css

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Eunjae Lee <hey@eunjae.dev>
2025-08-11 19:41:57 +01:00

79 lines
2.5 KiB
TypeScript

// Theme application function - will be stringified and injected. So, it must not use anything from the closure
export const applyTheme = function () {
try {
// This utility is a replica of @calcom/lib/webstorage.ts but we can reuse it because applyTheme is stringified and injected, so we can't have deps here
const safeLocalStorage = {
getItem: function (key: string) {
try {
// eslint-disable-next-line @calcom/eslint/avoid-web-storage
return localStorage.getItem(key);
} catch (e) {
return null;
}
},
key: function (index: number) {
try {
// eslint-disable-next-line @calcom/eslint/avoid-web-storage
return localStorage.key(index);
} catch (e) {
return null;
}
},
getLength: function () {
try {
// eslint-disable-next-line @calcom/eslint/avoid-web-storage
return localStorage.length;
} catch (e) {
return 0;
}
},
};
const appTheme = safeLocalStorage.getItem("app-theme");
if (!appTheme) return;
let bookingTheme: string | null = null;
let username: string | undefined;
for (let i = 0; i < safeLocalStorage.getLength(); i++) {
const key = safeLocalStorage.key(i);
if (key && key.startsWith("booking-theme:")) {
bookingTheme = safeLocalStorage.getItem(key);
username = key.split("booking-theme:")[1];
break;
}
}
const onReady = () => {
const isBookingPage = username && window.location.pathname.slice(1).startsWith(username);
if (document.body) {
document.body.classList.add(isBookingPage ? bookingTheme || appTheme : appTheme);
} else {
requestAnimationFrame(onReady);
}
};
requestAnimationFrame(onReady);
} catch (e) {
console.error("Error applying theme:", e);
}
};
// ToDesktop class application function - will be stringified and injected. So, it must not use anything from the closure
export const applyToDesktopClass = function () {
try {
const onReady = () => {
if (typeof window !== "undefined" && window.todesktop && document.documentElement) {
document.documentElement.classList.add("todesktop");
} else if (document.documentElement) {
return;
} else {
requestAnimationFrame(onReady);
}
};
requestAnimationFrame(onReady);
} catch (e) {
console.error("Error applying todesktop class:", e);
}
};