@carry0987/darkmode
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -7,4 +7,8 @@ function reportError(...error) { | ||
// Return generic Element type or NodeList | ||
if (typeof ele !== 'string') | ||
if (typeof ele !== 'string') { | ||
if (mode === 'all') { | ||
return [ele]; | ||
} | ||
return ele; | ||
} | ||
let searchContext = document; | ||
@@ -76,3 +80,3 @@ if (mode === null && parent) { | ||
static instance = null; | ||
static version = '1.1.0'; | ||
static version = '1.1.1'; | ||
darkModeToggleButton; | ||
@@ -120,3 +124,8 @@ options; | ||
if (buttonSelector !== null) { | ||
buttonElement = getElem(buttonSelector); | ||
if (buttonSelector instanceof HTMLElement) { | ||
buttonElement = buttonSelector; | ||
} | ||
else { | ||
buttonElement = getElem(buttonSelector); | ||
} | ||
if (!buttonElement) { | ||
@@ -123,0 +132,0 @@ throw new Error('ToggleButton could not be found with the selector provided.'); |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).DarkMode=e()}(this,(function(){"use strict";function t(e,...o){if(!o.length)return e;const r=o.shift();if(r)for(const o in r)if(Object.prototype.hasOwnProperty.call(r,o)){const n=r[o],s=o;"object"!=typeof(i=n)||null===i||Array.isArray(i)?e[s]=n:(e[s]&&"object"==typeof e[s]||(e[s]={}),t(e[s],n))}var i;return t(e,...o)}function e(t,e,o=!0){o&&(e=JSON.stringify(e)),window.localStorage.setItem(t,e)}function o(t,e=!0){let o=window.localStorage.getItem(t);if(e)try{o=JSON.parse(o)}catch(t){!function(...t){console.error(...t)}("Error while parsing stored json value: ",t)}return o}class r{static instance=null;static version="1.1.0";darkModeToggleButton;options;defaults={buttonSelector:null,onChange:t=>{},onDark:()=>{},onLight:()=>{},autoDetect:!0,preferSystem:!1,rootElement:document.documentElement,darkModeStorageKey:"user-color-scheme",darkModeMediaQueryKey:"--color-mode",rootElementDarkModeAttributeName:"data-user-color-scheme"};onChangeCallback=t=>{};onDarkCallback=()=>{};onLightCallback=()=>{};validColorModeKeys={dark:!0,light:!0};invertDarkModeObj={dark:"light",light:"dark"};constructor(t={}){if(r.instance)return r.instance;this.init(t),r.instance=this,Object.seal(this)}init(e){const o=t(this.defaults,e),{buttonSelector:i}=o;let n=null;if(null!==i&&(n=function(t,e,o){if("string"!=typeof t)return t;let r=document;return null===e&&o?r=o:e&&e instanceof Node&&"querySelector"in e?r=e:o&&o instanceof Node&&"querySelector"in o&&(r=o),"all"===e?r.querySelectorAll(t):r.querySelector(t)}(i),!n))throw new Error("ToggleButton could not be found with the selector provided.");o.autoDetect=!!o.preferSystem||o.autoDetect,this.options=o,this.darkModeToggleButton=n,this.onChangeCallback=this.options.onChange||this.onChangeCallback,this.onDarkCallback=this.options.onDark||this.onDarkCallback,this.onLightCallback=this.options.onLight||this.onLightCallback,this.setupDarkMode(),console.log(`DarkMode is loaded, version: ${r.version}`)}setupDarkMode(){this.options.preferSystem&&this.resetRootDarkModeAttribute();let t=this.options.preferSystem?null:o(this.options.darkModeStorageKey);null===t&&this.options.autoDetect?t=this.getModeFromSystemPreference():null===t&&(t="light"),this.applyCustomDarkModeSettings(t),"dark"===t?this.onDarkCallback():this.onLightCallback(),this.bindEvents(),(this.options.autoDetect||this.options.preferSystem)&&this.listenToSystemDarkModeChange()}bindEvents(){null!==this.darkModeToggleButton&&this.darkModeToggleButton.addEventListener("click",(()=>{const t=this.toggleCustomDarkMode();this.applyCustomDarkModeSettings(t),t&&(this.onChangeCallback(t),"dark"===t?this.onDarkCallback():"light"===t&&this.onLightCallback())}))}getModeFromSystemPreference(){return window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"}getModeFromCSSMediaQuery(){const t=getComputedStyle(this.options.rootElement).getPropertyValue(this.options.darkModeMediaQueryKey);return t.length?t.replace(/\"/g,"").trim():"dark"===t?"dark":"light"}resetRootDarkModeAttribute(){var t;this.options.rootElement.removeAttribute(this.options.rootElementDarkModeAttributeName),t=this.options.darkModeStorageKey,window.localStorage.removeItem(t)}applyCustomDarkModeSettings(t){const e=t||o(this.options.darkModeStorageKey)||this.getModeFromCSSMediaQuery();return this.validColorModeKeys[e]?this.options.rootElement.setAttribute(this.options.rootElementDarkModeAttributeName,e):this.resetRootDarkModeAttribute(),this.darkModeToggleButton&&this.darkModeToggleButton instanceof Element&&(this.darkModeToggleButton.classList.remove("dm-"+this.invertDarkModeObj[e]),this.darkModeToggleButton.classList.add("dm-"+e)),e}toggleCustomDarkMode(){let t=o(this.options.darkModeStorageKey);if(this.validColorModeKeys[t])t=this.invertDarkModeObj[t];else{if(null!==t)return;t=this.invertDarkModeObj[this.getModeFromCSSMediaQuery()]}return e(this.options.darkModeStorageKey,t),t}listenToSystemDarkModeChange(){window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change",(t=>{if(null===o(this.options.darkModeStorageKey)){const e=t.matches?"dark":"light";this.applyCustomDarkModeSettings(e),this.onChangeCallback(e),"dark"===e?this.onDarkCallback():this.onLightCallback()}}))}destroy(){this.resetRootDarkModeAttribute()}switchMode(t){if(!this.validColorModeKeys[t])return;const o=this.applyCustomDarkModeSettings(t);o&&(this.onChangeCallback(o),"dark"===o?this.onDarkCallback():this.onLightCallback(),e(this.options.darkModeStorageKey,o))}set onChange(t){this.onChangeCallback=t}set onDark(t){this.onDarkCallback=t}set onLight(t){this.onLightCallback=t}}return r})); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).DarkMode=e()}(this,(function(){"use strict";function t(e,...o){if(!o.length)return e;const r=o.shift();if(r)for(const o in r)if(Object.prototype.hasOwnProperty.call(r,o)){const n=r[o],a=o;"object"!=typeof(i=n)||null===i||Array.isArray(i)?e[a]=n:(e[a]&&"object"==typeof e[a]||(e[a]={}),t(e[a],n))}var i;return t(e,...o)}function e(t,e,o=!0){o&&(e=JSON.stringify(e)),window.localStorage.setItem(t,e)}function o(t,e=!0){let o=window.localStorage.getItem(t);if(e)try{o=JSON.parse(o)}catch(t){!function(...t){console.error(...t)}("Error while parsing stored json value: ",t)}return o}class r{static instance=null;static version="1.1.1";darkModeToggleButton;options;defaults={buttonSelector:null,onChange:t=>{},onDark:()=>{},onLight:()=>{},autoDetect:!0,preferSystem:!1,rootElement:document.documentElement,darkModeStorageKey:"user-color-scheme",darkModeMediaQueryKey:"--color-mode",rootElementDarkModeAttributeName:"data-user-color-scheme"};onChangeCallback=t=>{};onDarkCallback=()=>{};onLightCallback=()=>{};validColorModeKeys={dark:!0,light:!0};invertDarkModeObj={dark:"light",light:"dark"};constructor(t={}){if(r.instance)return r.instance;this.init(t),r.instance=this,Object.seal(this)}init(e){const o=t(this.defaults,e),{buttonSelector:i}=o;let n=null;if(null!==i&&(n=i instanceof HTMLElement?i:function(t,e,o){if("string"!=typeof t)return"all"===e?[t]:t;let r=document;return null===e&&o?r=o:e&&e instanceof Node&&"querySelector"in e?r=e:o&&o instanceof Node&&"querySelector"in o&&(r=o),"all"===e?r.querySelectorAll(t):r.querySelector(t)}(i),!n))throw new Error("ToggleButton could not be found with the selector provided.");o.autoDetect=!!o.preferSystem||o.autoDetect,this.options=o,this.darkModeToggleButton=n,this.onChangeCallback=this.options.onChange||this.onChangeCallback,this.onDarkCallback=this.options.onDark||this.onDarkCallback,this.onLightCallback=this.options.onLight||this.onLightCallback,this.setupDarkMode(),console.log(`DarkMode is loaded, version: ${r.version}`)}setupDarkMode(){this.options.preferSystem&&this.resetRootDarkModeAttribute();let t=this.options.preferSystem?null:o(this.options.darkModeStorageKey);null===t&&this.options.autoDetect?t=this.getModeFromSystemPreference():null===t&&(t="light"),this.applyCustomDarkModeSettings(t),"dark"===t?this.onDarkCallback():this.onLightCallback(),this.bindEvents(),(this.options.autoDetect||this.options.preferSystem)&&this.listenToSystemDarkModeChange()}bindEvents(){null!==this.darkModeToggleButton&&this.darkModeToggleButton.addEventListener("click",(()=>{const t=this.toggleCustomDarkMode();this.applyCustomDarkModeSettings(t),t&&(this.onChangeCallback(t),"dark"===t?this.onDarkCallback():"light"===t&&this.onLightCallback())}))}getModeFromSystemPreference(){return window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"}getModeFromCSSMediaQuery(){const t=getComputedStyle(this.options.rootElement).getPropertyValue(this.options.darkModeMediaQueryKey);return t.length?t.replace(/\"/g,"").trim():"dark"===t?"dark":"light"}resetRootDarkModeAttribute(){var t;this.options.rootElement.removeAttribute(this.options.rootElementDarkModeAttributeName),t=this.options.darkModeStorageKey,window.localStorage.removeItem(t)}applyCustomDarkModeSettings(t){const e=t||o(this.options.darkModeStorageKey)||this.getModeFromCSSMediaQuery();return this.validColorModeKeys[e]?this.options.rootElement.setAttribute(this.options.rootElementDarkModeAttributeName,e):this.resetRootDarkModeAttribute(),this.darkModeToggleButton&&this.darkModeToggleButton instanceof Element&&(this.darkModeToggleButton.classList.remove("dm-"+this.invertDarkModeObj[e]),this.darkModeToggleButton.classList.add("dm-"+e)),e}toggleCustomDarkMode(){let t=o(this.options.darkModeStorageKey);if(this.validColorModeKeys[t])t=this.invertDarkModeObj[t];else{if(null!==t)return;t=this.invertDarkModeObj[this.getModeFromCSSMediaQuery()]}return e(this.options.darkModeStorageKey,t),t}listenToSystemDarkModeChange(){window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change",(t=>{if(null===o(this.options.darkModeStorageKey)){const e=t.matches?"dark":"light";this.applyCustomDarkModeSettings(e),this.onChangeCallback(e),"dark"===e?this.onDarkCallback():this.onLightCallback()}}))}destroy(){this.resetRootDarkModeAttribute()}switchMode(t){if(!this.validColorModeKeys[t])return;const o=this.applyCustomDarkModeSettings(t);o&&(this.onChangeCallback(o),"dark"===o?this.onDarkCallback():this.onLightCallback(),e(this.options.darkModeStorageKey,o))}set onChange(t){this.onChangeCallback=t}set onDark(t){this.onDarkCallback=t}set onLight(t){this.onLightCallback=t}}return r})); |
{ | ||
"name": "@carry0987/darkmode", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "A simple and lightweight JavaScript library for dark mode toggle with localStorage support.", | ||
@@ -31,7 +31,7 @@ "type": "module", | ||
"devDependencies": { | ||
"@carry0987/utils": "^3.2.8", | ||
"@carry0987/utils": "^3.2.13", | ||
"@rollup/plugin-replace": "^5.0.5", | ||
"@rollup/plugin-terser": "^0.4.4", | ||
"@rollup/plugin-typescript": "^11.1.6", | ||
"rollup": "^4.10.0", | ||
"rollup": "^4.12.0", | ||
"rollup-plugin-delete": "^2.0.0", | ||
@@ -38,0 +38,0 @@ "rollup-plugin-dts": "^6.1.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18578
340