Skip to content

Commit 5b5b809

Browse files
jacobloganJacob Logan
andauthored
Terminal numbers (#7557)
* move code block line numbers to css * update tests for line numbers in css --------- Co-authored-by: Jacob Logan <lognjc@amazon.com>
1 parent 7996997 commit 5b5b809

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

src/components/MDXComponents/MDXCode.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export const MDXCode = ({
6767
return (
6868
<Highlight theme={theme} code={code} language={language}>
6969
{({ style, tokens, getLineProps, getTokenProps }) => (
70-
<View data-testid={testId}>
70+
<View data-testid={testId} className={'code-block'}>
7171
<View className="pre-container">
7272
{shouldShowHeader ? (
7373
<Flex className="pre-header" data-testid={testHeaderId}>

src/components/MDXComponents/TokenList.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Token } from 'prism-react-renderer';
22
import type { TokenListProps } from './types';
33
import { MDXHighlightedCopyCodeButton } from './MDXHighlightedCopyCodeButton';
4+
import classNames from 'classnames';
45

56
type ProcessedToken = {
67
line: Token[];
@@ -135,12 +136,11 @@ export const TokenList = ({
135136
{...getLineProps({ line: token.line })}
136137
className={`token-line${shouldHighlight ? ' line-highlight' : ''}`}
137138
>
138-
{showLineNumbers && (
139-
<span className="line-number">{token.lineNumber}</span>
140-
)}
141-
{token.line.map((token, key) => (
142-
<span key={key} {...getTokenProps({ token })} />
143-
))}
139+
<div className={classNames({ 'show-line-numbers': showLineNumbers })}>
140+
{token.line.map((token, key) => (
141+
<span key={key} {...getTokenProps({ token })} />
142+
))}
143+
</div>
144144
</div>
145145
) : null;
146146
}

src/components/MDXComponents/__tests__/MDXCode.test.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ describe('MDXCode', () => {
4444
expect(titleElement.className).toContain('pre-title');
4545
});
4646

47+
it('should reset the code-block css counter', async () => {
48+
// line numbers have been moved to css, so here we are looking for the class that has "counter-reset: code-block;"
49+
// line number css can be found in `/src/styles/code.scss`
50+
const { container } = render(
51+
<MDXCode testId={testId} codeString={codeString}></MDXCode>
52+
);
53+
expect(container.querySelector('.code-block')).toBeInTheDocument();
54+
});
55+
4756
it('should show a copy button for supported languages', async () => {
4857
render(<MDXCode codeString={codeString}></MDXCode>);
4958
const copyButton = await screen.findByRole('button', { name: /Copy/i });
@@ -70,17 +79,20 @@ describe('MDXCode', () => {
7079
});
7180

7281
it('should show line numbers by default', async () => {
82+
// Line numbers have been moved to css so we are just looking for the needed class name
7383
const codeString = 'test code';
74-
render(<MDXCode codeString={codeString}></MDXCode>);
75-
const number = screen.queryByText('1');
76-
expect(number).toBeInTheDocument();
84+
const { container } = render(<MDXCode codeString={codeString}></MDXCode>);
85+
expect(container.querySelector('.show-line-numbers')).toBeInTheDocument();
7786
});
7887

7988
it('should not have line numbers if showLineNumbers is false', async () => {
8089
const codeString = 'test code';
81-
render(<MDXCode showLineNumbers={false} codeString={codeString}></MDXCode>);
82-
const number = screen.queryByText('1');
83-
expect(number).not.toBeInTheDocument();
90+
const { container } = render(
91+
<MDXCode showLineNumbers={false} codeString={codeString}></MDXCode>
92+
);
93+
expect(
94+
container.querySelector('.show-line-numbers')
95+
).not.toBeInTheDocument();
8496
});
8597

8698
it('should have highlighted line when //highlight-next-line is used', async () => {

src/styles/code.scss

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ code:not([class]) {
109109
background-color: var(--amplify-colors-primary-40);
110110
z-index: 1;
111111
height: 100%;
112+
top: 0;
112113
}
113114
@include darkMode {
114115
&:before {
@@ -120,7 +121,13 @@ code:not([class]) {
120121
}
121122
}
122123

123-
.line-number {
124+
.code-block {
125+
counter-reset: code-block;
126+
}
127+
128+
.show-line-numbers:before {
129+
counter-increment: code-block;
130+
content: counter(code-block);
124131
z-index: 1;
125132
position: relative;
126133
color: var(--code-theme-comment);
@@ -195,4 +202,4 @@ code:not([class]) {
195202
--code-copy-focus-background-color: var(--amplify-colors-neutral-20);
196203
--code-copy-focus-box-shadow: var(--amplify-colors-neutral-100);
197204
}
198-
}
205+
}

0 commit comments

Comments
 (0)