Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dark/light mode on/off button Not working #234

Open
mustafa009 opened this issue Jun 30, 2023 · 22 comments
Open

Dark/light mode on/off button Not working #234

mustafa009 opened this issue Jun 30, 2023 · 22 comments

Comments

@mustafa009
Copy link

my website always load in dark or light mode as per system settings . The button does not work .

@suiato
Copy link

suiato commented Jul 4, 2023

Mine was working fine somehow, but the button stopped working recently... The error is something like

a2885c2625fb42caaba3ded2835d9735:41 Uncaught TypeError: Cannot read properties of undefined (reading 'ThemeStore')
    at onDark (a2885c2625fb42caaba3ded2835d9735:41:31)
    at addDarkModeButton (a2885c2625fb42caaba3ded2835d9735:61:9)
    at MutationObserver.<anonymous> (a2885c2625fb42caaba3ded2835d9735:71:11)

Probably, the line
__console.environment.ThemeStore.setState({ mode: 'dark' });
isn't working.
Is it because the function related to ThemeStore is updated on the Notion side?

@jiahao-shen
Copy link

The same problem

@JustSleightly
Copy link

+1
I've also noticed this more recently within the last week on my own notion

@jenpauros
Copy link

+1
I've also noticed the same issue just now

@dadwi91
Copy link

dadwi91 commented Jul 12, 2023

Same problem. I think it's not only the button but forcing the website to use dark mode as default does not work anymore either.

@menukaonline
Copy link

I also noticed the same issue. I think Notion has made changes to how styles are applied. Previously, they would add a class to the body tag when a dark theme was selected. Similarly, when switching to the light theme, the added class would be removed from the body tag. However, it seems that styles are now loaded differently.

But I think we can still resolve this issue. My suggestion is to simulate their default theme-switching keyboard shortcut when the theme-switching button is clicked using JavaScript.

For Windows, the keyboard shortcut is Ctrl + Shift + L, and for macOS, it is Cmd + Shift + L

@guoyiheng
Copy link

The reason:
The isEnabled property of the __console variable is set to false by default, which prevents the use of certain effects, including the environment.ThemeStore related to the theme.
Solution:
The original code:

function onDark() {
  el.innerHTML =
    '<div title="Change to Light Mode" style="margin-left: auto; margin-right: 14px; min-width: 0px;"><div role="button" tabindex="0" style="user-select: none; transition: background 120ms ease-in 0s; cursor: pointer; border-radius: 44px;"><div style="display: flex; flex-shrink: 0; height: 14px; width: 26px; border-radius: 44px; padding: 2px; box-sizing: content-box; background: rgb(46, 170, 220); transition: background 200ms ease 0s, box-shadow 200ms ease 0s;"><div style="width: 14px; height: 14px; border-radius: 44px; background: white; transition: transform 200ms ease-out 0s, background 200ms ease-out 0s; transform: translateX(12px) translateY(0px);"></div></div></div></div>'
  document.body.classList.add('dark')
  __console.environment.ThemeStore.setState({ mode: 'dark' })
}
function onLight() {
  el.innerHTML =
    '<div title="Change to Dark Mode" style="margin-left: auto; margin-right: 14px; min-width: 0px;"><div role="button" tabindex="0" style="user-select: none; transition: background 120ms ease-in 0s; cursor: pointer; border-radius: 44px;"><div style="display: flex; flex-shrink: 0; height: 14px; width: 26px; border-radius: 44px; padding: 2px; box-sizing: content-box; background: rgba(135, 131, 120, 0.3); transition: background 200ms ease 0s, box-shadow 200ms ease 0s;"><div style="width: 14px; height: 14px; border-radius: 44px; background: white; transition: transform 200ms ease-out 0s, background 200ms ease-out 0s; transform: translateX(0px) translateY(0px);"></div></div></div></div>'
  document.body.classList.remove('dark')
  __console.environment.ThemeStore.setState({ mode: 'light' })
}

Replaced code:

