1515// DIMENSIONS
1616$hds-code-block-line-numbers-width : 49px ; // 3em ≈ 49px
1717$hds-code-block-code-padding : 16px ;
18+ $hds-code-block-code-footer-height : 48px ;
1819
1920// CODE-BLOCK PARENT/WRAPPER
2021
@@ -35,7 +36,6 @@ $hds-code-block-code-padding: 16px;
3536 word-spacing : normal ;
3637 tab-size : 4 ;
3738 hyphens : none ;
38- scrollbar-width : thin ;
3939
4040 @media print {
4141 text-shadow : none ;
@@ -47,13 +47,15 @@ $hds-code-block-code-padding: 16px;
4747
4848// isStandalone
4949.hds-code-block--is-standalone {
50+ overflow : hidden ; // hide corners of scrollbar that poke out
5051 border-radius : var (--token-border-radius-medium );
5152}
5253
5354// hasLineWrapping
5455.hds-code-block--has-line-wrapping {
5556 .hds-code-block__code ,
5657 .hds-code-block__code code {
58+ display : block ;
5759 white-space : pre-wrap ;
5860 overflow-wrap : break-word ;
5961 }
@@ -128,10 +130,13 @@ $hds-code-block-code-padding: 16px;
128130// Code
129131.hds-code-block__code {
130132 @include hds-focus-ring-basic ();
131- display : block ;
133+ position : relative ;
134+ display : grid ;
132135 margin : 0 ;
133- padding : $hds-code-block-code-padding ;
136+ padding : $hds-code-block-code-padding 0 $hds-code-block-code-padding $hds-code-block-code-padding ;
134137 overflow : auto ;
138+ scrollbar-width : thin ;
139+ scrollbar-color : var (--token-color-palette-neutral-400 ) var (--token-color-palette-neutral-500 );
135140 font-size : 0.8125rem ;
136141 font-family : var (--token-typography-code-200-font-family );
137142 border-radius : inherit ;
@@ -140,32 +145,38 @@ $hds-code-block-code-padding: 16px;
140145 color : var (--hds-code-block-color-foreground-selection );
141146 background-color : var (--hds-code-block-color-surface-selection );
142147 }
148+
149+ code {
150+ display : inline-block ;
151+ padding-right : $hds-code-block-code-padding ;
152+ }
143153}
144154
145- // CopyButton
155+ // General dark button styles
156+ .hds-code-block__height-toggle-button ,
146157.hds-code-block__copy-button {
147- position : absolute ;
148- top : 11px ; // 12px -1px accounting for border
149- right : 12px ; // 12px -1px accounting for border
150158 // Overriding default colors
151159 color : var (--hds-code-block-color-foreground-primary );
152160 background-color : var (--hds-code-block-color-surface-faint );
153161 border : 1px solid var (--hds-code-block-color-border-strong );
154162
155163 & :hover ,
156164 & .mock-hover {
165+ color : var (--hds-code-block-color-foreground-primary );
157166 background-color : var (--hds-code-block-color-surface-primary );
158167 border-color : var (--hds-code-block-color-border-strong );
159168 }
160169
161170 & :active ,
162171 & .mock-active {
172+ color : var (--hds-code-block-color-foreground-primary );
163173 background-color : var (--hds-code-block-color-surface-interactive-active );
164174 }
165175
166176 & :focus ,
167177 & .mock-focus ,
168178 & :focus-visible {
179+ color : var (--hds-code-block-color-foreground-primary );
169180 background-color : var (--hds-code-block-color-surface-faint );
170181 border-color : var (--hds-code-block-color-focus-action-internal );
171182
@@ -174,6 +185,17 @@ $hds-code-block-code-padding: 16px;
174185 }
175186 }
176187
188+ .hds-icon {
189+ color : var (--hds-code-block-color-foreground-primary );
190+ }
191+ }
192+
193+ // CopyButton
194+ .hds-code-block__copy-button {
195+ position : absolute ;
196+ top : 11px ; // 12px -1px accounting for border
197+ right : 12px ;
198+
177199 & .hds-copy-button--status-success {
178200 .hds-icon {
179201 color : var (--hds-code-block-color-foreground-success );
@@ -185,12 +207,9 @@ $hds-code-block-code-padding: 16px;
185207 color : var (--hds-code-block-color-foreground-critical );
186208 }
187209 }
188-
189- .hds-icon {
190- color : var (--hds-code-block-color-foreground-primary );
191- }
192210}
193211
212+ // Prism.js plugins
194213.hds-code-block {
195214 // LineNumbers plugin styles ---------------
196215 // Note: Prism.js is using the specific class name "line-numbers" to determine implementation of line numbers in the UI
@@ -203,6 +222,11 @@ $hds-code-block-code-padding: 16px;
203222 padding-left : calc (#{$hds-code-block-line-numbers-width } + #{$hds-code-block-code-padding } );
204223 }
205224
225+ .hds-code-block__overlay-footer {
226+ // match horizontal padding of the code block
227+ margin-left : $hds-code-block-line-numbers-width ;
228+ }
229+
206230 .line-numbers-rows {
207231 position : absolute ;
208232 top : 0 ;
@@ -262,3 +286,64 @@ $hds-code-block-code-padding: 16px;
262286 font-style : italic ;
263287 }
264288}
289+
290+ // Footer
291+ .hds-code-block__overlay-footer {
292+ position : absolute ;
293+ right : 0 ;
294+ bottom : 0 ;
295+ left : 0 ;
296+ display : flex ;
297+ align-items : center ;
298+ justify-content : center ;
299+ padding : 10px $hds-code-block-code-padding 10px $hds-code-block-code-padding ;
300+ pointer-events : none ; // fix issue with scrolling when hovering over the footer
301+
302+ // re-enable pointer events for the button (or any content inside the footer)
303+ > * {
304+ pointer-events : auto ;
305+ }
306+ }
307+
308+ // Usage of sticky positioning, negative margins, and z-index are required to prevent styling issues
309+ // when horizontal scrolling is present.
310+ // https://hashicorp.slack.com/archives/C025N5V4PFZ/p1746659338984369
311+ .hds-code-block--has-overlay-footer {
312+ .hds-code-block__code ::after {
313+ // gradient element
314+ position : sticky ; // prevent gradient from scrolling together with content
315+ bottom : - $hds-code-block-code-padding ;
316+ left : 0 ;
317+ display : block ;
318+ height : $hds-code-block-code-footer-height ;
319+ margin : 0 0 - #{$hds-code-block-code-padding } -1000px ; // cover line highlights when line numbers are enabled
320+ background : linear-gradient (360deg , #0d0e12 37.07% , rgba (13 , 14 , 18 , 25% ) 100% );
321+ content : " " ;
322+ pointer-events : none ;
323+ }
324+
325+ .line-numbers .line-numbers-rows {
326+ padding-bottom : $hds-code-block-code-footer-height ;
327+ }
328+
329+ // place line numbers on top of footer gradient
330+ .line-numbers-rows {
331+ z-index : 1 ;
332+ isolation : isolate ;
333+ }
334+ }
335+
336+ .hds-code-block--is-expanded {
337+ .hds-code-block__code {
338+ // account for the footer at the bottom of the code block
339+ padding-bottom : $hds-code-block-code-footer-height ;
340+
341+ & ::after {
342+ content : none ;
343+ }
344+ }
345+
346+ .hds-code-block__overlay-footer {
347+ background : none ;
348+ }
349+ }
0 commit comments