nfl / react-helmet

A document head manager for React
MIT License
17.36k stars 661 forks source link

Change favicon based on color scheme #520

Open tijsluitse opened 4 years ago

tijsluitse commented 4 years ago

Hi is it possible to change the favicon based on the color scheme? I have created this webhook in my SEO component. But it's not changing. The listener is working though.

useEffect(() => { window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => setDarkMode(e.matches)) return () => { window.matchMedia('(prefers-color-scheme: dark)').removeEventListener('change', e => setDarkMode(e.matches)) } }, [])

Thanks in advance!

ayozebarrera commented 2 years ago

You could save in the state if your theme is dark or light and then, in the render of your component, render with de icon

const favIcon = isDark ? "/dark-favicon.ico" : "/light-favicon.ico";

<Helmet>
   <link id="favicon" rel="icon" href={favIcon} type="image/x-icon"/>
</Helmet>