function enableConsoleEffectAndSetMode(mode) {
  if (__console && !__console.isEnabled) {
    __console.enable()
    window.location.reload()
  } else {
    __console.environment.ThemeStore.setState({ mode: mode })
  }
}
function onDark() {
  el.innerHTML =
    '<div title="Change to Light Mode" style="margin-left: auto; margin-right: 14px; min-width: 0px;"><div role="button" tabindex="0" style="user-select: none; transition: background 120ms ease-in 0s; cursor: pointer; border-radius: 44px;"><div style="display: flex; flex-shrink: 0; height: 14px; width: 26px; border-radius: 44px; padding: 2px; box-sizing: content-box; background: rgb(46, 170, 220); transition: background 200ms ease 0s, box-shadow 200ms ease 0s;"><div style="width: 14px; height: 14px; border-radius: 44px; background: white; transition: transform 200ms ease-out 0s, background 200ms ease-out 0s; transform: translateX(12px) translateY(0px);"></div></div></div></div>'
  document.body.classList.add('dark')
  enableConsoleEffectAndSetMode('dark')
}
function onLight() {
  el.innerHTML =
    '<div title="Change to Dark Mode" style="margin-left: auto; margin-right: 14px; min-width: 0px;"><div role="button" tabindex="0" style="user-select: none; transition: background 120ms ease-in 0s; cursor: pointer; border-radius: 44px;"><div style="display: flex; flex-shrink: 0; height: 14px; width: 26px; border-radius: 44px; padding: 2px; box-sizing: content-box; background: rgba(135, 131, 120, 0.3); transition: background 200ms ease 0s, box-shadow 200ms ease 0s;"><div style="width: 14px; height: 14px; border-radius: 44px; background: white; transition: transform 200ms ease-out 0s, background 200ms ease-out 0s; transform: translateX(0px) translateY(0px);"></div></div></div></div>'
  document.body.classList.remove('dark')
  enableConsoleEffectAndSetMode('light')
}

NOTICE
Upon the first visit to the page, since isEnabled default false, the __console.enable() function is called, requiring a page refresh to take effect. Therefore, there is a code snippet window.location.reload() to trigger the refresh. If you do not need this, you can manually refresh the page.

On subsequent visits to the page, if isEnabled is true, the page will not be reloaded.

@dadwi91
Copy link

dadwi91 commented Jul 19, 2023

Thanks for this! it's working again on my side.

The reason: The isEnabled property of the __console variable is set to false by default, which prevents the use of certain effects, including the environment.ThemeStore related to the theme. Solution: The original code:

function onDark() {
  el.innerHTML =
    '<div title="Change to Light Mode" style="margin-left: auto; margin-right: 14px; min-width: 0px;"><div role="button" tabindex="0" style="user-select: none; transition: background 120ms ease-in 0s; cursor: pointer; border-radius: 44px;"><div style="display: flex; flex-shrink: 0; height: 14px; width: 26px; border-radius: 44px; padding: 2px; box-sizing: content-box; background: rgb(46, 170, 220); transition: background 200ms ease 0s, box-shadow 200ms ease 0s;"><div style="width: 14px; height: 14px; border-radius: 44px; background: white; transition: transform 200ms ease-out 0s, background 200ms ease-out 0s; transform: translateX(12px) translateY(0px);"></div></div></div></div>'
  document.body.classList.add('dark')
  __console.environment.ThemeStore.setState({ mode: 'dark' })
}
function onLight() {
  el.innerHTML =
    '<div title="Change to Dark Mode" style="margin-left: auto; margin-right: 14px; min-width: 0px;"><div role="button" tabindex="0" style="user-select: none; transition: background 120ms ease-in 0s; cursor: pointer; border-radius: 44px;"><div style="display: flex; flex-shrink: 0; height: 14px; width: 26px; border-radius: 44px; padding: 2px; box-sizing: content-box; background: rgba(135, 131, 120, 0.3); transition: background 200ms ease 0s, box-shadow 200ms ease 0s;"><div style="width: 14px; height: 14px; border-radius: 44px; background: white; transition: transform 200ms ease-out 0s, background 200ms ease-out 0s; transform: translateX(0px) translateY(0px);"></div></div></div></div>'
  document.body.classList.remove('dark')
  __console.environment.ThemeStore.setState({ mode: 'light' })
}

