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

refactor: customize code block line highlight color via CSS var #7176

Merged
merged 6 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ export default function CodeBlock({
});

if (highlightLines.includes(i)) {
lineProps.className += ' docusaurus-highlight-code-line';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we should keep this stable class, eg to make it easier adding custom styling.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should. In #7178 I'll allow any type of magic comments to be mapped to custom class names, so I think solely for consistency we should keep this class name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think that makes sense 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed docusaurus-highlight-code-line to theme-code-block-highlighted-line to be more consistent with the current approach.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, while the rename is fine, adding it to ThemeClassNames is unnecessary because in #7178 it will be injected from theme config...

We might change that API later though, so just keep it like this now.

lineProps.className = clsx(
lineProps.className,
styles.highlightedCodeLine,
);
}

const lineTokens = line.map((token, key) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
* LICENSE file in the root directory of this source tree.
*/

/* Intentionally has zero specificity, so that to be able to override
the background in custom CSS file due bug https://github.com/facebook/docusaurus/issues/3678 */
:where(:root) {
--docusaurus-highlighted-code-line-bg: rgb(72 77 91);
}

:where([data-theme='dark']) {
--docusaurus-highlighted-code-line-bg: rgb(100 100 100);
}

.codeBlockContainer {
background: var(--prism-background-color);
color: var(--prism-color);
Expand Down Expand Up @@ -52,6 +62,13 @@
padding: var(--ifm-pre-padding);
}

.highlightedCodeLine {
background-color: var(--docusaurus-highlighted-code-line-bg);
display: block;
margin: 0 calc(-1 * var(--ifm-pre-padding));
padding: 0 var(--ifm-pre-padding);
}

.codeBlockLinesWithNumbering {
display: table;
padding: var(--ifm-pre-padding) 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,20 +198,17 @@ Supported commenting syntax:

We will do our best to infer which set of comment styles to use based on the language, and default to allowing _all_ comment styles. If there's a comment style that is not currently supported, we are open to adding them! Pull requests welcome.

To accomplish this, Docusaurus adds the `docusaurus-highlight-code-line` class to the highlighted lines. You will need to define your own styling for this CSS, possibly in your `src/css/custom.css` with a custom background color which is dependent on your selected syntax highlighting theme. The color given below works for the default highlighting theme (Palenight), so if you are using another theme, you will have to tweak the color accordingly.
You can also set your own background color for highlighted code line in your `src/css/custom.css` which will better fit to your selected syntax highlighting theme. The color given below works for the default highlighting theme (Palenight), so if you are using another theme, you will have to tweak the color accordingly.

```css title="/src/css/custom.css"
.docusaurus-highlight-code-line {
background-color: rgb(72, 77, 91);
display: block;
margin: 0 calc(-1 * var(--ifm-pre-padding));
padding: 0 var(--ifm-pre-padding);
:root {
--docusaurus-highlighted-code-line-bg: rgb(72, 77, 91);
}

/* If you have a different syntax highlighting theme for dark mode. */
[data-theme='dark'] .docusaurus-highlight-code-line {
[data-theme='dark'] {
/* Color which works with dark mode syntax highlighting theme */
background-color: rgb(100, 100, 100);
--docusaurus-highlighted-code-line-bg: rgb(100, 100, 100);
}
```

Expand Down
13 changes: 2 additions & 11 deletions website/src/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
--site-color-svg-icon-favorite: #e9669e;
--site-color-checkbox-checked-bg: hsl(167deg 56% 73% / 25%);
--site-color-feedback-background: #fff;
--docusaurus-highlighted-code-line-bg: rgb(0 0 0 / 10%);
}

html[data-theme='dark'] {
--site-color-feedback-background: #f0f8ff;
--site-color-favorite-background: #1d1e1e;
--site-color-checkbox-checked-bg: hsl(167deg 56% 73% / 10%);
--docusaurus-highlighted-code-line-bg: rgb(66 66 66 / 30%);
}

[data-theme='light'] {
Expand Down Expand Up @@ -56,17 +58,6 @@ html[data-theme='dark'] {
);
}

.docusaurus-highlight-code-line {
background-color: rgb(0 0 0 / 10%);
display: block;
margin: 0 calc(-1 * var(--ifm-pre-padding));
padding: 0 var(--ifm-pre-padding);
}

[data-theme='dark'] .docusaurus-highlight-code-line {
background-color: rgb(66 66 66 / 30%);
}

.header-github-link:hover {
opacity: 0.6;
}
Expand Down