diff --git a/README.md b/README.md index 8fdf5ea..07d73c4 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ Very simple CSS dark/light mode toggler with saved preference via local storage - **classes:** An object containing the `` class names for the light and dark themes. (optional, default: `{ light: "light", dark: "dark" }`) - **default:** The initial `` 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 diff --git a/example/index.html b/example/index.html index a6979de..0340cd1 100644 --- a/example/index.html +++ b/example/index.html @@ -6,38 +6,38 @@ Welcome to the dark side 🌓 @@ -51,6 +51,10 @@ diff --git a/package.json b/package.json index 7da0744..669913e 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/index.d.ts b/src/index.d.ts index f18f405..73e961e 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -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; diff --git a/src/index.js b/src/index.js index 6fc808a..b51c099 100644 --- a/src/index.js +++ b/src/index.js @@ -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: diff --git a/yarn.lock b/yarn.lock index e98cc05..9796f25 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"