From 5957bd566b808aef35db77dcf7a529ef02f415c1 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Wed, 12 Aug 2026 22:59:35 -0500 Subject: [PATCH] fix: restore Related Stories resize focus --- assets/js/related-posts.js | 26 +++++-------------- .../2026-08-12-related-stories-layouts.md | 4 +-- ...26-08-12-related-stories-layouts-design.md | 4 +-- tests/e2e/related-stories.spec.mjs | 14 ++++++---- tests/js/related-posts.test.js | 13 ++++------ 5 files changed, 24 insertions(+), 37 deletions(-) diff --git a/assets/js/related-posts.js b/assets/js/related-posts.js index e4230d9..b2416aa 100644 --- a/assets/js/related-posts.js +++ b/assets/js/related-posts.js @@ -60,6 +60,7 @@ const previous = controls && controls.querySelector( '[data-chrnx-related-previous]' ); const next = controls && controls.querySelector( '[data-chrnx-related-next]' ); const cards = track ? [ ...track.querySelectorAll( 'article' ) ] : []; + const firstCardLink = cards[ 0 ] && cards[ 0 ].querySelector( 'a[href]' ); if ( ( ! adaptive && ! carousel ) || @@ -69,6 +70,8 @@ ! previous || ! next || ! cards.length || + ! firstCardLink || + typeof firstCardLink.focus !== 'function' || typeof cards[ 0 ].getBoundingClientRect !== 'function' || typeof track.scrollBy !== 'function' ) { @@ -84,7 +87,6 @@ tabIndex: rememberAttribute( track, 'tabindex' ), }; let rail = carousel || environment.media.matches; - let clearHeadingFocus = null; const updateEdges = () => { const maximum = Math.max( 0, track.scrollWidth - track.clientWidth ); @@ -111,30 +113,17 @@ behavior: environment.reducedMotion.matches ? 'auto' : 'smooth', } ); }; - const focusHeadingBeforeHide = () => { + const focusFirstCardBeforeHide = () => { const active = environment.document.activeElement; if ( active !== previous && active !== next ) { return; } - const previousTabIndex = rememberAttribute( heading, 'tabindex' ); - heading.setAttribute( 'tabindex', '-1' ); - focusWithoutScroll( heading ); - if ( environment.document.activeElement !== heading ) { - restoreAttribute( heading, 'tabindex', previousTabIndex ); - return; - } - const restoreHeadingTabIndex = () => { - heading.removeEventListener( 'blur', restoreHeadingTabIndex ); - restoreAttribute( heading, 'tabindex', previousTabIndex ); - clearHeadingFocus = null; - }; - heading.addEventListener( 'blur', restoreHeadingTabIndex, { once: true } ); - clearHeadingFocus = restoreHeadingTabIndex; + focusWithoutScroll( firstCardLink ); }; const applyViewport = () => { const nextRail = carousel || environment.media.matches; if ( rail && ! nextRail ) { - focusHeadingBeforeHide(); + focusFirstCardBeforeHide(); } rail = nextRail; if ( rail ) { @@ -178,9 +167,6 @@ if ( adaptive ) { environment.media.removeEventListener( 'change', onMediaChange ); } - if ( clearHeadingFocus ) { - clearHeadingFocus(); - } controls.hidden = original.controlsHidden; previous.disabled = original.previousDisabled; next.disabled = original.nextDisabled; diff --git a/docs/superpowers/plans/2026-08-12-related-stories-layouts.md b/docs/superpowers/plans/2026-08-12-related-stories-layouts.md index 82e5d53..71ab9da 100644 --- a/docs/superpowers/plans/2026-08-12-related-stories-layouts.md +++ b/docs/superpowers/plans/2026-08-12-related-stories-layouts.md @@ -272,7 +272,7 @@ Create real DOM fixtures for two block instances. Inject matchMedia and element - start/end buttons receive correct native disabled states after scroll and resize; - reduced motion selects instant/auto behavior and default permits smooth behavior; - missing buttons, track, cards, matchMedia, or scroll APIs leave controls hidden and do not throw; -- 900→901 while either button owns focus moves focus to the visible heading before hiding; +- 900→901 while either button owns focus moves focus to the first related-story link before hiding; - unrelated focus stays unchanged; - rail semantics and controls restore at 901→900; - reinitialization does not duplicate listeners; cleanup removes every listener; @@ -384,7 +384,7 @@ On a seeded single-post route containing the default block, add tests for: - button navigation moves exactly one card and updates disabled edges; - native horizontal wheel/touch-compatible scrolling remains possible; - Tab reaches the rail and buttons in DOM order and exits without wrapping/trapping; -- resizing at the 900/901 boundary repairs button focus to the heading; +- resizing at the 900/901 boundary repairs button focus to the first related-story link, which remains naturally focusable and visible in every engine's grid; - reduced motion produces no smooth programmatic scroll but preserves geometry; - JavaScript-disabled adaptive/carousel variants keep controls hidden and every card/link reachable through the focus order; - controller failure simulated before initialization leaves native rail and links usable; diff --git a/docs/superpowers/specs/2026-08-12-related-stories-layouts-design.md b/docs/superpowers/specs/2026-08-12-related-stories-layouts-design.md index 70bf704..de3c6db 100644 --- a/docs/superpowers/specs/2026-08-12-related-stories-layouts-design.md +++ b/docs/superpowers/specs/2026-08-12-related-stories-layouts-design.md @@ -96,7 +96,7 @@ A dedicated `assets/js/related-posts.js` controller enhances every rendered caro - 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. +- If a viewport change turns an adaptive rail into a grid while either navigation button owns focus, the controller focuses the first full-card related-story link with `preventScroll` before hiding the controls and removing the track's region/tab-stop enhancement. The link is naturally focusable and remains visible in the grid in Chromium, Firefox, and WebKit, so focus never depends on a temporary heading `tabindex`. If focus is elsewhere, it stays unchanged. Crossing back restores rail semantics and control state. ## Accessibility @@ -131,7 +131,7 @@ Test-driven implementation begins with failing tests for: - 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. +- Resize focus regression in both directions, including 900→901 while Previous owns focus and while Next owns focus; the first related-story link becomes the cross-browser 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. diff --git a/tests/e2e/related-stories.spec.mjs b/tests/e2e/related-stories.spec.mjs index ef70ad0..5e69eb7 100644 --- a/tests/e2e/related-stories.spec.mjs +++ b/tests/e2e/related-stories.spec.mjs @@ -602,9 +602,8 @@ for ( const buttonName of [ 'Previous stories', 'Next stories' ] ) { await page.setViewportSize( { width: 900, height: 900 } ); await page.goto( '/long-title-night-writing-fixture/' ); const section = page.locator( '#related-stories-3-adaptive .chrnx-related-posts' ); - const heading = section.getByRole( 'heading', { - name: 'Related stories', - } ); + const firstCardLink = section.locator( '.chrnx-related-card__link' ).first(); + const controls = section.locator( '.chrnx-related-posts__controls' ); if ( buttonName === 'Previous stories' ) { await section.getByRole( 'button', { name: 'Next stories' } ).click(); await expect( section.getByRole( 'button', { name: 'Previous stories' } ) ).toBeEnabled(); @@ -613,8 +612,13 @@ for ( const buttonName of [ 'Previous stories', 'Next stories' ] ) { await button.focus(); await expect( button ).toBeFocused(); await page.setViewportSize( { width: 901, height: 900 } ); - await expect( heading ).toBeFocused(); - await expect( section.locator( '.chrnx-related-posts__controls' ) ).toBeHidden(); + await expect( firstCardLink ).toBeFocused(); + await expect( controls ).toBeHidden(); + expect( + await controls.evaluate( ( element ) => + element.contains( element.ownerDocument.activeElement ), + ), + ).toBe( false ); } ); } diff --git a/tests/js/related-posts.test.js b/tests/js/related-posts.test.js index 00bb312..d3b4573 100644 --- a/tests/js/related-posts.test.js +++ b/tests/js/related-posts.test.js @@ -270,7 +270,7 @@ describe( 'Related Stories progressive controllers', () => { ); test.each( [ 'previous', 'next' ] )( - '900 to 901 moves %s focus to heading before controls hide', + '900 to 901 moves %s focus to the first card link before controls hide', ( button ) => { const { initialize } = require( modulePath ); const media = createMedia( true ); @@ -281,6 +281,7 @@ describe( 'Related Stories progressive controllers', () => { fixture.track.dispatchEvent( new Event( 'scroll' ) ); } fixture[ button ].focus(); + const firstLink = fixture.cards[ 0 ].querySelector( 'a' ); let focusAtHide = null; let hidden = fixture.controls.hidden; @@ -296,10 +297,8 @@ describe( 'Related Stories progressive controllers', () => { } ); media.change( false ); - expect( focusAtHide ).toBe( fixture.heading ); - expect( document.activeElement ).toBe( fixture.heading ); - expect( fixture.heading.getAttribute( 'tabindex' ) ).toBe( '-1' ); - document.querySelector( '[data-unrelated]' ).focus(); + expect( focusAtHide ).toBe( firstLink ); + expect( document.activeElement ).toBe( firstLink ); expect( fixture.heading.hasAttribute( 'tabindex' ) ).toBe( false ); expect( fixture.controls.hidden ).toBe( true ); }, @@ -316,11 +315,9 @@ describe( 'Related Stories progressive controllers', () => { fixture.geometry.clientWidth = fixture.geometry.scrollWidth; window.dispatchEvent( new Event( 'resize' ) ); - expect( document.activeElement ).toBe( fixture.heading ); + expect( document.activeElement ).toBe( fixture.cards[ 0 ].querySelector( 'a' ) ); expect( fixture.controls.hidden ).toBe( true ); expect( fixture.next.disabled ).toBe( false ); - expect( fixture.heading.getAttribute( 'tabindex' ) ).toBe( '-1' ); - document.querySelector( '[data-unrelated]' ).focus(); expect( fixture.heading.hasAttribute( 'tabindex' ) ).toBe( false ); } );