React hook for determining the preferred color scheme
- SSR support
- Written in Typescript
npm install --save use-prefers-color-scheme
Tip When rendered in node (SSR) the hook returns
no-preference
.
import React from 'react'
import usePrefersColorScheme from 'use-prefers-color-scheme'
const App = () => {
const prefersColorScheme = usePrefersColorScheme()
const isDarkMode = prefersColorScheme === 'dark'
return (
<div>You are using {isDarkMode ? 'Dark Mode 🌚' : 'Light Mode 🌞'}!</div>
)
}
MIT © rfoel