1
mirror of https://github.com/jakejarvis/dark-mode.git synced 2025-04-26 05:15:23 -04:00

remove unnecessary deps (now zero 😎)

This commit is contained in:
Jake Jarvis 2021-08-04 15:19:54 -04:00
parent 4881b908e9
commit e4d7e4f61b
Signed by: jake
GPG Key ID: 2B0C9CF251E69A39
3 changed files with 22 additions and 5 deletions

View File

@ -4,7 +4,7 @@
[![npm (scoped)](https://img.shields.io/npm/v/@jakejarvis/dark-mode)](https://www.npmjs.com/package/@jakejarvis/dark-mode) [![npm (scoped)](https://img.shields.io/npm/v/@jakejarvis/dark-mode)](https://www.npmjs.com/package/@jakejarvis/dark-mode)
[![GitHub](https://img.shields.io/github/license/jakejarvis/dark-mode?color=violet)](LICENSE) [![GitHub](https://img.shields.io/github/license/jakejarvis/dark-mode?color=violet)](LICENSE)
Very simple CSS dark/light mode toggler with saved preference via local storage & dynamic OS setting detection. Only ~700 bytes gzipped! Very simple CSS dark/light mode toggler with saved preference via local storage & dynamic OS setting detection. Zero dependencies and only ~700 bytes gzipped!
- [View the example.](https://jakejarvis.github.io/dark-mode-example/) - [View the example.](https://jakejarvis.github.io/dark-mode-example/)
- [Read the blog post.](https://jarv.is/notes/dark-mode/) - [Read the blog post.](https://jarv.is/notes/dark-mode/)
@ -64,6 +64,11 @@ darkMode.init({
}); });
``` ```
## To-Do
- [ ] Support more than two themes
- [ ] Add callback function `onChange` (or `onToggle` etc.) passed in as an option
## License ## License
MIT MIT

View File

@ -19,9 +19,7 @@
"lint": "eslint src/**/*.js", "lint": "eslint src/**/*.js",
"prepublishOnly": "run-s lint build" "prepublishOnly": "run-s lint build"
}, },
"dependencies": { "dependencies": {},
"storage-available": "^1.1.0"
},
"devDependencies": { "devDependencies": {
"copy-webpack-plugin": "^9.0.1", "copy-webpack-plugin": "^9.0.1",
"eslint": "^7.32.0", "eslint": "^7.32.0",

View File

@ -1,4 +1,4 @@
const storageAvailable = require("storage-available"); "use strict";
const initializeDarkMode = function (options) { const initializeDarkMode = function (options) {
// { toggle, classes: { light, dark }, default, storageKey } // { toggle, classes: { light, dark }, default, storageKey }
@ -88,4 +88,18 @@ const initializeDarkMode = function (options) {
} }
}; };
// recommended method (by MDN) to detect localStorage availability:
// https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#feature-detecting_localstorage
const storageAvailable = function (type) {
try {
var storage = window[type];
var x = "__storage_test__";
storage.setItem(x, x);
storage.removeItem(x);
return true;
} catch (e) {
return false;
}
};
module.exports.init = initializeDarkMode; module.exports.init = initializeDarkMode;