
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
dark-mode-react-hook
Advanced tools
This package contains a React custom hook that applies dark mode to your website. It uses css filter
to transform light schemes into dark themes (and viceversa). I took the idea from this post and extracted it into a single hook.
It works by injecting a global stylesheet with the following css:
html {
filter: invert(1) hue-rotate(180deg);
}
img {
filter: invert(1) hue-rotate(180deg);
}
.no-dark-mode {
filter: invert(1) hue-rotate(180deg);
}
What this css is doing is inverting the colors in the <body>
and then converting all colors to their complementaries. The final result is a dark variant (or light, if the original is dark) of the theme, while more-or-less keeping the main colors.
If there is an element that you do not wish to invert (for example, a dark modal background should probably not be bright under dark-mode), you can apply the no-dark-mode
class to it.
npm i --save react-hook-dark-mode
# or
yarn add react-hook-dark-mode
import useDarkMode from "react-hook-dark-mode";
function darkModeToggle() {
const [darkMode, setDarkMode] = useDarkMode();
return (
<button onClick={() => setDarkMode(!darkMode)}>
{darkMode ? 🌙 : ☀️}
</button>
)
}
The hook accepts a configuration object with the following options:
Name | Type | Description | Default |
---|---|---|---|
autoDetect | boolean | Use a prefers-color-scheme media query to detect user preferences and automatically set dark mode | true |
defaultValue | boolean | Apply dark mode by default | false |
FAQs
React custom hook to apply dark mode to any website
We found that dark-mode-react-hook demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.