Replaced code:

function enableConsoleEffectAndSetMode(mode) {
  if (__console && !__console.isEnabled) {
    __console.enable()
    window.location.reload()
  } else {
    __console.environment.ThemeStore.setState({ mode: mode })
  }
}
function onDark() {
  el.innerHTML =
    '<div title="Change to Light Mode" style="margin-left: auto; margin-right: 14px; min-width: 0px;"><div role="button" tabindex="0" style="user-select: none; transition: background 120ms ease-in 0s; cursor: pointer; border-radius: 44px;"><div style="display: flex; flex-shrink: 0; height: 14px; width: 26px; border-radius: 44px; padding: 2px; box-sizing: content-box; background: rgb(46, 170, 220); transition: background 200ms ease 0s, box-shadow 200ms ease 0s;"><div style="width: 14px; height: 14px; border-radius: 44px; background: white; transition: transform 200ms ease-out 0s, background 200ms ease-out 0s; transform: translateX(12px) translateY(0px);"></div></div></div></div>'
  document.body.classList.add('dark')
  enableConsoleEffectAndSetMode('dark')
}
function onLight() {
  el.innerHTML =
    '<div title="Change to Dark Mode" style="margin-left: auto; margin-right: 14px; min-width: 0px;"><div role="button" tabindex="0" style="user-select: none; transition: background 120ms ease-in 0s; cursor: pointer; border-radius: 44px;"><div style="display: flex; flex-shrink: 0; height: 14px; width: 26px; border-radius: 44px; padding: 2px; box-sizing: content-box; background: rgba(135, 131, 120, 0.3); transition: background 200ms ease 0s, box-shadow 200ms ease 0s;"><div style="width: 14px; height: 14px; border-radius: 44px; background: white; transition: transform 200ms ease-out 0s, background 200ms ease-out 0s; transform: translateX(0px) translateY(0px);"></div></div></div></div>'
  document.body.classList.remove('dark')
  enableConsoleEffectAndSetMode('light')
}

NOTICE Upon the first visit to the page, since isEnabled default false, the __console.enable() function is called, requiring a page refresh to take effect. Therefore, there is a code snippet window.location.reload() to trigger the refresh. If you do not need this, you can manually refresh the page.

On subsequent visits to the page, if isEnabled is true, the page will not be reloaded.

@remgriff
Copy link

Thank you, @guoyiheng! Your solution works flawlessly!

@screekworkshop
Copy link

The reason: The isEnabled property of the __console variable is set to false by default, which prevents the use of certain effects, including the environment.ThemeStore related to the theme. Solution: The original code:

function onDark() {
  el.innerHTML =
    '<div title="Change to Light Mode" style="margin-left: auto; margin-right: 14px; min-width: 0px;"><div role="button" tabindex="0" style="user-select: none; transition: background 120ms ease-in 0s; cursor: pointer; border-radius: 44px;"><div style="display: flex; flex-shrink: 0; height: 14px; width: 26px; border-radius: 44px; padding: 2px; box-sizing: content-box; background: rgb(46, 170, 220); transition: background 200ms ease 0s, box-shadow 200ms ease 0s;"><div style="width: 14px; height: 14px; border-radius: 44px; background: white; transition: transform 200ms ease-out 0s, background 200ms ease-out 0s; transform: translateX(12px) translateY(0px);"></div></div></div></div>'
  document.body.classList.add('dark')
  __console.environment.ThemeStore.setState({ mode: 'dark' })
}
function onLight() {
  el.innerHTML =
    '<div title="Change to Dark Mode" style="margin-left: auto; margin-right: 14px; min-width: 0px;"><div role="button" tabindex="0" style="user-select: none; transition: background 120ms ease-in 0s; cursor: pointer; border-radius: 44px;"><div style="display: flex; flex-shrink: 0; height: 14px; width: 26px; border-radius: 44px; padding: 2px; box-sizing: content-box; background: rgba(135, 131, 120, 0.3); transition: background 200ms ease 0s, box-shadow 200ms ease 0s;"><div style="width: 14px; height: 14px; border-radius: 44px; background: white; transition: transform 200ms ease-out 0s, background 200ms ease-out 0s; transform: translateX(0px) translateY(0px);"></div></div></div></div>'
  document.body.classList.remove('dark')
  __console.environment.ThemeStore.setState({ mode: 'light' })
}

