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

fix(CodeBlocks): make highlight lines become selective #786

Merged
merged 1 commit into from
May 3, 2022
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
18 changes: 10 additions & 8 deletions components/CodeBlocks/BlockCode.module.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.code {
@apply static;
@apply mb-0 px-0 py-5;
}

Expand All @@ -9,26 +8,29 @@

.hover,
.highlight {
@apply absolute;
@apply w-full h-full;
@apply border-l-4 border-solid;
}

.hover {
@apply border-transparent;
}

.hover:hover,
.highlight {
@apply bg-gray-100/10;
@apply border-l-primary;
}

.hover:hover,
.highlight {
@apply border-l-4 border-solid border-l-primary;
@apply bg-gray-100/10;
}

.number {
@apply inline-block;
@apply w-6;
@apply ml-5 mr-3;
@apply ml-4 mr-3;
@apply text-secondary select-none;
}

.placeholder {
@apply ml-5;
@apply ml-4;
}
12 changes: 7 additions & 5 deletions components/CodeBlocks/BlockCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ const BlockCode = ({
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<pre className={classNames(className, styles.code)} style={style}>
{tokens.map((line, index) => (
<div key={index} {...getLineProps({ line, key: index })}>
{lines.has(index + 1) ? (
<span className={styles.highlight}></span>
) : (
<span className={styles.hover}></span>
<div
key={index}
{...getLineProps({ line, key: index })}
className={classNames(
'token-line',
lines.has(index + 1) ? styles.highlight : styles.hover
)}
>
{enableLine ? (
<span className={styles.number}>{index + 1}</span>
) : (
Expand Down
2 changes: 1 addition & 1 deletion components/CodeBlocks/LiveCode.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import LiveCode from './LiveCode';
describe('LiveCode', () => {
test('should render live code correctly (snapshot)', () => {
const { container } = render(
<LiveCode className="language-type">Live Code</LiveCode>
<LiveCode className="language-type" code="Live Code" />
);

expect(container).toMatchSnapshot();
Expand Down
6 changes: 3 additions & 3 deletions components/CodeBlocks/LiveCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import styles from './LiveCode.module.css';
import { normalizeCode } from './utils';

interface Props {
children?: string;
code?: string;
className?: string;
}

const LiveCode = ({ children, className }: Props): JSX.Element => (
const LiveCode = ({ code, className }: Props): JSX.Element => (
<pre className={classNames(className, styles.code)}>
{normalizeCode(children)}
{normalizeCode(code)}
</pre>
);

Expand Down
2 changes: 1 addition & 1 deletion components/CodeBlocks/Pre.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const Pre = ({
>
{!nocopy ? <CopyButton code={code} /> : null}
{live ? (
<LiveCode className={languageClass}>{code}</LiveCode>
<LiveCode code={code} className={languageClass} />
) : (
<BlockCode
enableLine={!noline}
Expand Down
Loading