From 67850849d1ce424b4434b0867d0f2d96a5e70bd8 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Wed, 12 Aug 2026 23:13:15 -0500 Subject: [PATCH] fix: preserve Related Stories focus on hover --- blocks/related-posts/style.css | 6 ++-- tests/e2e/related-stories.spec.mjs | 54 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/blocks/related-posts/style.css b/blocks/related-posts/style.css index d177709..2f7d8a7 100644 --- a/blocks/related-posts/style.css +++ b/blocks/related-posts/style.css @@ -44,7 +44,7 @@ html[data-theme="light"] .chrnx-related-posts { min-width: 0; } -.chrnx-related-card:focus-within { +.chrnx-related-posts .chrnx-related-card:focus-within { box-shadow: inset 0 0 0 3px var(--chrnx-related-focus-ring); } @@ -123,7 +123,7 @@ html[data-theme="light"] .chrnx-related-posts { @media (hover: hover) { - .chrnx-related-posts.is-layout-carousel .chrnx-related-card:hover { + .chrnx-related-posts.is-layout-carousel .chrnx-related-card:hover:not(:focus-within) { box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--chrnx-green) 62%, transparent); transform: none; } @@ -147,7 +147,7 @@ html[data-theme="light"] .chrnx-related-posts { @media (hover: hover) { - .chrnx-related-posts.is-layout-adaptive .chrnx-related-card:hover { + .chrnx-related-posts.is-layout-adaptive .chrnx-related-card:hover:not(:focus-within) { box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--chrnx-green) 62%, transparent); transform: none; } diff --git a/tests/e2e/related-stories.spec.mjs b/tests/e2e/related-stories.spec.mjs index 5e69eb7..217fe3c 100644 --- a/tests/e2e/related-stories.spec.mjs +++ b/tests/e2e/related-stories.spec.mjs @@ -443,6 +443,60 @@ for ( const theme of [ 'light', 'dark' ] ) { } ); } +for ( const theme of [ 'light', 'dark' ] ) { + for ( const railCase of [ + { name: 'adaptive mobile', anchor: 'related-stories-5-adaptive', width: 900 }, + { name: 'carousel desktop', anchor: 'related-stories-5-carousel', width: 1440 }, + ] ) { + test( `${ railCase.name } ${ theme } card keeps its keyboard focus ring while hovered`, async ( { + page, + browserName, + } ) => { + await page.setViewportSize( { width: railCase.width, height: 900 } ); + await page.goto( '/long-title-night-writing-fixture/' ); + await page.evaluate( ( selectedTheme ) => { + window.localStorage.setItem( 'chrnx-theme', selectedTheme ); + }, theme ); + await page.reload(); + + const section = page.locator( `#${ railCase.anchor } .chrnx-related-posts` ); + const track = section.locator( '[data-chrnx-related-track]' ); + const link = section.locator( '.chrnx-related-card__link' ).first(); + await track.focus(); + await page.keyboard.press( tabShortcut( browserName ) ); + await expect( link ).toBeFocused(); + await link.hover(); + await expect( link ).toBeFocused(); + + const appearance = await link.evaluate( ( focusedLink ) => { + const card = focusedLink.closest( '.chrnx-related-card' ); + const style = window.getComputedStyle( card ); + const token = style.getPropertyValue( '--chrnx-related-focus-ring' ).trim(); + const probe = document.createElement( 'span' ); + probe.style.color = token; + card.append( probe ); + const normalizedToken = window.getComputedStyle( probe ).color; + probe.remove(); + const colors = style.boxShadow.match( + /rgba?\([^)]+\)|color\(srgb\s+[\d.]+\s+[\d.]+\s+[\d.]+(?:\s*\/\s*[\d.]+)?\)/gi, + ); + return { + background: style.backgroundColor, + ring: colors?.at( -1 ) || null, + shadow: style.boxShadow, + token: normalizedToken, + }; + } ); + + expect( appearance.shadow ).toContain( 'inset' ); + expect( appearance.shadow ).toMatch( /0px 0px 0px 3px/ ); + expect( appearance.ring ).toBeTruthy(); + expect( colorChannels( appearance.ring ) ).toEqual( colorChannels( appearance.token ) ); + expect( contrast( appearance.ring, appearance.background ) ).toBeGreaterThanOrEqual( 3 ); + } ); + } +} + test( 'layout matrix rail hover stays visibly contained in its scrollport', async ( { page } ) => { await page.setViewportSize( { width: 900, height: 900 } ); await page.goto( '/long-title-night-writing-fixture/' );