Skip to content

Commit 18198e1

Browse files
authored
fix(code-block): access to all properties of pre (#2299)
1 parent 7b1f815 commit 18198e1

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

.changeset/big-corners-wear.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@react-email/code-block": minor
3+
---
4+
5+
fix access to all `pre` properties

.changeset/brave-experts-cover.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@react-email/components": minor
3+
---
4+
5+
code-block: fix access to all `pre` properties

packages/code-block/src/code-block.tsx

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
/* eslint-disable react/no-array-index-key */
21
import * as React from 'react';
32
import type { PrismLanguage } from './languages-available';
43
import { Prism } from './prism';
54
import type { Theme } from './themes';
65

7-
export type CodeBlockProps = Readonly<{
6+
export interface CodeBlockProps extends React.ComponentPropsWithoutRef<'pre'> {
87
lineNumbers?: boolean;
9-
style?: React.CSSProperties;
108

119
/**
1210
* This applies a certain font family on all elements render in this component,
@@ -18,7 +16,7 @@ export type CodeBlockProps = Readonly<{
1816
theme: Theme;
1917
language: PrismLanguage;
2018
code: string;
21-
}>;
19+
}
2220

2321
const stylesForToken = (token: Prism.Token, theme: Theme) => {
2422
let styles = { ...theme[token.type] };
@@ -75,33 +73,34 @@ const CodeBlockLine = ({
7573
};
7674

7775
export const CodeBlock = React.forwardRef<HTMLPreElement, CodeBlockProps>(
78-
(props, ref) => {
79-
const languageGrammar = Prism.languages[props.language];
76+
({ code, fontFamily, lineNumbers, theme, language, ...rest }, ref) => {
77+
const languageGrammar = Prism.languages[language];
8078
if (typeof languageGrammar === 'undefined') {
8179
throw new Error(
82-
`CodeBlock: There is no language defined on Prism called ${props.language}`,
80+
`CodeBlock: There is no language defined on Prism called ${language}`,
8381
);
8482
}
8583

86-
const lines = props.code.split(/\r\n|\r|\n/gm);
84+
const lines = code.split(/\r\n|\r|\n/gm);
8785
const tokensPerLine = lines.map((line) =>
8886
Prism.tokenize(line, languageGrammar),
8987
);
9088

9189
return (
9290
<pre
91+
{...rest}
9392
ref={ref}
94-
style={{ ...props.theme.base, width: '100%', ...props.style }}
93+
style={{ ...theme.base, width: '100%', ...rest.style }}
9594
>
9695
<code>
9796
{tokensPerLine.map((tokensForLine, lineIndex) => (
9897
<p key={lineIndex} style={{ margin: 0, minHeight: '1em' }}>
99-
{props.lineNumbers ? (
98+
{lineNumbers ? (
10099
<span
101100
style={{
102101
width: '2em',
103102
display: 'inline-block',
104-
fontFamily: props.fontFamily,
103+
fontFamily: fontFamily,
105104
}}
106105
>
107106
{lineIndex + 1}
@@ -110,9 +109,9 @@ export const CodeBlock = React.forwardRef<HTMLPreElement, CodeBlockProps>(
110109

111110
{tokensForLine.map((token, i) => (
112111
<CodeBlockLine
113-
inheritedStyles={{ fontFamily: props.fontFamily }}
112+
inheritedStyles={{ fontFamily: fontFamily }}
114113
key={i}
115-
theme={props.theme}
114+
theme={theme}
116115
token={token}
117116
/>
118117
))}

0 commit comments

Comments
 (0)