Skip to content

Feature: Dark Mode Toggle Added (#179) #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ This book will teach you the basics of programming and Javascript. Whether you a
![](./assets/intro.png)

JavaScript (_JS for short_) is the programming language that enables web pages to respond to user interaction beyond the basic level. It was created in 1995, and is today one of the most famous and used programming languages.
<link rel="stylesheet" href="assets/darkmode.css">
<script src="assets/darkmode.js"></script>

12 changes: 12 additions & 0 deletions assets/darkmode.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
body.dark {
background-color: #121212;
color: #f0f0f0;
}

.dark h1, .dark h2, .dark h3 {
color: #ffffff;
}

.dark a {
color: #90caf9;
}
25 changes: 25 additions & 0 deletions assets/darkmode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
document.addEventListener('DOMContentLoaded', function () {
const toggle = document.createElement('button');
toggle.innerText = "🌙 Toggle Dark Mode";
toggle.style.position = 'fixed';
toggle.style.top = '10px';
toggle.style.right = '10px';
toggle.style.zIndex = '9999';
toggle.style.padding = '10px';
toggle.style.borderRadius = '5px';
toggle.style.border = 'none';
toggle.style.backgroundColor = '#333';
toggle.style.color = '#fff';
toggle.style.cursor = 'pointer';

toggle.onclick = () => {
document.body.classList.toggle('dark');
localStorage.setItem('theme', document.body.classList.contains('dark') ? 'dark' : 'light');
};

document.body.appendChild(toggle);

if (localStorage.getItem('theme') === 'dark') {
document.body.classList.add('dark');
}
});