Created
March 16, 2022 18:58
-
-
Save nirnejak/f30db4382c44506e2a1d8c134374461d to your computer and use it in GitHub Desktop.
A React hook to detect dark/light mode.
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
import React from "react" | |
const useUserTheme = (): boolean => { | |
const [isDark, setIsDark] = React.useState(false) | |
React.useEffect(() => { | |
window | |
.matchMedia("(prefers-color-scheme: dark)") | |
.addEventListener("change", (event) => { | |
setIsDark(event.matches) | |
}) | |
}, []) | |
return isDark | |
} | |
export default useUserTheme |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment