Skip to Content
Odoo Menú
  • Registra entrada
  • Prova-ho gratis
  • Aplicacions
    Finances
    • Comptabilitat
    • Facturació
    • Despeses
    • Full de càlcul (IA)
    • Documents
    • Signatura
    Vendes
    • CRM
    • Vendes
    • Punt de venda per a botigues
    • Punt de venda per a restaurants
    • Subscripcions
    • Lloguer
    Imatges de llocs web
    • Creació de llocs web
    • Comerç electrònic
    • Blog
    • Fòrum
    • Xat en directe
    • Aprenentatge en línia
    Cadena de subministrament
    • Inventari
    • Fabricació
    • PLM
    • Compres
    • Manteniment
    • Qualitat
    Recursos humans
    • Empleats
    • Reclutament
    • Absències
    • Avaluacions
    • Recomanacions
    • Flota
    Màrqueting
    • Màrqueting Social
    • Màrqueting per correu electrònic
    • Màrqueting per SMS
    • Esdeveniments
    • Automatització del màrqueting
    • Enquestes
    Serveis
    • Projectes
    • Fulls d'hores
    • Servei de camp
    • Suport
    • Planificació
    • Cites
    Productivitat
    • Converses
    • Validacions
    • IoT
    • VoIP
    • Coneixements
    • WhatsApp
    Aplicacions de tercers Odoo Studio Plataforma d'Odoo al núvol
  • Sectors
    Comerç al detall
    • Llibreria
    • Botiga de roba
    • Botiga de mobles
    • Botiga d'ultramarins
    • Ferreteria
    • Botiga de joguines
    Food & Hospitality
    • Bar i pub
    • Restaurant
    • Menjar ràpid
    • Guest House
    • Distribuïdor de begudes
    • Hotel
    Immobiliari
    • Agència immobiliària
    • Estudi d'arquitectura
    • Construcció
    • Gestió immobiliària
    • Jardineria
    • Associació de propietaris de béns immobles
    Consultoria
    • Empresa comptable
    • Partner d'Odoo
    • Agència de màrqueting
    • Bufet d'advocats
    • Captació de talent
    • Auditoria i certificació
    Fabricació
    • Textile
    • Metal
    • Mobles
    • Menjar
    • Brewery
    • Regals corporatius
    Salut i fitness
    • Club d'esport
    • Òptica
    • Centre de fitness
    • Especialistes en benestar
    • Farmàcia
    • Perruqueria
    Trades
    • Servei de manteniment
    • Hardware i suport informàtic
    • Sistemes d'energia solar
    • Shoe Maker
    • Serveis de neteja
    • Instal·lacions HVAC
    Altres
    • Nonprofit Organization
    • Agència del medi ambient
    • Lloguer de panells publicitaris
    • Fotografia
    • Lloguer de bicicletes
    • Distribuïdors de programari
    Browse all Industries
  • Comunitat
    Aprèn
    • Tutorials
    • Documentació
    • Certificacions
    • Formació
    • Blog
    • Pòdcast
    Potenciar l'educació
    • Programa educatiu
    • Scale-Up! El joc empresarial
    • Visita Odoo
    Obtindre el programari
    • Descarregar
    • Comparar edicions
    • Novetats de les versions
    Col·laborar
    • GitHub
    • Fòrum
    • Esdeveniments
    • Traduccions
    • Converteix-te en partner
    • Services for Partners
    • Registra la teva empresa comptable
    Obtindre els serveis
    • Troba un partner
    • Troba un comptable
    • Contacta amb un expert
    • Serveis d'implementació
    • Referències del client
    • Suport
    • Actualitzacions
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Programar una demo
  • Preus
  • Ajuda

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Comptabilitat
  • Inventari
  • PoS
  • Projectes
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
Ajuda

Dark Mode in Odoo 18: Not Persisting After Login Without Manual Refresh

Subscriure's

Get notified when there's activity on this post

This question has been flagged
OdooThemedark
2 Respostes
2694 Vistes
Avatar
Hamid

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:

  1. Used cookieManager to set the color_scheme cookie to "dark" permanently.
  2. 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();

0
Avatar
Descartar
Avatar
Cybrosys Techno Solutions Pvt.Ltd
Best Answer

Hi,


Try the following theme,

* https://apps.odoo.com/apps/themes/18.0/dark_mode_backend


Hope it helps

0
Avatar
Descartar
Avatar
Gracious Joseph
Best Answer

To ensure that dark mode persists as the default theme in Odoo 18 and prevents the need for a manual page refresh after login, the issue lies in how the Odoo front-end initializes the theme. When a user logs in, the cookie is set but the page doesn't automatically apply the dark theme until it's reloaded.

Here’s how you can fix this issue:

Solution 1: Force Dark Mode on Page Initialization

Modify your JavaScript module to apply the dark theme as soon as the page loads, regardless of the login state.

Updated 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 });
}

// Apply dark mode immediately during page initialization
function applyDarkModeImmediately(env) {
    const scheme = "dark";
    env.services.color_scheme.switchToColorScheme(scheme); // Force the color scheme service to apply dark mode
}

