diff --git a/.changeset/slow-kiwis-return.md b/.changeset/slow-kiwis-return.md new file mode 100644 index 0000000..9520282 --- /dev/null +++ b/.changeset/slow-kiwis-return.md @@ -0,0 +1,5 @@ +--- +"prism-react-renderer": patch +--- + +Remove client side hooks diff --git a/packages/prism-react-renderer/src/components/highlight.ts b/packages/prism-react-renderer/src/components/highlight.ts index 3c6d5d5..3d23ddc 100644 --- a/packages/prism-react-renderer/src/components/highlight.ts +++ b/packages/prism-react-renderer/src/components/highlight.ts @@ -1,8 +1,8 @@ import { InternalHighlightProps } from "../types" -import { useThemeDictionary } from "./useThemeDictionary" import { useGetLineProps } from "./useGetLineProps" import { useGetTokenProps } from "./useGetTokenProps" import { useTokenize } from "./useTokenize" +import themeToDict from "../utils/themeToDict" export const Highlight = ({ children, @@ -12,7 +12,7 @@ export const Highlight = ({ prism, }: InternalHighlightProps) => { const language = _language.toLowerCase() - const themeDictionary = useThemeDictionary(language, theme) + const themeDictionary = themeToDict(theme, language) const getLineProps = useGetLineProps(themeDictionary) const getTokenProps = useGetTokenProps(themeDictionary) const grammar = prism.languages[language] diff --git a/packages/prism-react-renderer/src/components/useThemeDictionary.ts b/packages/prism-react-renderer/src/components/useThemeDictionary.ts deleted file mode 100644 index fd6746f..0000000 --- a/packages/prism-react-renderer/src/components/useThemeDictionary.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { Language, PrismTheme } from "../types" -import { useEffect, useRef, useState } from "react" -import themeToDict, { ThemeDict } from "../utils/themeToDict" - -export const useThemeDictionary = (language: Language, theme: PrismTheme) => { - const [themeDictionary, setThemeDictionary] = useState( - themeToDict(theme, language) - ) - const previousTheme = useRef() - const previousLanguage = useRef() - - useEffect(() => { - if ( - theme !== previousTheme.current || - language !== previousLanguage.current - ) { - previousTheme.current = theme - previousLanguage.current = language - setThemeDictionary(themeToDict(theme, language)) - } - }, [language, theme]) - - return themeDictionary -} diff --git a/packages/prism-react-renderer/src/components/useTokenize.ts b/packages/prism-react-renderer/src/components/useTokenize.ts index 75901b9..7fac963 100644 --- a/packages/prism-react-renderer/src/components/useTokenize.ts +++ b/packages/prism-react-renderer/src/components/useTokenize.ts @@ -1,6 +1,6 @@ import { EnvConfig, Language, PrismGrammar, PrismLib } from "../types" import normalizeTokens from "../utils/normalizeTokens" -import { useMemo, useRef } from "react" +import { useMemo } from "react" type Options = { prism: PrismLib @@ -10,7 +10,6 @@ type Options = { } export const useTokenize = ({ prism, code, grammar, language }: Options) => { - const prismRef = useRef(prism) return useMemo(() => { if (grammar == null) return normalizeTokens([code]) @@ -21,9 +20,15 @@ export const useTokenize = ({ prism, code, grammar, language }: Options) => { tokens: [], } - prismRef.current.hooks.run("before-tokenize", prismConfig) - prismConfig.tokens = prismRef.current.tokenize(code, grammar) - prismRef.current.hooks.run("after-tokenize", prismConfig) + prism.hooks.run("before-tokenize", prismConfig) + prismConfig.tokens = prism.tokenize(code, grammar) + prism.hooks.run("after-tokenize", prismConfig) return normalizeTokens(prismConfig.tokens) - }, [code, grammar, language]) + }, [ + code, + grammar, + language, + // prism is a stable import + prism, + ]) }