Skip to content
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

Add support for dark mode images via JS #153

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Only apply the dark image filter to images that are not dark mode spe…
…cific. Remove the https check at the start of an image source otherwise image sources are not modified.
  • Loading branch information
astroDimitrios committed Oct 29, 2024
commit 9418af4e5e684f5a78205ea3efac44a68652048a
2 changes: 1 addition & 1 deletion inst/pkgdown/assets/assets/styles.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/pkgdown/assets/assets/styles.css.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/pkgdown/assets/assets/themetoggle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 13 additions & 14 deletions source/javascripts/custom/themetoggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,19 @@
const setImages = theme => {
document.querySelectorAll('img').forEach(img => {
var imgSrc = img.src
if (!imgSrc.startsWith("http")) {
if (theme === 'light') {
img.src = imgSrc.replace(/-dark./gi, ".")
}
else {
if (!imgSrc.includes("-dark.")) {
var imgName = imgSrc.replace(/\.[^/.]+$/, "");
var imgExt = imgSrc.slice((Math.max(0, imgSrc.lastIndexOf(".")) || Infinity) + 1);
var newImgSrc = imgName + "-dark." + imgExt
img.src = newImgSrc;
img.onerror = function() {
this.onerror = null;
this.src = imgSrc;
}
if (theme === 'light') {
img.src = imgSrc.replace(/-dark./gi, ".")
}
else {
if (!imgSrc.includes("-dark.")) {
var imgName = imgSrc.replace(/\.[^/.]+$/, "");
var imgExt = imgSrc.slice((Math.max(0, imgSrc.lastIndexOf(".")) || Infinity) + 1);
var newImgSrc = imgName + "-dark." + imgExt
img.src = newImgSrc;
img.onerror = function() {
this.onerror = null;
this.src = imgSrc;
this.classList.add('dark-filter')
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions source/stylesheets/dark.scss
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ $color-mode-type: data;
// are soley for a dark mode

// Darken any images and desaturate
.img {
filter: brightness(80%) saturate(80%);
img.dark-filter {
filter: invert(1) hue-rotate(180deg) brightness(90%) saturate(90%);
}

// Convert any logo to off white
.large-logo > img {
filter: brightness(0) saturate(100%) invert(100%) sepia(0%) saturate(0%) hue-rotate(52deg) brightness(105%) contrast(101%);
Expand Down