Skip to content

Commit 79eddcc

Browse files
Kirill BolotskyPepe Cano
authored andcommitted
feat(components): added ability to expand/collapse large code blocks
Side: - updated contribution docs accordingly
1 parent ebeaf11 commit 79eddcc

File tree

6 files changed

+99
-6
lines changed

6 files changed

+99
-6
lines changed

CONTRIBUTING_FILE_FORMAT.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,31 @@ See _'Then do this?'_ text line between tabs? **You can not do that**. Put nothi
324324

325325
```
326326

327+
</CodeGroup>
328+
329+
#### State control
330+
331+
Large code blocks (>400px) by default have a collapsed state, i.e. have a max-height prop of `400px` and user can change their behavior via toggler:
332+
333+
![Code Block States](internal-images/code-block-states.png)
334+
335+
If you are using `CodeGroup` wrapper you can specify the initial state of code blocks via `isInitiallyExpanded` prop which works very similarly to the other available props:
336+
337+
<CodeGroup labels={["Nice code!", "This one is better", "Oh my.."]} lineNumbers={[true, true, true]} isInitiallyExpanded=[true, false, false]>
338+
339+
```javascript
340+
// a lot of lines of important code
341+
```
342+
343+
```javascript
344+
// a lot of lines of not so important code
345+
```
346+
347+
```javascript
348+
// a lot of lines of not so important code
349+
```
350+
351+
327352
</CodeGroup>
328353

329354
## Table data
289 KB
Loading

src/components/shared/code/code.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,37 @@ import classNames from 'classnames';
22
import { WithCopyButton } from 'components/shared/with-copy-button';
33
import Highlight, { defaultProps } from 'prism-react-renderer';
44
import PropTypes from 'prop-types';
5-
import React from 'react';
5+
import React, { useEffect, useState, useRef } from 'react';
66

77
import styles from './code.module.scss';
88

9-
const Code = ({ children, showLineNumbers }) => {
9+
const Code = ({ children, showLineNumbers, isInitiallyExpanded }) => {
1010
if (!children) return null;
11+
const [isExpanded, setIsExpanded] = useState(true);
12+
const [height, setHeight] = useState('100%');
13+
const containerRef = useRef(null);
14+
const toggleHandler = () => setIsExpanded((prev) => !prev);
15+
16+
useEffect(() => {
17+
if (containerRef?.current) {
18+
const computedHeight = containerRef.current.offsetHeight;
19+
if (computedHeight > 420) {
20+
setHeight(computedHeight);
21+
}
22+
}
23+
if (!isInitiallyExpanded) {
24+
setIsExpanded(false);
25+
}
26+
}, [containerRef]);
27+
28+
let toggler = null;
29+
if (height !== '100%') {
30+
toggler = (
31+
<button className={styles.toggler} type="button" onClick={toggleHandler}>
32+
{isExpanded ? 'Collapse' : 'Expand'}
33+
</button>
34+
);
35+
}
1136

1237
return (
1338
<WithCopyButton dataToCopy={children.props.children}>
@@ -19,6 +44,11 @@ const Code = ({ children, showLineNumbers }) => {
1944
{({ className, tokens, getLineProps, getTokenProps }) => (
2045
<pre
2146
className={classNames(className, styles.code, styles.codeContainer)}
47+
ref={containerRef}
48+
style={{
49+
transition: 'max-height .2s ease',
50+
maxHeight: isExpanded ? height : '400px',
51+
}}
2252
>
2353
<code className={styles.code}>
2454
{tokens.map((line, i) => {
@@ -43,18 +73,21 @@ const Code = ({ children, showLineNumbers }) => {
4373
</pre>
4474
)}
4575
</Highlight>
76+
{toggler}
4677
</WithCopyButton>
4778
);
4879
};
4980

5081
Code.propTypes = {
5182
children: PropTypes.node,
5283
showLineNumbers: PropTypes.bool,
84+
isInitiallyExpanded: PropTypes.bool,
5385
};
5486

5587
Code.defaultProps = {
5688
children: null,
5789
showLineNumbers: false,
90+
isInitiallyExpanded: false,
5891
};
5992

6093
export default Code;

src/components/shared/code/code.module.scss

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@
1818
display: table-cell;
1919
}
2020

21+
.toggler {
22+
position: absolute;
23+
bottom: 10px;
24+
right: 10px;
25+
padding: 3px 5px 2px 6px;
26+
background-color: $color-secondary;
27+
border-radius: 2px;
28+
height: 20px;
29+
border: none;
30+
outline: none;
31+
color: $color-tertiary;
32+
font-weight: 600;
33+
line-height: initial;
34+
font-size: 10px;
35+
color: $color-tertiary;
36+
text-transform: uppercase;
37+
cursor: pointer;
38+
}
39+
2140
.code-container {
2241
:global {
2342
pre {

src/components/shared/with-copy-button/with-copy-button.module.scss

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,4 @@
1919
color: $color-tertiary;
2020
text-transform: uppercase;
2121
cursor: pointer;
22-
@include sm-down {
23-
top: unset;
24-
bottom: 10px;
25-
}
2622
}

src/data/markdown/docs/01 guides/01 Getting started/02 Installation.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,26 @@ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 379CE192D
1111
echo "deb https://dl.bintray.com/loadimpact/deb stable main" | sudo tee -a /etc/apt/sources.list
1212
sudo apt-get update
1313
sudo apt-get install k6
14+
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 379CE192D401AB61
15+
echo "deb https://dl.bintray.com/loadimpact/deb stable main" | sudo tee -a /etc/apt/sources.list
16+
sudo apt-get update
17+
sudo apt-get install k6
18+
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 379CE192D401AB61
19+
echo "deb https://dl.bintray.com/loadimpact/deb stable main" | sudo tee -a /etc/apt/sources.list
20+
sudo apt-get update
21+
sudo apt-get install k6
22+
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 379CE192D401AB61
23+
echo "deb https://dl.bintray.com/loadimpact/deb stable main" | sudo tee -a /etc/apt/sources.list
24+
sudo apt-get update
25+
sudo apt-get install k6
26+
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 379CE192D401AB61
27+
echo "deb https://dl.bintray.com/loadimpact/deb stable main" | sudo tee -a /etc/apt/sources.list
28+
sudo apt-get update
29+
sudo apt-get install k6
30+
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 379CE192D401AB61
31+
echo "deb https://dl.bintray.com/loadimpact/deb stable main" | sudo tee -a /etc/apt/sources.list
32+
sudo apt-get update
33+
sudo apt-get install k6
1434
```
1535

1636
> #### ⚠️ If you are behind a firewall or proxy

0 commit comments

Comments
 (0)