summaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-code-blocks-theme.tsx
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-05-18 22:40:59 +0200
committerArmand Philippot <git@armandphilippot.com>2022-05-18 22:40:59 +0200
commit584bd42f871d2e1618ca414749f09c38f0143a44 (patch)
tree45c821eec2ad9c77d5bccf83057cfc0a7e22ba09 /src/utils/hooks/use-code-blocks-theme.tsx
parentb214baab3e17d92f784b4f782863deafc5558ee4 (diff)
chore: handle settings change
Diffstat (limited to 'src/utils/hooks/use-code-blocks-theme.tsx')
-rw-r--r--src/utils/hooks/use-code-blocks-theme.tsx22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/utils/hooks/use-code-blocks-theme.tsx b/src/utils/hooks/use-code-blocks-theme.tsx
new file mode 100644
index 0000000..beb7b29
--- /dev/null
+++ b/src/utils/hooks/use-code-blocks-theme.tsx
@@ -0,0 +1,22 @@
+import { usePrismTheme } from '@utils/providers/prism-theme';
+import { useRouter } from 'next/router';
+import { RefObject, useEffect, useState } from 'react';
+import useIsMounted from './use-is-mounted';
+
+const useCodeBlocksTheme = (el: RefObject<HTMLDivElement>) => {
+ const [preElements, setPreElements] = useState<NodeListOf<HTMLPreElement>>();
+ const isMounted = useIsMounted(el);
+ const { setCodeBlocks } = usePrismTheme();
+ const { asPath } = useRouter();
+
+ useEffect(() => {
+ const result = document.querySelectorAll<HTMLPreElement>('pre');
+ setPreElements(result);
+ }, [asPath]);
+
+ useEffect(() => {
+ isMounted && preElements && setCodeBlocks(preElements);
+ }, [isMounted, preElements, setCodeBlocks]);
+};
+
+export default useCodeBlocksTheme;