Skip to content

Commit f36261d

Browse files
authored
Merge d414d01 into 37cc019
2 parents 37cc019 + d414d01 commit f36261d

File tree

7 files changed

+409
-169
lines changed

7 files changed

+409
-169
lines changed

.changeset/two-impalas-speak.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hashicorp/react-code-block': minor
3+
---
4+
5+
Implements support for code wrapping, through an options.wrapCode boolean property.

packages/code-block/docs.mdx

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ Longer lines of code may take up more space than the available content width. In
3434
````
3535
```
3636
A line that goes on for a very long time so that it overflows the container in which it is located, which might be a pretty wide container.
37+
This is a second line of code.
38+
And a third line.
39+
And another line, this is the fourth line.
3740
```
3841
````
3942

@@ -42,7 +45,54 @@ A line that goes on for a very long time so that it overflows the container in w
4245
<CodeBlock
4346
options={{ showClipboard: true }}
4447
theme="dark"
45-
code={`A line that goes on for a very long time so that it overflows the container in which it is located, which might be a pretty wide container.`}
48+
code={`A line that goes on for a very long time so that it overflows the container in which it is located, which might be a pretty wide container.\nThis is a second line of code.\nAnd a third line.\nAnd another line, this is the fourth line.`}
49+
/>
50+
51+
Note this also works with line numbers and line highlighting.
52+
53+
<CodeBlock
54+
options={{ showClipboard: true, lineNumbers: true, highlight: '1,3' }}
55+
theme="dark"
56+
code={`A line that goes on for a very long time so that it overflows the container in which it is located, which might be a pretty wide container.\nThis is a second line of code.\nAnd a third line.\nAnd another line, this is the fourth line.`}
57+
/>
58+
59+
#### Wrap Code
60+
61+
In cases where wrapping code to new lines is preferred over horizontal scrolling, the `options.wrapCode` prop can be set to `true`. Note that this option is not yet supported in MDX contexts.
62+
63+
`Source`
64+
65+
````
66+
```
67+
A line that goes on for a very long time so that it overflows the container in which it is located, which might be a pretty wide container.
68+
This is a second line of code.
69+
And a third line.
70+
And another line, this is the fourth line.
71+
```
72+
````
73+
74+
`Result`
75+
76+
<CodeBlock
77+
options={{
78+
showClipboard: true,
79+
wrapCode: true,
80+
}}
81+
theme="dark"
82+
code={`A line that goes on for a very long time so that it overflows the container in which it is located, which might be a pretty wide container.\nThis is a second line of code.\nAnd a third line.\nAnd another line, this is the fourth line.`}
83+
/>
84+
85+
Note this also works with line numbers and line highlighting.
86+
87+
<CodeBlock
88+
options={{
89+
showClipboard: true,
90+
lineNumbers: true,
91+
wrapCode: true,
92+
highlight: '1,3',
93+
}}
94+
theme="dark"
95+
code={`A line that goes on for a very long time so that it overflows the container in which it is located, which might be a pretty wide container.\nThis is a second line of code.\nAnd a third line.\nAnd another line, this is the fourth line.`}
4696
/>
4797

