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

add onInit and onChange callback function options

This commit is contained in:
Jake Jarvis 2021-08-05 07:54:47 -04:00
parent 8b40221791
commit cfa1f575e3
Signed by: jake
GPG Key ID: 2B0C9CF251E69A39
6 changed files with 53 additions and 38 deletions

View File

@ -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" }`)
- **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"`)
- **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
@ -83,8 +85,8 @@ darkMode.init({
## To-Do
- [ ] Support more than two themes
- [ ] Add callback function `onChange` (or `onToggle` etc.) passed in as an option
- [ ] Better readme docs
- [x] Add callback function `onChange` (or `onToggle` etc.) passed in as an option
## License

View File

@ -6,38 +6,38 @@
<title>Welcome to the dark side 🌓</title>
<style>/*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */html{line-height:1.15;-webkit-text-size-adjust:100%}body{margin:0}main{display:block}h1{font-size:2em;margin:.67em 0}hr{box-sizing:content-box;height:0;overflow:visible}pre{font-family:monospace,monospace;font-size:1em}a{background-color:transparent}abbr[title]{border-bottom:none;text-decoration:underline;text-decoration:underline dotted}b,strong{font-weight:bolder}code,kbd,samp{font-family:monospace,monospace;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}img{border-style:none}button,input,optgroup,select,textarea{font-family:inherit;font-size:100%;line-height:1.15;margin:0}button,input{overflow:visible}button,select{text-transform:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]::-moz-focus-inner,[type=reset]::-moz-focus-inner,[type=submit]::-moz-focus-inner,button::-moz-focus-inner{border-style:none;padding:0}[type=button]:-moz-focusring,[type=reset]:-moz-focusring,[type=submit]:-moz-focusring,button:-moz-focusring{outline:1px dotted ButtonText}fieldset{padding:.35em .75em .625em}legend{box-sizing:border-box;color:inherit;display:table;max-width:100%;padding:0;white-space:normal}progress{vertical-align:baseline}textarea{overflow:auto}[type=checkbox],[type=radio]{box-sizing:border-box;padding:0}[type=number]::-webkit-inner-spin-button,[type=number]::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}[type=search]::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}details{display:block}summary{display:list-item}template{display:none}[hidden]{display:none}</style>
<style>
/* Global styles */
body {
font-family: system-ui, -apple-system, sans-serif;
text-align: center;
}
/* Global styles */
body {
font-family: system-ui, -apple-system, sans-serif;
text-align: center;
}
/* Light theme */
body.light {
background-color: #fff;
color: #222;
}
body.light a {
color: #06f;
}
/* Light theme */
body.light {
background-color: #fff;
color: #222;
}
body.light a {
color: #06f;
}
/* Dark theme */
body.dark {
background-color: #222;
color: #fff;
}
body.dark a {
color: #fe0;
}
/* Dark theme */
body.dark {
background-color: #222;
color: #fff;
}
body.dark a {
color: #fe0;
}
/* The Toggle (TM) */
.dark-mode-toggle {
padding: 1em;
cursor: pointer;
/* The Toggle (TM) */
.dark-mode-toggle {
padding: 1em;
cursor: pointer;
/* Hide the toggle initially if the user's JS is disabled: */
visibility: hidden;
}
/* Hide the toggle initially if the user's JS is disabled: */
visibility: hidden;
}
</style>
</head>
<body class="light"> <!-- default to light theme -->
@ -51,6 +51,10 @@
<script>
window.darkMode.init({
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>
</body>

View File

@ -19,7 +19,7 @@
"scripts": {
"build": "rollup -c",
"lint": "eslint src/**/*.js",
"prepublishOnly": "yarn build"
"prepublishOnly": "rm -rf dist && yarn build"
},
"dependencies": {},
"devDependencies": {
@ -30,7 +30,7 @@
"@rollup/plugin-node-resolve": "^13.0.4",
"eslint": "^7.32.0",
"eslint-plugin-compat": "^3.11.1",
"rollup": "^2.55.1",
"rollup": "^2.56.0",
"rollup-plugin-copy": "^3.4.0",
"rollup-plugin-filesize": "^9.1.1",
"rollup-plugin-terser": "^7.0.2"

2
src/index.d.ts vendored
View File

@ -3,4 +3,6 @@ export function init(options?: {
classes?: { dark: string, light: string };
default?: string;
storageKey?: string;
onInit?: (toggle?: HTMLElement) => void;
onChange?: (theme?: string, toggle?: HTMLElement) => void;
}): void;

View File

@ -31,6 +31,11 @@ const init = function (options) {
if (storageAvailable && !!remember) {
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
@ -72,9 +77,6 @@ const init = function (options) {
// don't freak out if page happens not to have a toggle
if (toggle !== null) {
// toggle re-appears now that we know user has JS enabled
toggle.style.visibility = "visible";
// handle toggle click
toggle.addEventListener("click", function () {
// 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:

View File

@ -3002,10 +3002,10 @@ rollup-plugin-terser@^7.0.2:
serialize-javascript "^4.0.0"
terser "^5.0.0"
rollup@^2.55.1:
version "2.55.1"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-2.55.1.tgz#66a444648e2fb603d8e329e77a61c608a6510fda"
integrity sha512-1P9w5fpb6b4qroePh8vHKGIvPNxwoCQhjJpIqfZGHLKpZ0xcU2/XBmFxFbc9697/6bmHpmFTLk5R1dAQhFSo0g==
rollup@^2.56.0:
version "2.56.0"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-2.56.0.tgz#daa832955d2b58f1ed52a3c4c85b7d1adaf076d0"
integrity sha512-weEafgbjbHCnrtJPNyCrhYnjP62AkF04P0BcV/1mofy1+gytWln4VVB1OK462cq2EAyWzRDpTMheSP/o+quoiA==
optionalDependencies:
fsevents "~2.3.2"