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/utils/hooks/use-prism-plugins.tsx | 115 ++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/utils/hooks/use-prism-plugins.tsx (limited to 'src/utils') diff --git a/src/utils/hooks/use-prism-plugins.tsx b/src/utils/hooks/use-prism-plugins.tsx new file mode 100644 index 0000000..c4959ac --- /dev/null +++ b/src/utils/hooks/use-prism-plugins.tsx @@ -0,0 +1,115 @@ +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; -- cgit v1.2.3 From 947a06bfdfdc5bca62c27fa2ee27f0ab9fefa0ea Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Fri, 22 Apr 2022 18:33:04 +0200 Subject: chore: add a TableOfContents component --- .../widgets/table-of-contents.stories.tsx | 41 ++++++ .../organisms/widgets/table-of-contents.test.tsx | 12 ++ .../organisms/widgets/table-of-contents.tsx | 53 +++++++ src/utils/hooks/use-headings-tree.tsx | 153 +++++++++++++++++++++ 4 files changed, 259 insertions(+) create mode 100644 src/components/organisms/widgets/table-of-contents.stories.tsx create mode 100644 src/components/organisms/widgets/table-of-contents.test.tsx create mode 100644 src/components/organisms/widgets/table-of-contents.tsx create mode 100644 src/utils/hooks/use-headings-tree.tsx (limited to 'src/utils') diff --git a/src/components/organisms/widgets/table-of-contents.stories.tsx b/src/components/organisms/widgets/table-of-contents.stories.tsx new file mode 100644 index 0000000..fba7c54 --- /dev/null +++ b/src/components/organisms/widgets/table-of-contents.stories.tsx @@ -0,0 +1,41 @@ +import { ComponentMeta, ComponentStory } from '@storybook/react'; +import { IntlProvider } from 'react-intl'; +import ToCWidget from './table-of-contents'; + +/** + * TableOfContents - Storybook Meta + */ +export default { + title: 'Organisms/Widgets', + component: ToCWidget, + argTypes: { + wrapper: { + control: { + type: null, + }, + description: + 'A reference to the HTML element that contains the headings.', + type: { + name: 'string', + required: true, + }, + }, + }, + decorators: [ + (Story) => ( + + + + ), + ], +} as ComponentMeta; + +const Template: ComponentStory = (args) => ( + +); + +/** + * Widgets Stories - Table of Contents + */ +export const TableOfContents = Template.bind({}); +TableOfContents.args = {}; diff --git a/src/components/organisms/widgets/table-of-contents.test.tsx b/src/components/organisms/widgets/table-of-contents.test.tsx new file mode 100644 index 0000000..2064f39 --- /dev/null +++ b/src/components/organisms/widgets/table-of-contents.test.tsx @@ -0,0 +1,12 @@ +import { render, screen } from '@test-utils'; +import TableOfContents from './table-of-contents'; + +describe('TableOfContents', () => { + it('renders the ToC title', () => { + const divEl = document.createElement('div'); + render(); + expect( + screen.getByRole('heading', { level: 2, name: /Table of Contents/i }) + ).toBeInTheDocument(); + }); +}); diff --git a/src/components/organisms/widgets/table-of-contents.tsx b/src/components/organisms/widgets/table-of-contents.tsx new file mode 100644 index 0000000..3778e02 --- /dev/null +++ b/src/components/organisms/widgets/table-of-contents.tsx @@ -0,0 +1,53 @@ +import useHeadingsTree, { type Heading } from '@utils/hooks/use-headings-tree'; +import { FC } from 'react'; +import { useIntl } from 'react-intl'; +import LinksListWidget, { type LinksListItems } from './links-list-widget'; + +type TableOfContentsProps = { + /** + * A reference to the HTML element that contains the headings. + */ + wrapper: HTMLElement; +}; + +/** + * Table of Contents widget component + * + * Render a table of contents. + */ +const TableOfContents: FC = ({ wrapper }) => { + const intl = useIntl(); + const headingsTree = useHeadingsTree(wrapper); + const title = intl.formatMessage({ + defaultMessage: 'Table of Contents', + description: 'TableOfContents: the widget title', + id: 'WKG9wj', + }); + + /** + * Convert an headings tree to list items. + * + * @param {Heading[]} tree - The headings tree. + * @returns {LinksListItems[]} The list items. + */ + const getItems = (tree: Heading[]): LinksListItems[] => { + return tree.map((heading) => { + return { + name: heading.title, + url: `#${heading.id}`, + child: getItems(heading.children), + }; + }); + }; + + return ( + + ); +}; + +export default TableOfContents; diff --git a/src/utils/hooks/use-headings-tree.tsx b/src/utils/hooks/use-headings-tree.tsx new file mode 100644 index 0000000..5506e8b --- /dev/null +++ b/src/utils/hooks/use-headings-tree.tsx @@ -0,0 +1,153 @@ +import { slugify } from '@utils/helpers/slugify'; +import { useCallback, useEffect, useMemo, useState } from 'react'; + +export type Heading = { + /** + * The heading depth. + */ + depth: number; + /** + * The heading id. + */ + id: string; + /** + * The heading children. + */ + children: Heading[]; + /** + * The heading title. + */ + title: string; +}; + +/** + * Get the headings tree of the given HTML element. + * + * @param {HTMLElement} wrapper - An HTML element that contains the headings. + * @returns {Heading[]} The headings tree. + */ +const useHeadingsTree = (wrapper: HTMLElement): Heading[] => { + const depths = useMemo(() => ['h2', 'h3', 'h4', 'h5', 'h6'], []); + const [allHeadings, setAllHeadings] = + useState>(); + const [headingsTree, setHeadingsTree] = useState([]); + + useEffect(() => { + const query = depths.join(', '); + const result: NodeListOf = + wrapper.querySelectorAll(query); + setAllHeadings(result); + }, [depths, wrapper]); + + const getDepth = useCallback( + /** + * Retrieve the heading element depth. + * + * @param {HTMLHeadingElement} el - An heading element. + * @returns {number} The heading depth. + */ + (el: HTMLHeadingElement): number => { + return depths.findIndex((depth) => depth === el.localName); + }, + [depths] + ); + + const formatHeadings = useCallback( + /** + * Convert a list of headings into an array of Heading objects. + * + * @param {NodeListOf} headings - A list of headings. + * @returns {Heading[]} An array of Heading objects. + */ + (headings: NodeListOf): Heading[] => { + const formattedHeadings: Heading[] = []; + + Array.from(headings).forEach((heading) => { + const title: string = heading.textContent!; + const id = slugify(title); + const depth = getDepth(heading); + const children: Heading[] = []; + + heading.id = id; + + formattedHeadings.push({ + depth, + id, + children, + title, + }); + }); + + return formattedHeadings; + }, + [getDepth] + ); + + const buildSubTree = useCallback( + /** + * Build the heading subtree. + * + * @param {Heading} parent - The heading parent. + * @param {Heading} currentHeading - The current heading element. + */ + (parent: Heading, currentHeading: Heading): void => { + if (parent.depth === currentHeading.depth - 1) { + parent.children.push(currentHeading); + } else { + const lastItem = parent.children[parent.children.length - 1]; + buildSubTree(lastItem, currentHeading); + } + }, + [] + ); + + const buildTree = useCallback( + /** + * Build a heading tree. + * + * @param {Heading[]} headings - An array of Heading objects. + * @returns {Heading[]} The headings tree. + */ + (headings: Heading[]): Heading[] => { + const tree: Heading[] = []; + + headings.forEach((heading) => { + if (heading.depth === 0) { + tree.push(heading); + } else { + const lastItem = tree[tree.length - 1]; + buildSubTree(lastItem, heading); + } + }); + + return tree; + }, + [buildSubTree] + ); + + const getHeadingsTree = useCallback( + /** + * Retrieve a headings tree from a list of headings element. + * + * @param {NodeListOf} headings - A headings list. + * @returns {Heading[]} The headings tree. + */ + (headings: NodeListOf): Heading[] => { + const formattedHeadings = formatHeadings(headings); + + return buildTree(formattedHeadings); + }, + [formatHeadings, buildTree] + ); + + useEffect(() => { + if (allHeadings) { + const headingsList = getHeadingsTree(allHeadings); + setHeadingsTree(headingsList); + } + }, [allHeadings, getHeadingsTree]); + + return headingsTree; +}; + +export default useHeadingsTree; -- cgit v1.2.3 From 5a6584777e42e6e3e55294d357cb0adafe2853e7 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Fri, 22 Apr 2022 18:46:48 +0200 Subject: chore: add a Layout component It defines the different components used by all other layouts. --- .../organisms/toolbar/toolbar.module.scss | 6 +- .../organisms/toolbar/toolbar.stories.tsx | 3 + src/components/templates/layout/layout.module.scss | 35 ++++++ src/components/templates/layout/layout.stories.tsx | 57 ++++++++++ src/components/templates/layout/layout.test.tsx | 34 ++++++ src/components/templates/layout/layout.tsx | 125 +++++++++++++++++++++ src/styles/base/_fonts.scss | 12 +- src/styles/base/_helpers.scss | 6 - src/styles/base/_spacings.scss | 10 +- src/utils/config.ts | 2 +- 10 files changed, 267 insertions(+), 23 deletions(-) create mode 100644 src/components/templates/layout/layout.module.scss create mode 100644 src/components/templates/layout/layout.stories.tsx create mode 100644 src/components/templates/layout/layout.test.tsx create mode 100644 src/components/templates/layout/layout.tsx (limited to 'src/utils') diff --git a/src/components/organisms/toolbar/toolbar.module.scss b/src/components/organisms/toolbar/toolbar.module.scss index bb0a91f..cda9b37 100644 --- a/src/components/organisms/toolbar/toolbar.module.scss +++ b/src/components/organisms/toolbar/toolbar.module.scss @@ -26,7 +26,11 @@ .modal { &--search, &--settings { - min-width: 30ch; + @include mix.media("screen") { + @include mix.dimensions("sm") { + min-width: 35ch; + } + } } } diff --git a/src/components/organisms/toolbar/toolbar.stories.tsx b/src/components/organisms/toolbar/toolbar.stories.tsx index de94e31..4f9a3de 100644 --- a/src/components/organisms/toolbar/toolbar.stories.tsx +++ b/src/components/organisms/toolbar/toolbar.stories.tsx @@ -38,6 +38,9 @@ export default { ), ], + parameters: { + layout: 'fullscreen', + }, } as ComponentMeta; const Template: ComponentStory = (args) => ( diff --git a/src/components/templates/layout/layout.module.scss b/src/components/templates/layout/layout.module.scss new file mode 100644 index 0000000..eb84c70 --- /dev/null +++ b/src/components/templates/layout/layout.module.scss @@ -0,0 +1,35 @@ +@use "@styles/abstracts/functions" as fun; +@use "@styles/abstracts/mixins" as mix; + +:global { + #__next { + flex: 1; + display: flex; + flex-flow: column nowrap; + min-height: 100vh; + } +} + +.header { + padding: var(--spacing-sm) 0 var(--spacing-md); + border-bottom: fun.convert-px(3) solid var(--color-border-light); +} + +.main { + flex: 1; +} + +.footer { + border-top: fun.convert-px(3) solid var(--color-border-light); +} + +.noscript-spacing { + width: 100%; + height: fun.convert-px(75); + + @include mix.media("screen") { + @include mix.dimensions("xs") { + height: fun.convert-px(50); + } + } +} diff --git a/src/components/templates/layout/layout.stories.tsx b/src/components/templates/layout/layout.stories.tsx new file mode 100644 index 0000000..f3579e3 --- /dev/null +++ b/src/components/templates/layout/layout.stories.tsx @@ -0,0 +1,57 @@ +import { ComponentMeta, ComponentStory } from '@storybook/react'; +import { IntlProvider } from 'react-intl'; +import LayoutComponent from './layout'; + +/** + * Layout - Storybook Meta + */ +export default { + title: 'Templates/LayoutBase', + component: LayoutComponent, + argTypes: { + children: { + control: { + type: 'text', + }, + description: 'The article content.', + type: { + name: 'string', + required: true, + }, + }, + className: { + control: { + type: 'text', + }, + description: 'Set additional classnames to the article element.', + table: { + category: 'Styles', + }, + type: { + name: 'string', + required: false, + }, + }, + }, + decorators: [ + (Story) => ( + +
+ +
+
+ ), + ], + parameters: { + layout: 'fullscreen', + }, +} as ComponentMeta; + +const Template: ComponentStory = (args) => ( + +); + +/** + * Layout Stories - Default + */ +export const LayoutBase = Template.bind({}); diff --git a/src/components/templates/layout/layout.test.tsx b/src/components/templates/layout/layout.test.tsx new file mode 100644 index 0000000..914e1cd --- /dev/null +++ b/src/components/templates/layout/layout.test.tsx @@ -0,0 +1,34 @@ +import { render, screen } from '@test-utils'; +import Layout from './layout'; + +const body = + 'Sit dolorem eveniet. Sit sit odio nemo vitae corrupti modi sint est rerum. Pariatur quidem maiores distinctio. Quia et illum aspernatur est cum.'; + +describe('Layout', () => { + it('renders the website header', () => { + render({body}); + expect(screen.getByRole('banner')).toBeInTheDocument(); + }); + + it('renders the website main content', () => { + render({body}); + expect(screen.getByRole('main')).toBeInTheDocument(); + }); + + it('renders the website footer', () => { + render({body}); + expect(screen.getByRole('contentinfo')).toBeInTheDocument(); + }); + + it('renders a skip to content link', () => { + render({body}); + expect( + screen.getByRole('link', { name: 'Skip to content' }) + ).toBeInTheDocument(); + }); + + it('renders an article', () => { + render({body}); + expect(screen.getByRole('article')).toHaveTextContent(body); + }); +}); diff --git a/src/components/templates/layout/layout.tsx b/src/components/templates/layout/layout.tsx new file mode 100644 index 0000000..601ced4 --- /dev/null +++ b/src/components/templates/layout/layout.tsx @@ -0,0 +1,125 @@ +import photo from '@assets/images/armand-philippot.jpg'; +import ButtonLink from '@components/atoms/buttons/button-link'; +import Career from '@components/atoms/icons/career'; +import CCBySA from '@components/atoms/icons/cc-by-sa'; +import ComputerScreen from '@components/atoms/icons/computer-screen'; +import Envelop from '@components/atoms/icons/envelop'; +import Home from '@components/atoms/icons/home'; +import PostsStack from '@components/atoms/icons/posts-stack'; +import Main from '@components/atoms/layout/main'; +import NoScript from '@components/atoms/layout/no-script'; +import Footer from '@components/organisms/layout/footer'; +import Header, { HeaderProps } from '@components/organisms/layout/header'; +import { settings } from '@utils/config'; +import { FC, ReactNode } from 'react'; +import { useIntl } from 'react-intl'; +import styles from './layout.module.scss'; + +export type LayoutProps = Pick & { + /** + * The layout main content. + */ + children: ReactNode; + /** + * Set additional classnames to the article element. + */ + className?: string; +}; + +/** + * Layout component + * + * Render the base layout used by all pages. + */ +const Layout: FC = ({ children, isHome, ...props }) => { + const intl = useIntl(); + const skipToContent = intl.formatMessage({ + defaultMessage: 'Skip to content', + description: 'Layout: Skip to content link', + id: 'K4rYdT', + }); + const noScript = intl.formatMessage({ + defaultMessage: + 'Warning: If you want to benefit from all features (search for example), please activate Javascript.', + description: 'Layout: noscript message', + id: '7jVUT6', + }); + + const copyright = { + dates: { + start: settings.copyright.startYear, + end: settings.copyright.endYear, + }, + owner: settings.name, + icon: , + }; + + const homeLabel = intl.formatMessage({ + defaultMessage: 'Home', + description: 'Layout: main nav - home link', + id: 'bojYF5', + }); + const blogLabel = intl.formatMessage({ + defaultMessage: 'Blog', + description: 'Layout: main nav - blog link', + id: 'D8vB38', + }); + const projectsLabel = intl.formatMessage({ + defaultMessage: 'Projects', + description: 'Layout: main nav - projects link', + id: 'qnwsWV', + }); + const cvLabel = intl.formatMessage({ + defaultMessage: 'CV', + description: 'Layout: main nav - cv link', + id: 'R895yC', + }); + const contactLabel = intl.formatMessage({ + defaultMessage: 'Contact', + description: 'Layout: main nav - contact link', + id: 'AE4kCD', + }); + + const nav: HeaderProps['nav'] = [ + { id: 'home', label: homeLabel, href: '#', logo: }, + { id: 'blog', label: blogLabel, href: '#', logo: }, + { + id: 'projects', + label: projectsLabel, + href: '#', + logo: , + }, + { id: 'cv', label: cvLabel, href: '#', logo: }, + { id: 'contact', label: contactLabel, href: '#', logo: }, + ]; + + return ( + <> + + + + {skipToContent} + +
+
+
{children}
+
+