Skip to content

White flash on language switch in dark mode #101

Closed

Description

@DataDave-Dev

🇪🇸 Español

Con el tema oscuro activo, al cambiar de idioma desde el selector del header se ve un parpadeo blanco momentáneo: la página destella en claro y enseguida vuelve a oscuro. El tema no se queda en claro (eso es #95), es un flash transitorio.

Comportamiento esperado: cambiar de idioma no debe producir ningún destello; el fondo oscuro debe mantenerse de forma continua.

Causa raíz: la clase dark en <html> se gestiona de forma imperativa, no la posee React:

  • src/lib/use-theme.ts (useIsDark) re-aplica la clase dark dentro de un useEffect con dependencia pathname, que corre después del paint.
  • El <html> lo renderiza el layout (src/app/[lang]/layout.tsx) sin dark en su className. Al cambiar de locale (cambia el param [lang]), React reconcilia <html> y resetea su className, eliminando la clase dark que el script anti-FOUC/efecto habían añadido → se pinta en blanco → luego el useEffect vuelve a añadir dark. Ese ciclo es el parpadeo.

Relacionado: #95 (persistencia del tema al cambiar idioma) — misma área, síntoma distinto.

Archivos: src/lib/use-theme.ts, src/app/[lang]/layout.tsx, src/components/layout/Header.tsx

Solución sugerida: hacer que React sea dueño de la clase de tema en vez de mutarla imperativamente: guardar la preferencia en una cookie, leerla en el layout (Server Component) y renderizar <html className={isDark ? "dark" : ""}>. Así la clase es estable entre renders y navegaciones, se elimina el flash y se reduce la dependencia del script imperativo.


🇬🇧 English

With the dark theme active, switching languages from the header selector shows a brief white flash: the page flashes light and immediately returns to dark. The theme does not stay light (that's #95) — this is a transient flash.

Expected behavior: switching language should produce no flash; the dark background should stay continuous.

Root cause: the dark class on <html> is managed imperatively and not owned by React:

  • src/lib/use-theme.ts (useIsDark) re-applies the dark class inside a useEffect keyed on pathname, which runs after paint.
  • <html> is rendered by the layout (src/app/[lang]/layout.tsx) without dark in its className. On a locale change (the [lang] param changes), React reconciles <html> and resets its className, removing the dark class that the no-FOUC script/effect had added → a white paint → then the useEffect re-adds dark. That cycle is the flash.

Related: #95 (theme persistence on language switch) — same area, different symptom.

Files: src/lib/use-theme.ts, src/app/[lang]/layout.tsx, src/components/layout/Header.tsx

Suggested fix: make React own the theme class instead of mutating it imperatively: store the preference in a cookie, read it in the layout (Server Component), and render <html className={isDark ? "dark" : ""}>. The class then stays stable across renders and navigations, removing the flash and reducing reliance on the imperative script.

Activity

  1. added
    bug
    i18n
    on Jun 17, 2026
  2. rue-eru commented on Jun 18, 2026

    @rue-eru
    Contributor

    Hi! Maybe I can try to work on this Issue since the previous one (#60 ) you assign me to got resolved faster 😅

  3. DataDave-Dev commented on Jun 18, 2026

    @DataDave-Dev
    OwnerAuthor

    Hey @rue-eru,

    I just assigned you the issue so you can work on it.

  4. rue-eru commented on Jun 19, 2026

    @rue-eru
    Contributor

    @DataDave-Dev, I wanted to help with this issue, but I can't get the project to run locally. The build fails with a web-tree-sitter error, and the database (Turso) requires additional setup.

    I can still implement the fix based on the root cause analysis you provided, but I won't be able to test it. Could you please test the fix on your end if I open a PR?

  5. DataDave-Dev commented on Jun 20, 2026

    @DataDave-Dev
    OwnerAuthor

    @DataDave-Dev, I wanted to help with this issue, but I can't get the project to run locally. The build fails with a web-tree-sitter error, and the database (Turso) requires additional setup.

    I can still implement the fix based on the root cause analysis you provided, but I won't be able to test it. Could you please test the fix on your end if I open a PR?

    @rue-eru Of course yes, open the PR and I will review it without any problem, don't worry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugi18n

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions