Files
chrnx-dev-wp/docs/superpowers/specs/2026-08-12-related-stories-layouts-design.md
T

164 lines
12 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 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 block, block-editor, components, element, i18n, and server-side-render dependencies. The script calls `wp.blocks.registerBlockType( 'chrnx/related-posts', settings )` with the exact server attribute schema, a dynamic `edit` implementation, and `save: () => null`. The edit component renders `wp.serverSideRender` plus `InspectorControls`; it does not duplicate the PHP card query in JavaScript. This makes the block recognized, selectable, configurable, and serializable in both the post editor and Site Editor, where the Single template owns the default 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 has `flex-basis: calc(100% - clamp(3rem, 12vw, 7rem))` with no fixed maximum. After subtracting the computed track gap, the next card's visible continuation is at least 24 CSS pixels at every required test width and no more than 25% of that next card. One-card rails may use the full track width because no continuation cue is needed.
- 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 while either navigation button owns focus, the controller first gives the always-visible section heading a temporary `tabindex="-1"`, focuses it with `preventScroll`, removes the temporary attribute, and only then hides the controls and removes the track's region/tab-stop enhancement. If focus is elsewhere, it stays unchanged. 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.
- Client registration under WordPress 6.9.6: `getBlockType()` is non-null; the block is selectable in post and Site editors; inspector changes persist numeric `limit` and allowlisted `layout`; serialized dynamic markup contains the configured comment attributes and no saved HTML.
- 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 browser `3/5 × adaptive/grid/carousel` matrix, plus isolated PHPUnit factory coverage for availability counts of one, two, and four; browser fixtures never mutate unowned posts to manufacture partial results.
- Runtime geometry: exact grid column counts; carousel card width formula within two CSS pixels; 24px-to-25% continuation preview bounds; correct control visibility at 900/901; track-only overflow; and `document.documentElement.scrollWidth <= document.documentElement.clientWidth`.
- Resize focus regression in both directions, including 900→901 while Previous owns focus and while Next owns focus; the visible section heading becomes the destination before controls hide.
- 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.