Skip to content

Outdated Dark Mode Variable #2060

@stupidJoon

Description

@stupidJoon

It seems that the dark mode section in the official documentation is using both theme and currentTheme inconsistent.

https://tailwindcss.com/docs/dark-mode#with-system-theme-support

// On page load or when changing themes, best to add inline in `head` to avoid FOUC
document.documentElement.classList.toggle(
  "dark",
  localStorage.currentTheme === "dark" ||
    (!("theme" in localStorage) && window.matchMedia("(prefers-color-scheme: dark)").matches),
);
// Whenever the user explicitly chooses light mode
localStorage.currentTheme = "light";
// Whenever the user explicitly chooses dark mode
localStorage.currentTheme = "dark";
// Whenever the user explicitly chooses to respect the OS preference
localStorage.removeItem("theme");

Based on the commit history, it seems that the variable was renamed from theme to currentTheme

24d9da6

The documentation should be updated accordingly.

Fix:

// On page load or when changing themes, best to add inline in `head` to avoid FOUC
document.documentElement.classList.toggle(
  "dark",
  localStorage.currentTheme === "dark" ||
    (!("currentTheme" in localStorage) && window.matchMedia("(prefers-color-scheme: dark)").matches),
);
// Whenever the user explicitly chooses light mode
localStorage.currentTheme = "light";
// Whenever the user explicitly chooses dark mode
localStorage.currentTheme = "dark";
// Whenever the user explicitly chooses to respect the OS preference
localStorage.removeItem("currentTheme");

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions