mirror of
https://github.com/jakejarvis/dark-mode.git
synced 2025-04-25 12:55:23 -04:00
add onInit and onChange callback function options
This commit is contained in:
parent
8b40221791
commit
cfa1f575e3
@ -20,6 +20,8 @@ Very simple CSS dark/light mode toggler with saved preference via local storage
|
|||||||
- **classes:** An object containing the `<body>` class names for the light and dark themes. (optional, default: `{ light: "light", dark: "dark" }`)
|
- **classes:** An object containing the `<body>` class names for the light and dark themes. (optional, default: `{ light: "light", dark: "dark" }`)
|
||||||
- **default:** The initial `<body>` class hard-coded into the HTML template. (optional, default: `"light"`)
|
- **default:** The initial `<body>` class hard-coded into the HTML template. (optional, default: `"light"`)
|
||||||
- **storageKey:** Name of the `localStorage` key holding the user's preference. (optional, default: `"dark_mode_pref"`)
|
- **storageKey:** Name of the `localStorage` key holding the user's preference. (optional, default: `"dark_mode_pref"`)
|
||||||
|
- **onInit(toggle?)** Callback function executed at the end of initialization. The toggle above is passed in if set. (optional, default: `null`)
|
||||||
|
- **onChange(theme?, toggle?)** Callback function executed when theme is switched. The new theme and the toggle above (if set) are passed in. (optional, default: `null`)
|
||||||
|
|
||||||
### Browser
|
### Browser
|
||||||
|
|
||||||
@ -83,8 +85,8 @@ darkMode.init({
|
|||||||
## To-Do
|
## To-Do
|
||||||
|
|
||||||
- [ ] Support more than two themes
|
- [ ] Support more than two themes
|
||||||
- [ ] Add callback function `onChange` (or `onToggle` etc.) passed in as an option
|
|
||||||
- [ ] Better readme docs
|
- [ ] Better readme docs
|
||||||
|
- [x] Add callback function `onChange` (or `onToggle` etc.) passed in as an option
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
@ -51,6 +51,10 @@
|
|||||||
<script>
|
<script>
|
||||||
window.darkMode.init({
|
window.darkMode.init({
|
||||||
toggle: document.querySelector(".dark-mode-toggle"),
|
toggle: document.querySelector(".dark-mode-toggle"),
|
||||||
|
onInit: function (t) {
|
||||||
|
t.style.visibility = "visible";
|
||||||
|
console.log("Toggle is visible now that we know user has JS enabled.");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "rollup -c",
|
"build": "rollup -c",
|
||||||
"lint": "eslint src/**/*.js",
|
"lint": "eslint src/**/*.js",
|
||||||
"prepublishOnly": "yarn build"
|
"prepublishOnly": "rm -rf dist && yarn build"
|
||||||
},
|
},
|
||||||
"dependencies": {},
|
"dependencies": {},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
@ -30,7 +30,7 @@
|
|||||||
"@rollup/plugin-node-resolve": "^13.0.4",
|
"@rollup/plugin-node-resolve": "^13.0.4",
|
||||||
"eslint": "^7.32.0",
|
"eslint": "^7.32.0",
|
||||||
"eslint-plugin-compat": "^3.11.1",
|
"eslint-plugin-compat": "^3.11.1",
|
||||||
"rollup": "^2.55.1",
|
"rollup": "^2.56.0",
|
||||||
"rollup-plugin-copy": "^3.4.0",
|
"rollup-plugin-copy": "^3.4.0",
|
||||||
"rollup-plugin-filesize": "^9.1.1",
|
"rollup-plugin-filesize": "^9.1.1",
|
||||||
"rollup-plugin-terser": "^7.0.2"
|
"rollup-plugin-terser": "^7.0.2"
|
||||||
|
2
src/index.d.ts
vendored
2
src/index.d.ts
vendored
@ -3,4 +3,6 @@ export function init(options?: {
|
|||||||
classes?: { dark: string, light: string };
|
classes?: { dark: string, light: string };
|
||||||
default?: string;
|
default?: string;
|
||||||
storageKey?: string;
|
storageKey?: string;
|
||||||
|
onInit?: (toggle?: HTMLElement) => void;
|
||||||
|
onChange?: (theme?: string, toggle?: HTMLElement) => void;
|
||||||
}): void;
|
}): void;
|
||||||
|
13
src/index.js
13
src/index.js
@ -31,6 +31,11 @@ const init = function (options) {
|
|||||||
if (storageAvailable && !!remember) {
|
if (storageAvailable && !!remember) {
|
||||||
localStorage.setItem(storageKey, theme);
|
localStorage.setItem(storageKey, theme);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// optional onChange callback function passed as option
|
||||||
|
if (typeof options.onChange === "function") {
|
||||||
|
options.onChange(theme, toggle);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// user has never clicked the button, so go by their OS preference until/if they do so
|
// user has never clicked the button, so go by their OS preference until/if they do so
|
||||||
@ -72,9 +77,6 @@ const init = function (options) {
|
|||||||
|
|
||||||
// don't freak out if page happens not to have a toggle
|
// don't freak out if page happens not to have a toggle
|
||||||
if (toggle !== null) {
|
if (toggle !== null) {
|
||||||
// toggle re-appears now that we know user has JS enabled
|
|
||||||
toggle.style.visibility = "visible";
|
|
||||||
|
|
||||||
// handle toggle click
|
// handle toggle click
|
||||||
toggle.addEventListener("click", function () {
|
toggle.addEventListener("click", function () {
|
||||||
// switch to the opposite theme & save preference in local storage
|
// switch to the opposite theme & save preference in local storage
|
||||||
@ -85,6 +87,11 @@ const init = function (options) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// optional onInit callback function passed as option
|
||||||
|
if (typeof options.onInit === "function") {
|
||||||
|
options.onInit(toggle);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// recommended method (by MDN) to detect localStorage availability:
|
// recommended method (by MDN) to detect localStorage availability:
|
||||||
|
@ -3002,10 +3002,10 @@ rollup-plugin-terser@^7.0.2:
|
|||||||
serialize-javascript "^4.0.0"
|
serialize-javascript "^4.0.0"
|
||||||
terser "^5.0.0"
|
terser "^5.0.0"
|
||||||
|
|
||||||
rollup@^2.55.1:
|
rollup@^2.56.0:
|
||||||
version "2.55.1"
|
version "2.56.0"
|
||||||
resolved "https://registry.yarnpkg.com/rollup/-/rollup-2.55.1.tgz#66a444648e2fb603d8e329e77a61c608a6510fda"
|
resolved "https://registry.yarnpkg.com/rollup/-/rollup-2.56.0.tgz#daa832955d2b58f1ed52a3c4c85b7d1adaf076d0"
|
||||||
integrity sha512-1P9w5fpb6b4qroePh8vHKGIvPNxwoCQhjJpIqfZGHLKpZ0xcU2/XBmFxFbc9697/6bmHpmFTLk5R1dAQhFSo0g==
|
integrity sha512-weEafgbjbHCnrtJPNyCrhYnjP62AkF04P0BcV/1mofy1+gytWln4VVB1OK462cq2EAyWzRDpTMheSP/o+quoiA==
|
||||||
optionalDependencies:
|
optionalDependencies:
|
||||||
fsevents "~2.3.2"
|
fsevents "~2.3.2"
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user