remark42 icon indicating copy to clipboard operation
remark42 copied to clipboard

`changeTheme` does not work when `REMARK42::ready` event is fired

Open alex4814 opened this issue 2 years ago • 4 comments

Calling window.REMARK42.changeTheme while <div id="remark42"> is not fully initialized (i.e., not showing in the page footer, but REMARK42::ready event is fired) does not really affect the style.

What I want to do is to ensure the dark/light theme be same with blog theme.

Maybe something like REMARK::div-ready is needed? or any workarounds.

alex4814 avatar Feb 19 '23 10:02 alex4814

Hey, why would you need to call it when it's not loaded? Initial theme value can be sent in remark_config. Could you please describe your case?

akellbl4 avatar Feb 24 '23 01:02 akellbl4

Target is the theme between remark42 and the site should match when clicking "toggle-theme" button.

Suppose the initial theme of the site is "light", then it is also set in remark_config. However, the toggle-theme button may be clicked when the remark42 div is not loaded yet (i.e., click toggle theme button right after refreshing the current page before remark42 div at the bottom loaded). During this gap, remark42 does not know anything about theme change.

I need the commented line below to match themes, cuz what I found at the moment is only "REMARK42::ready" event.

<script>
  window.addEventListener("REMARK42::ready", () => {
    window.addEventListener('papermod::theme-toggle', e => {
      window.REMARK42.changeTheme(e.detail)
    });
    // window.REMARK42.changeTheme(current_theme)
  })
</script>

P.S. I'm not so familiar with javascript or DOM, so may be there's another way to achieve the same thing.

alex4814 avatar Feb 26 '23 13:02 alex4814

This should help.

<script>
  window.addEventListener("REMARK42::ready", () => {
    window.addEventListener('papermod::theme-toggle', e => {
      window.REMARK42.changeTheme(e.detail)
    });
    window.REMARK42.changeTheme(document.body.classList.add('dark') ? 'dark' : 'light')
  })
</script>

As I understand you're using https://github.com/adityatelange/hugo-PaperMod so code below should be compatible with it

akellbl4 avatar Feb 28 '23 00:02 akellbl4

Yes, I did that before and it is exactly the problem this issue is for.

<script>
  window.addEventListener("REMARK42::ready", () => {
    window.addEventListener('papermod::theme-toggle', e => {
      console.log("theme-toggle", e.detail)
      window.REMARK42.changeTheme(e.detail)
    });
    console.log("local storage pref theme", localStorage.getItem('pref-theme'))
    window.REMARK42.changeTheme(localStorage.getItem('pref-theme'))
  })
</script>

The calling of changeTheme does not actually change the theme.
This screen video may help clarify the case.

alex4814 avatar Feb 28 '23 07:02 alex4814