Skip to content

Commit d6cbf6f

Browse files
authored
fix(theme): Fix code block text selection copy on Firefox? (#11565)
1 parent f13adec commit d6cbf6f

File tree

1 file changed

+14
-8
lines changed
  • packages/docusaurus-theme-classic/src/theme/CodeBlock/Line

1 file changed

+14
-8
lines changed

packages/docusaurus-theme-classic/src/theme/CodeBlock/Line/index.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,23 @@ import styles from './styles.module.css';
1414

1515
type Token = Props['line'][number];
1616

17-
// Replaces '\n' by ''
18-
// Historical code, not sure why we even need this :/
17+
// This <br/ seems useful when the line has no content to prevent collapsing.
18+
// For code blocks with "diff" languages, this makes the empty lines collapse to
19+
// zero height lines, which is undesirable.
20+
// See also https://github.com/facebook/docusaurus/pull/11565
21+
function LineBreak() {
22+
return <br />;
23+
}
24+
25+
// Replaces single lines with '\n' by '' so that we don't end up with
26+
// duplicate line breaks (the '\n' + the artificial <br/> above)
27+
// see also https://github.com/facebook/docusaurus/pull/11565
1928
function fixLineBreak(line: Token[]) {
2029
const singleLineBreakToken =
2130
line.length === 1 && line[0]!.content === '\n' ? line[0] : undefined;
22-
2331
if (singleLineBreakToken) {
2432
return [{...singleLineBreakToken, content: ''}];
2533
}
26-
2734
return line;
2835
}
2936

@@ -35,7 +42,6 @@ export default function CodeBlockLine({
3542
getTokenProps,
3643
}: Props): ReactNode {
3744
const line = fixLineBreak(lineProp);
38-
3945
const lineProps = getLineProps({
4046
line,
4147
className: clsx(classNames, showLineNumbers && styles.codeLine),
@@ -51,7 +57,7 @@ export default function CodeBlockLine({
5157
});
5258

5359
return (
54-
<span {...lineProps}>
60+
<div {...lineProps}>
5561
{showLineNumbers ? (
5662
<>
5763
<span className={styles.codeLineNumber} />
@@ -60,7 +66,7 @@ export default function CodeBlockLine({
6066
) : (
6167
lineTokens
6268
)}
63-
<br />
64-
</span>
69+
<LineBreak />
70+
</div>
6571
);
6672
}

0 commit comments

Comments
 (0)