Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit bc9da6b

Browse files
Implement theme toggle functionality and update styles for light/dark modes (#320)
1 parent 92f676e commit bc9da6b

File tree

7 files changed

+3613
-98
lines changed

7 files changed

+3613
-98
lines changed
Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,76 @@
1-
// components.js
21
function AboutPage() {
3-
// Create the main container
42
const container = document.createElement('div');
5-
container.classList.add('space-y-4', 'p-6', 'text-center');
3+
container.classList.add('min-h-screen', 'p-6', 'text-center', 'bg-blue-100', 'dark:bg-blue-900', 'text-gray-900', 'dark:text-white', 'transition-colors', 'duration-300');
64

7-
// Create the main title
85
const title = document.createElement('h1');
96
title.textContent = 'DWA Starter Vanilla';
10-
title.classList.add('text-3xl', 'font-bold', 'mb-4');
7+
title.classList.add('text-3xl', 'font-bold', 'mb-4', 'text-blue-800', 'dark:text-blue-200');
118

12-
// Create the first paragraph
139
const para1 = document.createElement('p');
1410
para1.textContent = "Decentralized Web App: it's a Web5 Progressive Web App.";
15-
para1.classList.add('text-lg');
11+
para1.classList.add('text-lg', 'text-blue-700', 'dark:text-blue-300');
1612

17-
// Create the subtitle
1813
const subtitle = document.createElement('h2');
1914
subtitle.textContent = 'Why PWA?';
20-
subtitle.classList.add('text-2xl', 'font-semibold', 'mt-4');
15+
subtitle.classList.add('text-2xl', 'font-semibold', 'mt-4', 'text-blue-800', 'dark:text-blue-200');
2116

22-
// Create the second paragraph
2317
const para2 = document.createElement('p');
2418
para2.textContent = 'It\'s a perfect match with Web5 DWNs since a PWA can work offline and DWN has a synced local storage.';
25-
para2.classList.add('text-lg');
19+
para2.classList.add('text-lg', 'text-blue-700', 'dark:text-blue-300');
2620

27-
// Append elements to the container
2821
container.appendChild(title);
2922
container.appendChild(para1);
3023
container.appendChild(subtitle);
3124
container.appendChild(para2);
3225

33-
// Return the container element
3426
return container;
3527
}
3628

37-
export function Home() {
38-
document.getElementById('app').innerHTML = `<h1>Home</h1>`;
39-
}
40-
41-
export function About() {
29+
export function Home() {
30+
const homeContainer = document.createElement('div');
31+
homeContainer.classList.add('min-h-screen', 'p-6', 'bg-green-100', 'dark:bg-green-900', 'text-gray-900', 'dark:text-white', 'transition-colors', 'duration-300');
32+
33+
const title = document.createElement('h1');
34+
title.textContent = 'Home';
35+
title.classList.add('text-3xl', 'font-bold', 'mb-4', 'text-green-800', 'dark:text-green-200');
36+
37+
homeContainer.appendChild(title);
38+
39+
document.getElementById('app').innerHTML = '';
40+
document.getElementById('app').appendChild(homeContainer);
41+
}
42+
43+
export function About() {
4244
const app = document.getElementById('app');
43-
app.innerHTML = ''; // Clear the current content
44-
45-
// Create and append the About page
46-
const aboutPage = AboutPage(); // Call the AboutPage function to get the component
45+
app.innerHTML = '';
46+
const aboutPage = AboutPage();
4747
app.appendChild(aboutPage);
48-
}
49-
50-
export function Settings() {
51-
document.getElementById('app').innerHTML = `<h1>Settings</h1>`;
52-
}
53-
54-
export function NotFound() {
55-
document.getElementById('app').innerHTML = `<h1>404 - Page Not Found</h1>`;
56-
}
57-
48+
}
49+
50+
export function Settings() {
51+
const settingsContainer = document.createElement('div');
52+
settingsContainer.classList.add('min-h-screen', 'p-6', 'bg-purple-100', 'dark:bg-purple-900', 'text-gray-900', 'dark:text-white', 'transition-colors', 'duration-300');
53+
54+
const title = document.createElement('h1');
55+
title.textContent = 'Settings';
56+
title.classList.add('text-3xl', 'font-bold', 'mb-4', 'text-purple-800', 'dark:text-purple-200');
57+
58+
settingsContainer.appendChild(title);
59+
60+
document.getElementById('app').innerHTML = '';
61+
document.getElementById('app').appendChild(settingsContainer);
62+
}
63+
64+
export function NotFound() {
65+
const notFoundContainer = document.createElement('div');
66+
notFoundContainer.classList.add('min-h-screen', 'p-6', 'bg-red-100', 'dark:bg-red-900', 'text-gray-900', 'dark:text-white', 'transition-colors', 'duration-300');
67+
68+
const title = document.createElement('h1');
69+
title.textContent = '404 - Page Not Found';
70+
title.classList.add('text-3xl', 'font-bold', 'mb-4', 'text-red-800', 'dark:text-red-200');
71+
72+
notFoundContainer.appendChild(title);
73+
74+
document.getElementById('app').innerHTML = '';
75+
document.getElementById('app').appendChild(notFoundContainer);
76+
}

javascript/dwa-starter-vanillajs-vite/index.html

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
<!-- index.html -->
2-
3-
<!doctype html>
1+
<!DOCTYPE html>
42
<html lang="en">
53
<head>
64
<meta charset="UTF-8" />
7-
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
85
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
96
<title>DWA Starter</title>
10-
<meta
11-
name="description"
12-
content="A Decentralized Web Application template"
13-
/>
14-
<link rel="stylesheet" href="dist/output.css" />
7+
<link rel="stylesheet" href="style.css" />
158
</head>
169
<body>
1710
<nav>
Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,101 @@
1-
// main.js
1+
// Function to create and render the toggle button
2+
function createThemeToggleButton() {
3+
console.log('Creating theme toggle button');
4+
const nav = document.querySelector('nav');
5+
const button = document.createElement('button');
6+
button.id = 'theme-toggle';
7+
button.textContent = 'Toggle Theme';
8+
button.setAttribute('aria-label', 'Toggle Dark Mode');
9+
button.classList.add('theme-toggle-btn');
10+
nav.appendChild(button);
11+
button.addEventListener('click', toggleTheme);
12+
console.log('Theme toggle button created and added to nav');
13+
}
14+
15+
function toggleTheme() {
16+
console.log('Toggle theme function called');
17+
const body = document.body;
18+
const isDarkMode = body.classList.contains('dark-mode');
19+
console.log('Current mode is dark:', isDarkMode);
20+
21+
if (isDarkMode) {
22+
body.classList.remove('dark-mode');
23+
body.classList.add('light-mode');
24+
console.log('Switched to light mode:', body.classList); // Log class list
25+
} else {
26+
body.classList.remove('light-mode');
27+
body.classList.add('dark-mode');
28+
console.log('Switched to dark mode:', body.classList); // Log class list
29+
}
30+
localStorage.setItem('theme', isDarkMode ? 'light' : 'dark');
31+
}
32+
33+
34+
// Apply stored theme preference or system preference on load
35+
function applyStoredTheme() {
36+
console.log('Applying stored theme');
37+
const storedTheme = localStorage.getItem('theme');
38+
const prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)');
39+
const body = document.body;
40+
41+
console.log('Stored theme:', storedTheme);
42+
console.log('System prefers dark scheme:', prefersDarkScheme.matches);
43+
44+
if (storedTheme === 'dark' || (storedTheme === null && prefersDarkScheme.matches)) {
45+
body.classList.add('dark-mode');
46+
console.log('Applied dark mode');
47+
} else {
48+
body.classList.add('light-mode');
49+
console.log('Applied light mode');
50+
}
51+
}
52+
53+
// Initial setup on DOM content loaded
54+
document.addEventListener('DOMContentLoaded', () => {
55+
console.log('DOM content loaded');
56+
applyStoredTheme(); // Apply the stored theme or system preference
57+
createThemeToggleButton(); // Create the theme toggle button and attach to nav
58+
// Initial routing setup (if using navigation in your app)
59+
router();
60+
console.log('Initial setup completed');
61+
});
262

