From 9a6e47b039f7e87acee335d989cb797bfbe41d91 Mon Sep 17 00:00:00 2001 From: Nathan Kluth Date: Mon, 9 Dec 2024 11:27:13 -0700 Subject: [PATCH 1/3] refactor: rm useThemeDictionary --- .../src/components/highlight.ts | 4 ++-- .../src/components/useThemeDictionary.ts | 24 ------------------- 2 files changed, 2 insertions(+), 26 deletions(-) delete mode 100644 packages/prism-react-renderer/src/components/useThemeDictionary.ts 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 -} From db43793737ad3f0954c07b59c5afd8375b6b0f4e Mon Sep 17 00:00:00 2001 From: Nathan Kluth Date: Mon, 9 Dec 2024 12:53:59 -0700 Subject: [PATCH 2/3] rm useref --- .../src/components/useTokenize.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) 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, + ]) } From 3cfe2397c95fa4bdc1ea7904bdd59e28293a1002 Mon Sep 17 00:00:00 2001 From: Nathan Kluth Date: Mon, 9 Dec 2024 12:59:19 -0700 Subject: [PATCH 3/3] changelog --- .changeset/slow-kiwis-return.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/slow-kiwis-return.md 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