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

Added option to wrap lines in EuiCodeBlock #3103

Merged
merged 20 commits into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from 14 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## [`master`](https://github.com/elastic/eui/tree/master)

- Added `whiteSpace ` prop to `EuiCodeBlock` ([#3103](https://github.com/elastic/eui/pull/3103))
- Added `sortMatchesBy` prop for `EuiComboBox` ([#3089](https://github.com/elastic/eui/pull/3089))
- Added `prepend` and `append` ability to `EuiFieldPassword` ([#3122](https://github.com/elastic/eui/pull/3122))
- Added `Enter` key press functionality to `EuiSuperDatePicker` ([#3048](https://github.com/elastic/eui/pull/3048))
Expand Down
36 changes: 36 additions & 0 deletions src-docs/src/views/code/code_block_pre.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';

import { EuiCodeBlock, EuiSpacer } from '../../../../src/components';

export default () => (
<div>
<EuiCodeBlock
language="js"
fontSize="m"
paddingSize="m"
color="dark"
overflowHeight={300}
whiteSpace="pre"
isCopyable>
{`export default () => (
<div>
<EuiCodeBlock
language="js"
fontSize="m"
paddingSize="m"
color="dark"
overflowHeight={300}
isCopyable>
<div>
Phasellus posuere quam neque, ac tristique ipsum pulvinar non. Sed ultricies enim nibh, vel ullamcorper tortor commodo sed. Nam ut egestas enim. Pellentesque commodo tempus sapien nec lobortis. Suspendisse suscipit quis lacus quis tincidunt. Aenean pulvinar eros vel felis maximus tristique. Sed rutrum enim quis vestibulum dapibus. Aenean rhoncus iaculis justo, et dapibus magna ultricies non. Etiam non lacus condimentum, commodo erat ut, ultrices ligula. Interdum et malesuada fames ac ante ipsum primis in faucibus.
anishagg17 marked this conversation as resolved.
Show resolved Hide resolved
</div>
</EuiCodeBlock>

<EuiSpacer />
</div>
);`}
</EuiCodeBlock>

<EuiSpacer />
</div>
);
27 changes: 26 additions & 1 deletion src-docs/src/views/code/code_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ import CodeBlock from './code_block';
const codeBlockSource = require('!!raw-loader!./code_block');
const codeBlockHtml = renderToHtml(CodeBlock);
const codeBlockSnippet = `<EuiCodeBlock language="html" paddingSize="s" isCopyable>
{ \`<h1>Title</h1>\` }
{ \`<h1>Title</h1>\` }
</EuiCodeBlock>
`;

import CodeBlockPre from './code_block_pre';
const codeBlockPreSource = require('!!raw-loader!./code_block_pre');
const codeBlockPreHtml = renderToHtml(CodeBlockPre);

export const CodeExample = {
title: 'Code',
sections: [
Expand Down Expand Up @@ -67,5 +71,26 @@ export const CodeExample = {
props: { EuiCodeBlockImpl },
demo: <CodeBlock />,
},
{
title: 'CodeBlock with pre white space',
source: [
{
type: GuideSectionTypes.JS,
code: codeBlockPreSource,
},
{
type: GuideSectionTypes.HTML,
code: codeBlockPreHtml,
},
],
text: (
<p>
<EuiCode>EuiCodeBlock</EuiCode> allows whiteSpace property to be set
allowing user to breaks/white space forcefuly or not.
</p>
),
props: { EuiCodeBlockImpl },
demo: <CodeBlockPre />,
},
],
};
25 changes: 22 additions & 3 deletions src/components/code/__snapshots__/_code_block.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`EuiCodeBlockImpl block highlights javascript code, adding "js" class 1`
class="euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingLarge"
>
<pre
class="euiCodeBlock__pre"
class="euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePreWrap"
>
<code
class="euiCodeBlock__code js hljs javascript"
Expand All @@ -19,7 +19,7 @@ exports[`EuiCodeBlockImpl block renders a pre block tag 1`] = `
class="euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingLarge testClass1 testClass2"
>
<pre
class="euiCodeBlock__pre"
class="euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePreWrap"
>
<code
aria-label="aria-label"
Expand All @@ -38,7 +38,7 @@ exports[`EuiCodeBlockImpl block renders with transparent background 1`] = `
class="euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingLarge euiCodeBlock--transparentBackground"
>
<pre
class="euiCodeBlock__pre"
class="euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePreWrap"
>
<code
class="euiCodeBlock__code"
Expand Down Expand Up @@ -81,3 +81,22 @@ exports[`EuiCodeBlockImpl inline renders with transparent background 1`] = `
/>
</span>
`;

exports[`EuiCodeBlockImpl props whiteSpace 1`] = `
<div
class="euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingLarge testClass1 testClass2"
>
<pre
class="euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePre"
>
<code
aria-label="aria-label"
class="euiCodeBlock__code"
data-test-subj="test subject string"
>
var some = 'code';
console.log(some);
</code>
</pre>
</div>
`;
28 changes: 14 additions & 14 deletions src/components/code/__snapshots__/code_block.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`EuiCodeBlock dynamic content updates DOM when input changes 1`] = `
"<div>
<div class=\\"euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingLarge\\">
<pre class=\\"euiCodeBlock__pre\\">
<pre class=\\"euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePreWrap\\">
<code class=\\"euiCodeBlock__code javascript hljs\\">
<span class=\\"hljs-keyword\\">const</span>value =
<span class=\\"hljs-string\\">'State 1'</span>
Expand All @@ -16,7 +16,7 @@ exports[`EuiCodeBlock dynamic content updates DOM when input changes 1`] = `
exports[`EuiCodeBlock dynamic content updates DOM when input changes 2`] = `
"<div>
<div class=\\"euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingLarge\\">
<pre class=\\"euiCodeBlock__pre\\">
<pre class=\\"euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePreWrap\\">
<code class=\\"euiCodeBlock__code javascript hljs\\">
<span class=\\"hljs-keyword\\">const</span>value =
<span class=\\"hljs-string\\">'State 2'</span>
Expand All @@ -31,7 +31,7 @@ exports[`EuiCodeBlock props fontSize l is rendered 1`] = `
class="euiCodeBlock euiCodeBlock--fontLarge euiCodeBlock--paddingLarge"
>
<pre
class="euiCodeBlock__pre"
class="euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePreWrap"
>
<code
class="euiCodeBlock__code"
Expand All @@ -48,7 +48,7 @@ exports[`EuiCodeBlock props fontSize m is rendered 1`] = `
class="euiCodeBlock euiCodeBlock--fontMedium euiCodeBlock--paddingLarge"
>
<pre
class="euiCodeBlock__pre"
class="euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePreWrap"
>
<code
class="euiCodeBlock__code"
Expand All @@ -65,7 +65,7 @@ exports[`EuiCodeBlock props fontSize s is rendered 1`] = `
class="euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingLarge"
>
<pre
class="euiCodeBlock__pre"
class="euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePreWrap"
>
<code
class="euiCodeBlock__code"
Expand All @@ -82,7 +82,7 @@ exports[`EuiCodeBlock props isCopyable is rendered 1`] = `
class="euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingLarge euiCodeBlock--hasControls"
>
<pre
class="euiCodeBlock__pre"
class="euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePreWrap"
>
<code
class="euiCodeBlock__code"
Expand Down Expand Up @@ -122,7 +122,7 @@ exports[`EuiCodeBlock props language is rendered 1`] = `
class="euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingLarge"
>
<pre
class="euiCodeBlock__pre"
class="euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePreWrap"
>
<code
class="euiCodeBlock__code html hljs xml"
Expand All @@ -140,7 +140,7 @@ exports[`EuiCodeBlock props overflowHeight is rendered 1`] = `
style="max-height: 200px;"
>
<pre
class="euiCodeBlock__pre"
class="euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePreWrap"
style="max-height: 200px;"
>
<code
Expand Down Expand Up @@ -173,7 +173,7 @@ exports[`EuiCodeBlock props paddingSize l is rendered 1`] = `
class="euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingLarge"
>
<pre
class="euiCodeBlock__pre"
class="euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePreWrap"
>
<code
class="euiCodeBlock__code"
Expand All @@ -190,7 +190,7 @@ exports[`EuiCodeBlock props paddingSize m is rendered 1`] = `
class="euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingMedium"
>
<pre
class="euiCodeBlock__pre"
class="euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePreWrap"
>
<code
class="euiCodeBlock__code"
Expand All @@ -207,7 +207,7 @@ exports[`EuiCodeBlock props paddingSize none is rendered 1`] = `
class="euiCodeBlock euiCodeBlock--fontSmall"
>
<pre
class="euiCodeBlock__pre"
class="euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePreWrap"
>
<code
class="euiCodeBlock__code"
Expand All @@ -224,7 +224,7 @@ exports[`EuiCodeBlock props paddingSize s is rendered 1`] = `
class="euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingSmall"
>
<pre
class="euiCodeBlock__pre"
class="euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePreWrap"
>
<code
class="euiCodeBlock__code"
Expand All @@ -241,7 +241,7 @@ exports[`EuiCodeBlock props transparentBackground is rendered 1`] = `
class="euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingLarge euiCodeBlock--transparentBackground"
>
<pre
class="euiCodeBlock__pre"
class="euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePreWrap"
>
<code
class="euiCodeBlock__code"
Expand All @@ -258,7 +258,7 @@ exports[`EuiCodeBlock renders a code block 1`] = `
class="euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingLarge testClass1 testClass2"
>
<pre
class="euiCodeBlock__pre"
class="euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePreWrap"
>
<code
aria-label="aria-label"
Expand Down
7 changes: 7 additions & 0 deletions src/components/code/_code_block.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
height: 100%;
overflow: auto;
display: block;
}

.euiCodeBlock__pre--whiteSpacePre {
white-space: pre;
}

.euiCodeBlock__pre--whiteSpacePreWrap {
white-space: pre-wrap;
}

Expand Down
12 changes: 12 additions & 0 deletions src/components/code/_code_block.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,16 @@ describe('EuiCodeBlockImpl', () => {
expect(snapshotCodeBlock(component)).toMatchSnapshot();
});
});

describe('props', () => {
anishagg17 marked this conversation as resolved.
Show resolved Hide resolved
test('whiteSpace', () => {
const component = mount(
<EuiCodeBlockImpl whiteSpace="pre" {...requiredProps}>
{code}
</EuiCodeBlockImpl>
);

expect(snapshotCodeBlock(component)).toMatchSnapshot();
});
});
});
17 changes: 15 additions & 2 deletions src/components/code/_code_block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ interface Props {
overflowHeight?: number;
paddingSize: PaddingSize;
transparentBackground: boolean;
/**
* Specify how `white-space` inside the element is handled.
* `pre` respects line breaks/white space but doesn't force them to wrap the line
* `pre-wrap` respects line breaks/white space but does force them to wrap the line when necessary.
*/
whiteSpace?: 'pre' | 'pre-wrap';
}

interface State {
Expand All @@ -71,6 +77,7 @@ export class EuiCodeBlockImpl extends Component<Props, State> {
paddingSize: 'l',
fontSize: 's',
isCopyable: false,
whiteSpace: 'pre-wrap',
};

constructor(props: Props) {
Expand Down Expand Up @@ -152,6 +159,7 @@ export class EuiCodeBlockImpl extends Component<Props, State> {
paddingSize,
transparentBackground,
isCopyable,
whiteSpace,
...otherProps
} = this.props;

Expand All @@ -169,6 +177,11 @@ export class EuiCodeBlockImpl extends Component<Props, State> {

const codeClasses = classNames('euiCodeBlock__code', language);

const preClasses = classNames('euiCodeBlock__pre', {
'euiCodeBlock__pre--whiteSpacePre': whiteSpace === 'pre',
'euiCodeBlock__pre--whiteSpacePreWrap': whiteSpace === 'pre-wrap',
});

const optionalStyles: CSSProperties = {};

if (overflowHeight) {
Expand Down Expand Up @@ -286,7 +299,7 @@ export class EuiCodeBlockImpl extends Component<Props, State> {
<EuiOverlayMask>
<EuiFocusTrap clickOutsideDisables={true}>
<div className={fullScreenClasses}>
<pre className="euiCodeBlock__pre">
<pre className={preClasses}>
<code
ref={ref => {
this.codeFullScreen = ref;
Expand Down Expand Up @@ -318,7 +331,7 @@ export class EuiCodeBlockImpl extends Component<Props, State> {
<pre
ref={innerTextRef}
style={optionalStyles}
className="euiCodeBlock__pre">
className={preClasses}>
{codeSnippet}
</pre>

Expand Down