Dark & Light Mode: How to switch manifest and favicon?
Asked Answered
S

2

10

The manifest and the favicon are dependent on the light/darkmode is there any way of changing them as the user changes the mode in the operating system?

Ive got the event listener firing

  window.matchMedia('(prefers-color-scheme: dark)').addEventListener( "change", (e) => {
    if (e.matches) console.log('your in dark mode);
  });

but the manifest is loaded from the react public folder..

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="logo192.png" />
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <title>My Site</title>
  </head>
  <body>
    <noscript>Please enable JavaScript to run this app.</noscript>
    <div id="root"></div>
  </body>
</html>

Not sure how to handle the favicon that is also in the root of the public folder. Also the meta tag for theme-color would need to change.

Spheroid answered 2/9, 2019 at 9:44 Comment(1)
npmjs.com/package/react-helmetLactic
S
9

Using the suggestion from @kishore I've managed to get the favicon working, I'm sure someone better can tighten up my code but it works, in the html I added an id...

<link rel="shortcut icon" id='favicon' href="%PUBLIC_URL%/favicon.ico" />
<link rel="manifest" id='manifest' href="%PUBLIC_URL%/manifest.json" />

then in the head section I added...

    <script>
      const usesDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches || false;
      const favicon = document.getElementById('favicon');
      const manifest = document.getElementById('manifest');

      function switchIcon(usesDarkMode) {
        if (usesDarkMode) { 
          favicon.href = '%PUBLIC_URL%/favicon-dark.ico';
          manifest.href='%PUBLIC_URL%/manifest-dark.json' 
        } else {
        favicon.href = '%PUBLIC_URL%/favicon-light.ico';
        manifest.href='%PUBLIC_URL%/manifest-light.json' 
        }
      }

      window.matchMedia('(prefers-color-scheme: dark)').addEventListener( "change", (e) => switchIcon(e.matches));
      switchIcon(usesDarkMode);
    </script>

Spheroid answered 2/9, 2019 at 16:19 Comment(1)
Why do you need to update the manifest.json?Flashbulb
D
-1

you can change dynamically using js

document.getElementById('favicon_id').href = 'required_link'

do this inside onchange function

Degraded answered 2/9, 2019 at 12:9 Comment(3)
I didn't downvote, but you should supply an explanation of your code, how it helps and why. You basically said "do this" which is not a good answer. See the answer that Bill posted himself - that's how you should answer questions.Preachy
I can just suggest an idea to code. I cant give do your work which is coding. All people need is just idea. If it works u could have upvoted and accepted my answer.Degraded
I did, it took you from -1 to 0Spheroid

© 2022 - 2024 — McMap. All rights reserved.