From 8bed3dda1db2fd3cdecffd3bd8dd365f9e3d3da8 Mon Sep 17 00:00:00 2001 From: BetaHuhn Date: Wed, 7 Apr 2021 19:22:05 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=A1=20Add=20more=20jsdoc=20comments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dist/drkmd-js.min.js | 2 +- src/drkmd.js | 31 ++++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/dist/drkmd-js.min.js b/dist/drkmd-js.min.js index 856acfd..4220339 100644 --- a/dist/drkmd-js.min.js +++ b/dist/drkmd-js.min.js @@ -1 +1 @@ -!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).drkmdJs=e()}}((function(){return function e(t,n,o){function r(a,d){if(!n[a]){if(!t[a]){var c="function"==typeof require&&require;if(!d&&c)return c(a,!0);if(i)return i(a,!0);var u=new Error("Cannot find module '"+a+"'");throw u.code="MODULE_NOT_FOUND",u}var s=n[a]={exports:{}};t[a][0].call(s.exports,(function(e){return r(t[a][1][e]||e)}),s,s.exports,e,t,n,o)}return n[a].exports}for(var i="function"==typeof require&&require,a=0;a0)for(var c=0;c0)for(var u=0;u0)for(var s=0;s0)for(var c=0;c0)for(var u=0;u0)for(var l=0;l e.matches && this._switchThemePrefers()) - window.matchMedia('(prefers-color-scheme: light)').addListener((e) => e.matches && this._switchThemePrefers()) + window.matchMedia('(prefers-color-scheme: dark)').addListener((e) => e.matches && this._handlePreferedThemeChangeEvent()) + window.matchMedia('(prefers-color-scheme: light)').addListener((e) => e.matches && this._handlePreferedThemeChangeEvent()) } // Determine to which theme should be set, start with default, precendence based on descending order @@ -176,19 +176,39 @@ export default class Darkmode { return this.dark ? 'dark' : 'light' } + + /** + * Check if the system theme is dark + * @private + * @returns {boolean} isDark - true if system theme is dark + */ _preferedThemeIsDark() { return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches } - _switchThemePrefers() { + /** + * Switch the current theme if the prefered system theme changes + * @private + */ + _handlePreferedThemeChangeEvent() { const val = this._preferedThemeIsDark() this._changeThemeToDark(val) } + /** + * Change theme to dark if true, else change to light + * @private + * @param {boolean} toDark - change theme to dark + */ _changeThemeToDark(toDark) { toDark ? this.toDark() : this.toLight() } + /** + * Save the current theme choice in either local storage as a cookie + * @private + * @param {boolean} value - true for dark, false for light + */ _setStorageValue(value) { if (this.options.localStorage && window.localStorage !== null) { window.localStorage.setItem('darkmode', value) @@ -201,6 +221,11 @@ export default class Darkmode { } } + /** + * Add the specified styles as a new stylesheet to the document + * @private + * @param {sring} css - the css which should be added to the document + */ _addStyle(css) { const linkElement = document.createElement('link')