162 lines
10 KiB
Markdown
162 lines
10 KiB
Markdown
# Related Stories Layouts Design
|
||
|
||
**Status:** Proposed for implementation
|
||
**Date:** 2026-08-12
|
||
|
||
## Goal
|
||
|
||
Turn `chrnx/related-posts` into a configurable editorial block that displays three or five related stories as an adaptive layout, a fixed grid, or a manual horizontal carousel.
|
||
|
||
## Editorial defaults
|
||
|
||
- The block defaults to three stories and `adaptive` layout.
|
||
- Editors may choose only three or five stories. Existing saved values outside that set render as three.
|
||
- The current topic → shared tags → recency selection algorithm remains unchanged.
|
||
- If fewer eligible stories exist, the block renders only those stories. It never adds placeholders or duplicates.
|
||
- The public heading remains “Related stories,” with the existing configurable semantic heading level.
|
||
|
||
## Block attributes and editor controls
|
||
|
||
`blocks/related-posts/block.json` exposes:
|
||
|
||
- `limit`: integer, enum `[3, 5]`, default `3`.
|
||
- `layout`: string, enum `adaptive`, `grid`, `carousel`, default `adaptive`.
|
||
- `headingLevel`: existing integer enum `2–6`.
|
||
|
||
The block editor adds an inspector panel named “Related stories settings” with:
|
||
|
||
- A `SelectControl` named “Stories to show” containing `3` and `5`.
|
||
- A `SelectControl` named “Layout” containing “Adaptive,” “Grid,” and “Carousel.”
|
||
- Short help text: adaptive uses a grid on larger screens and horizontal scrolling on smaller screens.
|
||
|
||
`SelectControl` returns strings. Its `limit` handler strictly allowlists the incoming value and persists an integer: `setAttributes({ limit: Number(value) === 5 ? 5 : 3 })`. Tests prove the saved attribute is numeric `5`, never string `"5"`.
|
||
|
||
The block metadata declares `editorScript: file:../../assets/js/editor-related-posts.js`. Its adjacent asset metadata declares the WordPress dependencies used by a narrowly scoped `editor.BlockEdit` wrapper. This makes the controls available both in the post editor and in the Site Editor, where the Single template owns the default block instance; it does not rely on the post-only enqueue guard in `inc/editor.php`.
|
||
|
||
The server renderer owns validation. Malformed or legacy attributes fail closed to the defaults. Editor controls cannot be the only validation boundary.
|
||
|
||
## Rendered contract
|
||
|
||
The existing semantic section and card markup stay intact. The list wrapper changes from the fixed `chrnx-related-posts__grid` contract to:
|
||
|
||
```html
|
||
<section class="wp-block-chrnx-related-posts chrnx-related-posts is-layout-adaptive" aria-labelledby="…">
|
||
<h2 id="…">Related stories</h2>
|
||
<div class="chrnx-related-posts__items" data-chrnx-related-track role="region" aria-labelledby="…" tabindex="0">
|
||
<!-- existing semantic article cards -->
|
||
</div>
|
||
<div class="chrnx-related-posts__controls" role="group" aria-label="Related stories navigation" hidden>
|
||
<button type="button" data-chrnx-related-previous>Previous stories</button>
|
||
<button type="button" data-chrnx-related-next>Next stories</button>
|
||
</div>
|
||
</section>
|
||
```
|
||
|
||
The renderer emits exactly one allowlisted class: `is-layout-adaptive`, `is-layout-grid`, or `is-layout-carousel`.
|
||
|
||
- `grid`: controls are not rendered.
|
||
- `adaptive`: controls are rendered for the mobile carousel enhancement and hidden at the desktop grid breakpoint.
|
||
- `carousel`: controls are rendered at every viewport.
|
||
- With JavaScript disabled, every story remains reachable in source order through native horizontal scrolling. Buttons are enhancement controls, never the only navigation method.
|
||
- Carousel-capable tracks are keyboard-focusable regions whose `aria-labelledby` points to the visible section heading. Grid tracks have neither `role="region"` nor `tabindex`. The controls use `role="group"` and the localized accessible name “Related stories navigation.” Cards remain articles containing one full-card link.
|
||
- The frontend controller removes the adaptive track's region role and tab stop while it is a non-scrollable desktop grid, then restores them when it crosses into the mobile rail. The explicit carousel retains both at every width.
|
||
|
||
## Layout behavior
|
||
|
||
### Adaptive
|
||
|
||
- At widths above 900px, cards form a grid.
|
||
- Three stories use three equal columns.
|
||
- Five stories use three equal-width columns and wrap naturally as `3 + 2`; four available stories wrap as `3 + 1`, two as two columns, and one as one column. Empty columns are never rendered.
|
||
- At 900px and below, cards become a horizontal rail.
|
||
|
||
### Grid
|
||
|
||
- Cards always wrap in a grid.
|
||
- Large screens use up to three columns.
|
||
- Narrow screens collapse to one column, so no horizontal page overflow is introduced.
|
||
|
||
### Carousel
|
||
|
||
- Cards stay in a horizontal rail at every width.
|
||
- Above 900px, each card is `(track width - 2 × computed gap) / 3`, within two CSS pixels, so three cards are visible.
|
||
- At 900px and below, each card occupies 82% of the track width, capped at 20rem. When another card exists, at least 24 CSS pixels and no more than 25% of it remain visible as a continuation cue.
|
||
- The rail uses `overflow-x: auto`, `scroll-snap-type: x mandatory`, and source-order cards with `scroll-snap-align: start`.
|
||
- The page itself never gains horizontal overflow.
|
||
|
||
## Controls and progressive enhancement
|
||
|
||
A dedicated `assets/js/related-posts.js` controller enhances every rendered carousel independently. `blocks/related-posts/block.json` declares it as `viewScript: file:../../assets/js/related-posts.js`; WordPress therefore registers the local asset with the block and prints it only when the block renders.
|
||
|
||
- Previous and next buttons scroll by one card plus the measured gap.
|
||
- The script measures live card geometry; it does not hardcode pixel distances.
|
||
- Buttons use native buttons, retain normal Tab order, and never trap focus.
|
||
- Buttons become disabled at the corresponding edge. Edge state updates after scrolling, resizing, and initialization.
|
||
- A pointer, trackpad, touch gesture, keyboard scrolling, or button press reaches the same rail.
|
||
- The script does not autoplay, loop, clone cards, modify card links, export globals, or make network requests.
|
||
- Missing markup, zero cards, unsupported browser APIs, or a reduced-motion preference fail safely. Reduced motion changes programmatic scrolling from smooth to instant without changing layout or content.
|
||
- Controls carry the native `hidden` attribute in server output. Each controller removes it only after its track, cards, buttons, media query, and scroll APIs initialize successfully. With JavaScript disabled or controller initialization rejected, controls stay hidden and native scrolling remains available.
|
||
- If a viewport change turns an adaptive rail into a grid, the controller hides controls, removes the track's region/tab-stop enhancement, and leaves focus on a visible control before hiding when necessary. Crossing back restores rail semantics and control state.
|
||
|
||
## Accessibility
|
||
|
||
- Cards retain DOM and reading order.
|
||
- Every link and button has a visible focus indicator meeting the existing light/dark focus token contract.
|
||
- Control targets are at least 45×45 CSS pixels.
|
||
- Previous/next labels are translated with text domain `chrnx` and provide meaningful accessible names.
|
||
- Disabled states use the native `disabled` attribute.
|
||
- No live region announces ordinary scrolling; it would add noise without conveying new content.
|
||
- At 200% zoom, cards wrap or scroll within the section and do not clip text.
|
||
- Themes Light, Dark, and System preserve contrast and functionality.
|
||
|
||
## Synthetic content and permalinks
|
||
|
||
Related Stories must use ordinary `get_permalink()` output from seeded published fixtures. The seed remains local-only and idempotent. Integration tests click a rendered related-card link and require an HTTP 200 single-post destination, preventing the synthetic 404 behavior observed during review.
|
||
|
||
This feature does not create production content, alter public permalink settings, or add test-only URL behavior to production rendering.
|
||
|
||
## Testing
|
||
|
||
Test-driven implementation begins with failing tests for:
|
||
|
||
- Exact block attribute enums/defaults and editor script registration.
|
||
- Server-side sanitization of malformed `limit` and `layout` attributes.
|
||
- Exact item count for three, five, and fewer-than-requested results.
|
||
- Layout classes and conditional control markup.
|
||
- Localized editor labels and frontend controls.
|
||
- Editor `SelectControl` updates for both attributes.
|
||
- Independent JavaScript controllers, measured one-card movement, edge disabling, resize updates, reduced motion, missing-markup safety, and cleanup.
|
||
- CSS contracts for adaptive, grid, carousel, scroll snap, 45px controls, no-JS, reduced motion, and absence of page overflow.
|
||
- Playwright coverage at 375, 768, 900, 901, 1440, and 2560 pixels in light and dark themes.
|
||
- The complete `3/5 × adaptive/grid/carousel` matrix, plus adaptive availability counts of one, two, and four.
|
||
- Runtime geometry: exact grid column counts; carousel card width tolerances above; continuation preview bounds; correct control visibility at 900/901; track-only overflow; and `document.documentElement.scrollWidth <= document.documentElement.clientWidth`.
|
||
- Real pointer/button and keyboard interaction without a focus trap.
|
||
- No-JS access to every related story.
|
||
- Clicking a seeded related story returns HTTP 200 rather than 404.
|
||
- Automated accessibility checks on a single-post page containing the block.
|
||
|
||
The existing topic/tag/recency query semantics, heading hierarchy, card escaping, empty result behavior, and unrelated block tests must remain green.
|
||
|
||
## Files and boundaries
|
||
|
||
Expected implementation files:
|
||
|
||
- `blocks/related-posts/block.json`
|
||
- `blocks/related-posts/render.php`
|
||
- `blocks/related-posts/style.css`
|
||
- `assets/js/related-posts.js`
|
||
- `assets/js/editor-related-posts.js`
|
||
- `assets/js/editor-related-posts.asset.php`
|
||
- `tests/php/BlockRenderingTest.php`
|
||
- `tests/php/EditorAssetsTest.php`
|
||
- `tests/js/related-posts.test.js`
|
||
- `tests/js/editor-related-posts.test.js`
|
||
- `tests/e2e/related-stories.spec.mjs`
|
||
- relevant smoke fixture/metadata contracts only if required
|
||
|
||
The work stays separate from the character-led section branch. It does not change the related-post selection algorithm, global navigation, footer, theme preference, image generation, or production data.
|
||
|
||
## Acceptance
|
||
|
||
The design is complete when an editor can select `3` or `5` and `Adaptive`, `Grid`, or `Carousel`; the frontend renders the exact selection accessibly; all stories remain usable without JavaScript; no autoplay or infinite loop exists; seeded links resolve; and the full repository verification passes in the isolated test environment.
|