Replaced code:

function enableConsoleEffectAndSetMode(mode) {
  if (__console && !__console.isEnabled) {
    __console.enable()
    window.location.reload()
  } else {
    __console.environment.ThemeStore.setState({ mode: mode })
  }
}
function onDark() {
  el.innerHTML =
    '<div title="Change to Light Mode" style="margin-left: auto; margin-right: 14px; min-width: 0px;"><div role="button" tabindex="0" style="user-select: none; transition: background 120ms ease-in 0s; cursor: pointer; border-radius: 44px;"><div style="display: flex; flex-shrink: 0; height: 14px; width: 26px; border-radius: 44px; padding: 2px; box-sizing: content-box; background: rgb(46, 170, 220); transition: background 200ms ease 0s, box-shadow 200ms ease 0s;"><div style="width: 14px; height: 14px; border-radius: 44px; background: white; transition: transform 200ms ease-out 0s, background 200ms ease-out 0s; transform: translateX(12px) translateY(0px);"></div></div></div></div>'
  document.body.classList.add('dark')
  enableConsoleEffectAndSetMode('dark')
}
function onLight() {
  el.innerHTML =
    '<div title="Change to Dark Mode" style="margin-left: auto; margin-right: 14px; min-width: 0px;"><div role="button" tabindex="0" style="user-select: none; transition: background 120ms ease-in 0s; cursor: pointer; border-radius: 44px;"><div style="display: flex; flex-shrink: 0; height: 14px; width: 26px; border-radius: 44px; padding: 2px; box-sizing: content-box; background: rgba(135, 131, 120, 0.3); transition: background 200ms ease 0s, box-shadow 200ms ease 0s;"><div style="width: 14px; height: 14px; border-radius: 44px; background: white; transition: transform 200ms ease-out 0s, background 200ms ease-out 0s; transform: translateX(0px) translateY(0px);"></div></div></div></div>'
  document.body.classList.remove('dark')
  enableConsoleEffectAndSetMode('light')
}

NOTICE Upon the first visit to the page, since isEnabled default false, the __console.enable() function is called, requiring a page refresh to take effect. Therefore, there is a code snippet window.location.reload() to trigger the refresh. If you do not need this, you can manually refresh the page.

On subsequent visits to the page, if isEnabled is true, the page will not be reloaded.

work perfect, thx.

@suiato
Copy link

suiato commented Aug 8, 2023

Thank you, @guoyiheng, for the code. My public website's mode button resumed working correcly.

@jesenator
Copy link

Thanks, this worked except for when I opened my website on computers with dark theme defaulted. Then it would open in dark mode, and then refresh to light mode. If you want the theme to be preserved you can replace the addDarkModeButton function with this. I haven't tested this extensively, but it works for me.
If you don't call the onDark or onLight functions it doesn't had the toggle for some reason, so this addition calls the correct function based off of whichever theme is already active

function addDarkModeButton(device) {
        const nav = device === 'web' ? document.querySelector('.notion-topbar').firstChild : document.querySelector('.notion-topbar-mobile');
        el.className = 'toggle-mode';
        el.addEventListener('click', toggle);
        nav.appendChild(el);

        /* get the current theme and add the toggle to represent that theme */
        const currentTheme = JSON.parse(localStorage.getItem('theme')).mode;
        if (currentTheme == 'dark') { onDark(); }
        if (currentTheme == 'light') { onLight(); }      
      }

@jayviddy
Copy link

jayviddy commented Aug 13, 2023

Wow, thank you! I've been looking for this fix and the code worked perfectly.

