This repository has been archived by the owner on Dec 16, 2024. It is now read-only.
generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dark mode toggle and store in local storage (#32)
![Screenshot 2023-05-30 at 12 12 02 PM](https://github.com/TBD54566975/ftl/assets/51647/a0860a4a-c599-45e3-9639-bf5a350d3587) ![Screenshot 2023-05-30 at 12 12 05 PM](https://github.com/TBD54566975/ftl/assets/51647/f72f7cf2-27fb-4507-b784-018030068de6) Makes the experience a bit more obvious and testing a bit easier
- Loading branch information
1 parent
be24cf7
commit b6ab744
Showing
7 changed files
with
68 additions
and
94 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Switch } from '@headlessui/react' | ||
import { MoonIcon, SunIcon } from '@heroicons/react/20/solid' | ||
import { useEffect } from 'react' | ||
import useLocalStorage from '../hooks/use-local-storage' | ||
import { classNames } from '../utils/react.utils' | ||
|
||
export default function DarkModeSwitch() { | ||
const [darkMode, setDarkMode] = useLocalStorage('dark-mode', false) | ||
|
||
useEffect(() => { | ||
if (darkMode) { | ||
document.documentElement.classList.add('dark') | ||
} else { | ||
document.documentElement.classList.remove('dark') | ||
} | ||
}, [darkMode]) | ||
|
||
return ( | ||
<Switch | ||
checked={darkMode} | ||
onChange={setDarkMode} | ||
className="bg-indigo-700 relative inline-flex h-6 w-11 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus:ring-2 focus:ring-indigo-600 focus:ring-offset-2" | ||
> | ||
<span className="sr-only">Dark mode toggle</span> | ||
<span | ||
aria-hidden="true" | ||
className={classNames( | ||
darkMode ? 'translate-x-5' : 'translate-x-0', | ||
'pointer-events-none inline-block h-5 w-5 transform rounded-full bg-white dark:bg-indigo-600 shadow ring-0 transition duration-200 ease-in-out', | ||
)} | ||
> | ||
{darkMode ? <MoonIcon className="text-white p-0.5" /> : <SunIcon className="text-indigo-600 p-0.5" />} | ||
</span> | ||
</Switch> | ||
) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { useEffect, useState } from 'react' | ||
|
||
export default function useLocalStorage(key: string, initialValue) { | ||
const [value, setValue] = useState(() => { | ||
const jsonValue = localStorage.getItem(key) | ||
if (jsonValue != null) return JSON.parse(jsonValue) | ||
return initialValue | ||
}) | ||
|
||
useEffect(() => { | ||
localStorage.setItem(key, JSON.stringify(value)) | ||
}, [key, value]) | ||
|
||
return [value, setValue] | ||
} |
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