Skip to content

Commit

Permalink
Add vertical scroll and text highlighting for functions (elastic#3918)
Browse files Browse the repository at this point in the history
* Add vertical scroll and text highlighting for functions

* use code block instead of markdown

* Swap usages of code for code block and fix overflow

* One codeblock with line breaks when multiple functions

* Fixed warnings of missing keys

Co-authored-by: cchaos <[email protected]>
  • Loading branch information
2 people authored and snide committed Aug 20, 2020
1 parent e4833f3 commit 134f305
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src-docs/src/components/guide_section/_guide_section.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@
.guideSection__space {
height: $euiSizeL;
}

.guideSection__tableCodeBlock {
padding-left: $euiSizeXS;
padding-right: $euiSizeXS;
}
60 changes: 54 additions & 6 deletions src-docs/src/components/guide_section/guide_section.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,12 @@ export class GuideSection extends Component {
type,
} = props[propName];

const codeBlockProps = {
className: 'guideSection__tableCodeBlock',
paddingSize: 'none',
language: 'ts',
};

let humanizedName = (
<strong className="eui-textBreakNormal">{propName}</strong>
);
Expand All @@ -324,28 +330,70 @@ export class GuideSection extends Component {

const humanizedType = humanizeType(type);

const functionMatches = [
...humanizedType.matchAll(/\([^=]*\) =>\s\w*\)*/g),
];
const types = humanizedType.split(/\([^=]*\) =>\s\w*\)*/);

const typeMarkup = (
<span className="eui-textBreakNormal">{markup(humanizedType)}</span>
);
const descriptionMarkup = markup(propDescription);
let defaultValueMarkup = '';
if (defaultValue) {
defaultValueMarkup = [
<EuiCode key={`defaultValue-${propName}`}>
<span className="eui-textBreakNormal">{defaultValue.value}</span>
</EuiCode>,
<EuiCodeBlock {...codeBlockProps} key={`defaultValue-${propName}`}>
{defaultValue.value}
</EuiCodeBlock>,
];
if (defaultValue.comment) {
defaultValueMarkup.push(`(${defaultValue.comment})`);
}
}

let defaultTypeCell = (
<EuiTableRowCell key="type" header="Type" textOnly={false}>
<EuiCodeBlock {...codeBlockProps}>{typeMarkup}</EuiCodeBlock>
</EuiTableRowCell>
);
if (functionMatches.length > 0) {
const elements = [];
let j = 0;
for (let i = 0; i < types.length; i++) {
if (functionMatches[j]) {
elements.push(
<Fragment key={`type-${i}`}>
{types[i]} <br />
</Fragment>
);
elements.push(
<Fragment key={`function-${i}`}>
{functionMatches[j][0]} <br />
</Fragment>
);
j++;
} else {
elements.push(
<Fragment key={`type-${i}`}>
{types[i]} <br />
</Fragment>
);
}
}
defaultTypeCell = (
<EuiTableRowCell key="type" header="Type" textOnly={false}>
<EuiCodeBlock whiteSpace="pre" {...codeBlockProps}>
{elements}
</EuiCodeBlock>
</EuiTableRowCell>
);
}

const cells = [
<EuiTableRowCell key="name" header="Prop">
{humanizedName}
</EuiTableRowCell>,
<EuiTableRowCell key="type" header="Type">
<EuiCode>{typeMarkup}</EuiCode>
</EuiTableRowCell>,
defaultTypeCell,
<EuiTableRowCell
key="defaultValue"
header="Default"
Expand Down
1 change: 1 addition & 0 deletions src/components/code/_code_block.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.euiCodeBlock {
max-width: 100%;
display: block;
position: relative;
background: $euiCodeBlockBackgroundColor;
Expand Down

0 comments on commit 134f305

Please sign in to comment.