Skip to content

Commit 71ad192

Browse files
authored
Merge pull request #2990 from Samir-Rashid/more-theme-fixes
More theme fixes
2 parents c755f3e + 1ea82d9 commit 71ad192

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

_sass/theme/_dark.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ html[data-theme="dark"] {
4242
--global-dark-border-color : #{$background-light};
4343
--global-code-background-color : #fafafa;
4444
--global-code-text-color : #{$darker-gray};
45-
--global-fig-caption-color : #{$dark-gray};
45+
--global-fig-caption-color : #{$light-gray};
4646
--global-link-color : #{$link};
4747
--global-link-color-hover : #{$link-dark};
4848
--global-link-color-visited : #{$link-light};

assets/js/_main.js

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,48 @@
33
========================================================================== */
44

55
$(document).ready(function () {
6-
// Set the theme on page load
6+
// detect OS/browser preference
7+
const browserPref = window.matchMedia('(prefers-color-scheme: dark)').matches
8+
? 'dark'
9+
: 'light';
10+
11+
// Set the theme on page load or when explicitly called
712
var setTheme = function (theme) {
8-
const use_theme = theme || localStorage.getItem("theme") || $("html").attr("data-theme");
13+
const use_theme =
14+
theme ||
15+
localStorage.getItem("theme") ||
16+
$("html").attr("data-theme") ||
17+
browserPref;
18+
919
if (use_theme === "dark") {
1020
$("html").attr("data-theme", "dark");
1121
$("#theme-icon").removeClass("fa-sun").addClass("fa-moon");
1222
} else if (use_theme === "light") {
1323
$("html").removeAttr("data-theme");
1424
$("#theme-icon").removeClass("fa-moon").addClass("fa-sun");
1525
}
16-
}
26+
};
27+
1728
setTheme();
1829

19-
// Toggle the theme
30+
// if user hasn't chosen a theme, follow OS changes
31+
window
32+
.matchMedia('(prefers-color-scheme: dark)')
33+
.addEventListener("change", (e) => {
34+
if (!localStorage.getItem("theme")) {
35+
setTheme(e.matches ? "dark" : "light");
36+
}
37+
});
38+
39+
// Toggle the theme manually
2040
var toggleTheme = function () {
2141
const current_theme = $("html").attr("data-theme");
2242
const new_theme = current_theme === "dark" ? "light" : "dark";
2343
localStorage.setItem("theme", new_theme);
2444
setTheme(new_theme);
25-
}
26-
$('#theme-toggle').on('click', function () {
27-
toggleTheme();
28-
});
45+
};
46+
47+
$('#theme-toggle').on('click', toggleTheme);
2948

3049
// These should be the same as the settings in _variables.scss
3150
const scssLarge = 925; // pixels
@@ -65,10 +84,35 @@ $(document).ready(function () {
6584
});
6685

6786
// init smooth scroll, this needs to be slightly more than then fixed masthead height
68-
$("a").smoothScroll({ offset: -65 });
87+
$("a").smoothScroll({
88+
offset: -75, // needs to match $masthead-height
89+
preventDefault: false,
90+
});
6991

7092
// add lightbox class to all image links
71-
$("a[href$='.jpg'],a[href$='.jpeg'],a[href$='.JPG'],a[href$='.png'],a[href$='.gif']").addClass("image-popup");
93+
// Add "image-popup" to links ending in image extensions,
94+
// but skip any <a> that already contains an <img>
95+
$("a[href$='.jpg'],\
96+
a[href$='.jpeg'],\
97+
a[href$='.JPG'],\
98+
a[href$='.png'],\
99+
a[href$='.gif'],\
100+
a[href$='.webp']")
101+
.not(':has(img)')
102+
.addClass("image-popup");
103+
104+
// 1) Wrap every <p><img> (except emoji images) in an <a> pointing at the image, and give it the lightbox class
105+
$('p > img').not('.emoji').each(function() {
106+
var $img = $(this);
107+
// skip if it’s already wrapped in an <a.image-popup>
108+
if ( ! $img.parent().is('a.image-popup') ) {
109+
$('<a>')
110+
.addClass('image-popup')
111+
.attr('href', $img.attr('src'))
112+
.insertBefore($img) // place the <a> right before the <img>
113+
.append($img); // move the <img> into the <a>
114+
}
115+
});
72116

73117
// Magnific-Popup options
74118
$(".image-popup").magnificPopup({

assets/js/main.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)