summaryrefslogtreecommitdiffstats
path: root/src/components
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-05-19 19:46:24 +0200
committerArmand Philippot <git@armandphilippot.com>2022-05-19 19:46:24 +0200
commitbbd63400f94b43fde04449e0c71d14763d893e6a (patch)
tree057055dce19fc71c7c2e2fa05b691144224dfbd0 /src/components
parent806004ab79ac4e1cb49cef93ab3f35a08c5c82b5 (diff)
refactor: rewrite Prism hooks and providers
It avoid some hydratation errors on project pages (not in article however) and the hooks are now reusable.
Diffstat (limited to 'src/components')
-rw-r--r--src/components/molecules/forms/motion-toggle.tsx3
-rw-r--r--src/components/molecules/layout/code.tsx25
-rw-r--r--src/components/templates/page/page-layout.tsx5
3 files changed, 11 insertions, 22 deletions
diff --git a/src/components/molecules/forms/motion-toggle.tsx b/src/components/molecules/forms/motion-toggle.tsx
index e3bb11a..cbe38fe 100644
--- a/src/components/molecules/forms/motion-toggle.tsx
+++ b/src/components/molecules/forms/motion-toggle.tsx
@@ -33,7 +33,8 @@ const MotionToggle: FC<MotionToggleProps> = ({
value
);
useAttributes({
- attribute: 'reducedMotion',
+ element: document.documentElement || undefined,
+ attribute: 'reduced-motion',
value: `${isReduced}`,
});
diff --git a/src/components/molecules/layout/code.tsx b/src/components/molecules/layout/code.tsx
index 2959ae5..f5b60b9 100644
--- a/src/components/molecules/layout/code.tsx
+++ b/src/components/molecules/layout/code.tsx
@@ -1,7 +1,5 @@
-import usePrismPlugins, {
- type PrismPlugin,
-} from '@utils/hooks/use-prism-plugins';
-import { FC } from 'react';
+import usePrism, { OptionalPrismPlugin } from '@utils/hooks/use-prism';
+import { FC, useRef } from 'react';
import styles from './code.module.scss';
export type PrismLanguage =
@@ -35,15 +33,6 @@ export type PrismLanguage =
| 'twig'
| 'yaml';
-export type OptionalPrismPlugin = Extract<
- PrismPlugin,
- | 'command-line'
- | 'diff-highlight'
- | 'inline-color'
- | 'line-highlight'
- | 'line-numbers'
->;
-
export type CodeProps = {
/**
* The code to highlight.
@@ -79,17 +68,19 @@ const Code: FC<CodeProps> = ({
plugins = [],
outputPattern = '#output#',
}) => {
- const { pluginsAttribute, pluginsClassName } = usePrismPlugins(plugins);
+ const wrapperRef = useRef<HTMLDivElement>(null);
+ const { attributes, className } = usePrism({ language, plugins });
const outputAttribute = filterOutput
? { 'data-filter-output': outputPattern }
: {};
return (
- <div className={styles.wrapper}>
+ <div className={styles.wrapper} ref={wrapperRef}>
<pre
- className={`language-${language} ${pluginsClassName}`}
- {...pluginsAttribute}
+ className={className}
+ tabIndex={0}
+ {...attributes}
{...outputAttribute}
>
<code className={`language-${language}`}>{children}</code>
diff --git a/src/components/templates/page/page-layout.tsx b/src/components/templates/page/page-layout.tsx
index 54b8d6e..8ff44d5 100644
--- a/src/components/templates/page/page-layout.tsx
+++ b/src/components/templates/page/page-layout.tsx
@@ -20,7 +20,6 @@ import CommentsList, {
import TableOfContents from '@components/organisms/widgets/table-of-contents';
import { type SendCommentVars } from '@services/graphql/api';
import { sendComment } from '@services/graphql/comments';
-import useCodeBlocksTheme from '@utils/hooks/use-code-blocks-theme';
import useIsMounted from '@utils/hooks/use-is-mounted';
import Script from 'next/script';
import { FC, HTMLAttributes, ReactNode, useRef, useState } from 'react';
@@ -181,11 +180,9 @@ const PageLayout: FC<PageLayoutProps> = ({
* @param {MetaData} meta - The metadata.
*/
const hasMeta = (meta: MetaData) => {
- return Object.values(meta).every((value) => value === null);
+ return Object.values(meta).every((value) => value);
};
- useCodeBlocksTheme(bodyRef);
-
return (
<>
<Script