Question: This works on desktop for me, but when I load the notion fruition website on mobile it's still inheriting system setting vs. defaulting to a forced light mode. Are y'all having this problem too? Ideas?

@guoyiheng
Copy link

guoyiheng commented Aug 14, 2023

  1. Thanks for the code of jesenator.

  2. Notion problem. The mode settings of Notion will not be saved and will reset every time. I suspect that there is a problem with the priority of the code logic, where the system settings are checked first and then the localStorage, causing it to reset every time it is refreshed. Even if you use Cmd + Shift + L, it will still reset.
    I use a new localStorage variable newTheme to store the current mode. However, **it is still not perfect because the notion code will be executed first, causing a delay in the settings. **

  3. Mobile problem . On the mobile phone, there is no problem with the display when modeButton is first added. After refreshing, there will be a problem with the display as shown in the figure. modeButton has been moved to the front for unknown reasons, most likely caused by code issues in notion.

image

Based on the previous problems and code, the final code is as follows.

function enableConsoleEffectAndSetMode(mode){
  if (__console && !__console.isEnabled) {
    __console.enable();
    window.location.reload();
  } else {
    __console.environment.ThemeStore.setState({ mode: mode });
 	localStorage.setItem('newTheme', JSON.stringify({ mode: mode }));
  }
}
function onDark() {
  el.innerHTML = '<div title="Change to Light Mode" style="margin-left: 14px; margin-right: 14px; min-width: 0px;"><div role="button" tabindex="0" style="user-select: none; transition: background 120ms ease-in 0s; cursor: pointer; border-radius: 44px;"><div style="display: flex; flex-shrink: 0; height: 14px; width: 26px; border-radius: 44px; padding: 2px; box-sizing: content-box; background: rgb(46, 170, 220); transition: background 200ms ease 0s, box-shadow 200ms ease 0s;"><div style="width: 14px; height: 14px; border-radius: 44px; background: white; transition: transform 200ms ease-out 0s, background 200ms ease-out 0s; transform: translateX(12px) translateY(0px);"></div></div></div></div>';
  document.body.classList.add('dark');
  enableConsoleEffectAndSetMode('dark')
}
function onLight() {
  el.innerHTML = '<div title="Change to Dark Mode" style="margin-left: 14px; margin-right: 14px; min-width: 0px;"><div role="button" tabindex="0" style="user-select: none; transition: background 120ms ease-in 0s; cursor: pointer; border-radius: 44px;"><div style="display: flex; flex-shrink: 0; height: 14px; width: 26px; border-radius: 44px; padding: 2px; box-sizing: content-box; background: rgba(135, 131, 120, 0.3); transition: background 200ms ease 0s, box-shadow 200ms ease 0s;"><div style="width: 14px; height: 14px; border-radius: 44px; background: white; transition: transform 200ms ease-out 0s, background 200ms ease-out 0s; transform: translateX(0px) translateY(0px);"></div></div></div></div>';
  document.body.classList.remove('dark');
  enableConsoleEffectAndSetMode('light')
}
function toggle() {
  if (document.body.classList.contains('dark')) {
    onLight();
  } else {
    onDark();
  }
}
function addDarkModeButton(device) {
  const nav =
    device === 'web'
      ? document.querySelector('.notion-topbar').firstChild
      : document.querySelector('.notion-topbar-mobile')
  el.className = 'toggle-mode'
  el.addEventListener('click', toggle)
  const timeout = device === 'web' ? 0 : 500
  setTimeout(() => {
    nav.appendChild(el)
  }, timeout)
  // get the current theme and add the toggle to represent that theme
  const currentTheme = JSON.parse(localStorage.getItem('newTheme'))?.mode
  if (currentTheme) {
    if (currentTheme === 'dark') {
      onDark()
    }else{
      onLight()
    }
  } else {
    // enable smart dark mode based on user-preference
    if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
      onDark()
    } else {
      onLight()
    }
  }
  // try to detect if user-preference change
  window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
    toggle()
  })
}

One more thing

If you want to customize the head of the notion. For example, the buttons of try notion are not aesthetically pleasing. You can remove them yourself.
Change the number of nth-child to change the visible of the button.

