fix: harden CHRNX header state transitions
This commit is contained in:
2 files changed
+126
-19
No files matched your search
+22
-11
@@ -50,7 +50,7 @@
|
||||
const shell = menuButton && menuButton.parentElement;
|
||||
const brand = shell && shell.querySelector( '.chrnx-site-brand' );
|
||||
const navLink = nav && nav.querySelector( 'a[href]' );
|
||||
const checkedRadio = themePanel && themePanel.querySelector( 'input[type="radio"]:checked' );
|
||||
const themeRadio = themePanel && themePanel.querySelector( 'input[type="radio"]' );
|
||||
const required = [
|
||||
menuButton,
|
||||
nav,
|
||||
@@ -61,7 +61,7 @@
|
||||
theme,
|
||||
themeButton,
|
||||
themePanel,
|
||||
checkedRadio,
|
||||
themeRadio,
|
||||
brand,
|
||||
navLink,
|
||||
shell,
|
||||
@@ -106,6 +106,8 @@
|
||||
let mobile = media.matches;
|
||||
let activePanel = null;
|
||||
|
||||
const currentThemeRadio = () =>
|
||||
themePanel.querySelector( 'input[type="radio"]:checked' ) || themeRadio;
|
||||
const restoreOriginalOrder = () => {
|
||||
elements.forEach( ( element ) => {
|
||||
const anchor = anchors.get( element );
|
||||
@@ -168,7 +170,7 @@
|
||||
} else if ( ! mobile && focusWasMenuButton ) {
|
||||
navLink.focus();
|
||||
} else if ( ! mobile && focusWasThemeButton ) {
|
||||
checkedRadio.focus();
|
||||
currentThemeRadio().focus();
|
||||
} else if ( ! mobile && ( focusWasNav || focusWasThemePanel ) ) {
|
||||
focused.focus();
|
||||
} else if ( mobile && focusWasNav ) {
|
||||
@@ -177,11 +179,6 @@
|
||||
themeButton.focus();
|
||||
}
|
||||
};
|
||||
const closeForOutsideTarget = ( target ) => {
|
||||
if ( activePanel === PANEL.theme && ! theme.contains( target ) ) {
|
||||
setActivePanel( null );
|
||||
}
|
||||
};
|
||||
const focusSafely = ( element ) => {
|
||||
try {
|
||||
element.focus( { preventScroll: true } );
|
||||
@@ -189,6 +186,20 @@
|
||||
element.focus();
|
||||
}
|
||||
};
|
||||
const closeForOutsideTarget = ( target ) => {
|
||||
if ( activePanel !== PANEL.theme || theme.contains( target ) ) {
|
||||
return;
|
||||
}
|
||||
const focused = doc.activeElement;
|
||||
const targetOwnsFocus =
|
||||
focused !== doc.body &&
|
||||
focused !== doc.documentElement &&
|
||||
( focused === target || focused.contains( target ) || target.contains( focused ) );
|
||||
if ( ! targetOwnsFocus ) {
|
||||
focusSafely( themeButton );
|
||||
}
|
||||
setActivePanel( null );
|
||||
};
|
||||
const onMenuClick = () => {
|
||||
focusSafely( menuButton );
|
||||
setActivePanel( activePanel === PANEL.menu ? null : PANEL.menu );
|
||||
@@ -198,7 +209,7 @@
|
||||
setActivePanel( activePanel === PANEL.theme ? null : PANEL.theme );
|
||||
};
|
||||
const onSearchSubmit = ( event ) => {
|
||||
if ( searchInput.value.trim() ) {
|
||||
if ( activePanel === PANEL.search && searchInput.value.trim() ) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
@@ -223,8 +234,8 @@
|
||||
themeButton.addEventListener( 'click', onThemeClick );
|
||||
search.addEventListener( 'submit', onSearchSubmit );
|
||||
doc.addEventListener( 'keydown', onKeydown );
|
||||
doc.addEventListener( 'pointerdown', onOutside );
|
||||
doc.addEventListener( 'focusin', onOutside );
|
||||
doc.addEventListener( 'click', onOutside );
|
||||
media.addEventListener( 'change', onMediaChange );
|
||||
doc.documentElement.classList.add( 'chrnx-navigation-enhanced' );
|
||||
applyViewport( media.matches );
|
||||
@@ -234,8 +245,8 @@
|
||||
themeButton.removeEventListener( 'click', onThemeClick );
|
||||
search.removeEventListener( 'submit', onSearchSubmit );
|
||||
doc.removeEventListener( 'keydown', onKeydown );
|
||||
doc.removeEventListener( 'pointerdown', onOutside );
|
||||
doc.removeEventListener( 'focusin', onOutside );
|
||||
doc.removeEventListener( 'click', onOutside );
|
||||
media.removeEventListener( 'change', onMediaChange );
|
||||
restoreOriginalOrder();
|
||||
anchors.forEach( ( anchor ) => anchor.remove() );
|
||||
|
||||
Reference in new issue
Block a user