diff --git a/docs/hero-integration.md b/docs/hero-integration.md new file mode 100644 index 0000000..f5d12cb --- /dev/null +++ b/docs/hero-integration.md @@ -0,0 +1,36 @@ +# Hero from the CMS (SLP Software site) + +The title, lead and buttons of the hero come from the CMS. The baked hero stays as the fallback. Contract and CMS side: the CMS repository, `aidlc-docs/features/hero-module/inception/requirements/site-contract-agreed-with-asf.md` and `docs/hero-module.md`. + +## What is CMS content and what is fixed + +| Part | Source | +|---|---| +| Title (one field, a new line is a line break) | CMS, key `hero.title` | +| Lead | CMS, `hero.lead` (no lead: no paragraph) | +| Two buttons (label + target) | CMS, `hero.buttons` (none: no buttons) | +| Tagline, slides, display | Not used by this site (ignored) | +| Small line "Web & .NET development", code line, prompt | Fixed text, `src/i18n/nl.json`, block `home.hero` | +| Screen-reader and search-engine `h1` | Derived: `home.hero.headingPrefix` ("SLP Software:") + a space + the title lines joined with single spaces | +| Baked fallback (title lines, lead, buttons, heading) | `src/i18n/nl.json`, `home.hero.fallback`, via `heroContent` in `src/features/landing/data/content.ts` | + +Seed (`hero-sls.json`): title "Websites,\napps\n& meer." gives the heading "SLP Software: Websites, apps & meer." The baked heading stays "SLP software: websites, apps & meer" (unchanged, so the prerender is unchanged); the two differ only in capitals and the final full stop. + +## How it works + +1. `index.html` has `` and `` followed by a baked ``. The build (`scripts/prerender.mjs`) writes `` ... `` around the baked title, lead and buttons in the prerendered `#root` (the code strip is outside the region). The host replaces the regions and the data block per request (60 s cache); nothing else about the page changes. Without a host, or when the host answers 204, the markers are plain comments. +2. At start-up the app reads `#cms-data` once (`src/cms-client/hero`, `useCmsHero`), synchronously in the first render, so there is no flash of baked text. `parseHero` checks everything: no usable title, invalid JSON or no `hero` key gives the baked hero. Buttons with an empty label or a target that is not `#anchor`, `/path` or `https://...` are skipped (`isSafeTarget`), at most two are used. +3. `Hero.tsx` (design B) and `Era2026.tsx` (time travel 2026) render the same content: the first button solid, the second outline. +4. A change in the CMS is visible on the next page load (host cache 60 s, direct invalidation in the host). + +## Without a CMS hero (fallback) + +No data block, `hero: null` (204: no hero, not live, outside the schedule), or invalid data: the baked hero. The prerender always uses the baked hero. + +## Baseline + +The only change in the prerendered text compared with the build before this feature is the extra marker comments and the empty data block (`pnpm baseline:check` keeps `CMS:` comments and JSON blocks, so the saved baseline has to be saved again once: `pnpm baseline:save`; checked line by line that only those lines differ). + +## Open points for the CMS host + +See the report of this feature: the host renders the hero region only when the hero has a usable slide, and it writes picture/`h1` HTML without the lead. For this text-only site the region therefore stays the baked hero and the visitor gets the CMS text from the data block after the app has loaded. The site already styles the host's `cms-hero-*` classes (`src/index.css`) for the day the host can write a text-only region. diff --git a/docs/translations.md b/docs/translations.md index 2f1d5aa..ff8eae1 100644 --- a/docs/translations.md +++ b/docs/translations.md @@ -31,12 +31,12 @@ A key is a dotted path: the namespace, then the section, then the element, for e ## What is deliberately not in the language files -- The Hero (component and hero data): it becomes a CMS module. +- The title, lead and buttons of the Hero: they come from the CMS (data block `#cms-data`, see `docs/hero-integration.md`). The baked fallback texts of the Hero and its fixed lines (small line, code line, brand prefix of the heading) are in the language file, block `home.hero`. - Everything that comes from the CMS: packages, banners, and so on. - `src/cms-client`: it is a movable package and has its own messages (`messages.nl.ts`); it may not import from the app. - The privacy statement: it is its own feature and has its own text. -ESLint knows these exceptions: `react/jsx-no-literals` and a rule for text attributes (`aria-label`, `aria-description`, `alt`, `title`, `placeholder`, `label`) warn about a text typed straight into JSX in `src/**/*.tsx`, except in the Hero, tests and the privacy statement. Single technical signs (`/`, `|`, `-`, `:`, and so on), whitespace and numbers are allowed. They are warnings for now; they become errors when the move is done. +ESLint knows these exceptions: `react/jsx-no-literals` and a rule for text attributes (`aria-label`, `aria-description`, `alt`, `title`, `placeholder`, `label`) warn about a text typed straight into JSX in `src/**/*.tsx`, except in tests and the privacy statement. Single technical signs (`/`, `|`, `-`, `:`, and so on), whitespace and numbers are allowed. They are warnings for now; they become errors when the move is done. ## Adding a second language diff --git a/eslint.config.js b/eslint.config.js index cf8e1d8..0855257 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -26,13 +26,10 @@ export default tseslint.config( }, { // Texts of the site live in the language files (src/i18n), not in the JSX. A warning for now while - // the components are moved over. Left out: the Hero (becomes a CMS module), tests and the privacy - // statement (its own feature). + // the components are moved over. Left out: tests and the privacy statement (its own feature). The Hero is + // checked too: its texts come from the CMS or from the language file (home.hero). files: ['src/**/*.tsx'], ignores: [ - '**/Hero.tsx', - '**/Hero/**', - '**/hero/**', '**/*.test.tsx', '**/__tests__/**', 'src/test/**', diff --git a/index.html b/index.html index 7f5af33..c8a2937 100644 --- a/index.html +++ b/index.html @@ -45,6 +45,11 @@ } + + + diff --git a/scripts/prerender.mjs b/scripts/prerender.mjs index bf865e2..3ec436b 100644 --- a/scripts/prerender.mjs +++ b/scripts/prerender.mjs @@ -20,7 +20,18 @@ await build({ }); const { render } = await import(pathToFileURL(path.join(ssrDir, 'entry-server.mjs')).href); -const markup = render(); +// The hero marks the region the CMS host may replace; React cannot write comments, so the component renders +// placeholders that become here. Without a host they are +// plain comments and nothing changes for the visitor. +const markerPattern = /]*data-cms-marker="([^"]+)"[^>]*><\/i>/g; +const rendered = render(); +const markup = rendered.replace(markerPattern, (_all, name) => ``); +const expectedMarkers = ['', '']; +for (const marker of expectedMarkers) { + if (markup.split(marker).length !== 2 || /data-cms-marker/.test(markup)) { + throw new Error(`prerender: expected exactly one ${marker} in the static landing`); + } +} const html = await readFile(htmlPath, 'utf8'); const placeholder = '
'; diff --git a/src/features/landing/__tests__/HeroCms.test.tsx b/src/features/landing/__tests__/HeroCms.test.tsx new file mode 100644 index 0000000..187d511 --- /dev/null +++ b/src/features/landing/__tests__/HeroCms.test.tsx @@ -0,0 +1,128 @@ +import { render, screen } from '@testing-library/react'; +import { afterEach, describe, expect, it } from 'vitest'; +import { Hero } from '../components/Hero'; +import { Era2026 } from '../../../variants/e/Era2026'; +import { heroContent } from '../data/content'; + +const cmsHero = { + display: 'single', + title: 'Sites,\nen apps\nvoor jou.', + tagline: null, + lead: 'Een andere lead uit het CMS.', + buttons: [ + { label: 'Eerste knop', target: '#pakketten' }, + { label: 'Tweede knop', target: 'https://example.com/plan' }, + ], + slides: [], +}; + +function setBlock(data: unknown) { + document.body.innerHTML = ''; + document.getElementById('cms-data')!.textContent = typeof data === 'string' ? data : JSON.stringify(data); +} + +afterEach(() => { + document.body.innerHTML = ''; +}); + +describe('Hero with a CMS hero in the data block', () => { + it('shows the CMS title lines, lead and buttons', () => { + setBlock({ siteUrl: 'https://x', hero: cmsHero }); + render(); + expect(screen.getByTestId('hero-line-0')).toHaveTextContent('Sites,'); + expect(screen.getByTestId('hero-line-1')).toHaveTextContent('en apps'); + expect(screen.getByTestId('hero-line-2')).toHaveTextContent('voor jou.'); + expect(screen.getByText('Een andere lead uit het CMS.')).toBeInTheDocument(); + expect(screen.getByRole('link', { name: 'Eerste knop' })).toHaveAttribute('href', '#pakketten'); + const second = screen.getByRole('link', { name: 'Tweede knop' }); + expect(second).toHaveAttribute('href', 'https://example.com/plan'); + expect(second).toHaveAttribute('rel', 'noopener noreferrer'); + expect(screen.queryByText(heroContent.lead)).toBeNull(); + }); + + it('derives the screen-reader h1 from the title lines, with the brand prefix', () => { + setBlock({ hero: cmsHero }); + render(); + expect( + screen.getByRole('heading', { level: 1, name: 'SLP Software: Sites, en apps voor jou.' }), + ).toBeInTheDocument(); + }); + + it('gives the seed title exactly the heading that the contract expects', () => { + setBlock({ hero: { ...cmsHero, title: 'Websites,\napps\n& meer.' } }); + render(); + expect( + screen.getByRole('heading', { level: 1, name: 'SLP Software: Websites, apps & meer.' }), + ).toBeInTheDocument(); + }); + + it('leaves out the lead and the buttons when the CMS has none', () => { + setBlock({ hero: { ...cmsHero, lead: null, buttons: [] } }); + render(); + expect(screen.queryByRole('link')).toBeNull(); + expect(screen.queryByText('Een andere lead uit het CMS.')).toBeNull(); + }); + + it('does not render a button with an unsafe target', () => { + setBlock({ + hero: { + ...cmsHero, + buttons: [ + { label: 'Gevaarlijk', target: 'javascript:alert(1)' }, + { label: 'Veilig', target: '/pad' }, + ], + }, + }); + render(); + expect(screen.queryByRole('link', { name: 'Gevaarlijk' })).toBeNull(); + expect(screen.getByRole('link', { name: 'Veilig' })).toHaveAttribute('href', '/pad'); + }); + + it('keeps the fixed texts: the small line and the code line', () => { + setBlock({ hero: cmsHero }); + render(); + expect(screen.getByText(heroContent.eyebrow)).toBeInTheDocument(); + expect(screen.getByText(heroContent.codeLine, { exact: false })).toBeInTheDocument(); + }); +}); + +describe('Hero fallback (the baked hero)', () => { + it.each([ + ['no data block', null], + ['the baked block', '{}'], + ['hero null (204, not live, outside the schedule)', { hero: null }], + ['invalid JSON', '{"hero":'], + ['a hero without title', { hero: { title: '', buttons: [] } }], + ])('shows the baked hero for %s', (_name, data) => { + if (data === null) { + document.body.innerHTML = ''; + } else { + setBlock(data); + } + render(); + expect(screen.getByRole('heading', { level: 1, name: heroContent.heading })).toBeInTheDocument(); + expect(screen.getByText(heroContent.lead)).toBeInTheDocument(); + expect(screen.getByRole('link', { name: 'Bekijk pakketten' })).toHaveAttribute('href', '#pakketten'); + expect(screen.getByRole('link', { name: 'Plan een gesprek' })).toHaveAttribute('href', '#contact'); + }); + + it('writes no region markers in the running app', () => { + const { container } = render(); + expect(container.querySelector('[data-cms-marker]')).toBeNull(); + }); +}); + +describe('Era2026 hero', () => { + it('shows the CMS title and buttons, and falls back to the baked hero', () => { + setBlock({ hero: cmsHero }); + const { unmount } = render(); + expect(screen.getByRole('heading', { level: 1 })).toHaveTextContent('Sites, en apps voor jou.'); + expect(screen.getByRole('link', { name: 'Eerste knop' })).toHaveAttribute('href', '#pakketten'); + unmount(); + + document.body.innerHTML = ''; + render(); + expect(screen.getByRole('heading', { level: 1 })).toHaveTextContent(heroContent.displayHeading.join(' ')); + expect(screen.getByRole('link', { name: 'Bekijk pakketten' })).toHaveAttribute('href', '#pakketten'); + }); +}); diff --git a/src/features/landing/components/Hero.tsx b/src/features/landing/components/Hero.tsx index ad9a53c..c563f5b 100644 --- a/src/features/landing/components/Hero.tsx +++ b/src/features/landing/components/Hero.tsx @@ -1,22 +1,47 @@ +import { useCmsHero } from '../../../cms-client/hero'; import { heroContent } from '../data/content'; -import { handleAnchorClick } from '../../../utils/scrollToHash'; +import { resolveHero } from '../data/resolveHero'; +import { HeroButtonLink } from './HeroButtonLink'; + +const BUTTON_CLASSES = [ + 'bg-white px-[26px] py-[17px] text-base font-bold text-[#0e0d0d] no-underline transition-colors hover:bg-inv hover:text-white', + 'border-2 border-white px-6 py-[15px] text-base font-bold text-white no-underline transition-colors hover:bg-white hover:text-brand', +]; /** - * Hero of design B: display headline on the brand-red block, lead + two buttons, and the black - * code strip with the blinking caret. The nav sits directly above in the same colour. + * Marks the place where the CMS host may write its hero HTML (`...`). React + * cannot write comments, so the build-time prerender renders these two elements and scripts/prerender.mjs turns them + * into the comments. Never rendered in the running app. */ -export function Hero() { +function RegionMarker({ name }: { name: string }) { + return