Skip to content

Commit

Permalink
toggle button implement
Browse files Browse the repository at this point in the history
  • Loading branch information
shibasarkar committed Oct 29, 2024
1 parent 1b39a7f commit def4113
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
16 changes: 11 additions & 5 deletions public/js/script.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
document.addEventListener('DOMContentLoaded', function(){
const darkModeToggle = document.getElementById("dark-mode-toggle");
const icon=document.getElementById('dark-icon');
const body = document.body;

const savedDarkMode = localStorage.getItem('darkMode');
if (savedDarkMode == 'enabled') {

body.classList.add('dark-mode');
icon.classList.add('fa-sun')
} else {
body.classList.remove('dark-mode');
if (darkModeToggle) darkModeToggle.textContent ='Dark Mode';
icon.classList.add('fa-moon')
}


if (darkModeToggle) {

darkModeToggle.addEventListener("click", function() {
body.classList.toggle("dark-mode");

if (body.classList.contains('dark-mode')){
localStorage.setItem('darkMode','enabled');
darkModeToggle.textContent ="Light Mode";
icon.classList.remove('fa-moon');
icon.classList.add('fa-sun');
} else {
localStorage.setItem('darkMode','disabled');
darkModeToggle.textContent ="Dark Mode";
icon.classList.remove('fa-sun');
icon.classList.add('fa-moon');
}
});
}
Expand Down
6 changes: 3 additions & 3 deletions views/contact.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<section class="contact-section">
<div class="contact-container">
<section class="welcome-section ">
<!-- <div class="contact-container"> -->
<h1>Contact Us</h1>
<p>If you have any questions or feedback, feel free to reach out!</p>

Expand All @@ -25,5 +25,5 @@
</div>
<button type="submit" class="btn-submit">Send Message</button>
</form>
</div>
<!-- </div> -->
</section>
8 changes: 1 addition & 7 deletions views/layouts/main.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@
<div class="container">

<%- include('../partials/header.ejs') %>
<!-- Dark Mode Toggle Button -->
<div>
<button id="dark-mode-toggle">Dark Mode</button>
</div>

<%- include('../partials/header.ejs', { currentRoute: locals.currentRoute }) %>


<main class="main">
<%- body %>
</main>
Expand Down
10 changes: 6 additions & 4 deletions views/partials/header.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
<li>
<a href="/contact" class="<%= isActiveRoute('/contact', currentRoute) %>">Contact</a>
</li>
<li>
<a href="/admin" class="<%= isActiveRoute('/admin', currentRoute) %>">Login</a>
</li>
<li>
<a href="/add-post" class="<%= isActiveRoute('/admin', currentRoute) %>">Post</a>
</li>
Expand All @@ -38,7 +35,12 @@
<button id="LogIn" onclick="window.location.href = '/admin'">Log In</button>
<% } %>


<!-- Dark Mode Toggle Button -->
<button id="dark-mode-toggle" style="margin-left: 0.5rem;">
<i class="fa-solid" id="dark-icon"></i>
</button>


</div>

</header>
Expand Down

0 comments on commit def4113

Please sign in to comment.