Skip to content

Commit a95ff63

Browse files
committed
Fix: Line highlight with no line numbers offset
1 parent 8ce856f commit a95ff63

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

.changeset/selfish-apes-breathe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"@hashicorp/design-system-components": patch
33
---
44

5-
`CodeBlock` - Fixed issues with line numbers when line wrapping is present and when the number of lines changes dynamically. Also fixed line highlighting when Code Block is hidden from view initially such as when used inside a Tabs component.
5+
`CodeBlock` - Fixed issues with line numbers when line wrapping is present and when the number of lines changes dynamically; line highlighting when the Code Block is hidden from view initially such as when used inside a Tabs component; and line highlighting when hasLineNumbers is false.

packages/components/src/styles/components/code-block/index.scss

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ $hds-code-block-code-footer-height: 48px;
147147
}
148148

149149
code {
150+
position: relative;
150151
display: inline-block;
151152
padding-right: $hds-code-block-code-padding;
152153
}
@@ -220,6 +221,13 @@ $hds-code-block-code-footer-height: 48px;
220221
position: relative;
221222
// reserve space for line numbers
222223
padding-left: calc(#{$hds-code-block-line-numbers-width} + #{$hds-code-block-code-padding});
224+
225+
// When line numbers are enabled, line highlighing is calculated based on the pre element instead of the code element
226+
// To ensure the offset is correct, we need to set the position of the code element to static
227+
// Source: https://github.com/PrismJS/prism/blob/v2/src/plugins/line-highlight/prism-line-highlight.ts#L92
228+
code {
229+
position: static;
230+
}
223231
}
224232

225233
.hds-code-block__overlay-footer {
@@ -250,15 +258,17 @@ $hds-code-block-code-footer-height: 48px;
250258
}
251259
}
252260
}
261+
262+
.line-highlight {
263+
left: 0;
264+
}
253265
}
254266

255267
// Highlighted Lines
256268
.line-highlight {
257269
position: absolute;
258270
right: 0;
259-
left: 0;
260-
// Note: position seems off by a few px although not sure why
261-
margin-top: -3px;
271+
left: -$hds-code-block-code-padding;
262272
background-color: var(--hds-code-block-color-line-highlight);
263273
border: solid var(--hds-code-block-color-line-highlight-border);
264274
border-width: 1px 0 1px 4px;

0 commit comments

Comments
 (0)