PC:
image

Mobile:
iShot_2023-08-14_10 22 47

Here is the code position. HeadRewriter

class HeadRewriter {
  element(element) {
    if (GOOGLE_FONT !== '') {
      element.append(
        `<link href='https://fonts.googleapis.com/css?family=${GOOGLE_FONT.replace(
          ' ',
          '+'
        )}:Regular,Bold,Italic&display=swap' rel='stylesheet'>
        <style>* { font-family: "${GOOGLE_FONT}" !important; }</style>`,
        {
          html: true,
        }
      )
    }
    element.append(
      `<style>
      div.notion-topbar > div > div:nth-child(3) { display: none !important; }
      div.notion-topbar > div > div:nth-child(5) { display: none !important; }
      div.notion-topbar > div > div:nth-child(6) { display: none !important; }
      div.notion-topbar > div > div:nth-child(7) { display: none !important; }
      div.notion-topbar > div > div:nth-child(1n).toggle-mode { display: block !important; }

      div.notion-topbar-mobile > div:nth-child(4) { display: none !important; }
      div.notion-topbar-mobile > div:nth-child(1n).toggle-mode { display: block !important; }
      </style>`,
      {
        html: true,
      }
    )
  }
}

I have modified my own code several times without thorough testing. If there are any issues, welcome to communicate

Full Code: https://github.com/guoyiheng/vercel-notion

@jfmadridc
Copy link

Hi is there a simple way to just always show the dark theme for everyone? even if that means not having the Toogle button

thanks in advance.

@guoyiheng
Copy link

Hi is there a simple way to just always show the dark theme for everyone? even if that means not having the Toogle button

thanks in advance.

Just remove addDarkModeButton method & execute the onDark method.

@jfmadridc
Copy link

Hi is there a simple way to just always show the dark theme for everyone? even if that means not having the Toogle button

thanks in advance.

Just remove addDarkModeButton method & execute the onDark method.

Hi, sorry to be so annoying your suggestion works great just with one detail

When i do it and go to root domain it ends up at rootdomain.com/longNotion url even if it its the root page

That normally with the regular code does just loads rootdomain.com

Also would love to implement the search button that you showed in desktop and mobile

I tried to understand the syntax but got confused, to new to programming

Any way of, always dark mode, no toogle button, yes search button, And the back button shows login screen fix? Im trying to implement from your and the other screen fix but when i fix one i break the other

Thank you so much

@jraadd
Copy link

jraadd commented Aug 24, 2023

Hi is there a simple way to just always show the dark theme for everyone? even if that means not having the Toogle button
thanks in advance.

Just remove addDarkModeButton method & execute the onDark method.

Hi, I'm also trying to keep my site on light mode always, but even with this suggestion it switches to dark mode if my Mac is on dark mode.

If you could please share the exact code to only enable light mode, that'd be very helpful. Thanks!

@mbochynski
Copy link

I've just tested solution by @guoyiheng and it works fine for me. Furthermore I was not a fan of reloading the page, and tested it without the window.location.reload();, and it works perfectly fine to me. Might be worth checking for your scenarios as well. Tested in FF, Safari, and Chrome.

@jraadd
Copy link

jraadd commented Aug 28, 2023

Thanks for testing @mbochynski. I've just tried again but still no luck. Is there something I'm missing?

