Skip to content

Commit cfcda43

Browse files
guiguebBill Guiguetw15egankodiakhq[bot]
authored
feat(CodeSnippet): set closed and expanded max number of rows (#8085)
* feat(codesnippet): set closed and expanded max number of rows defaulted the height to show everything (100%) added two new props maxClosedNumberOfRows and maxExpandedNumberOfRows added logic using the new props to determine when to show moreLessBtn added maxHeight style based on new props * feat(CodeSnippet): apply auto scroll to snippet-mult allow snippet-mult + snippet-container to auto scroll, both x and y remove scroll/overflow from nested snippet-expand, pre, and code conditions this will show the horizontal scroll when the snippet is closed or expanded it is a behavour change/bug fix, it will address 7574 * feat(CodeSnippet): add background to snippet buttons add background to buttons to stop scrollbars from stomping on them * feat(CodeSnippet): revert to only adding overflow-y changing to overflow - from -x/-y - on the snippet-container caused issues with scrollbars colliding with the buttons. for now only add overflow-y and address overflow clean up and scrollbars/button collisions in #7574 * feat(CodeSnippet): move overflow-y placement apply it for both closed and expanded modes * feat(CodeSnippet): changed closed to collapsed changed closed to collapsed * feat(CodeSnippet): update snapshot update snapshot * feat(CodeSnippet): rename closed to collapsed in the story rename closed to collapsed in the story Co-authored-by: Bill Guigue <Bill.Guigue@ca.ibm.com> Co-authored-by: TJ Egan <tw15egan@gmail.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent eb43164 commit cfcda43

File tree

4 files changed

+87
-25
lines changed

4 files changed

+87
-25
lines changed

packages/components/src/components/code-snippet/_code-snippet.scss

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,20 +197,19 @@
197197
}
198198
}
199199

200-
//closed snippet container
200+
//collapsed snippet container
201201
.#{$prefix}--snippet--multi .#{$prefix}--snippet-container {
202202
position: relative;
203203
order: 1;
204204
min-height: rem(56px);
205-
max-height: rem(238px);
206-
overflow: hidden;
205+
max-height: 100%;
206+
overflow-y: auto;
207207
transition: max-height $duration--moderate-01 motion(standard, productive);
208208
}
209209

210210
// expanded snippet container
211211
.#{$prefix}--snippet--multi.#{$prefix}--snippet--expand
212212
.#{$prefix}--snippet-container {
213-
max-height: 100%;
214213
padding-bottom: $spacing-05;
215214
transition: max-height $duration--moderate-01 motion(standard, productive);
216215
}
@@ -220,7 +219,7 @@
220219
word-wrap: break-word;
221220
}
222221

223-
// closed pre
222+
// collapsed pre
224223
.#{$prefix}--snippet--multi .#{$prefix}--snippet-container pre {
225224
padding-right: $carbon--spacing-08;
226225
padding-bottom: rem(24px);

packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,12 @@ Map {
383383
"light": Object {
384384
"type": "bool",
385385
},
386+
"maxCollapsedNumberOfRows": Object {
387+
"type": "number",
388+
},
389+
"maxExpandedNumberOfRows": Object {
390+
"type": "number",
391+
},
386392
"onClick": Object {
387393
"type": "func",
388394
},

packages/react/src/components/CodeSnippet/CodeSnippet-story.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77

88
import React from 'react';
99
import { action } from '@storybook/addon-actions';
10-
import { withKnobs, boolean, text, select } from '@storybook/addon-knobs';
10+
import {
11+
withKnobs,
12+
boolean,
13+
text,
14+
select,
15+
number,
16+
} from '@storybook/addon-knobs';
1117
import CodeSnippet from '../CodeSnippet';
1218
import CodeSnippetSkeleton from './CodeSnippet.Skeleton';
1319
import mdx from './CodeSnippet.mdx';
@@ -43,6 +49,14 @@ const props = () => ({
4349
copyButtonDescription: text('Copy button title', 'Copy code snippet'),
4450
ariaLabel: text('ARIA label', 'Container label'),
4551
wrapText: boolean('Wrap text (wrapText)', true),
52+
maxCollapsedNumberOfRows: number(
53+
'maxCollapsedNumberOfRows: Specify the maximum number of rows to be shown when in collapsed view',
54+
15
55+
),
56+
maxExpandedNumberOfRows: number(
57+
'maxExpandedNumberOfRows: Specify the maximum number of rows to be shown when in expanded view',
58+
0
59+
),
4660
});
4761

4862
export const inline = () => (

packages/react/src/components/CodeSnippet/CodeSnippet.js

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import copy from 'copy-to-clipboard';
2020

2121
const { prefix } = settings;
2222

23-
const rowHeightInPixels = 18;
24-
const defaultCollapsedHeightInRows = 15;
23+
const rowHeightInPixels = 16;
24+
const defaultMaxCollapsedNumberOfRows = 15;
25+
const defaultMaxExpandedNumberOfRows = 0;
2526

2627
function CodeSnippet({
2728
className,
@@ -38,6 +39,8 @@ function CodeSnippet({
3839
showLessText,
3940
hideCopyButton,
4041
wrapText,
42+
maxCollapsedNumberOfRows = defaultMaxCollapsedNumberOfRows,
43+
maxExpandedNumberOfRows = defaultMaxExpandedNumberOfRows,
4144
...rest
4245
}) {
4346
const [expandedCode, setExpandedCode] = useState(false);
@@ -93,23 +96,35 @@ function CodeSnippet({
9396
);
9497
}, [type, getCodeRefDimensions]);
9598

96-
useResizeObserver({
97-
ref: getCodeRef(),
98-
onResize: () => {
99-
if (codeContentRef?.current && type === 'multi') {
100-
const { height } = codeContentRef.current.getBoundingClientRect();
101-
setShouldShowMoreLessBtn(
102-
height > defaultCollapsedHeightInRows * rowHeightInPixels
103-
);
104-
}
105-
if (
106-
(codeContentRef?.current && type === 'multi') ||
107-
(codeContainerRef?.current && type === 'single')
108-
) {
109-
debounce(handleScroll, 200);
110-
}
99+
useResizeObserver(
100+
{
101+
ref: getCodeRef(),
102+
onResize: () => {
103+
if (codeContentRef?.current && type === 'multi') {
104+
const { height } = codeContentRef.current.getBoundingClientRect();
105+
106+
if (
107+
maxCollapsedNumberOfRows > 0 &&
108+
(maxExpandedNumberOfRows === 0 ||
109+
maxExpandedNumberOfRows > maxCollapsedNumberOfRows) &&
110+
height > maxCollapsedNumberOfRows * rowHeightInPixels
111+
) {
112+
setShouldShowMoreLessBtn(true);
113+
} else {
114+
setShouldShowMoreLessBtn(false);
115+
setExpandedCode(false);
116+
}
117+
}
118+
if (
119+
(codeContentRef?.current && type === 'multi') ||
120+
(codeContainerRef?.current && type === 'single')
121+
) {
122+
debounce(handleScroll, 200);
123+
}
124+
},
111125
},
112-
});
126+
[type, maxCollapsedNumberOfRows, maxExpandedNumberOfRows, rowHeightInPixels]
127+
);
113128

114129
useEffect(() => {
115130
handleScroll();
@@ -156,6 +171,23 @@ function CodeSnippet({
156171
);
157172
}
158173

174+
let containerStyle = {};
175+
if (type === 'multi') {
176+
if (expandedCode) {
177+
if (maxExpandedNumberOfRows > 0) {
178+
containerStyle.style = {
179+
maxHeight: maxExpandedNumberOfRows * rowHeightInPixels,
180+
};
181+
}
182+
} else {
183+
if (maxCollapsedNumberOfRows > 0) {
184+
containerStyle.style = {
185+
maxHeight: maxCollapsedNumberOfRows * rowHeightInPixels,
186+
};
187+
}
188+
}
189+
}
190+
159191
return (
160192
<div {...rest} className={codeSnippetClasses}>
161193
<div
@@ -164,7 +196,8 @@ function CodeSnippet({
164196
tabIndex={type === 'single' && !disabled ? 0 : null}
165197
className={`${prefix}--snippet-container`}
166198
aria-label={ariaLabel || copyLabel || 'code-snippet'}
167-
onScroll={(type === 'single' && handleScroll) || null}>
199+
onScroll={(type === 'single' && handleScroll) || null}
200+
{...containerStyle}>
168201
<code>
169202
<pre
170203
ref={codeContentRef}
@@ -262,6 +295,16 @@ CodeSnippet.propTypes = {
262295
*/
263296
light: PropTypes.bool,
264297

298+
/**
299+
* Specify the maximum number of rows to be shown when in collapsed view
300+
*/
301+
maxCollapsedNumberOfRows: PropTypes.number,
302+
303+
/**
304+
* Specify the maximum number of rows to be shown when in expanded view
305+
*/
306+
maxExpandedNumberOfRows: PropTypes.number,
307+
265308
/**
266309
* An optional handler to listen to the `onClick` even fired by the Copy
267310
* Button

0 commit comments

Comments
 (0)