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 all 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: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## [`master`](https://github.com/elastic/eui/tree/master)

No public interface changes since `37.7.0`.
- Added optional line numbers to `EuiCodeBlock` ([#4993](https://github.com/elastic/eui/pull/4993))

## [`37.7.0`](https://github.com/elastic/eui/tree/v37.7.0)

Expand Down
28 changes: 27 additions & 1 deletion src-docs/src/views/code/code_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@ 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>
{}
</EuiCodeBlock>
`;

import CodeBlockVirtualized from './virtualized';
const codeBlockVirtualizedSource = require('!!raw-loader!./virtualized');
const codeBlockVirtualizedSnippet = `<EuiCodeBlock language="json" isVirtualized overflowHeight={300}>
{ \`{}\` }
{}
</EuiCodeBlock>
`;

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
42 changes: 42 additions & 0 deletions src-docs/src/views/code/line_numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React from 'react';

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

export default () => (
<>
<EuiCodeBlock language="json" fontSize="m" paddingSize="m" lineNumbers>
{`{
"id": "1",
"rawResponse": {
"took": 19,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
}
}
}`}
</EuiCodeBlock>

<EuiSpacer />

<EuiCodeBlock
language="json"
fontSize="m"
paddingSize="m"
lineNumbers={{ start: 32 }}
>
{`"OriginLocation": [
{
"coordinates": [
-97.43309784,
37.64989853
],
"type": "Point"
}
],`}
</EuiCodeBlock>
</>
);
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 euiCodeBlock--hasLineNumbers 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__lineNumber"
data-line-number="1"
style="width:8px"
/>
<span
class="euiCodeBlock__lineText"
style="margin-left:16px"
>
var some = 'code';

</span>
</span>
<span
class="euiCodeBlock__line"
>
<span
aria-hidden="true"
class="euiCodeBlock__lineNumber"
data-line-number="2"
style="width:8px"
/>
<span
class="euiCodeBlock__lineText"
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 euiCodeBlock--hasLineNumbers 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__lineNumber"
data-line-number="10"
style="width:16px"
/>
<span
class="euiCodeBlock__lineText"
style="margin-left:24px"
>
var some = 'code';

</span>
</span>
<span
class="euiCodeBlock__line"
>
<span
aria-hidden="true"
class="euiCodeBlock__lineNumber"
data-line-number="11"
style="width:16px"
/>
<span
class="euiCodeBlock__lineText"
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
Loading