Skip to content

Commit 324c514

Browse files
committed
modernize dark mode toggle with switch and enhanced UI
1 parent be74881 commit 324c514

3 files changed

Lines changed: 270 additions & 43 deletions

File tree

‎19-dark-mode/final/app.js‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1-
const toggleBtn = document.querySelector('.btn');
1+
const toggleSwitch = document.querySelector('#theme-toggle');
22
const 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
821
const 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;

‎19-dark-mode/final/index.html‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,26 @@
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 -->
@@ -33,4 +38,4 @@
3338
<!-- logic -->
3439
<script src="./app.js"></script>
3540
</body>
36-
</html>
41+
</html>

0 commit comments

Comments
 (0)