1
1
// Function to create and render the toggle button
2
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' ) ;
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
13
}
14
14
15
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
- }
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 ) ;
32
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
+ }
33
32
34
33
// Apply stored theme preference or system preference on load
35
34
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
- }
35
+ console . log ( "Applying stored theme" ) ;
36
+ const storedTheme = localStorage . getItem ( "theme" ) ;
37
+ const prefersDarkScheme = window . matchMedia ( "(prefers-color-scheme: dark)" ) ;
38
+ const body = document . body ;
39
+
40
+ console . log ( "Stored theme:" , storedTheme ) ;
41
+ console . log ( "System prefers dark scheme:" , prefersDarkScheme . matches ) ;
42
+
43
+ if (
44
+ storedTheme === "dark" ||
45
+ ( storedTheme === null && prefersDarkScheme . matches )
46
+ ) {
47
+ body . classList . add ( "dark-mode" ) ;
48
+ console . log ( "Applied dark mode" ) ;
49
+ } else {
50
+ body . classList . add ( "light-mode" ) ;
51
+ console . log ( "Applied light mode" ) ;
52
+ }
51
53
}
52
54
53
55
// 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' ) ;
56
+ document . addEventListener ( " DOMContentLoaded" , ( ) => {
57
+ console . log ( " DOM content loaded" ) ;
58
+ applyStoredTheme ( ) ; // Apply the stored theme or system preference
59
+ createThemeToggleButton ( ) ; // Create the theme toggle button and attach to nav
60
+ // Initial routing setup (if using navigation in your app)
61
+ router ( ) ;
62
+ console . log ( " Initial setup completed" ) ;
61
63
} ) ;
62
64
63
65
// Import your components for routing (if necessary)
64
- import { Home , About , Settings , NotFound } from ' ./components.js' ;
66
+ import { Home , About , Settings , NotFound } from " ./components.js" ;
65
67
66
68
// Define routes and their corresponding components (if necessary)
67
69
const routes = {
68
- '/' : Home ,
69
- ' /about' : About ,
70
- ' /settings' : Settings ,
70
+ "/" : Home ,
71
+ " /about" : About ,
72
+ " /settings" : Settings ,
71
73
} ;
72
74
73
75
// Function to handle navigation (if necessary)
74
76
function navigateTo ( url ) {
75
- console . log ( ' Navigating to:' , url ) ;
76
- history . pushState ( null , null , url ) ;
77
- router ( ) ;
77
+ console . log ( " Navigating to:" , url ) ;
78
+ history . pushState ( null , null , url ) ;
79
+ router ( ) ;
78
80
}
79
81
80
82
// Router function to render components based on the current URL
81
83
function router ( ) {
82
- console . log ( ' Router function called' ) ;
83
- const path = window . location . pathname ;
84
- console . log ( ' Current path:' , path ) ;
85
- const route = routes [ path ] || NotFound ;
86
- route ( ) ;
84
+ console . log ( " Router function called" ) ;
85
+ const path = window . location . pathname ;
86
+ console . log ( " Current path:" , path ) ;
87
+ const route = routes [ path ] || NotFound ;
88
+ route ( ) ;
87
89
}
88
90
89
91
// Event delegation for link clicks (if necessary)
90
- document . addEventListener ( ' click' , ( e ) => {
91
- if ( e . target . matches ( ' [data-link]' ) ) {
92
- console . log ( ' Link clicked:' , e . target . href ) ;
93
- e . preventDefault ( ) ;
94
- navigateTo ( e . target . href ) ;
95
- }
92
+ document . addEventListener ( " click" , ( e ) => {
93
+ if ( e . target . matches ( " [data-link]" ) ) {
94
+ console . log ( " Link clicked:" , e . target . href ) ;
95
+ e . preventDefault ( ) ;
96
+ navigateTo ( e . target . href ) ;
97
+ }
96
98
} ) ;
97
99
98
100
// Listen to popstate event (back/forward navigation) (if necessary)
99
- window . addEventListener ( 'popstate' , router ) ;
101
+ window . addEventListener ( "popstate" , router ) ;
102
+
103
+ console . log ( "Script loaded" ) ;
104
+
105
+ // Service worker registration
106
+ if ( "serviceWorker" in navigator ) {
107
+ window . addEventListener ( "load" , ( ) => {
108
+ navigator . serviceWorker
109
+ . register ( "sw.js" )
110
+ . then ( ( reg ) => {
111
+ console . log ( "Service Worker: Registered" ) ;
112
+
113
+ // Check if a new SW is waiting to activate
114
+ reg . onupdatefound = ( ) => {
115
+ const newWorker = reg . installing ;
116
+ newWorker . onstatechange = ( ) => {
117
+ if (
118
+ newWorker . state === "installed" &&
119
+ navigator . serviceWorker . controller
120
+ ) {
121
+ // Notify user about new version availability
122
+ if (
123
+ confirm (
124
+ "A new version of the app is available. Would you like to update?"
125
+ )
126
+ ) {
127
+ newWorker . postMessage ( { action : "skipWaiting" } ) ;
128
+ }
129
+ }
130
+ } ;
131
+ } ;
132
+ } )
133
+ . catch ( ( err ) => console . log ( `Service Worker Error: ${ err } ` ) ) ;
100
134
101
- console . log ( 'Script loaded' ) ;
135
+ // Listen for `controllerchange` to reload the page when the new SW takes control
136
+ navigator . serviceWorker . addEventListener ( "controllerchange" , ( ) => {
137
+ window . location . reload ( ) ;
138
+ } ) ;
139
+ } ) ;
140
+ }
141
+
142
+ let deferredPrompt ;
143
+
144
+ window . addEventListener ( "beforeinstallprompt" , ( e ) => {
145
+ e . preventDefault ( ) ;
146
+ deferredPrompt = e ;
147
+
148
+ // Show custom install button or UI (ensure an element with id="install-button" exists in your HTML)
149
+ const addToHomeScreen = document . querySelector ( "#install-button" ) ;
150
+ addToHomeScreen . style . display = "block" ;
151
+
152
+ addToHomeScreen . addEventListener ( "click" , ( ) => {
153
+ // Show the install prompt
154
+ deferredPrompt . prompt ( ) ;
155
+ deferredPrompt . userChoice . then ( ( choiceResult ) => {
156
+ if ( choiceResult . outcome === "accepted" ) {
157
+ console . log ( "User accepted the install prompt" ) ;
158
+ }
159
+ deferredPrompt = null ;
160
+ } ) ;
161
+ } ) ;
162
+ } ) ;
0 commit comments