Skip to content

Commit

Permalink
Correctly update medium-zoom background when switching theme (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
alshedivat committed Jul 5, 2021
1 parent 495c4aa commit 9c8bd1a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 41 deletions.
1 change: 0 additions & 1 deletion _posts/2015-05-15-images.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,3 @@ The rest of the images in this post are all zoomable, arranged into different mi
<img class="img-fluid rounded z-depth-1" src="{{ site.baseurl }}/assets/img/7.jpg" data-zoomable>
</div>
</div>

29 changes: 1 addition & 28 deletions assets/js/dark_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,6 @@ $(document).ready(function() {
const mode_toggle = document.getElementById("light-toggle");

mode_toggle.addEventListener("click", function() {
const temp = localStorage.getItem("theme");
toggleTheme(temp);
toggleTheme(localStorage.getItem("theme"));
});

let toggleTheme = (theme) => {
if (theme == "dark") {
setTheme("light");
} else {
setTheme("dark");
}
}

let setTheme = (theme) => {
trans();
if (theme) {
document.documentElement.setAttribute("data-theme", theme)
}
else {
document.documentElement.removeAttribute("data-theme");
}
localStorage.setItem("theme", theme);
};

let trans = () => {
document.documentElement.classList.add("transition");
window.setTimeout(() => {
document.documentElement.classList.remove("transition")
}, 500)
}
});
46 changes: 40 additions & 6 deletions assets/js/theme.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,51 @@
// Has to be in the head tag, otherwise a flicker effect will occur.

let toggleTheme = (theme) => {
if (theme == "dark") {
setTheme("light");
} else {
setTheme("dark");
}
}


let setTheme = (theme) => {
transTheme();
if (theme) {
document.documentElement.setAttribute("data-theme", theme);
}
else {
document.documentElement.removeAttribute("data-theme");
}
localStorage.setItem("theme", theme);

// Updates the background of medium-zoom overlay.
if (typeof medium_zoom !== 'undefined') {
medium_zoom.update({
background: getComputedStyle(document.documentElement)
.getPropertyValue('--global-bg-color') + 'ee', // + 'ee' for trasparency.
})
}
};


let transTheme = () => {
document.documentElement.classList.add("transition");
window.setTimeout(() => {
document.documentElement.classList.remove("transition");
}, 500)
}


let initTheme = (theme) => {
if (theme == null) {
const userPref = window.matchMedia;
if (userPref && userPref('(prefers-color-scheme: dark)').matches) {
theme = 'dark';
}
}

if (theme) {
document.documentElement.setAttribute('data-theme', theme)
}

localStorage.setItem("theme", theme);
setTheme(theme);
}


initTheme(localStorage.getItem("theme"));
12 changes: 6 additions & 6 deletions assets/js/zoom.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

// Initialize medium zoom.
$(document).ready(function() {
mediumZoom('[data-zoomable]', {
margin: 100,
background: getComputedStyle(document.documentElement)
.getPropertyValue('--global-bg-color') + 'ee',
})
medium_zoom = mediumZoom('[data-zoomable]', {
margin: 100,
background: getComputedStyle(document.documentElement)
.getPropertyValue('--global-bg-color') + 'ee', // + 'ee' for trasparency.
})
});

0 comments on commit 9c8bd1a

Please sign in to comment.