Skip to content

Commit 23f0955

Browse files
committed
feat: Convert InternalHighlight to forwardRef
1 parent e1c83a4 commit 23f0955

File tree

3 files changed

+4794
-3908
lines changed

3 files changed

+4794
-3908
lines changed
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1+
import { forwardRef } from "react"
12
import { InternalHighlightProps } from "../types"
23
import { useGetLineProps } from "./useGetLineProps"
34
import { useGetTokenProps } from "./useGetTokenProps"
45
import { useTokenize } from "./useTokenize"
56
import themeToDict from "../utils/themeToDict"
67

7-
export const Highlight = ({
8-
children,
9-
language: _language,
10-
code,
11-
theme,
12-
prism,
13-
}: InternalHighlightProps) => {
14-
const language = _language.toLowerCase()
15-
const themeDictionary = themeToDict(theme, language)
16-
const getLineProps = useGetLineProps(themeDictionary)
17-
const getTokenProps = useGetTokenProps(themeDictionary)
18-
const grammar = prism.languages[language]
19-
const tokens = useTokenize({ prism, language, code, grammar })
8+
export const Highlight = forwardRef<HTMLDivElement, InternalHighlightProps>(
9+
({ children, language: _language, code, theme, prism }, ref) => {
10+
const language = _language.toLowerCase()
11+
const themeDictionary = themeToDict(theme, language)
12+
const getLineProps = useGetLineProps(themeDictionary)
13+
const getTokenProps = useGetTokenProps(themeDictionary)
14+
const grammar = prism.languages[language]
15+
const tokens = useTokenize({ prism, language, code, grammar })
2016

21-
return children({
22-
tokens,
23-
className: `prism-code language-${language}`,
24-
style: themeDictionary != null ? themeDictionary.root : {},
25-
getLineProps,
26-
getTokenProps,
27-
})
28-
}
17+
return children({
18+
tokens,
19+
className: `prism-code language-${language}`,
20+
style: themeDictionary != null ? themeDictionary.root : {},
21+
getLineProps,
22+
getTokenProps,
23+
ref,
24+
})
25+
}
26+
)
27+
28+
Highlight.displayName = "Highlight"

packages/prism-react-renderer/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export type RenderProps = {
6464
style: CSSProperties
6565
getLineProps: (input: LineInputProps) => LineOutputProps
6666
getTokenProps: (input: TokenInputProps) => TokenOutputProps
67+
ref: React.Ref<HTMLDivElement>
6768
}
6869
export type PrismThemeEntry = {
6970
color?: string

0 commit comments

Comments
 (0)