-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix dark mode styling for code blocks in Book pages #2122
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
base: gh-pages
Are you sure you want to change the base?
Conversation
dscho
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for taking this on!
The form, however, needs a bit of TLC. The changes really need explanations, and there are none.
If you used AI to help you implement these changes, that's fine, but you have to understand those changes (in particular when they look both too narrow and broad almost at the same time) and be able to explain the rationale behind them, preferably in the commit message.
assets/sass/book.scss
Outdated
| margin-right: 0; | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not put this into dark-mode.scss just like everything else concerned with the dark mode?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic belongs into dark-mode.scss. I initially placed it in book.scss while iterating on the fix, but I agree that following the existing dark-mode structure is the correct approach.
assets/sass/book.scss
Outdated
| Fix: Dark mode styling for code blocks in Book pages | ||
| -------------------------------------------------- */ | ||
|
|
||
| html[data-theme="dark"] .edition2 .book code { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why .edition2?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The .edition2 selector was added to match the specificity of the existing .edition2 code { background: #eee; } rule, which was overriding dark-mode styles on Book pages. This was confirmed via DevTools. However, I agree that the override should mirror the light-mode scope instead of introducing a new one. I will adjust the selectors accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, there is an .edition2 block. But it is in book2.scss, not in book.scss (and therefore the latter is doubly incorrect a location for these changes).
This issue convinces me that the changes in this PR need to be made more carefully, i.e. with a more thorough approach as to what to override and where.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This issue convinces me that the changes in this PR need to be made more carefully, i.e. with a more thorough approach as to what to override and where.
For example, I bet that you did not even see https://github.com/Subhajit-das21/git-scm.com/blob/424c22d46385f5b426cc444f31d829c3c655e90f/assets/sass/book2.scss#L614-L691 (which needs to be the location where to change this, using CSS variables instead of hard-coded values, and overriding those variables in dark mode).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW if you want to fix the <code> colors, this would be the correct location to fix the dark-mode colors for <code> sections in the Book: again, by replacing the hard-coded colors with CSS variables (with those variables having different values depending on light/dark mode).
assets/sass/book.scss
Outdated
|
|
||
| html[data-theme="dark"] .edition2 .book code { | ||
| background-color: var(--color-canvas-subtle); | ||
| color: var(--color-fg-default); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why were those colors chosen? Which CSS do they override (it sure isn't .book code...)? Wouldn't it be better to use the same scope as light-mode for the override?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The colors were chosen from existing design tokens already used on Docs pages and in other dark-mode contexts. No new colors were introduced. I will clarify this and ensure the override aligns with existing patterns
assets/sass/book.scss
Outdated
| padding: 0.2em 0.4em; | ||
| border-radius: 4px; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the padding and border radius in need of overriding in dark mode?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right — these do not need to be overridden specifically for dark mode. I will remove those overrides and rely on the existing light-mode styles.
assets/sass/book.scss
Outdated
| background-color: var(--color-canvas-inset); | ||
| color: var(--color-fg-default); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same question as before about these color overrides: Which is the original CSS (in particular the original scope) that is overridden here? Shouldn't the same scope be used here, in particular skipping the questionable .edition2?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, and why is it a different background color for code and pre?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inline and block code are treated differently elsewhere (e.g. Docs and man-pages). I will rework this to follow the same approach as in commit f3771e3 for man-pages.
assets/sass/book.scss
Outdated
| background: none; | ||
| padding: 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do these have to be set specifically for dark mode?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They actually do not need to be set specifically for dark mode.
These overrides were added while iterating on the fix to avoid double backgrounds when testing, but on second thought they are unnecessary: the existing light-mode rules already handle padding and background
correctly for pre code .
I agree that dark mode should not override layout-related properties like padding or background resets here, and I will remove these lines and rely on the existing styles instead.
|
please have a look at f3771e3 for how this was dealt with for man-pages |
|
Thanks for the clarification — I’ve reworked the change accordingly. The hard-coded code/pre colors in Please let me know if this matches what you had in mind. |
assets/sass/dark-mode.scss
Outdated
| @include mode | ||
| @include mode($mode: dark, $theme: '[data-theme="dark"]') | ||
| @include mode; | ||
| @include mode($mode: dark, $theme: '[data-theme="dark"]'); | ||
|
|
||
| @media screen and (prefers-color-scheme: dark) { | ||
| @include mode($mode: dark, $theme: ':not([data-theme="light"])') | ||
| @include mode($mode: light) | ||
| @include mode($mode: dark, $theme: ':not([data-theme="light"])'); | ||
| @include mode($mode: light); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes unrelated to what you intended to change reminds me very much of the anti-pattern that is so indicative of AI-assisted (and not self-reviewed) PRs. Not a fan.
dscho
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The essential part of this PR now looks good to me. To turn this into something I would be eager to merge, the following still would need to be done:
- Instead of keeping commits with incorrect changes, this PR's commits should be squashed into a single one.
- This commit would need to have a message that identifies the problem, talks about the missing parts (think of all the
code.<some-letters>color definitions I pointed out in this PR, and why they are not touched), and explains what other approach was considered (and rejected, and why). - The commit would need to drop all unwanted changes.
- The PR would need a "before/after" screenshot comparison.
assets/sass/book.scss
Outdated
| margin-right: 0; | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's another unintended change. It is always a good sign when contributors catch those problems before asking others to review their contributions.
assets/sass/dark-mode.scss
Outdated
| #documentation #main div.verseblock pre.content { | ||
| color: var(--light-font-color); | ||
| background-color: #5e5951; | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is another left-over from an unwanted change that is first introduced in one commit, only to be (incompletely) reverted in another commit in the same PR. If anyone wants to learn from the commit history how to do things correctly (including AI assistants), this would mislead them.
Fix Book code styling in dark mode by replacing hard-coded colors with variables Book pages used hard-coded colors for inline and block code (e.g. `#eee`, `git#333`) in `book2.scss`, which prevented dark-mode styles from taking effect. As a result, code blocks in the Git Book did not adapt correctly when switching themes. This change replaces those hard-coded values with CSS variables (`--book-code-bg`, `--book-code-color`, `--book-code-border`), allowing dark mode to override Book code styling in the same way as other sections (e.g. man-pages). Not all `code.<language>` color rules are adjusted here, as they relate to syntax highlighting rather than theme contrast and require a separate, more holistic approach. An alternative approach—overriding Book code styles directly in `dark-mode.scss` using more specific selectors—was considered but rejected, as it would further increase selector complexity instead of fixing the root cause. Fixes git#2120
66a00e7 to
8325191
Compare
|
Would it be hard to show a real "after" screenshot? Like one that isn't just one single code span? |
This corresponds to git#2122 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
I don't think that it would be a good idea to leave this for later. It's probably a much better idea to first determine whether these definitions are actually used. To that end, downloading the build artifact from the latest deployment, extracting it, and then performing an analysis (e.g. via If they aren't even used, add a commit to delete them, with a thorough description of the analysis and its results. However, if they are still used, generate a small page that has one element per style, and work on overriding the colors in dark mode with appropriate values. Personally, I'd probably upload screenshots of light/dark mode versions of some small manual page to Copilot and then ask it to generate appropriate dark mode versions of those colors.
That's a very good suggestion. I say this because currently, this PR branch does not do what we want (see https://dscho.github.io/git-scm.com/book/en/v2/Git-on-the-Server-GitWeb):
I think the culprit is that the dark-mode override does need the diff --git a/assets/sass/dark-mode.scss b/assets/sass/dark-mode.scss
index 6e226ca6e..b911ed3a2 100644
--- a/assets/sass/dark-mode.scss
+++ b/assets/sass/dark-mode.scss
@@ -84,9 +84,11 @@
}
/* Book pages: code colors */
- --book-code-color: #{$fixed-width-font-color};
- --book-code-bg: #{$no-changes-bg-color};
- --book-code-border: #{$pre-border};
+ .edition2 {
+ --book-code-color: #{$fixed-width-font-color};
+ --book-code-bg: #{$no-changes-bg-color};
+ --book-code-border: #{$pre-border};
+ }
img {
filter: brightness(.6) contrast(1.2); |
This corresponds to git#2122 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This branch was developed to generate before/after comparisons a là git#2122 (comment) Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>







Book pages were missing proper dark-mode styling for inline and block
code elements due to existing
.edition2 coderules overriding thetheme styles.
This change adds scoped dark-mode styles for
.edition2 .bookcodeand
preelements using existing design tokens, aligning Book pageswith Docs styling and restoring proper contrast in dark mode.
Fixes #2120