@carry0987/darkmode
Advanced tools
Comparing version 1.1.2 to 1.1.3
@@ -79,3 +79,3 @@ function reportError(...error) { | ||
static instance = null; | ||
static version = '1.1.2'; | ||
static version = '1.1.3'; | ||
darkModeToggleButton; | ||
@@ -82,0 +82,0 @@ options; |
@@ -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],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.2";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})); | ||
!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.3";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.2", | ||
"version": "1.1.3", | ||
"description": "A simple and lightweight JavaScript library for dark mode toggle with localStorage support.", | ||
@@ -35,2 +35,3 @@ "type": "module", | ||
"@rollup/plugin-typescript": "^11.1.6", | ||
"@rollup/plugin-node-resolve": "^15.2.3", | ||
"rollup": "^4.12.1", | ||
@@ -41,6 +42,3 @@ "rollup-plugin-delete": "^2.0.0", | ||
"typescript": "^5.4.2" | ||
}, | ||
"dependencies": { | ||
"@rollup/plugin-node-resolve": "^15.2.3" | ||
} | ||
} |
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
0
18576
10
- Removed@rollup/plugin-node-resolve@^15.2.3
- Removed@rollup/plugin-node-resolve@15.3.0(transitive)
- Removed@rollup/pluginutils@5.1.3(transitive)
- Removed@types/estree@1.0.6(transitive)
- Removed@types/resolve@1.20.2(transitive)
- Removeddeepmerge@4.3.1(transitive)
- Removedestree-walker@2.0.2(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedhasown@2.0.2(transitive)
- Removedis-core-module@2.15.1(transitive)
- Removedis-module@1.0.0(transitive)
- Removedpath-parse@1.0.7(transitive)
- Removedpicomatch@4.0.2(transitive)
- Removedresolve@1.22.8(transitive)
- Removedsupports-preserve-symlinks-flag@1.0.0(transitive)