summaryrefslogtreecommitdiffstats
path: root/src/utils/hooks
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/hooks')
-rw-r--r--src/utils/hooks/use-add-classname.tsx34
-rw-r--r--src/utils/hooks/use-add-prism-class-attr.tsx60
-rw-r--r--src/utils/hooks/use-attributes.tsx35
-rw-r--r--src/utils/hooks/use-code-blocks-theme.tsx22
-rw-r--r--src/utils/hooks/use-prism-plugins.tsx115
-rw-r--r--src/utils/hooks/use-prism.tsx182
-rw-r--r--src/utils/hooks/use-query-selector-all.tsx12
7 files changed, 253 insertions, 207 deletions
diff --git a/src/utils/hooks/use-add-classname.tsx b/src/utils/hooks/use-add-classname.tsx
new file mode 100644
index 0000000..0584084
--- /dev/null
+++ b/src/utils/hooks/use-add-classname.tsx
@@ -0,0 +1,34 @@
+import { useCallback, useEffect } from 'react';
+
+export type UseAddClassNameProps = {
+ className: string;
+ element?: HTMLElement;
+ elements?: NodeListOf<HTMLElement> | HTMLElement[];
+};
+
+/**
+ * Add className to the given element(s).
+ *
+ * @param {UseAddClassNameProps} props - An object with classnames and one or more elements.
+ */
+const useAddClassName = ({
+ className,
+ element,
+ elements,
+}: UseAddClassNameProps) => {
+ const classNames = className.split(' ').filter((string) => string !== '');
+
+ const setClassName = useCallback(
+ (el: HTMLElement) => {
+ el.classList.add(...classNames);
+ },
+ [classNames]
+ );
+
+ useEffect(() => {
+ if (element) setClassName(element);
+ if (elements && elements.length > 0) elements.forEach(setClassName);
+ }, [element, elements, setClassName]);
+};
+
+export default useAddClassName;
diff --git a/src/utils/hooks/use-add-prism-class-attr.tsx b/src/utils/hooks/use-add-prism-class-attr.tsx
deleted file mode 100644
index 7d33cc2..0000000
--- a/src/utils/hooks/use-add-prism-class-attr.tsx
+++ /dev/null
@@ -1,60 +0,0 @@
-import { useCallback, useEffect, useState } from 'react';
-
-export type AttributesMap = {
- [key: string]: string;
-};
-
-export type useAddPrismClassAttrProps = {
- attributes?: AttributesMap;
- classNames?: string;
-};
-
-/**
- * Add classnames and/or attributes to pre elements.
- *
- * @param props - An object of attributes and classnames.
- */
-const useAddPrismClassAttr = ({
- attributes,
- classNames,
-}: useAddPrismClassAttrProps) => {
- const [elements, setElements] = useState<HTMLPreElement[]>([]);
-
- useEffect(() => {
- const targetElements = document.querySelectorAll('pre');
- setElements(Array.from(targetElements));
- }, []);
-
- const setClassNameAndAttributes = useCallback(
- (array: HTMLElement[]) => {
- array.forEach((el) => {
- if (classNames) {
- const classNamesArray = classNames.split(' ');
- const isCommandLine = el.classList.contains('command-line');
- const removedClassName = isCommandLine
- ? 'line-numbers'
- : 'command-line';
- const filteredClassNames = classNamesArray.filter(
- (className) => className !== removedClassName
- );
- filteredClassNames.forEach((className) =>
- el.classList.add(className)
- );
- }
-
- if (attributes) {
- for (const [key, value] of Object.entries(attributes)) {
- el.setAttribute(key, value);
- }
- }
- });
- },
- [attributes, classNames]
- );
-
- useEffect(() => {
- if (elements.length > 0) setClassNameAndAttributes(elements);
- }, [elements, setClassNameAndAttributes]);
-};
-
-export default useAddPrismClassAttr;
diff --git a/src/utils/hooks/use-attributes.tsx b/src/utils/hooks/use-attributes.tsx
index 97a7b43..6d18048 100644
--- a/src/utils/hooks/use-attributes.tsx
+++ b/src/utils/hooks/use-attributes.tsx
@@ -1,4 +1,5 @@
-import { useEffect } from 'react';
+import { fromKebabCaseToCamelCase } from '@utils/helpers/strings';
+import { useCallback, useEffect } from 'react';
export type useAttributesProps = {
/**
@@ -6,6 +7,10 @@ export type useAttributesProps = {
*/
element?: HTMLElement;
/**
+ * A node list of HTML Element.
+ */
+ elements?: NodeListOf<HTMLElement> | HTMLElement[];
+ /**
* The attribute name.
*/
attribute: string;
@@ -20,14 +25,28 @@ export type useAttributesProps = {
*
* @param props - An object with element, attribute name and value.
*/
-const useAttributes = ({ element, attribute, value }: useAttributesProps) => {
+const useAttributes = ({
+ element,
+ elements,
+ attribute,
+ value,
+}: useAttributesProps) => {
+ const setAttribute = useCallback(
+ (el: HTMLElement) => {
+ if (attribute.startsWith('data')) {
+ el.setAttribute(attribute, value);
+ } else {
+ const camelCaseAttribute = fromKebabCaseToCamelCase(attribute);
+ el.dataset[camelCaseAttribute] = value;
+ }
+ },
+ [attribute, value]
+ );
+
useEffect(() => {
- if (element) {
- element.dataset[attribute] = value;
- } else {
- document.documentElement.dataset[attribute] = value;
- }
- }, [attribute, element, value]);
+ if (element) setAttribute(element);
+ if (elements && elements.length > 0) elements.forEach(setAttribute);
+ }, [element, elements, setAttribute]);
};
export default useAttributes;
diff --git a/src/utils/hooks/use-code-blocks-theme.tsx b/src/utils/hooks/use-code-blocks-theme.tsx
deleted file mode 100644
index beb7b29..0000000
--- a/src/utils/hooks/use-code-blocks-theme.tsx
+++ /dev/null
@@ -1,22 +0,0 @@
-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;
diff --git a/src/utils/hooks/use-prism-plugins.tsx b/src/utils/hooks/use-prism-plugins.tsx
deleted file mode 100644
index c4959ac..0000000
--- a/src/utils/hooks/use-prism-plugins.tsx
+++ /dev/null
@@ -1,115 +0,0 @@
-import Prism from 'prismjs';
-import { useEffect, useMemo } from 'react';
-import { useIntl } from 'react-intl';
-
-export type PrismPlugin =
- | 'autoloader'
- | 'color-scheme'
- | 'command-line'
- | 'copy-to-clipboard'
- | 'diff-highlight'
- | 'inline-color'
- | 'line-highlight'
- | 'line-numbers'
- | 'match-braces'
- | 'normalize-whitespace'
- | 'show-language'
- | 'toolbar';
-
-/**
- * Import and configure all given Prism plugins.
- *
- * @param {PrismPlugin[]} prismPlugins - The Prism plugins to activate.
- */
-const loadPrismPlugins = async (prismPlugins: PrismPlugin[]) => {
- for (const plugin of prismPlugins) {
- try {
- if (plugin === 'color-scheme') {
- await import(`@utils/plugins/prism-${plugin}`);
- } else {
- await import(`prismjs/plugins/${plugin}/prism-${plugin}.min.js`);
- }
-
- if (plugin === 'autoloader') {
- Prism.plugins.autoloader.languages_path = '/prism/';
- }
- } catch (error) {
- console.error(
- 'usePrismPlugins: an error occurred while loading Prism plugins.'
- );
- console.error(error);
- }
- }
-};
-
-/**
- * Load both the given Prism plugins and the default plugins.
- *
- * @param {PrismPlugin[]} plugins - The Prism plugins to activate.
- */
-const usePrismPlugins = (plugins: PrismPlugin[]) => {
- const intl = useIntl();
-
- const copyText = intl.formatMessage({
- defaultMessage: 'Copy',
- description: 'usePrismPlugins: copy button text (not clicked)',
- id: 'FIE/eC',
- });
- const copiedText = intl.formatMessage({
- defaultMessage: 'Copied!',
- description: 'usePrismPlugins: copy button text (clicked)',
- id: 'MzLdEl',
- });
- const errorText = intl.formatMessage({
- defaultMessage: 'Use Ctrl+c to copy',
- description: 'usePrismPlugins: copy button error text',
- id: '0XePFn',
- });
- const darkTheme = intl.formatMessage({
- defaultMessage: 'Dark Theme 🌙',
- description: 'usePrismPlugins: toggle dark theme button text',
- id: 'jo9vr5',
- });
- const lightTheme = intl.formatMessage({
- defaultMessage: 'Light Theme 🌞',
- description: 'usePrismPlugins: toggle light theme button text',
- id: '6EUEtH',
- });
-
- const attributes = {
- 'data-prismjs-copy': copyText,
- 'data-prismjs-copy-success': copiedText,
- 'data-prismjs-copy-error': errorText,
- 'data-prismjs-color-scheme-dark': darkTheme,
- 'data-prismjs-color-scheme-light': lightTheme,
- };
-
- const defaultPlugins: PrismPlugin[] = useMemo(
- () => [
- 'toolbar',
- 'autoloader',
- 'show-language',
- 'copy-to-clipboard',
- 'color-scheme',
- 'match-braces',
- 'normalize-whitespace',
- ],
- []
- );
-
- useEffect(() => {
- loadPrismPlugins([...defaultPlugins, ...plugins]).then(() => {
- Prism.highlightAll();
- });
- }, [defaultPlugins, plugins]);
-
- const defaultPluginsClasses = 'match-braces';
- const pluginsClasses = plugins.join(' ');
-
- return {
- pluginsAttribute: attributes,
- pluginsClassName: `${defaultPluginsClasses} ${pluginsClasses}`,
- };
-};
-
-export default usePrismPlugins;
diff --git a/src/utils/hooks/use-prism.tsx b/src/utils/hooks/use-prism.tsx
new file mode 100644
index 0000000..ef1a4c8
--- /dev/null
+++ b/src/utils/hooks/use-prism.tsx
@@ -0,0 +1,182 @@
+import Prism from 'prismjs';
+import { useEffect, useMemo } from 'react';
+import { useIntl } from 'react-intl';
+
+const PRISM_PLUGINS = [
+ 'autoloader',
+ 'color-scheme',
+ 'command-line',
+ 'copy-to-clipboard',
+ 'diff-highlight',
+ 'inline-color',
+ 'line-highlight',
+ 'line-numbers',
+ 'match-braces',
+ 'normalize-whitespace',
+ 'show-language',
+ 'toolbar',
+] as const;
+
+export type PrismPlugin = typeof PRISM_PLUGINS[number];
+
+export type DefaultPrismPlugin = Extract<
+ PrismPlugin,
+ | 'autoloader'
+ | 'color-scheme'
+ | 'copy-to-clipboard'
+ | 'match-braces'
+ | 'normalize-whitespace'
+ | 'show-language'
+ | 'toolbar'
+>;
+
+export type OptionalPrismPlugin = Exclude<PrismPlugin, DefaultPrismPlugin>;
+
+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 PrismAttributes = {
+ 'data-prismjs-copy': string;
+ 'data-prismjs-copy-success': string;
+ 'data-prismjs-copy-error': string;
+ 'data-prismjs-color-scheme-dark': string;
+ 'data-prismjs-color-scheme-light': string;
+};
+
+export type UsePrismProps = {
+ language?: PrismLanguage;
+ plugins: OptionalPrismPlugin[];
+};
+
+export type UsePrismReturn = {
+ attributes: PrismAttributes;
+ className: string;
+};
+
+/**
+ * Import and configure all given Prism plugins.
+ *
+ * @param {PrismPlugin[]} plugins - The Prism plugins to activate.
+ */
+const loadPrismPlugins = async (plugins: PrismPlugin[]) => {
+ for (const plugin of plugins) {
+ try {
+ if (plugin === 'color-scheme') {
+ await import(`@utils/plugins/prism-${plugin}`);
+ } else {
+ await import(`prismjs/plugins/${plugin}/prism-${plugin}.min.js`);
+ }
+
+ if (plugin === 'autoloader') {
+ Prism.plugins.autoloader.languages_path = '/prism/';
+ }
+ } catch (error) {
+ console.error('usePrism: an error occurred while loading Prism plugins.');
+ console.error(error);
+ }
+ }
+};
+
+/**
+ * Use Prism and its plugins.
+ *
+ * @param {UsePrismProps} props - An object of options.
+ * @returns {UsePrismReturn} An object of data.
+ */
+const usePrism = ({ language, plugins }: UsePrismProps): UsePrismReturn => {
+ /**
+ * The order matter. Toolbar must be loaded before some other plugins.
+ */
+ const defaultPlugins: DefaultPrismPlugin[] = useMemo(
+ () => [
+ 'toolbar',
+ 'autoloader',
+ 'show-language',
+ 'copy-to-clipboard',
+ 'color-scheme',
+ 'match-braces',
+ 'normalize-whitespace',
+ ],
+ []
+ );
+
+ useEffect(() => {
+ loadPrismPlugins([...defaultPlugins, ...plugins]).then(() => {
+ Prism.highlightAll();
+ });
+ }, [defaultPlugins, plugins]);
+
+ const defaultClassName = 'match-braces';
+ const languageClassName = language ? `language-${language}` : '';
+ const pluginsClassName = plugins.join(' ');
+ const className = `${defaultClassName} ${pluginsClassName} ${languageClassName}`;
+
+ const intl = useIntl();
+ const copyText = intl.formatMessage({
+ defaultMessage: 'Copy',
+ description: 'usePrism: copy button text (not clicked)',
+ id: '6GySNl',
+ });
+ const copiedText = intl.formatMessage({
+ defaultMessage: 'Copied!',
+ description: 'usePrism: copy button text (clicked)',
+ id: 'nsw6Th',
+ });
+ const errorText = intl.formatMessage({
+ defaultMessage: 'Use Ctrl+c to copy',
+ description: 'usePrism: copy button error text',
+ id: 'lKhTGM',
+ });
+ const darkTheme = intl.formatMessage({
+ defaultMessage: 'Dark Theme 🌙',
+ description: 'usePrism: toggle dark theme button text',
+ id: 'QLisK6',
+ });
+ const lightTheme = intl.formatMessage({
+ defaultMessage: 'Light Theme 🌞',
+ description: 'usePrism: toggle light theme button text',
+ id: 'hHVgW3',
+ });
+ const attributes = {
+ 'data-prismjs-copy': copyText,
+ 'data-prismjs-copy-success': copiedText,
+ 'data-prismjs-copy-error': errorText,
+ 'data-prismjs-color-scheme-dark': darkTheme,
+ 'data-prismjs-color-scheme-light': lightTheme,
+ };
+
+ return {
+ attributes,
+ className,
+ };
+};
+
+export default usePrism;
diff --git a/src/utils/hooks/use-query-selector-all.tsx b/src/utils/hooks/use-query-selector-all.tsx
index dbeec90..6ac8a08 100644
--- a/src/utils/hooks/use-query-selector-all.tsx
+++ b/src/utils/hooks/use-query-selector-all.tsx
@@ -1,14 +1,22 @@
+import { useRouter } from 'next/router';
import { useEffect, useState } from 'react';
+/**
+ * Use `document.querySelectorAll`.
+ *
+ * @param {string} query - A query.
+ * @returns {NodeListOf<HTMLElementTagNameMap[T]|undefined>} - The node list.
+ */
const useQuerySelectorAll = <T extends keyof HTMLElementTagNameMap>(
query: string
-) => {
+): NodeListOf<HTMLElementTagNameMap[T]> | undefined => {
const [elements, setElements] =
useState<NodeListOf<HTMLElementTagNameMap[T]>>();
+ const { asPath } = useRouter();
useEffect(() => {
setElements(document.querySelectorAll(query));
- }, [query]);
+ }, [asPath, query]);
return elements;
};