@carry0987/darkmode
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -34,3 +34,3 @@ type DarkModeCallback = () => void; | ||
private getModeFromCSSMediaQuery; | ||
private resetRootDarkModeAttributeAndLS; | ||
private resetRootDarkModeAttribute; | ||
private applyCustomDarkModeSettings; | ||
@@ -37,0 +37,0 @@ private toggleCustomDarkMode; |
@@ -73,3 +73,3 @@ function reportError(...error) { | ||
static instance = null; | ||
static version = '1.0.0'; | ||
static version = '1.0.1'; | ||
darkModeToggleButton; | ||
@@ -134,4 +134,10 @@ options; | ||
setupDarkMode() { | ||
this.applyCustomDarkModeSettings(); | ||
const currentSetting = this.applyCustomDarkModeSettings(); | ||
this.bindEvents(); | ||
if (currentSetting && currentSetting === 'dark') { | ||
this._onDark(); | ||
} | ||
else { | ||
this._onLight(); | ||
} | ||
} | ||
@@ -160,3 +166,3 @@ bindEvents() { | ||
} | ||
resetRootDarkModeAttributeAndLS() { | ||
resetRootDarkModeAttribute() { | ||
this.options.rootElement.removeAttribute(this.options.rootElementDarkModeAttributeName); | ||
@@ -168,3 +174,3 @@ removeLocalValue(this.options.darkModeStorageKey); | ||
if (currentSetting === this.getModeFromCSSMediaQuery()) { | ||
this.resetRootDarkModeAttributeAndLS(); | ||
this.resetRootDarkModeAttribute(); | ||
} | ||
@@ -175,9 +181,9 @@ else if (this.validColorModeKeys[currentSetting]) { | ||
else { | ||
this.resetRootDarkModeAttributeAndLS(); | ||
this.resetRootDarkModeAttribute(); | ||
} | ||
if (this.darkModeToggleButton && | ||
this.darkModeToggleButton instanceof Element) { | ||
if (this.darkModeToggleButton && this.darkModeToggleButton instanceof Element) { | ||
this.darkModeToggleButton.classList.remove('dm-' + this.invertDarkModeObj[currentSetting]); | ||
this.darkModeToggleButton.classList.add('dm-' + currentSetting); | ||
} | ||
return currentSetting; | ||
} | ||
@@ -190,4 +196,3 @@ toggleCustomDarkMode() { | ||
else if (currentSetting === null) { | ||
currentSetting = | ||
this.invertDarkModeObj[this.getModeFromCSSMediaQuery()]; | ||
currentSetting = this.invertDarkModeObj[this.getModeFromCSSMediaQuery()]; | ||
} | ||
@@ -194,0 +199,0 @@ else { |
@@ -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 i=o.shift();if(i)for(const o in i)if(Object.prototype.hasOwnProperty.call(i,o)){const r=i[o],s=o;"object"!=typeof(n=r)||null===n||Array.isArray(n)?e[s]=r:(e[s]&&"object"==typeof e[s]||(e[s]={}),t(e[s],r))}var n;return t(e,...o)}function e(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 o{static instance=null;static version="1.0.0";darkModeToggleButton;options;defaults={onChange:()=>{},onDark:()=>{},onLight:()=>{},rootElement:document.documentElement,darkModeStorageKey:"user-color-scheme",darkModeMediaQueryKey:"--color-mode",rootElementDarkModeAttributeName:"data-user-color-scheme"};_onChange=()=>{};_onDark=()=>{};_onLight=()=>{};validColorModeKeys={dark:!0,light:!0};invertDarkModeObj={dark:"light",light:"dark"};constructor(t,e={}){if(o.instance)return o.instance;const i=function(t,e,o){if("string"!=typeof t)return t;let i=document;return null===e&&o?i=o:e&&e instanceof Node&&"querySelector"in e?i=e:o&&o instanceof Node&&"querySelector"in o&&(i=o),"all"===e?i.querySelectorAll(t):i.querySelector(t)}(t);if(!i)throw new Error("ToggleButton could not be found with the selector provided.");this.init(i,e),o.instance=this,Object.seal(this)}set onChange(t){this._onChange=t}set onDark(t){this._onDark=t}set onLight(t){this._onLight=t}init(e,i){const n=t(this.defaults,i);this.options=n,this.darkModeToggleButton=e,this._onChange=this.options.onChange||this._onChange,this._onDark=this.options.onDark||this._onDark,this._onLight=this.options.onLight||this._onLight,this.setupDarkMode(),console.log(`DarkMode is loaded, version: ${o.version}`)}setupDarkMode(){this.applyCustomDarkModeSettings(),this.bindEvents()}bindEvents(){this.darkModeToggleButton.addEventListener("click",(()=>{const t=this.toggleCustomDarkMode();this.applyCustomDarkModeSettings(t),t&&(this._onChange(),"dark"===t?this._onDark():"light"===t&&this._onLight())}))}getModeFromCSSMediaQuery(){const t=getComputedStyle(this.options.rootElement).getPropertyValue(this.options.darkModeMediaQueryKey);return t.length?t.replace(/\"/g,"").trim():"dark"===t?"dark":"light"}resetRootDarkModeAttributeAndLS(){var t;this.options.rootElement.removeAttribute(this.options.rootElementDarkModeAttributeName),t=this.options.darkModeStorageKey,window.localStorage.removeItem(t)}applyCustomDarkModeSettings(t){const o=t||e(this.options.darkModeStorageKey);o===this.getModeFromCSSMediaQuery()?this.resetRootDarkModeAttributeAndLS():this.validColorModeKeys[o]?this.options.rootElement.setAttribute(this.options.rootElementDarkModeAttributeName,o):this.resetRootDarkModeAttributeAndLS(),this.darkModeToggleButton&&this.darkModeToggleButton instanceof Element&&(this.darkModeToggleButton.classList.remove("dm-"+this.invertDarkModeObj[o]),this.darkModeToggleButton.classList.add("dm-"+o))}toggleCustomDarkMode(){let t=e(this.options.darkModeStorageKey);if(this.validColorModeKeys[t])t=this.invertDarkModeObj[t];else{if(null!==t)return;t=this.invertDarkModeObj[this.getModeFromCSSMediaQuery()]}return function(t,e,o=!0){o&&(e=JSON.stringify(e)),window.localStorage.setItem(t,e)}(this.options.darkModeStorageKey,t),t}}return o})); | ||
!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 i=o.shift();if(i)for(const o in i)if(Object.prototype.hasOwnProperty.call(i,o)){const n=i[o],s=o;"object"!=typeof(r=n)||null===r||Array.isArray(r)?e[s]=n:(e[s]&&"object"==typeof e[s]||(e[s]={}),t(e[s],n))}var r;return t(e,...o)}function e(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 o{static instance=null;static version="1.0.1";darkModeToggleButton;options;defaults={onChange:()=>{},onDark:()=>{},onLight:()=>{},rootElement:document.documentElement,darkModeStorageKey:"user-color-scheme",darkModeMediaQueryKey:"--color-mode",rootElementDarkModeAttributeName:"data-user-color-scheme"};_onChange=()=>{};_onDark=()=>{};_onLight=()=>{};validColorModeKeys={dark:!0,light:!0};invertDarkModeObj={dark:"light",light:"dark"};constructor(t,e={}){if(o.instance)return o.instance;const i=function(t,e,o){if("string"!=typeof t)return t;let i=document;return null===e&&o?i=o:e&&e instanceof Node&&"querySelector"in e?i=e:o&&o instanceof Node&&"querySelector"in o&&(i=o),"all"===e?i.querySelectorAll(t):i.querySelector(t)}(t);if(!i)throw new Error("ToggleButton could not be found with the selector provided.");this.init(i,e),o.instance=this,Object.seal(this)}set onChange(t){this._onChange=t}set onDark(t){this._onDark=t}set onLight(t){this._onLight=t}init(e,i){const r=t(this.defaults,i);this.options=r,this.darkModeToggleButton=e,this._onChange=this.options.onChange||this._onChange,this._onDark=this.options.onDark||this._onDark,this._onLight=this.options.onLight||this._onLight,this.setupDarkMode(),console.log(`DarkMode is loaded, version: ${o.version}`)}setupDarkMode(){const t=this.applyCustomDarkModeSettings();this.bindEvents(),t&&"dark"===t?this._onDark():this._onLight()}bindEvents(){this.darkModeToggleButton.addEventListener("click",(()=>{const t=this.toggleCustomDarkMode();this.applyCustomDarkModeSettings(t),t&&(this._onChange(),"dark"===t?this._onDark():"light"===t&&this._onLight())}))}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 o=t||e(this.options.darkModeStorageKey);return o===this.getModeFromCSSMediaQuery()?this.resetRootDarkModeAttribute():this.validColorModeKeys[o]?this.options.rootElement.setAttribute(this.options.rootElementDarkModeAttributeName,o):this.resetRootDarkModeAttribute(),this.darkModeToggleButton&&this.darkModeToggleButton instanceof Element&&(this.darkModeToggleButton.classList.remove("dm-"+this.invertDarkModeObj[o]),this.darkModeToggleButton.classList.add("dm-"+o)),o}toggleCustomDarkMode(){let t=e(this.options.darkModeStorageKey);if(this.validColorModeKeys[t])t=this.invertDarkModeObj[t];else{if(null!==t)return;t=this.invertDarkModeObj[this.getModeFromCSSMediaQuery()]}return function(t,e,o=!0){o&&(e=JSON.stringify(e)),window.localStorage.setItem(t,e)}(this.options.darkModeStorageKey,t),t}}return o})); |
{ | ||
"name": "@carry0987/darkmode", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A simple and lightweight JavaScript library for dark mode toggle with localStorage support.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
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
13911
248