Getting Started
Requirements
npm or Yarn
Node.js
React 16.8+
Installation
npm install react-color-mode
# OR
yarn add react-color-modeUsage
Setup your styles
react-color-mode will set a special attribute on your <html> tag, allowing you to style your site based on it.
/* Set default light mode colors on <html> */
:root {
background-color: white;
color: black;
}
/* User is in light mode, but has selected dark mode */
:root[react-color-mode="dark"] {
background-color: black;
color: white;
}
/* User is in dark mode and HAS NOT has selected light mode */
@media (prefers-color-scheme: dark) {
:root:not([react-color-mode="light"]) {
background-color: black;
color: white;
}
}Install the script
react-color-mode exports a <script> tag to be injected into your <head>. This script allows react-color-mode to setup the color mode before the rest of the page is loaded, preventing the page from flickering on page load. This component should be rendered as early as possible. See our recipes section for the best way to accomplish this in your app.
Install the Context Provider
The easiest way to use and update the color mode is via the Context API. This can be done with <ColorModeContextProvider> and the useColorMode hook.
Last updated
Was this helpful?