-
-
Notifications
You must be signed in to change notification settings - Fork 430
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
100 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,42 @@ | ||
import { FC, useEffect } from 'react'; | ||
import { MoonIcon, SunIcon } from '@heroicons/react/solid'; | ||
import { FC, useEffect, useState } from 'react'; | ||
import { HiMoon, HiSun } from 'react-icons/hi'; | ||
|
||
export type Theme = 'dark' | 'light'; | ||
|
||
export const DarkThemeToggle: FC = () => { | ||
useEffect(() => { | ||
const themeToggleDarkIcon = document.getElementById('theme-toggle-dark-icon'); | ||
const themeToggleLightIcon = document.getElementById('theme-toggle-light-icon'); | ||
const [theme, setTheme] = useState<Theme>(); | ||
|
||
// Change the icons inside the button based on previous settings | ||
useEffect(() => { | ||
if ( | ||
localStorage.getItem('color-theme') === 'dark' || | ||
(!('color-theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches) | ||
) { | ||
themeToggleLightIcon?.classList.remove('hidden'); | ||
setTheme('dark'); | ||
} else { | ||
themeToggleDarkIcon?.classList.remove('hidden'); | ||
setTheme('light'); | ||
} | ||
}, []); | ||
|
||
const themeToggleBtn = document.getElementById('theme-toggle'); | ||
|
||
const listener = () => { | ||
// toggle icons inside button | ||
themeToggleDarkIcon?.classList.toggle('hidden'); | ||
themeToggleLightIcon?.classList.toggle('hidden'); | ||
|
||
// if set via local storage previously | ||
if (localStorage.getItem('color-theme')) { | ||
if (localStorage.getItem('color-theme') === 'light') { | ||
document.documentElement.classList.add('dark'); | ||
localStorage.setItem('color-theme', 'dark'); | ||
} else { | ||
document.documentElement.classList.remove('dark'); | ||
localStorage.setItem('color-theme', 'light'); | ||
} | ||
|
||
// if NOT set via local storage previously | ||
useEffect(() => { | ||
if (theme) { | ||
localStorage.setItem('color-theme', theme); | ||
if (theme === 'dark') { | ||
document.documentElement.classList.add('dark'); | ||
} else { | ||
if (document.documentElement.classList.contains('dark')) { | ||
document.documentElement.classList.remove('dark'); | ||
localStorage.setItem('color-theme', 'light'); | ||
} else { | ||
document.documentElement.classList.add('dark'); | ||
localStorage.setItem('color-theme', 'dark'); | ||
} | ||
document.documentElement.classList.remove('dark'); | ||
} | ||
}; | ||
|
||
themeToggleBtn?.addEventListener('click', listener); | ||
} | ||
}, [theme]); | ||
|
||
return () => themeToggleBtn?.removeEventListener('click', listener); | ||
}, []); | ||
const toggleTheme = () => setTheme(theme === 'dark' ? 'light' : 'dark'); | ||
|
||
return ( | ||
<button | ||
id="theme-toggle" | ||
type="button" | ||
className="text-gray-500 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700 focus:outline-none focus:ring-4 focus:ring-gray-200 dark:focus:ring-gray-700 rounded-lg text-sm p-2.5" | ||
onClick={toggleTheme} | ||
> | ||
<MoonIcon id="theme-toggle-dark-icon" className="hidden w-5 h-5" /> | ||
<SunIcon id="theme-toggle-light-icon" className="hidden w-5 h-5" /> | ||
{theme === 'light' ? <HiMoon className="w-5 h-5" /> : <HiSun className="w-5 h-5" />} | ||
</button> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.