From 362cf45bc520a68a1c1be20e1189ca2307577dde Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Tue, 19 Apr 2022 12:12:15 +0200 Subject: chore: add a Code component --- src/components/molecules/layout/code.tsx | 101 +++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/components/molecules/layout/code.tsx (limited to 'src/components/molecules/layout/code.tsx') diff --git a/src/components/molecules/layout/code.tsx b/src/components/molecules/layout/code.tsx new file mode 100644 index 0000000..2959ae5 --- /dev/null +++ b/src/components/molecules/layout/code.tsx @@ -0,0 +1,101 @@ +import usePrismPlugins, { + type PrismPlugin, +} from '@utils/hooks/use-prism-plugins'; +import { FC } from 'react'; +import styles from './code.module.scss'; + +export type PrismLanguage = + | 'apacheconf' + | 'bash' + | 'css' + | 'diff' + | 'docker' + | 'editorconfig' + | 'ejs' + | 'git' + | 'graphql' + | 'html' + | 'ignore' + | 'ini' + | 'javascript' + | 'jsdoc' + | 'json' + | 'jsx' + | 'makefile' + | 'markup' + | 'php' + | 'phpdoc' + | 'regex' + | 'scss' + | 'shell-session' + | 'smarty' + | 'tcl' + | 'toml' + | 'tsx' + | 'twig' + | 'yaml'; + +export type OptionalPrismPlugin = Extract< + PrismPlugin, + | 'command-line' + | 'diff-highlight' + | 'inline-color' + | 'line-highlight' + | 'line-numbers' +>; + +export type CodeProps = { + /** + * The code to highlight. + */ + children: string; + /** + * Filter command line output. Default: false. + */ + filterOutput?: boolean; + /** + * The code language. + */ + language: PrismLanguage; + /** + * The optional Prism plugins. + */ + plugins?: OptionalPrismPlugin[]; + /** + * Filter command line output using the given string. Default: #output# + */ + outputPattern?: string; +}; + +/** + * Code component + * + * Render a code block with syntax highlighting. + */ +const Code: FC = ({ + children, + filterOutput = false, + language, + plugins = [], + outputPattern = '#output#', +}) => { + const { pluginsAttribute, pluginsClassName } = usePrismPlugins(plugins); + + const outputAttribute = filterOutput + ? { 'data-filter-output': outputPattern } + : {}; + + return ( +
+
+        {children}
+      
+
+ ); +}; + +export default Code; -- cgit v1.2.3 From bbd63400f94b43fde04449e0c71d14763d893e6a Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Thu, 19 May 2022 19:46:24 +0200 Subject: refactor: rewrite Prism hooks and providers It avoid some hydratation errors on project pages (not in article however) and the hooks are now reusable. --- src/components/molecules/forms/motion-toggle.tsx | 3 +- src/components/molecules/layout/code.tsx | 25 +--- src/components/templates/page/page-layout.tsx | 5 +- src/pages/article/[slug].tsx | 54 +++++-- src/styles/pages/partials/_article-prism.scss | 2 +- src/utils/helpers/strings.ts | 10 ++ src/utils/hooks/use-add-classname.tsx | 34 +++++ src/utils/hooks/use-add-prism-class-attr.tsx | 60 -------- src/utils/hooks/use-attributes.tsx | 35 ++++- src/utils/hooks/use-code-blocks-theme.tsx | 22 --- src/utils/hooks/use-prism-plugins.tsx | 115 -------------- src/utils/hooks/use-prism.tsx | 182 +++++++++++++++++++++++ src/utils/hooks/use-query-selector-all.tsx | 12 +- src/utils/providers/prism-theme.tsx | 78 ++++------ 14 files changed, 346 insertions(+), 291 deletions(-) create mode 100644 src/utils/hooks/use-add-classname.tsx delete mode 100644 src/utils/hooks/use-add-prism-class-attr.tsx delete mode 100644 src/utils/hooks/use-code-blocks-theme.tsx delete mode 100644 src/utils/hooks/use-prism-plugins.tsx create mode 100644 src/utils/hooks/use-prism.tsx (limited to 'src/components/molecules/layout/code.tsx') 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 = ({ 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 = ({ plugins = [], outputPattern = '#output#', }) => { - const { pluginsAttribute, pluginsClassName } = usePrismPlugins(plugins); + const wrapperRef = useRef(null); + const { attributes, className } = usePrism({ language, plugins }); const outputAttribute = filterOutput ? { 'data-filter-output': outputPattern } : {}; return ( -
+
         {children}
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 = ({
    * @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 (
     <>