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:
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
|
||||||
|
|
||||||
|
@ -6,38 +6,38 @@
|
|||||||
<title>Welcome to the dark side 🌓</title>
|
<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>/*! 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>
|
<style>
|
||||||
/* Global styles */
|
/* Global styles */
|
||||||
body {
|
body {
|
||||||
font-family: system-ui, -apple-system, sans-serif;
|
font-family: system-ui, -apple-system, sans-serif;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Light theme */
|
/* Light theme */
|
||||||
body.light {
|
body.light {
|
||||||
background-color: #fff;
|
background-color: #fff;
|
||||||
color: #222;
|
color: #222;
|
||||||
}
|
}
|
||||||
body.light a {
|
body.light a {
|
||||||
color: #06f;
|
color: #06f;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Dark theme */
|
/* Dark theme */
|
||||||
body.dark {
|
body.dark {
|
||||||
background-color: #222;
|
background-color: #222;
|
||||||
color: #fff;
|
color: #fff;
|
||||||
}
|
}
|
||||||
body.dark a {
|
body.dark a {
|
||||||
color: #fe0;
|
color: #fe0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The Toggle (TM) */
|
/* The Toggle (TM) */
|
||||||
.dark-mode-toggle {
|
.dark-mode-toggle {
|
||||||
padding: 1em;
|
padding: 1em;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
|
|
||||||
/* Hide the toggle initially if the user's JS is disabled: */
|
/* Hide the toggle initially if the user's JS is disabled: */
|
||||||
visibility: hidden;
|
visibility: hidden;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body class="light"> <!-- default to light theme -->
|
<body class="light"> <!-- default to light theme -->
|
||||||
@ -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