Skip to content

Commit f6bd997

Browse files
committed
add auto dark script
1 parent a8acc8e commit f6bd997

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

_includes/head.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,7 @@
2828
<![endif]-->
2929

3030
<link rel="stylesheet" href="{{ "/assets/css/cookiealert.css" | prepend: site.baseurl }}">
31+
32+
<script src="{{ "/assets/js/bootstrap-auto-dark-mode.js" | prepend: site.baseurl }}"></script>
33+
3134
</head>

_layouts/default.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
---
44

55
<!DOCTYPE html>
6-
<html lang="en">
6+
<html lang="en" data-bs-theme="auto">
77

88
{% include head.html %}
99

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
document.addEventListener('DOMContentLoaded', function() {
2+
function toggleTheme() {
3+
const html = document.documentElement;
4+
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
5+
if (prefersDark) {
6+
html.setAttribute('data-bs-theme', 'dark');
7+
} else {
8+
html.setAttribute('data-bs-theme', 'light');
9+
}
10+
}
11+
12+
// Initial call to set the correct theme
13+
toggleTheme();
14+
15+
// Listen for system theme changes
16+
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', toggleTheme);
17+
18+
// Optionally, add a toggle button to allow manual switching
19+
// You would need to add a button to your HTML and handle the click event
20+
// to toggle the theme.
21+
});

0 commit comments

Comments
 (0)