Gathering detailed insights and metrics for @oceanprotocol/use-dark-mode
Gathering detailed insights and metrics for @oceanprotocol/use-dark-mode
Gathering detailed insights and metrics for @oceanprotocol/use-dark-mode
Gathering detailed insights and metrics for @oceanprotocol/use-dark-mode
A custom React Hook to help you implement a "dark mode" component.
npm install @oceanprotocol/use-dark-mode
Typescript
Module System
Node Version
NPM Version
61.5
Supply Chain
94.9
Quality
78.9
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
33,596
Last Day
15
Last Week
834
Last Month
4,081
Last Year
20,594
48 Commits
1 Watching
1 Branches
3 Contributors
Latest Version
2.4.3
Package Id
@oceanprotocol/use-dark-mode@2.4.3
Unpacked Size
21.81 kB
Size
7.14 kB
File Count
9
NPM Version
8.19.2
Node Version
18.9.0
Cumulative downloads
Total Downloads
Last day
-93.1%
15
Compared to previous day
Last week
-27%
834
Compared to previous week
Last month
-33.7%
4,081
Compared to previous month
Last year
110.6%
20,594
Compared to previous year
This is a fork of use-dark-mode by donovan which adds some much needed maintenance support for React 18.
A custom React Hook to help you implement a "dark mode" component for your application.
The user setting persists to localStorage
.
useDarkMode
works in one of two ways:
By toggling a CSS class on whatever element you specify (defaults to document.body
).
You then setup your CSS to display different views based on the presence of the selector. For example, the following CSS is used in the demo app to ease the background color in/out of dark mode.
1body.light-mode { 2 background-color: #fff; 3 color: #333; 4 transition: background-color 0.3s ease; 5} 6body.dark-mode { 7 background-color: #1a1919; 8 color: #999; 9}
If you don't use global classes, you can specify an onChange
handler and take care of the implementation of switching to dark mode yourself.
useDarkMode
now persists between sessions. It stores the user setting in
localStorage
.
It shares dark mode state with all other useDarkMode
components on the page.
It shares dark mode state with all other tabs/browser windows.
The initial dark mode is queried from the system. Note: this requires a browser that supports the prefers-color-scheme: dark
media query
(currently Chrome, Firefox, Safari and Edge)
and a system that supports dark mode, such as macOS Mojave.
Changing the system dark mode state will also change the state of useDarkMode
(i.e, change to light mode in the system will change to light mode in your app).
Support for Server Side Rendering (SSR) in version 2.2 and above.
To use use-dark-mode
, you must use react@16.8.0
or greater which includes Hooks.
1$ npm i use-dark-mode
1const darkMode = useDarkMode(initialState, darkModeConfig);
You pass useDarkMode
an initialState
(a boolean specifying whether it should be in dark mode
by default) and an optional darkModeConfig
object. The configuration object may contain the following keys.
Key | Description |
---|---|
classNameDark | The class to apply. Default = dark-mode . |
classNameLight | The class to apply. Default = light-mode . |
element | The element to apply the class name. Default = document.body . |
onChange | A function that will be called when the dark mode value changes and it is safe to access the DOM (i.e. it is called from within a useEffect ). If you specify onChange then classNameDark , classNameLight , and element are ignored (i.e. no classes are automatically placed on the DOM). You have full control! |
storageKey | A string that will be used by the storageProvider to persist the dark mode value. If you specify a value of null , nothing will be persisted. Default = darkMode . |
storageProvider | A storage provider. Default = localStorage . You will generally never need to change this value. |
A darkMode
object is returned with the following properties.
Key | Description |
---|---|
value | A boolean containing the current state of dark mode. |
enable() | A function that allows you to set dark mode to true . |
disable() | A function that allows you to set dark mode to false . |
toggle() | A function that allows you to toggle dark mode. |
Note that because the methods don't require any parameters, you can call them
direcly from an onClick
handler from a button, for example
(i.e., no lambda function is required).
Here is a simple component that uses useDarkMode
to provide a dark mode toggle control.
If dark mode is selected, the CSS class dark-mode
is applied to document.body
and is removed
when de-selected.
1import React from 'react'; 2import useDarkMode from 'use-dark-mode'; 3 4import Toggle from './Toggle'; 5 6const DarkModeToggle = () => { 7 const darkMode = useDarkMode(false); 8 9 return ( 10 <div> 11 <button type="button" onClick={darkMode.disable}> 12 ☀ 13 </button> 14 <Toggle checked={darkMode.value} onChange={darkMode.toggle} /> 15 <button type="button" onClick={darkMode.enable}> 16 ☾ 17 </button> 18 </div> 19 ); 20}; 21 22export default DarkModeToggle;
If your CSS is setup to default to light mode, but the user selects dark mode,
the next time they visit your app, they will be in dark mode.
However, the user will see a flash of light mode before the app is spun up
and useDarkMode
is called.
To prevent this, I've included some vanilla JavaScript that you can insert in your
index.html
just after the <body>
tag. It is in a file named noflash.js.txt.
You can either insert the contents of this file in a <script>
tag or automate the
step in your build process.
Note that if you change any of the default—such as storageKey
or classNameDark
for example—the noflash.js
file will need to be modified with the same values.
Gatsby users may leverage gatsby-plugin-use-dark-mode
to inject noflash.js
for you.
For next.js uses copy the noflash.js.txt
to your public
folder (public/noflash.js
) and then create a _document.js
and include the script before <Main />
.
1import Document, { Html, Head, Main, NextScript } from 'next/document'; 2 3class MyDocument extends Document { 4 render() { 5 return ( 6 <Html> 7 <Head /> 8 <body> 9 <script src="noflash.js" /> 10 <Main /> 11 <NextScript /> 12 </body> 13 </Html> 14 ); 15 } 16} 17 18export default MyDocument;
Here is a list of apps build with use-dark-mode
.
If you have an app you would like to include on this list, open a PR.
MIT Licensed
Thanks goes to these wonderful people (emoji key):
Donavon West 🚇 ⚠️ 💡 🤔 🚧 👀 🔧 💻 | Revel Carlberg West 🤔 | Mateusz Burzyński 💻 | Justin Hall 💻 | Jeremy 📓 🐛 | Janosh Riebesell 📖 | Andrew Lisowski 📖 |
Jorge Gonzalez 💻 |
This project follows the all-contributors specification. Contributions of any kind welcome!
No vulnerabilities found.
No security vulnerabilities found.