Skip to content

Commit 68c8a1b

Browse files
authored
Merge pull request RedHatOfficial#385 from RedHatOfficial/fix/system-pref-dark-mode
fix: system preference in dark mode error
2 parents adbbca3 + f4dcdc5 commit 68c8a1b

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

static/js/rht-picture.js

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,52 @@
11
document.addEventListener('DOMContentLoaded', function() {
2-
const body = document.querySelector('body');
2+
const body = document.body;
33
const pictureEls = document.querySelectorAll('rht-picture');
44

5+
let prefersDark = false;
6+
const colorScheme = localStorage.getItem('rhdsColorScheme');
7+
8+
if (!colorScheme || colorScheme === 'light dark') {
9+
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
10+
prefersDark = true;
11+
pictureEls.forEach((art) => {
12+
art.classList.add('dark');
13+
});
14+
}
15+
}
16+
17+
if (colorScheme === 'dark') {
18+
pictureEls.forEach((art) => {
19+
art.classList.add('dark');
20+
});
21+
} else if (colorScheme === 'light') {
22+
pictureEls.forEach((art) => {
23+
art.classList.remove('dark');
24+
});
25+
}
26+
527
var observer = new MutationObserver(function(mutations) {
628
mutations.forEach(function(mutation) {
7-
if (mutation.type === "attributes") {
8-
let styleAttr = mutation.target.getAttribute('style');
29+
if (mutation.type === 'attributes' && mutation.attributeName === 'style') {
30+
const styleAttr = body.getAttribute('style');
931

10-
if (styleAttr.includes('color-scheme: dark')) {
32+
if (styleAttr === 'color-scheme: dark;') {
1133
pictureEls.forEach((art) => {
1234
art.classList.add('dark');
1335
});
14-
} else {
36+
} else if (styleAttr === 'color-scheme: light;') {
1537
pictureEls.forEach((art) => {
1638
art.classList.remove('dark');
1739
});
40+
} else {
41+
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
42+
pictureEls.forEach((art) => {
43+
art.classList.add('dark');
44+
});
45+
} else {
46+
pictureEls.forEach((art) => {
47+
art.classList.remove('dark');
48+
});
49+
}
1850
}
1951
}
2052
});

static/styles/main.css

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
@layer base {
1919
body {
2020
color: var(--rh-color-text-primary);
21+
color-scheme: light dark;
2122
}
2223

2324
a {
@@ -349,16 +350,6 @@
349350
display: none;
350351
}
351352
}
352-
353-
@media (prefers-color-scheme: dark) {
354-
.img-on-dark {
355-
display: inline;
356-
}
357-
358-
.img-on-light {
359-
display: none;
360-
}
361-
}
362353
}
363354

364355
rht-text-input {

0 commit comments

Comments
 (0)