Extracts shared section blocks for use beyond the homepage
HomeSection becomes PageSection, since every page needs the same section frame, and gains a flushTop spacing for a block right below a page head. The framed photo next to text from the homepage About block becomes MediaText, and the accent label that four components wrote out by hand becomes Eyebrow, which can also render as a heading. The homepage renders exactly as before: a layout comparison of every element against master at three widths shows no differences. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import { screen } from '@testing-library/react';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { axeResults } from '@/test/axe';
|
||||
import { renderWithProviders } from '@/test/renderWithProviders';
|
||||
import PageSection from './PageSection';
|
||||
|
||||
describe('PageSection', () => {
|
||||
it('is a region named by its heading', async () => {
|
||||
const { container } = await renderWithProviders(
|
||||
<PageSection labelledBy="about-title" data-testid="about">
|
||||
<h2 id="about-title">Over mij</h2>
|
||||
</PageSection>,
|
||||
);
|
||||
expect(screen.getByRole('region', { name: 'Over mij' })).toBe(
|
||||
screen.getByTestId('about'),
|
||||
);
|
||||
expect(await axeResults(container)).toHaveNoViolations();
|
||||
});
|
||||
|
||||
it('passes the anchor id through for every tone, width and spacing', async () => {
|
||||
await renderWithProviders(
|
||||
<PageSection
|
||||
labelledBy="contact-title"
|
||||
id="contact"
|
||||
tone="alt"
|
||||
width="narrow"
|
||||
spacing="tight"
|
||||
>
|
||||
<h2 id="contact-title">Contact</h2>
|
||||
</PageSection>,
|
||||
);
|
||||
expect(screen.getByRole('region', { name: 'Contact' })).toHaveAttribute(
|
||||
'id',
|
||||
'contact',
|
||||
);
|
||||
});
|
||||
|
||||
it('has no space above when it sits flush below a page head', async () => {
|
||||
await renderWithProviders(
|
||||
<>
|
||||
<PageSection labelledBy="flush-title" data-testid="flush">
|
||||
<h2 id="flush-title">Hoi!</h2>
|
||||
</PageSection>
|
||||
<PageSection
|
||||
labelledBy="next-title"
|
||||
spacing="flushTop"
|
||||
data-testid="flush-top"
|
||||
>
|
||||
<h2 id="next-title">Verder</h2>
|
||||
</PageSection>
|
||||
</>,
|
||||
);
|
||||
const regular = getComputedStyle(screen.getByTestId('flush'));
|
||||
const flush = getComputedStyle(screen.getByTestId('flush-top'));
|
||||
expect(flush.paddingTop).toBe('0px');
|
||||
expect(flush.paddingBottom).toBe(regular.paddingBottom);
|
||||
expect(regular.paddingTop).not.toBe('0px');
|
||||
});
|
||||
|
||||
it('leaves out the content column at full width', async () => {
|
||||
await renderWithProviders(
|
||||
<PageSection labelledBy="insta-title" width="full">
|
||||
<h2 id="insta-title" data-testid="child">
|
||||
Instagram
|
||||
</h2>
|
||||
</PageSection>,
|
||||
);
|
||||
// The heading is a direct child of the section, not of a Container
|
||||
expect(screen.getByTestId('child').parentElement).toBe(
|
||||
screen.getByRole('region', { name: 'Instagram' }),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
import type { ReactNode } from 'react';
|
||||
import { Box, Container } from '@chakra-ui/react';
|
||||
|
||||
type PageSectionProps = {
|
||||
// Id of the heading that names this section for assistive technology
|
||||
labelledBy: string;
|
||||
id?: string;
|
||||
tone?: 'paper' | 'alt';
|
||||
// full: no content column, for edge-to-edge content; the section then
|
||||
// places its own Container around what should stay in the column
|
||||
width?: 'default' | 'narrow' | 'full';
|
||||
// flushTop: no space above, for the first block right below a page head
|
||||
// (design .section--flush-top)
|
||||
spacing?: 'default' | 'tight' | 'flushTop';
|
||||
'data-testid'?: string;
|
||||
children: ReactNode;
|
||||
};
|
||||
|
||||
// Frame of a content section on any page (design .section): vertical
|
||||
// rhythm, an optional alternate background and the content column.
|
||||
export default function PageSection({
|
||||
labelledBy,
|
||||
id,
|
||||
tone = 'paper',
|
||||
width = 'default',
|
||||
spacing = 'default',
|
||||
'data-testid': testId,
|
||||
children,
|
||||
}: PageSectionProps) {
|
||||
const padding = spacing === 'tight' ? 'sectionYTight' : 'sectionY';
|
||||
|
||||
return (
|
||||
<Box
|
||||
as="section"
|
||||
id={id}
|
||||
aria-labelledby={labelledBy}
|
||||
data-testid={testId}
|
||||
bg={tone === 'alt' ? 'paper.alt' : 'paper'}
|
||||
// An anchor target (#contact) lands right below the fixed header,
|
||||
// which has shrunk to its sticky height by the time it gets there
|
||||
scrollMarginTop={id ? 'header.sticky' : undefined}
|
||||
pt={spacing === 'flushTop' ? 0 : padding}
|
||||
pb={padding}
|
||||
>
|
||||
{width === 'full' ? (
|
||||
children
|
||||
) : (
|
||||
<Container
|
||||
maxW={width === 'narrow' ? 'contentNarrow' : 'content'}
|
||||
>
|
||||
{children}
|
||||
</Container>
|
||||
)}
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user