63+
// Import your components for routing (if necessary)
364
import { Home, About, Settings, NotFound } from './components.js';
465

5-
// Define routes and their corresponding components
66+
// Define routes and their corresponding components (if necessary)
667
const routes = {
768
'/': Home,
869
'/about': About,
970
'/settings': Settings,
1071
};
1172

12-
// Function to handle navigation
73+
// Function to handle navigation (if necessary)
1374
function navigateTo(url) {
75+
console.log('Navigating to:', url);
1476
history.pushState(null, null, url);
1577
router();
1678
}
1779

1880
// Router function to render components based on the current URL
1981
function router() {
82+
console.log('Router function called');
2083
const path = window.location.pathname;
84+
console.log('Current path:', path);
2185
const route = routes[path] || NotFound;
2286
route();
2387
}
2488

25-
// Event delegation for link clicks
89+
// Event delegation for link clicks (if necessary)
2690
document.addEventListener('click', (e) => {
2791
if (e.target.matches('[data-link]')) {
92+
console.log('Link clicked:', e.target.href);
2893
e.preventDefault();
2994
navigateTo(e.target.href);
3095
}
3196
});
3297

33-
// Listen to popstate event (back/forward navigation)
98+
// Listen to popstate event (back/forward navigation) (if necessary)
3499
window.addEventListener('popstate', router);
35100

36-
// Initial call to router to render the correct component on page load
37-
document.addEventListener('DOMContentLoaded', router);
101+
console.log('Script loaded');

0 commit comments

Comments
 (0)