I changed the included code under class BodyRewriter from:

  function onDark() {
    el.innerHTML = '<div title="Change to Light Mode" style="margin-left: auto; margin-right: 14px; min-width: 0px;"><div role="button" tabindex="0" style="user-select: none; transition: background 120ms ease-in 0s; cursor: pointer; border-radius: 44px;"><div style="display: flex; flex-shrink: 0; height: 14px; width: 26px; border-radius: 44px; padding: 2px; box-sizing: content-box; background: rgb(46, 170, 220); transition: background 200ms ease 0s, box-shadow 200ms ease 0s;"><div style="width: 14px; height: 14px; border-radius: 44px; background: white; transition: transform 200ms ease-out 0s, background 200ms ease-out 0s; transform: translateX(12px) translateY(0px);"></div></div></div></div>';
    document.body.classList.add('dark');
    __console.environment.ThemeStore.setState({ mode: 'dark' });
  };
  function onLight() {
    el.innerHTML = '<div title="Change to Dark Mode" style="margin-left: auto; margin-right: 14px; min-width: 0px;"><div role="button" tabindex="0" style="user-select: none; transition: background 120ms ease-in 0s; cursor: pointer; border-radius: 44px;"><div style="display: flex; flex-shrink: 0; height: 14px; width: 26px; border-radius: 44px; padding: 2px; box-sizing: content-box; background: rgba(135, 131, 120, 0.3); transition: background 200ms ease 0s, box-shadow 200ms ease 0s;"><div style="width: 14px; height: 14px; border-radius: 44px; background: white; transition: transform 200ms ease-out 0s, background 200ms ease-out 0s; transform: translateX(0px) translateY(0px);"></div></div></div></div>';
    document.body.classList.remove('dark');
    __console.environment.ThemeStore.setState({ mode: 'light' });
  }
  function toggle() {
    if (document.body.classList.contains('dark')) {
      onLight();
    } else {
      onDark();
    }
  }
  function addDarkModeButton(device) {
    const nav = device === 'web' ? document.querySelector('.notion-topbar').firstChild : document.querySelector('.notion-topbar-mobile');
    el.className = 'toggle-mode';
    el.addEventListener('click', toggle);
    nav.appendChild(el);
    onLight();
  }

To this:

  function enableConsoleEffectAndSetMode(mode){
    if (__console && !__console.isEnabled) {
      __console.enable();
      window.location.reload();
    } else {
      __console.environment.ThemeStore.setState({ mode: mode });
     localStorage.setItem('newTheme', JSON.stringify({ mode: mode }));
    }
  }
  function onLight() {
    el.innerHTML = '<div title="Change to Dark Mode" style="margin-left: 14px; margin-right: 14px; min-width: 0px;"><div role="button" tabindex="0" style="user-select: none; transition: background 120ms ease-in 0s; cursor: pointer; border-radius: 44px;"><div style="display: flex; flex-shrink: 0; height: 14px; width: 26px; border-radius: 44px; padding: 2px; box-sizing: content-box; background: rgba(135, 131, 120, 0.3); transition: background 200ms ease 0s, box-shadow 200ms ease 0s;"><div style="width: 14px; height: 14px; border-radius: 44px; background: white; transition: transform 200ms ease-out 0s, background 200ms ease-out 0s; transform: translateX(0px) translateY(0px);"></div></div></div></div>';
    document.body.classList.remove('dark');
    enableConsoleEffectAndSetMode('light')
  }
  function toggle() {
    if (document.body.classList.contains('dark')) {
      onLight();
    } else {
      onLight();
    }
  }
    // try to detect if user-preference change
    window.matchMedia('(prefers-color-scheme: light)').addEventListener('change', (e) => {
      toggle()
    })

The site still loads on the system default dark mode instead of always remaining on light mode.
I only want light mode and I've already removed the light/dark toggle UI since I don't need it.

@JustSleightly
Copy link

I've just tested solution by @guoyiheng and it works fine for me. Furthermore I was not a fan of reloading the page, and tested it without the window.location.reload();, and it works perfectly fine to me. Might be worth checking for your scenarios as well. Tested in FF, Safari, and Chrome.

I definitely noticed that removing the window reload doesn't set the dark theme default when navigating straight to a sub-page, tested with an incognito browser. It does when going to my root fruitionsite.

Implementing this PR #243 however resolved another issue I was having, along with this one without needing the window reload by setting the console differently.

@yinan-star
Copy link

The reason: The isEnabled property of the __console variable is set to false by default, which prevents the use of certain effects, including the environment.ThemeStore related to the theme. Solution: The original code:

