fix: restore Related Stories resize focus

This commit is contained in:
chrnx committed 2026-08-12 22:59:35 -05:00
1 parent 12259f4837
commit 5957bd566b
5 files changed
+24 -37

No files matched your search

+6 -20
View File
@@ -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;