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

[EuiCodeBlock] Add line numbers #4993

Merged
merged 22 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from 12 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## [`master`](https://github.com/elastic/eui/tree/master)

- Added optional line numbers to `EuiCodeBlock` ([#4993](https://github.com/elastic/eui/pull/4993))

**Bug fixes**

- Fixed `EuiSuperSelect`'s focus keyboard behavior when no initial value is passed, and focus label behavior ([#5097](https://github.com/elastic/eui/pull/5097))
Expand Down
26 changes: 26 additions & 0 deletions src-docs/src/views/code/code_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ const codeBlockSnippet = `<EuiCodeBlock language="html" paddingSize="s" isCopyab
</EuiCodeBlock>
`;

import CodeBlockLines from './line_numbers';
const codeBlockLinesSource = require('!!raw-loader!./line_numbers');
const codeBlockLinesSnippet = `<EuiCodeBlock language="json" lineNumbers>
{ \`{}\` }
thompsongl marked this conversation as resolved.
Show resolved Hide resolved
</EuiCodeBlock>
`;

import CodeBlockVirtualized from './virtualized';
const codeBlockVirtualizedSource = require('!!raw-loader!./virtualized');
const codeBlockVirtualizedSnippet = `<EuiCodeBlock language="json" isVirtualized overflowHeight={300}>
Expand Down Expand Up @@ -121,6 +128,25 @@ export const CodeExample = {
demo: <CodeBlock />,
playground: codeBlockConfig,
},
{
title: 'Line numbers',
source: [
{
type: GuideSectionTypes.JS,
code: codeBlockLinesSource,
},
],
text: (
<p>
To render line numbers, add <EuiCode>lineNumbers</EuiCode>, and
optionally change the starting number by passing a configuration
object: <EuiCode>{'lineNumbers={{ start: 32 }}'}</EuiCode>
</p>
),
props: { EuiCodeBlock },
snippet: codeBlockLinesSnippet,
demo: <CodeBlockLines />,
},
{
title: 'Code block virtualization',
source: [
Expand Down
43 changes: 43 additions & 0 deletions src-docs/src/views/code/line_numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React from 'react';

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

export default () => (
<div>
thompsongl marked this conversation as resolved.
Show resolved Hide resolved
<EuiCodeBlock language="json" fontSize="m" paddingSize="m" lineNumbers>
{`{
"id": "1",
"rawResponse": {
"took": 19,
"timed_out": false and a longer sentence so the line will break,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
}
}`}
thompsongl marked this conversation as resolved.
Show resolved Hide resolved
</EuiCodeBlock>

<EuiSpacer />

<EuiCodeBlock
language="json"
fontSize="m"
paddingSize="m"
overflowHeight={300}
isCopyable
thompsongl marked this conversation as resolved.
Show resolved Hide resolved
lineNumbers={{ start: 32 }}
>
{`"OriginLocation": [
{
"coordinates": [
-97.43309784,
37.64989853
],
"type": "Point"
}
],`}
</EuiCodeBlock>
</div>
);
8 changes: 7 additions & 1 deletion src-docs/src/views/code/virtualized.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { EuiCodeBlock } from '../../../../src/components';

export default () => (
<div>
<EuiCodeBlock language="json" overflowHeight={300} isCopyable isVirtualized>
<EuiCodeBlock
language="json"
overflowHeight={300}
isCopyable
isVirtualized
lineNumbers
>
{`{
"id": "1",
"rawResponse": {
Expand Down
138 changes: 138 additions & 0 deletions src/components/code/__snapshots__/_code_block.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,144 @@ exports[`EuiCodeBlockImpl block renders a virtualized code block 1`] = `
</div>
`;

exports[`EuiCodeBlockImpl block renders line numbers 1`] = `
<div
class="euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingLarge euiCodeBlock--hasControl prismjs language-text testClass1 testClass2"
style="max-height:300px"
>
<pre
class="euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePreWrap"
style="max-height:300px"
tabindex="-1"
>
<code
aria-label="aria-label"
class="euiCodeBlock__code text"
data-test-subj="test subject string"
>
<span
class="euiCodeBlock__line"
>
<span
aria-hidden="true"
class="euiCodeBlock__line__number"
data-line-number="1"
style="width:8px"
/>
<span
class="euiCodeBlock__line__text"
style="margin-left:16px"
>
var some = 'code';

</span>
</span>
<span
class="euiCodeBlock__line"
>
<span
aria-hidden="true"
class="euiCodeBlock__line__number"
data-line-number="2"
style="width:8px"
/>
<span
class="euiCodeBlock__line__text"
style="margin-left:16px"
>
console.log(some);
</span>
</span>
</code>
</pre>
<div
class="euiCodeBlock__controls"
>
<button
aria-label="Expand"
class="euiButtonIcon euiButtonIcon--text euiButtonIcon--empty euiButtonIcon--xSmall euiCodeBlock__fullScreenButton"
type="button"
>
<span
aria-hidden="true"
class="euiButtonIcon__icon"
color="inherit"
data-euiicon-type="fullScreen"
/>
</button>
</div>
</div>
`;

exports[`EuiCodeBlockImpl block renders line numbers with a start value 1`] = `
<div
class="euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingLarge euiCodeBlock--hasControl prismjs language-text testClass1 testClass2"
style="max-height:300px"
>
<pre
class="euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePreWrap"
style="max-height:300px"
tabindex="-1"
>
<code
aria-label="aria-label"
class="euiCodeBlock__code text"
data-test-subj="test subject string"
>
<span
class="euiCodeBlock__line"
>
<span
aria-hidden="true"
class="euiCodeBlock__line__number"
data-line-number="10"
style="width:16px"
/>
<span
class="euiCodeBlock__line__text"
style="margin-left:24px"
>
var some = 'code';

</span>
</span>
<span
class="euiCodeBlock__line"
>
<span
aria-hidden="true"
class="euiCodeBlock__line__number"
data-line-number="11"
style="width:16px"
/>
<span
class="euiCodeBlock__line__text"
style="margin-left:24px"
>
console.log(some);
</span>
</span>
</code>
</pre>
<div
class="euiCodeBlock__controls"
>
<button
aria-label="Expand"
class="euiButtonIcon euiButtonIcon--text euiButtonIcon--empty euiButtonIcon--xSmall euiCodeBlock__fullScreenButton"
type="button"
>
<span
aria-hidden="true"
class="euiButtonIcon__icon"
color="inherit"
data-euiicon-type="fullScreen"
/>
</button>
</div>
</div>
`;

exports[`EuiCodeBlockImpl block renders with transparent background 1`] = `
<div
class="euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingLarge euiCodeBlock--transparentBackground prismjs language-text"
Expand Down
37 changes: 32 additions & 5 deletions src/components/code/_code_block.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,38 @@

// Necessary for virtualized code blocks to have appropriate padding
.euiCodeBlock__pre--isVirtualized {
position: relative;
.euiCodeBlock__code {
position: relative;
}
}

.euiCodeBlock__line {
display: block;
user-select: none;
elizabetdev marked this conversation as resolved.
Show resolved Hide resolved
}

.euiCodeBlock__line__text,
.euiCodeBlock__line__number {
thompsongl marked this conversation as resolved.
Show resolved Hide resolved
display: inline-block;
}

.euiCodeBlock__line__text {
padding-left: $euiSizeS;
margin-left: $euiSizeS;
border-left: $euiBorderThin;
user-select: text;
}

.euiCodeBlock__line__number {
position: absolute;
user-select: none;

&:before {
content: attr(data-line-number);
color: $euiTextSubduedColor;
text-align: right;
display: block;
thompsongl marked this conversation as resolved.
Show resolved Hide resolved
}
}

.euiCodeBlock__code {
Expand All @@ -43,10 +74,6 @@
margin-top: $euiSizeXS;
}

.euiCodeBlock__line {
display: block;
}

&.euiCodeBlock-isFullScreen {
position: fixed;
top: 0;
Expand Down
28 changes: 28 additions & 0 deletions src/components/code/_code_block.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,33 @@ describe('EuiCodeBlockImpl', () => {
);
expect(component).toMatchSnapshot();
});

test('renders line numbers', () => {
const component = render(
<EuiCodeBlockImpl
inline={false}
lineNumbers
overflowHeight={300}
{...requiredProps}
>
{code}
</EuiCodeBlockImpl>
);
expect(component).toMatchSnapshot();
});

test('renders line numbers with a start value', () => {
const component = render(
<EuiCodeBlockImpl
inline={false}
lineNumbers={{ start: 10 }}
overflowHeight={300}
{...requiredProps}
>
{code}
</EuiCodeBlockImpl>
);
expect(component).toMatchSnapshot();
});
});
});
Loading