-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
148 lines (124 loc) · 4.63 KB
/
script.js
File metadata and controls
148 lines (124 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Dark Mode Toggle Functionality
document.addEventListener('DOMContentLoaded', function () {
// Create and add the dark mode toggle button
createThemeToggle();
// Initialize theme based on user preference or system preference
initializeTheme();
// Add event listener for theme toggle
const themeToggle = document.querySelector('.theme-toggle');
themeToggle.addEventListener('click', toggleThemeWithTransition);
});
function createThemeToggle() {
// Create the toggle button element
const toggleButton = document.createElement('button');
toggleButton.className = 'theme-toggle';
toggleButton.innerHTML = '<i class="fas fa-moon"></i>';
toggleButton.setAttribute('aria-label', 'Toggle dark mode');
toggleButton.setAttribute('title', 'Toggle dark mode');
// Add to the container
const container = document.querySelector('.container');
container.appendChild(toggleButton);
}
function initializeTheme() {
// Check if user has a saved preference
const savedTheme = localStorage.getItem('theme');
if (savedTheme) {
// Use saved preference
setTheme(savedTheme);
} else {
// Check system preference
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
setTheme(prefersDark ? 'dark' : 'light');
}
// Listen for system theme changes
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
if (!localStorage.getItem('theme')) {
setTheme(e.matches ? 'dark' : 'light');
}
});
}
function toggleTheme() {
const currentTheme = document.documentElement.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
setTheme(newTheme);
// Save user preference
localStorage.setItem('theme', newTheme);
}
function setTheme(theme) {
const html = document.documentElement;
const toggleButton = document.querySelector('.theme-toggle');
const icon = toggleButton.querySelector('i');
// Set theme attribute
html.setAttribute('data-theme', theme);
// Update toggle button icon with animation
icon.style.transform = 'rotate(180deg)';
setTimeout(() => {
if (theme === 'dark') {
icon.className = 'fas fa-sun';
toggleButton.setAttribute('aria-label', 'Toggle light mode');
toggleButton.setAttribute('title', 'Toggle light mode');
} else {
icon.className = 'fas fa-moon';
toggleButton.setAttribute('aria-label', 'Toggle dark mode');
toggleButton.setAttribute('title', 'Toggle dark mode');
}
icon.style.transform = 'rotate(0deg)';
}, 150);
}
// Add smooth transition when theme changes
function addThemeTransition() {
const css = document.createElement('style');
css.textContent = `
*, *::before, *::after {
transition: background-color 0.3s ease,
color 0.3s ease,
border-color 0.3s ease,
box-shadow 0.3s ease !important;
}
`;
document.head.appendChild(css);
// Remove transition after theme change to avoid interfering with other animations
setTimeout(() => {
css.remove();
}, 300);
}
// Call this function when toggling theme for smooth transitions
function toggleThemeWithTransition() {
addThemeTransition();
toggleTheme();
}
// Notes App Functionality
const noteInput = document.getElementById("noteInput")
const saveNoteButton = document.getElementById("saveNote")
const notesContainer = document.getElementById("notesContainer")
document.addEventListener("DOMContentLoaded", displayNotes)
saveNoteButton.addEventListener("click", function () {
let noteText = noteInput.value.trim();
if (noteText === "") {
return;
}
let notes = JSON.parse(localStorage.getItem("notes")) || [];
notes.push(noteText);
localStorage.setItem("notes", JSON.stringify(notes));
noteInput.value = "";
displayNotes();
});
function displayNotes() {
notesContainer.innerHTML = "";
let notes = JSON.parse(localStorage.getItem("notes")) || [];
notes.forEach((note, index) => {
let noteDiv = document.createElement("div");
noteDiv.classList.add("note");
noteDiv.innerHTML = `
<p>${note}</p>
<button class="deleteBtn" onclick="deleteNote(${index})">Delete</button>
`;
notesContainer.appendChild(noteDiv);
});
}
function deleteNote(index) {
let notes = JSON.parse(localStorage.getItem("notes")) || [];
notes.splice(index, 1);
localStorage.setItem("notes", JSON.stringify(notes));
displayNotes();
}