Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(CodeSnippet): set closed and expanded max number of rows #8085

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,16 @@
position: relative;
order: 1;
min-height: rem(56px);
max-height: rem(238px);
max-height: 100%;
overflow: hidden;
transition: max-height $duration--moderate-01 motion(standard, productive);
}

// expanded snippet container
.#{$prefix}--snippet--multi.#{$prefix}--snippet--expand
.#{$prefix}--snippet-container {
max-height: 100%;
padding-bottom: $spacing-05;
overflow-y: auto;
tw15egan marked this conversation as resolved.
Show resolved Hide resolved
transition: max-height $duration--moderate-01 motion(standard, productive);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,12 @@ Map {
"light": Object {
"type": "bool",
},
"maxClosedNumberOfRows": Object {
"type": "number",
},
"maxExpandedNumberOfRows": Object {
"type": "number",
},
"onClick": Object {
"type": "func",
},
Expand Down
16 changes: 15 additions & 1 deletion packages/react/src/components/CodeSnippet/CodeSnippet-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@

import React from 'react';
import { action } from '@storybook/addon-actions';
import { withKnobs, boolean, text, select } from '@storybook/addon-knobs';
import {
withKnobs,
boolean,
text,
select,
number,
} from '@storybook/addon-knobs';
import CodeSnippet from '../CodeSnippet';
import CodeSnippetSkeleton from './CodeSnippet.Skeleton';
import mdx from './CodeSnippet.mdx';
Expand Down Expand Up @@ -43,6 +49,14 @@ const props = () => ({
copyButtonDescription: text('Copy button title', 'Copy code snippet'),
ariaLabel: text('ARIA label', 'Container label'),
wrapText: boolean('Wrap text (wrapText)', true),
maxClosedNumberOfRows: number(
'maxClosedNumberOfRows: Specify the maximum number of rows to be shown when in closed view',
guigueb marked this conversation as resolved.
Show resolved Hide resolved
15
),
maxExpandedNumberOfRows: number(
'maxExpandedNumberOfRows: Specify the maximum number of rows to be shown when in expanded view',
0
),
});

export const inline = () => (
Expand Down
81 changes: 62 additions & 19 deletions packages/react/src/components/CodeSnippet/CodeSnippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import getUniqueId from '../../tools/uniqueId';

const { prefix } = settings;

const rowHeightInPixels = 18;
const defaultCollapsedHeightInRows = 15;
const rowHeightInPixels = 16;
const defaultMaxClosedNumberOfRows = 15;
const defaultMaxExpandedNumberOfRows = 0;

function CodeSnippet({
className,
Expand All @@ -37,6 +38,8 @@ function CodeSnippet({
showLessText,
hideCopyButton,
wrapText,
maxClosedNumberOfRows = defaultMaxClosedNumberOfRows,
maxExpandedNumberOfRows = defaultMaxExpandedNumberOfRows,
...rest
}) {
const [expandedCode, setExpandedCode] = useState(false);
Expand Down Expand Up @@ -92,23 +95,35 @@ function CodeSnippet({
);
}, [type, getCodeRefDimensions]);

useResizeObserver({
ref: getCodeRef(),
onResize: () => {
if (codeContentRef?.current && type === 'multi') {
const { height } = codeContentRef.current.getBoundingClientRect();
setShouldShowMoreLessBtn(
height > defaultCollapsedHeightInRows * rowHeightInPixels
);
}
if (
(codeContentRef?.current && type === 'multi') ||
(codeContainerRef?.current && type === 'single')
) {
debounce(handleScroll, 200);
}
useResizeObserver(
{
ref: getCodeRef(),
onResize: () => {
if (codeContentRef?.current && type === 'multi') {
const { height } = codeContentRef.current.getBoundingClientRect();

if (
maxClosedNumberOfRows > 0 &&
(maxExpandedNumberOfRows === 0 ||
maxExpandedNumberOfRows > maxClosedNumberOfRows) &&
height > maxClosedNumberOfRows * rowHeightInPixels
) {
setShouldShowMoreLessBtn(true);
} else {
setShouldShowMoreLessBtn(false);
setExpandedCode(false);
}
}
if (
(codeContentRef?.current && type === 'multi') ||
(codeContainerRef?.current && type === 'single')
) {
debounce(handleScroll, 200);
}
},
},
});
[type, maxClosedNumberOfRows, maxExpandedNumberOfRows, rowHeightInPixels]
);

useEffect(() => {
handleScroll();
Expand Down Expand Up @@ -147,6 +162,23 @@ function CodeSnippet({
);
}

let containerStyle = {};
if (type === 'multi') {
if (expandedCode) {
if (maxExpandedNumberOfRows > 0) {
containerStyle.style = {
maxHeight: maxExpandedNumberOfRows * rowHeightInPixels,
};
}
} else {
if (maxClosedNumberOfRows > 0) {
guigueb marked this conversation as resolved.
Show resolved Hide resolved
containerStyle.style = {
maxHeight: maxClosedNumberOfRows * rowHeightInPixels,
};
}
}
}

return (
<div {...rest} className={codeSnippetClasses}>
<div
Expand All @@ -155,7 +187,8 @@ function CodeSnippet({
tabIndex={type === 'single' && !disabled ? 0 : null}
className={`${prefix}--snippet-container`}
aria-label={ariaLabel || copyLabel || 'code-snippet'}
onScroll={(type === 'single' && handleScroll) || null}>
onScroll={(type === 'single' && handleScroll) || null}
{...containerStyle}>
<code>
<pre
ref={codeContentRef}
Expand Down Expand Up @@ -253,6 +286,16 @@ CodeSnippet.propTypes = {
*/
light: PropTypes.bool,

/**
* Specify the maximum number of rows to be shown when in closed view
*/
maxClosedNumberOfRows: PropTypes.number,
guigueb marked this conversation as resolved.
Show resolved Hide resolved

/**
* Specify the maximum number of rows to be shown when in expanded view
*/
maxExpandedNumberOfRows: PropTypes.number,

/**
* An optional handler to listen to the `onClick` even fired by the Copy
* Button
Expand Down