I’m trying to enforce dark mode as the default and only theme in Odoo 18 for all users. I’ve implemented the following approach:
- Used cookieManager to set the color_scheme cookie to "dark" permanently.
- Ensured the switchColorSchemeItem function forces dark mode and prevents switching back.prevents switching back.
What I've done worked but when the user logout the cookie is removed and when the user logs in again the cookie is set and the color_scheme in the cookie is dark but the Odoo theme is light and it seems after login the cookie does not work and should refresh the page to work correctly.|
What should I do to make the theme dark without manually refreshing it after login?
my code:
/** @odoo-module **/
import { cookie as cookieManager } from "@web/core/browser/cookie";
function setDarkMode() {
// Set the color scheme cookie to dark mode permanently
cookieManager.set("color_scheme", "dark", { path: "/", expires: 365 * 24 * 60 * 60 });
}
export function switchColorSchemeItem(env) {
// Ensure that the color scheme remains Dark and the switch cannot happen
setDarkMode();
return {
type: "item",
id: "color_scheme.switch_theme",
description: "Dark Mode",
callback: () => {
// Prevent any switch action, ensuring it stays in Dark Mode
setDarkMode();
const cookie = cookieManager.get("color_scheme");
const scheme = "dark";
env.services.color_scheme.switchToColorScheme(scheme);
},
isChecked: true, // Reflect Dark Mode as active
sequence: 30,
};
}
// Set Dark Mode on load and prevent switching back
setDarkMode();