Skip to content

Commit 9da6761

Browse files
authored
improve logic against exception cases
Improve automatic dark mode setting logic. + avoid conflict errors, when other color was already set. + fix conflict errors, when dark was already set as the CSS class.
1 parent dba7d73 commit 9da6761

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

JS-CSS-Color-Settings-Recall-on-load.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,15 @@ document.addEventListener("DOMContentLoaded", AfterDark() , true ) ;
4242

4343
function AfterDark() {
4444

45-
var hour = new Date().getHours() ;
45+
let hour = new Date().getHours() ;
4646

47-
if ( !localStorage.getItem( "ColorMode" ) && ( hour >= 20 || hour < 7 ) ) {
47+
let ColorWasSet = localStorage.getItem( "ColorMode" ) ;
4848

49-
document.querySelector("html").classList.add( "dark" ) ;
49+
let DarkWasSet = document.querySelector("html").classList.contains("dark") ;
50+
51+
if ( ( hour >= 20 || hour < 7 ) && !( ColorWasSet || DarkWasSet ) )
52+
{
53+
document.querySelector("html").classList.add( "dark" ) ;
5054
}
5155
}
5256

0 commit comments

Comments
 (0)