Skip to content

Instantly share code, notes, and snippets.

@nirnejak
Created March 16, 2022 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nirnejak/f30db4382c44506e2a1d8c134374461d to your computer and use it in GitHub Desktop.
Save nirnejak/f30db4382c44506e2a1d8c134374461d to your computer and use it in GitHub Desktop.
A React hook to detect dark/light mode.
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