// Ensure the theme remains dark
export function switchColorSchemeItem(env) {
    // Apply dark mode when toggled
    setDarkMode();
    applyDarkModeImmediately(env);

    return {
        type: "item",
        id: "color_scheme.switch_theme",
        description: "Dark Mode",
        callback: () => {
            setDarkMode();
            applyDarkModeImmediately(env);
        },
        isChecked: true,
        sequence: 30,
    };
}

// Set dark mode cookie and apply the theme on module initialization
setDarkMode();

// Automatically apply dark mode on page load
odoo.define("custom_dark_mode", function (require) {
    const { registry } = require("@web/core/registry");
    const { services } = require("@web/core/environment");

    registry.category("services").add("dark_mode_service", {
        dependencies: ["color_scheme"],
        start(env) {
            // Apply dark mode immediately after login
            applyDarkModeImmediately(env);
        },
    });
});

What This Does:

  1. Cookie Initialization:
    • The color_scheme cookie is set to dark permanently.
  2. Theme Application on Load:
    • The applyDarkModeImmediately function ensures that the dark theme is applied as soon as the page loads, forcing the color scheme service to use the dark mode.
  3. Persistent Enforcement:
    • The custom service ensures dark mode is applied right after login without requiring a refresh.

Solution 2: Reload the Page Automatically After Login

If you're unable to initialize the dark mode immediately, you can trigger an automatic page reload after login to ensure the dark mode is applied without manual intervention.

Code Snippet:

/** @odoo-module **/

import { cookie as cookieManager } from "@web/core/browser/cookie";

// Set Dark Mode
function setDarkMode() {
    cookieManager.set("color_scheme", "dark", { path: "/", expires: 365 * 24 * 60 * 60 });
}

// Auto-reload after login to apply dark mode
odoo.define("auto_reload_after_login", function (require) {
    const session = require("web.session");

    // Trigger reload if the user just logged in
    session.on("login", () => {
        if (!cookieManager.get("reloaded")) {
            cookieManager.set("reloaded", "true", { path: "/", expires: 1 });
            window.location.reload();
        } else {
            cookieManager.delete("reloaded");
        }
    });
});

// Set dark mode on module load
setDarkMode();

How It Works:

  • After a login event, the page reloads automatically to ensure the dark theme is applied properly.
  • The reloaded cookie prevents the page from reloading infinitely.

Solution 3: Use Backend Override (Preferred for Full Control)

If modifying the frontend is not enough, you can enforce dark mode from the backend by customizing the user preferences.

Steps:

  1. Create a Custom Backend Module:
    • Write a custom module that sets the color_scheme preference for all users to "dark."
  2. Python Code for Backend Enforcement:
    from odoo import models, fields, api
    
    class ResUsers(models.Model):
        _inherit = 'res.users'
    
        @api.model
        def create(self, vals):
            # Set default color scheme to dark for new users
            vals['color_scheme'] = 'dark'
            return super(ResUsers, self).create(vals)
    
        def write(self, vals):
            # Ensure color scheme stays dark
            if 'color_scheme' not in vals:
                vals['color_scheme'] = 'dark'
            return super(ResUsers, self).write(vals)
    
  3. Enforce During Login:
    • Override the login method to set the color_scheme cookie for users.

Final Steps

  • Test in Different Scenarios:
    • Test the behavior for different users and ensure dark mode applies without manual refresh.
  • Clear Cache:
    • Clear browser and Odoo caches to ensure changes take effect.
  • Apply Custom Service Carefully:
    • If using a custom service, ensure it doesn’t conflict with other Odoo modules.

By implementing one of the above solutions, you can ensure dark mode is applied seamlessly after login without requiring a manual refresh. Let me know if you need further guidance!

0
Avatar
Descartar
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Registrar-se
Related Posts Respostes Vistes Activitat
How do you add direct attributable cost to an asset...
Odoo
Avatar
0
d’oct. 22
796
How to get view type in Odoo 8 Solved
Odoo
Avatar
Avatar
Avatar
3
de maig 25
5558
Report Actions Button(ir.actions.report) and Domain
Odoo v15
Avatar
Avatar
Avatar
Avatar
3
d’ag. 24
7364
eBay Error
ebay Odoo
Avatar
0
de maig 24
1960
Pass a many2one field from Purchase module to Inventory module Solved
Odoo v17
Avatar
Avatar
2
d’abr. 24
2100
Community
  • Tutorials
  • Documentació
  • Fòrum
Codi obert
  • Descarregar
  • GitHub
  • Runbot
  • Traduccions
Serveis
  • Allotjament a Odoo.sh
  • Suport
  • Actualització
  • Desenvolupaments personalitzats
  • Educació
  • Troba un comptable
  • Troba un partner
  • Converteix-te en partner
Sobre nosaltres
  • La nostra empresa
  • Actius de marca
  • Contacta amb nosaltres
  • Llocs de treball
  • Esdeveniments
  • Pòdcast
  • Blog
  • Clients
  • Informació legal • Privacitat
  • Seguretat
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo és un conjunt d'aplicacions empresarials de codi obert que cobreix totes les necessitats de la teva empresa: CRM, comerç electrònic, comptabilitat, inventari, punt de venda, gestió de projectes, etc.

La proposta única de valor d'Odoo és ser molt fàcil d'utilitzar i estar totalment integrat, ambdues alhora.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now