File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- const toggleBtn = document . querySelector ( '.btn ' ) ;
1+ const toggleSwitch = document . querySelector ( '#theme-toggle ' ) ;
22const articlesContainer = document . querySelector ( '.articles' ) ;
33
4- toggleBtn . addEventListener ( 'click' , ( ) => {
4+ // Check for saved theme preference or default to light mode
5+ const currentTheme = localStorage . getItem ( 'theme' ) || 'light' ;
6+ if ( currentTheme === 'dark' ) {
7+ document . documentElement . classList . add ( 'dark-theme' ) ;
8+ toggleSwitch . checked = true ;
9+ }
10+
11+ // Toggle theme and save preference
12+ toggleSwitch . addEventListener ( 'change' , ( ) => {
513 document . documentElement . classList . toggle ( 'dark-theme' ) ;
14+ const theme = document . documentElement . classList . contains ( 'dark-theme' )
15+ ? 'dark'
16+ : 'light' ;
17+ localStorage . setItem ( 'theme' , theme ) ;
618} ) ;
719
20+ // Display articles
821const articlesData = articles
922 . map ( ( article ) => {
1023 const { title, date, length, snippet } = article ;
@@ -22,4 +35,4 @@ const articlesData = articles
2235 } )
2336 . join ( '' ) ;
2437
25- articlesContainer . innerHTML = articlesData ;
38+ articlesContainer . innerHTML = articlesData ;
Original file line number Diff line number Diff line change 44 < meta charset ="UTF-8 " />
55 < meta http-equiv ="X-UA-Compatible " content ="IE=edge " />
66 < meta name ="viewport " content ="width=device-width, initial-scale=1.0 " />
7- < title > Dark Mode</ title >
7+ < meta name ="description " content ="Dark mode toggle blog with modern UI " />
8+ < title > Dark Mode - overreacted</ title >
89 < link rel ="stylesheet " href ="./styles.css " />
910 </ head >
1011 < body >
1112 < main >
1213 < nav >
1314 < div class ="nav-center ">
1415 < span > overreacted</ span >
15- < button class ="btn "> toggle</ button >
16+ < div class ="toggle-container ">
17+ < label for ="theme-toggle " class ="toggle-label "> Dark Mode</ label >
18+ < label class ="switch ">
19+ < input type ="checkbox " id ="theme-toggle " aria-label ="Toggle dark mode " />
20+ < span class ="slider "> </ span >
21+ </ label >
22+ </ div >
1623 </ div >
1724 </ nav >
1825 < section class ="articles ">
19- <!-- single post -->
20-
21- <!-- end of single post -->
26+ <!-- Articles will be dynamically inserted here -->
2227 </ section >
2328 </ main >
2429 <!-- moment -->
3338 <!-- logic -->
3439 < script src ="./app.js "> </ script >
3540 </ body >
36- </ html >
41+ </ html >
You can’t perform that action at this time.
0 commit comments