aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/templates/layout
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-05-17 22:48:41 +0200
committerArmand Philippot <git@armandphilippot.com>2022-05-17 22:48:41 +0200
commit271ef6debaca7ed9a01829dcef3a37e90a2dff05 (patch)
treeaa1513b40e7020a44cfcaaedc3a33d39ecfb8af7 /src/components/templates/layout
parent4e53a8654441481029746ff4e35a4a19c8d83709 (diff)
chore: use persistent layout
It prevents to rerender the common components between pages (header, footer...).
Diffstat (limited to 'src/components/templates/layout')
-rw-r--r--src/components/templates/layout/layout.module.scss14
-rw-r--r--src/components/templates/layout/layout.test.tsx11
-rw-r--r--src/components/templates/layout/layout.tsx54
3 files changed, 51 insertions, 28 deletions
diff --git a/src/components/templates/layout/layout.module.scss b/src/components/templates/layout/layout.module.scss
index 09904dc..1080732 100644
--- a/src/components/templates/layout/layout.module.scss
+++ b/src/components/templates/layout/layout.module.scss
@@ -1,5 +1,6 @@
@use "@styles/abstracts/functions" as fun;
@use "@styles/abstracts/mixins" as mix;
+@use "@styles/abstracts/placeholders";
.header {
border-bottom: fun.convert-px(3) solid var(--color-border-light);
@@ -9,6 +10,19 @@
flex: 1;
}
+.article {
+ &--grid {
+ @extend %grid;
+
+ grid-auto-flow: column dense;
+ align-items: baseline;
+ }
+
+ &--padding {
+ padding-bottom: var(--spacing-lg);
+ }
+}
+
.footer {
border-top: fun.convert-px(3) solid var(--color-border-light);
}
diff --git a/src/components/templates/layout/layout.test.tsx b/src/components/templates/layout/layout.test.tsx
index 94145ec..78547d4 100644
--- a/src/components/templates/layout/layout.test.tsx
+++ b/src/components/templates/layout/layout.test.tsx
@@ -4,33 +4,32 @@ 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.';
-const breadcrumbSchema: BreadcrumbList['itemListElement'][] = [];
describe('Layout', () => {
it('renders the website header', () => {
- render(<Layout breadcrumbSchema={breadcrumbSchema}>{body}</Layout>);
+ render(<Layout>{body}</Layout>);
expect(screen.getByRole('banner')).toBeInTheDocument();
});
it('renders the website main content', () => {
- render(<Layout breadcrumbSchema={breadcrumbSchema}>{body}</Layout>);
+ render(<Layout>{body}</Layout>);
expect(screen.getByRole('main')).toBeInTheDocument();
});
it('renders the website footer', () => {
- render(<Layout breadcrumbSchema={breadcrumbSchema}>{body}</Layout>);
+ render(<Layout>{body}</Layout>);
expect(screen.getByRole('contentinfo')).toBeInTheDocument();
});
it('renders a skip to content link', () => {
- render(<Layout breadcrumbSchema={breadcrumbSchema}>{body}</Layout>);
+ render(<Layout>{body}</Layout>);
expect(
screen.getByRole('link', { name: 'Skip to content' })
).toBeInTheDocument();
});
it('renders an article', () => {
- render(<Layout breadcrumbSchema={breadcrumbSchema}>{body}</Layout>);
+ render(<Layout>{body}</Layout>);
expect(screen.getByRole('article')).toHaveTextContent(body);
});
});
diff --git a/src/components/templates/layout/layout.tsx b/src/components/templates/layout/layout.tsx
index 3eb02ee..559eaed 100644
--- a/src/components/templates/layout/layout.tsx
+++ b/src/components/templates/layout/layout.tsx
@@ -9,18 +9,13 @@ import Main from '@components/atoms/layout/main';
import NoScript from '@components/atoms/layout/no-script';
import Footer, { type FooterProps } from '@components/organisms/layout/footer';
import Header, { type HeaderProps } from '@components/organisms/layout/header';
+import { type NextPageWithLayoutOptions } from '@ts/types/app';
import useScrollPosition from '@utils/hooks/use-scroll-position';
import useSettings from '@utils/hooks/use-settings';
import Script from 'next/script';
-import { FC, ReactNode, useState } from 'react';
+import { FC, ReactElement, ReactNode, useState } from 'react';
import { useIntl } from 'react-intl';
-import {
- BreadcrumbList,
- Person,
- SearchAction,
- WebSite,
- WithContext,
-} from 'schema-dts';
+import { Person, SearchAction, WebSite, WithContext } from 'schema-dts';
import styles from './layout.module.scss';
export type QueryAction = SearchAction & {
@@ -29,17 +24,17 @@ export type QueryAction = SearchAction & {
export type LayoutProps = Pick<HeaderProps, 'isHome'> & {
/**
- * The breadcrumb JSON schema.
- */
- breadcrumbSchema: BreadcrumbList['itemListElement'][];
- /**
* The layout main content.
*/
children: ReactNode;
/**
- * Set additional classnames to the article element.
+ * Determine if article has a comments section.
+ */
+ withExtraPadding?: boolean;
+ /**
+ * Determine if article should use grid. Default: false.
*/
- className?: string;
+ useGrid?: boolean;
};
/**
@@ -48,14 +43,16 @@ export type LayoutProps = Pick<HeaderProps, 'isHome'> & {
* Render the base layout used by all pages.
*/
const Layout: FC<LayoutProps> = ({
- breadcrumbSchema,
children,
+ withExtraPadding = false,
isHome,
- ...props
+ useGrid = false,
}) => {
const intl = useIntl();
const { website } = useSettings();
const { baseline, copyright, locales, name, picture, url } = website;
+ const articleGridClass = useGrid ? 'article--grid' : '';
+ const articleCommentsClass = withExtraPadding ? 'article--padding' : '';
const skipToContent = intl.formatMessage({
defaultMessage: 'Skip to content',
@@ -188,11 +185,6 @@ const Layout: FC<LayoutProps> = ({
type="application/ld+json"
dangerouslySetInnerHTML={{ __html: JSON.stringify(brandingSchema) }}
/>
- <Script
- id="schema-breadcrumb"
- type="application/ld+json"
- dangerouslySetInnerHTML={{ __html: JSON.stringify(breadcrumbSchema) }}
- />
<noscript>
<div className={styles['noscript-spacing']}></div>
</noscript>
@@ -211,7 +203,11 @@ const Layout: FC<LayoutProps> = ({
className={styles.header}
/>
<Main id="main" className={styles.main}>
- <article {...props}>{children}</article>
+ <article
+ className={`${styles[articleGridClass]} ${styles[articleCommentsClass]}`}
+ >
+ {children}
+ </article>
</Main>
<Footer
copyright={copyrightData}
@@ -227,4 +223,18 @@ const Layout: FC<LayoutProps> = ({
);
};
+/**
+ * Get the global layout.
+ *
+ * @param {ReactElement} page - A page.
+ * @param {boolean} [isHome] - Determine if it is the homepage.
+ * @returns A page wrapped with the global layout.
+ */
+export const getLayout = (
+ page: ReactElement,
+ props: NextPageWithLayoutOptions
+) => {
+ return <Layout {...props}>{page}</Layout>;
+};
+
export default Layout;