Skip to content

Commit 9f7ec4f

Browse files
authored
Fix disqus background in dark mode. (luizdepra#732)
### Prerequisites - [x] This pull request fixes a bug. - [x] This pull request adds a feature. - [ ] This pull request introduces breaking change. ### Description This PR fixes the bug about disqus iframe background in dark mode, it also fix another bug when switching color mode. Before: <img width="881" alt="opaque background in dark mode" src="https://user-images.githubusercontent.com/17041194/202862887-720dbb82-f0c8-4a24-97fd-78baef2a7cb4.png"> After: <img width="883" alt="CleanShot 2022-11-20 at 01 12 52@2x" src="https://user-images.githubusercontent.com/17041194/202863146-d5a246a7-bbe5-4bef-9687-883ce33f5b1a.png"> #### Why this happens The problem is widely discussed [here](w3c/csswg-drafts#4772). >If the color scheme of an iframe differs from embedding document, iframe gets an opaque canvas bg appropriate to its color scheme. So I made two fixes: 1. Add `color-scheme: light` CSS property for disqus `iframe`, this creates a transparent background and fixes the opaque issue. 2. Add `themeChanged` event to notice disqus comments board refresh when the theme change, this fixes the opaque problem when you switch the mode. #### Reference I've been working on this issue for a whole night and the fixing idea was strongly inspired by [Sergeyski's Blog](https://sergeyski.com/css-color-scheme-and-iframes-lessons-learned-from-disqus-background-bug/). ### Issues Resolved Fixes luizdepra#722.
1 parent 5c4502a commit 9f7ec4f

File tree

8 files changed

+40
-3
lines changed

8 files changed

+40
-3
lines changed

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,4 @@
118118
- [Daniel Nduati](https://github.com/DanNduati)
119119
- [Simon Hollingshead](https://github.com/simonhollingshead)
120120
- [yangyangdaji](https://github.com/yangyangdaji)
121+
- [xiaotianxt](https://github.com/xiaotianxt)

assets/js/coder.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ function setTheme(theme) {
7676

7777
}
7878

79+
// Create and send event
80+
const event = new Event('themeChanged');
81+
document.dispatchEvent(event);
7982
}
8083

8184
function rememberTheme(theme) {

assets/scss/_base.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ body {
2323
}
2424
}
2525

26+
iframe[src*=disqus] {
27+
color-scheme: light;
28+
}
29+
2630
a {
2731
font-weight: 500;
2832
color: $link-color;

exampleSite/resources/_gen/assets/scss/scss/coder.scss_fd4b5b3f9a48bc0c7f005d2f7a4cc30f.content

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3050,6 +3050,9 @@ body {
30503050
body {
30513051
font-size: 1.6em;
30523052
line-height: 1.6em; } }
3053+
iframe[src*=disqus] {
3054+
color-scheme: light; }
3055+
30533056
a {
30543057
font-weight: 500;
30553058
color: #1565c0;

layouts/partials/posts/disqus.html

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
11
{{- if and (not (eq (.Site.DisqusShortname | default "") "")) (eq (.Params.disableComments | default false) false) -}}
2-
{{ template "_internal/disqus.html" . }}
2+
<div id="disqus_thread"></div>
3+
<script>
4+
window.disqus_config = function () {
5+
{{with .Params.disqus_identifier }}this.page.identifier = '{{ . }}';{{end}}
6+
{{with .Params.disqus_title }}this.page.title = '{{ . }}';{{end}}
7+
{{with .Params.disqus_url }}this.page.url = '{{ . | html }}';{{end}}
8+
};
9+
(function() {
10+
if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
11+
document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
12+
return;
13+
}
14+
var d = document, s = d.createElement('script'); s.async = true;
15+
s.src = '//' + {{ .Site.DisqusShortname }} + '.disqus.com/embed.js';
16+
s.setAttribute('data-timestamp', +new Date());
17+
(d.head || d.body).appendChild(s);
18+
})();
19+
// Disqus theme switching
20+
document.addEventListener('themeChanged', function (e) {
21+
if (document.readyState == 'complete') {
22+
DISQUS.reset({ reload: true, config: disqus_config });
23+
}
24+
});
25+
</script>
326
{{- end -}}

resources/_gen/assets/scss/scss/coder.scss_5e1eb8e37c42cdfb6215b61e44dcfa5f.content

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"Target":"css/coder.min.c4d7e93a158eda5a65b3df343745d2092a0a1e2170feeec909b8a89443903c6a.css","MediaType":"text/css","Data":{"Integrity":"sha256-xNfpOhWO2lpls980N0XSCSoKHiFw/u7JCbiolEOQPGo="}}
1+
{"Target":"css/coder.min.7f9d180e3b3bebba9ba80d55eed1255c35e00764872854736d6ad7db38884ffc.css","MediaType":"text/css","Data":{"Integrity":"sha256-f50YDjs767qbqA1V7tElXDXgB2SHKFRzbWrX2ziIT/w="}}

resources/_gen/assets/scss/scss/coder.scss_fd4b5b3f9a48bc0c7f005d2f7a4cc30f.content

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3050,6 +3050,9 @@ body {
30503050
body {
30513051
font-size: 1.6em;
30523052
line-height: 1.6em; } }
3053+
iframe[src*=disqus] {
3054+
color-scheme: light; }
3055+
30533056
a {
30543057
font-weight: 500;
30553058
color: #1565c0;

0 commit comments

Comments
 (0)