From 67fe49b3227881b159a75c9d0280653bca03df4b Mon Sep 17 00:00:00 2001 From: wookie184 Date: Sat, 2 Apr 2022 07:05:13 +0100 Subject: [PATCH] Fix colour scheme toggling when switching from default dark mode (#2494) * Fix colour scheme toggling when switching from default dark mode * Add workaround for Chrome bug where disabling element doesn't cause rerender --- pep_sphinx_extensions/pep_theme/static/colour_scheme.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pep_sphinx_extensions/pep_theme/static/colour_scheme.js b/pep_sphinx_extensions/pep_theme/static/colour_scheme.js index 2cb6e9fab..cb69aa6ad 100644 --- a/pep_sphinx_extensions/pep_theme/static/colour_scheme.js +++ b/pep_sphinx_extensions/pep_theme/static/colour_scheme.js @@ -3,21 +3,24 @@ const pygmentsNormal = document.getElementById("pyg"); const pygmentsDark = document.getElementById("pyg-dark"); const makeLight = () => { - dark.media = pygmentsNormal.media = pygmentsDark.media = "" dark.disabled = pygmentsDark.disabled = true + dark.media = pygmentsNormal.media = pygmentsDark.media = "" pygmentsNormal.disabled = false } const makeDark = () => { - dark.media = pygmentsNormal.media = pygmentsDark.media = "" dark.disabled = pygmentsDark.disabled = false + dark.media = pygmentsNormal.media = pygmentsDark.media = "" pygmentsNormal.disabled = true } const toggleColourScheme = () => { - if (localStorage.getItem("colour_scheme") === "dark") { + const colourScheme = localStorage.getItem("colour_scheme") + const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches + + if ((colourScheme === "dark") || (!colourScheme && prefersDark)) { makeLight() localStorage.setItem("colour_scheme", "light") } else {