-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
1 parent
624f288
commit 1efa612
Showing
4 changed files
with
84 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useState, useEffect } from "react"; | ||
|
||
/** | ||
* | ||
* Returns true if user prefers dark mode and false if not. | ||
* @returns {boolean} Does user prefers dark mode. | ||
*/ | ||
const _getUserPreferredMode = () => { | ||
//If localStorage is available user preference is stored, return the stored value. | ||
if (localStorage) { | ||
const localPreference = localStorage.getItem("dark-mode"); | ||
if (localPreference) { | ||
return JSON.parse(localStorage.getItem("dark-mode")); | ||
} | ||
} | ||
//Else return the global dark-mode preference. | ||
else { | ||
return _getGlobalPreference(); | ||
} | ||
}; | ||
|
||
/** | ||
* | ||
* Returns true if globally preferred theme is dark and false if not. | ||
* @returns {boolean} Is dark mode globally preferred. | ||
*/ | ||
const _getGlobalPreference = () => window.matchMedia("(prefers-color-scheme: dark)").matches; | ||
|
||
/** | ||
* | ||
* Custom hook which let's you toggle dark-mode by adding a class to the body. | ||
* | ||
* @param {string} className Class name which is to be added to body when dark mode. | ||
* @returns {Array} Array containing a boolean isDarkMode and a darkModeToggle function. | ||
*/ | ||
const useDarkMode = (className) => { | ||
const [isDarkMode, setIsDarkMode] = useState(_getUserPreferredMode()); | ||
|
||
useEffect(() => { | ||
if (isDarkMode) { | ||
document.body.classList.add(className); | ||
} else { | ||
document.body.classList.remove(className); | ||
} | ||
}, [isDarkMode]); | ||
|
||
const toggleDarkMode = () => { | ||
localStorage.setItem("dark-mode", !isDarkMode); | ||
setIsDarkMode(!isDarkMode); | ||
}; | ||
|
||
return [isDarkMode, toggleDarkMode]; | ||
}; | ||
|
||
export default useDarkMode; |
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,3 +1,4 @@ | ||
export { default as useDarkMode } from "./hooks/useDarkMode"; | ||
export { default as useDebounce } from "./hooks/useDebounce"; | ||
export { default as useGeoLocation } from "./hooks/useGeoLocation"; | ||
export { default as useLocalStorage } from "./hooks/useLocalStorage"; |
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