4898
#### Syntax Highlighting
@@ -219,7 +269,7 @@ function hello() {
219269
options={{ lineNumbers: true, showClipboard: true }}
220270
code={`<span class="token keyword">const</span> foo <span class="token operator">=</span> <span class="token string">'bar'</span>
221271
<span class="token keyword">function</span> <span class="token function">hello</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
222-
{/* */} <span class="token keyword control-flow">return</span> <span class="token known-class-name class-name">Math</span><span class="token punctuation">.</span><span class="token method function property-access">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">0.5</span> <span class="token operator">?</span> <span class="token string">'Hello'</span> <span class="token operator">:</span> <span class="token string">'Bonjour'</span>
272+
<span class="token keyword control-flow">return</span> <span class="token known-class-name class-name">Math</span><span class="token punctuation">.</span><span class="token method function property-access">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">0.5</span> <span class="token operator">?</span> <span class="token string">'Hello'</span> <span class="token operator">:</span> <span class="token string">'Bonjour'</span>
223273
<span class="token punctuation">}</span>`}
224274
/>
225275

packages/code-block/index.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ import s from './style.module.css'
2121

2222
export interface CodeBlockOptions {
2323
showChrome?: boolean
24-
highlight?: boolean
24+
highlight?: string
2525
lineNumbers?: boolean
2626
showClipboard?: boolean
2727
showWindowBar?: boolean
2828
filename?: string
2929
heading?: string
30+
wrapCode?: boolean
3031
}
3132

3233
export interface CodeBlockProps {
@@ -48,10 +49,10 @@ function CodeBlock({
4849
onCopyCallBack,
4950
options = {
5051
showChrome: false,
51-
highlight: false,
5252
lineNumbers: false,
5353
showClipboard: false,
5454
showWindowBar: false,
55+
wrapCode: false,
5556
},
5657
}: CodeBlockProps) {
5758
const copyRef = useRef<HTMLPreElement>()
@@ -76,6 +77,7 @@ function CodeBlock({
7677
lineNumbers,
7778
showClipboard,
7879
showWindowBar,
80+
wrapCode,
7981
} = options
8082
if (showWindowBar) {
8183
console.warn(
@@ -119,6 +121,7 @@ function CodeBlock({
119121
highlight={highlight}
120122
lineNumbers={lineNumbers}
121123
hasFloatingCopyButton={hasFloatingCopyButton}
124+
wrapCode={wrapCode}
122125
/>
123126
{hasFloatingCopyButton ? (
124127
<div className={s.copyButtonContainer}>

packages/code-block/partials/code-lines/style.module.css renamed to packages/code-block/partials/code-lines/code-lines.module.css

Lines changed: 102 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ code-block/theme-(dark|light).module.css
99
to be present.
1010
*/
1111

12+
/*
13+
14+
SHARED
15+
16+
*/
17+
1218
pre.pre {
1319
--code-padding: 16px;
1420
--code-font-size: 0.84375rem; /* 13.5 px at default text size */
@@ -32,6 +38,83 @@ pre.pre {
3238
overflow: hidden;
3339
}
3440

41+
.LineNumber {
42+
composes: g-type-code from global;
43+
display: block;
44+
font-size: var(--code-font-size);
45+
line-height: var(--code-line-height);
46+
white-space: pre;
47+
padding: 0 var(--code-padding);
48+
color: var(--text-color-faded);
49+
50+
&.wrap {
51+
border-right: 1px solid var(--divider-line-color);
52+
}
53+
54+
&.highlight {
55+
background: var(--background-highlighted-line);
56+
color: var(--text-color-base);
57+
}
58+
59+
&.dim > * {
60+
color: var(--text-color-faded);
61+
}
62+
}
63+
64+
.LineOfCode {
65+
composes: g-type-code from global;
66+
display: block;
67+
color: var(--text-color-base);
68+
padding-right: calc(var(--code-padding) * 2);
69+
padding-left: var(--code-padding);
70+
font-size: var(--code-font-size);
71+
line-height: var(--code-line-height);
72+
min-width: max-content;
73+
white-space: pre;
74+
75+
&.wrap {
76+
white-space: pre-wrap;
77+
overflow-wrap: break-word;
78+
min-width: 0;
79+
width: 100%;
80+
}
81+
82+
&.padRight {
83+
/* Adds right padding so that the floating copy button
84+
does not obscure content at the end of the line */
85+
padding-right: 96px;
86+
}
87+
88+
&.highlight {
89+
background: var(--background-highlighted-line);
90+
}
91+
92+
&.dim {
93+
opacity: 0.7;
94+
95+
&,
96+
& * {
97+
/* !important is necessary here to override
98+
syntax highlight color styles, which are
99+
globally scoped by necessity */
100+
color: var(--text-color-faded) !important;
101+
}
102+
}
103+
}
104+
105+
/*
106+
107+
SCROLL OVERFLOW LAYOUT
108+
109+
*/
110+
111+
.numbersColumn {
112+
display: block;
113+
border-right: 1px solid var(--divider-line-color);
114+
flex-shrink: 0;
115+
padding: var(--code-padding) 0;
116+
}
117+
35118
.linesColumn {
36119
display: block;
37120
flex-grow: 1;
@@ -76,70 +159,36 @@ pre.pre {
76159
}
77160
}
78161

79-
.linesWrapper {
162+
.linesScrollableContent {
80163
display: flex;
81164
width: max-content;
82165
min-width: 100%;
83166
flex-direction: column;
84167
flex-shrink: 0;
85168
}
86169

87-
.numbersColumn {
88-
display: block;
89-
border-right: 1px solid var(--divider-line-color);
90-
flex-shrink: 0;
91-
padding: var(--code-padding) 0;
92-
}
170+
/*
93171
94-
.LineNumber {
95-
composes: g-type-code from global;
96-
display: block;
97-
font-size: var(--code-font-size);
98-
line-height: var(--code-line-height);
99-
white-space: pre;
100-
padding: 0 var(--code-padding);
101-
color: var(--text-color-faded);
172+
WRAP LAYOUT
102173
103-
&.isHighlighted {
104-
background: var(--background-highlighted-line);
105-
color: var(--text-color-base);
106-
}
174+
*/
107175

108-
&.isNotHighlighted > * {
109-
color: var(--text-color-faded);
110-
}
176+
.wrappedLinesContainer {
177+
display: flex;
178+
width: 100%;
179+
flex-direction: column;
180+
flex-shrink: 0;
111181
}
112182

113-
.LineOfCode {
114-
composes: g-type-code from global;
115-
display: block;
116-
color: var(--text-color-base);
117-
padding-right: calc(var(--code-padding) * 2);
118-
padding-left: var(--code-padding);
119-
font-size: var(--code-font-size);
120-
line-height: var(--code-line-height);
121-
min-width: max-content;
122-
white-space: pre;
123-
124-
&.hasFloatingCopyButton {
125-
/* Adds right padding so that the floating copy button
126-
does not obscure content at the end of the line */
127-
padding-right: 96px;
128-
}
129-
130-
&.isHighlighted {
131-
background: var(--background-highlighted-line);
132-
}
133-
134-
&.isNotHighlighted {
135-
opacity: 0.7;
183+
.wrappedLine {
184+
display: flex;
185+
flex-wrap: nowrap;
186+
}
136187

137-
&,
138-
& * {
139-
/* !important is necessary here to override
140-
syntax highlight color styles, which are
141-
globally scoped by necessity */
142-
color: var(--text-color-faded) !important;
143-
}
144-
}
188+
/* adds space at the top and bottom of the block,
189+
while supporting a continuous border for line numbers */
190+
.wrappedLinesSpacer {
191+
display: flex;
192+
flex-wrap: nowrap;
193+
height: var(--code-padding);
145194
}

0 commit comments

Comments
 (0)