Skip to content

Commit

Permalink
Add Light/Dark Mode Toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
EmpressAutumn committed Oct 27, 2024
1 parent e36c387 commit def03b5
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 48 deletions.
9 changes: 5 additions & 4 deletions devnotes/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
<head>
<meta charset="UTF-8">
<title>Dev Notes | Atom596.com</title>
<script type="text/javascript" src="/script/topbar.js"></script>
<script type="text/javascript" src="/script/lightdark.js"></script>
<script type="module" src="https://md-block.verou.me/md-block.js"></script>
<!--<script type="module" src="https://md-block.verou.me/md-block.js"></script>
<script>
latest = "latest"
window.addEventListener("load", function() {
document.getElementById("article").src = latest + ".md"
})
</script>
</script>-->
<link rel="stylesheet" href="/style/devnotes.css">
<link id="theme-css" rel="stylesheet" href="/style/light.css">
</head>
<body>
<h1>Dev Notes</h1>
<div id="topbar"></div>
<div class="article"><md-block id="article"></md-block></div>

<script type="text/javascript" src="/script/common.js"></script>
</body>
</html>
7 changes: 4 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
<head>
<meta charset="UTF-8">
<title>Home | Atom596.com</title>
<script type="text/javascript" src="/script/topbar.js"></script>
<link rel="stylesheet" href="/style/light.css">
<link rel="stylesheet" href="/style/home.css">
<link id="theme-css" rel="stylesheet" href="/style/light.css">
</head>
<body>
<h1>Atom596.com</h1>
<div id="topbar"></div>
<div class="plans">
<div class="subtitle">
<div class="subtitle" id="subtitle">
<h2>New Website Under Construction</h2>
</div>
<div class="column" id="bluesky">
Expand All @@ -24,5 +23,7 @@ <h3>Live GitHub Tracker</h3>
<h3>Live Modrinth Tracker</h3>
</div>
</div>

<script src="/script/common.js"></script>
</body>
</html>
46 changes: 46 additions & 0 deletions script/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
window.addEventListener("load", function() {
fetch("/topbar.html")
.then(response => response.text())
.then((data) => {
document.getElementById("topbar").innerHTML = data

const toggleButton = document.getElementById("mode-toggle");
if (toggleButton) {
toggleButton.addEventListener("click", toggleMode);
}
})
})

function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
}

// Function to set a cookie
function setCookie(name, value, days) {
const date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
const expires = `expires=${date.toUTCString()}`;
document.cookie = `${name}=${value}; ${expires}; path=/`;
}

// Function to toggle between light and dark mode
function toggleMode() {
const currentMode = getCookie("theme") || "light";
const newMode = currentMode === "light" ? "dark" : "light";
setCookie("theme", newMode, 365); // Save new mode for one year
location.reload(); // Reload page to apply new theme
}

// Apply the correct CSS based on the theme cookie
function applyTheme() {
const theme = getCookie("theme") || "light";
const themeCss = document.getElementById('theme-css');

console.log(`Applying theme: ${theme}`);

themeCss.href = "/style/" + theme + ".css";
}

applyTheme()
18 changes: 0 additions & 18 deletions script/cookies.js

This file was deleted.

13 changes: 0 additions & 13 deletions script/lightdark.js

This file was deleted.

7 changes: 0 additions & 7 deletions script/topbar.js

This file was deleted.

3 changes: 2 additions & 1 deletion style/dark.css
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
h1 {
text-align: center;
color:palegoldenrod;
}

body {
background-image: url("https://www.toptal.com/designers/subtlepatterns/uploads/fancy-cushion.pngflower.png");
background-image: url("https://www.toptal.com/designers/subtlepatterns/uploads/fancy-cushion.png");
background-repeat: repeat;
font-family: Cambria, serif;
}
17 changes: 17 additions & 0 deletions style/topbar.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,20 @@
margin-top: 10px;
margin-bottom: 10px;
}

.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 90px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
padding: 12px 16px;
z-index: 1;
}
.dropdown:hover .dropdown-content {
display: block;
}
9 changes: 7 additions & 2 deletions topbar.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<link rel="stylesheet" href="/style/topbar.css">
<div class="topbar">
<h3 class="links">
<a href="https://www.atom596.com/">Home</a>
| <a href="https://www.atom596.com/devnotes/">Dev Notes</a>
<a href="/">Home</a>
| <a href="/devnotes/">Dev Notes</a>
| <div class="dropdown">
<span>Settings</span>
<div class="dropdown-content">
<button id="mode-toggle">Switch Mode</button>
</div></div>
</h3>
</div>

0 comments on commit def03b5

Please sign in to comment.