function onDark() {
  el.innerHTML =
    '<div title="Change to Light Mode" style="margin-left: auto; margin-right: 14px; min-width: 0px;"><div role="button" tabindex="0" style="user-select: none; transition: background 120ms ease-in 0s; cursor: pointer; border-radius: 44px;"><div style="display: flex; flex-shrink: 0; height: 14px; width: 26px; border-radius: 44px; padding: 2px; box-sizing: content-box; background: rgb(46, 170, 220); transition: background 200ms ease 0s, box-shadow 200ms ease 0s;"><div style="width: 14px; height: 14px; border-radius: 44px; background: white; transition: transform 200ms ease-out 0s, background 200ms ease-out 0s; transform: translateX(12px) translateY(0px);"></div></div></div></div>'
  document.body.classList.add('dark')
  __console.environment.ThemeStore.setState({ mode: 'dark' })
}
function onLight() {
  el.innerHTML =
    '<div title="Change to Dark Mode" style="margin-left: auto; margin-right: 14px; min-width: 0px;"><div role="button" tabindex="0" style="user-select: none; transition: background 120ms ease-in 0s; cursor: pointer; border-radius: 44px;"><div style="display: flex; flex-shrink: 0; height: 14px; width: 26px; border-radius: 44px; padding: 2px; box-sizing: content-box; background: rgba(135, 131, 120, 0.3); transition: background 200ms ease 0s, box-shadow 200ms ease 0s;"><div style="width: 14px; height: 14px; border-radius: 44px; background: white; transition: transform 200ms ease-out 0s, background 200ms ease-out 0s; transform: translateX(0px) translateY(0px);"></div></div></div></div>'
  document.body.classList.remove('dark')
  __console.environment.ThemeStore.setState({ mode: 'light' })
}

Replaced code:

function enableConsoleEffectAndSetMode(mode) {
  if (__console && !__console.isEnabled) {
    __console.enable()
    window.location.reload()
  } else {
    __console.environment.ThemeStore.setState({ mode: mode })
  }
}
function onDark() {
  el.innerHTML =
    '<div title="Change to Light Mode" style="margin-left: auto; margin-right: 14px; min-width: 0px;"><div role="button" tabindex="0" style="user-select: none; transition: background 120ms ease-in 0s; cursor: pointer; border-radius: 44px;"><div style="display: flex; flex-shrink: 0; height: 14px; width: 26px; border-radius: 44px; padding: 2px; box-sizing: content-box; background: rgb(46, 170, 220); transition: background 200ms ease 0s, box-shadow 200ms ease 0s;"><div style="width: 14px; height: 14px; border-radius: 44px; background: white; transition: transform 200ms ease-out 0s, background 200ms ease-out 0s; transform: translateX(12px) translateY(0px);"></div></div></div></div>'
  document.body.classList.add('dark')
  enableConsoleEffectAndSetMode('dark')
}
function onLight() {
  el.innerHTML =
    '<div title="Change to Dark Mode" style="margin-left: auto; margin-right: 14px; min-width: 0px;"><div role="button" tabindex="0" style="user-select: none; transition: background 120ms ease-in 0s; cursor: pointer; border-radius: 44px;"><div style="display: flex; flex-shrink: 0; height: 14px; width: 26px; border-radius: 44px; padding: 2px; box-sizing: content-box; background: rgba(135, 131, 120, 0.3); transition: background 200ms ease 0s, box-shadow 200ms ease 0s;"><div style="width: 14px; height: 14px; border-radius: 44px; background: white; transition: transform 200ms ease-out 0s, background 200ms ease-out 0s; transform: translateX(0px) translateY(0px);"></div></div></div></div>'
  document.body.classList.remove('dark')
  enableConsoleEffectAndSetMode('light')
}

NOTICE Upon the first visit to the page, since isEnabled default false, the __console.enable() function is called, requiring a page refresh to take effect. Therefore, there is a code snippet window.location.reload() to trigger the refresh. If you do not need this, you can manually refresh the page.

On subsequent visits to the page, if isEnabled is true, the page will not be reloaded.

Fuck Yes!!! I have resolved already!!!!! Both the dark or llight button and the back buttom of the google.But the tricky thing is the time of loding my page was too long. That's ridiculous.....does anyone have this kind od issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests