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 shell = menuButton && menuButton.parentElement;
|
||||||
const brand = shell && shell.querySelector( '.chrnx-site-brand' );
|
const brand = shell && shell.querySelector( '.chrnx-site-brand' );
|
||||||
const navLink = nav && nav.querySelector( 'a[href]' );
|
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 = [
|
const required = [
|
||||||
menuButton,
|
menuButton,
|
||||||
nav,
|
nav,
|
||||||
@@ -61,7 +61,7 @@
|
|||||||
theme,
|
theme,
|
||||||
themeButton,
|
themeButton,
|
||||||
themePanel,
|
themePanel,
|
||||||
checkedRadio,
|
themeRadio,
|
||||||
brand,
|
brand,
|
||||||
navLink,
|
navLink,
|
||||||
shell,
|
shell,
|
||||||
@@ -106,6 +106,8 @@
|
|||||||
let mobile = media.matches;
|
let mobile = media.matches;
|
||||||
let activePanel = null;
|
let activePanel = null;
|
||||||
|
|
||||||
|
const currentThemeRadio = () =>
|
||||||
|
themePanel.querySelector( 'input[type="radio"]:checked' ) || themeRadio;
|
||||||
const restoreOriginalOrder = () => {
|
const restoreOriginalOrder = () => {
|
||||||
elements.forEach( ( element ) => {
|
elements.forEach( ( element ) => {
|
||||||
const anchor = anchors.get( element );
|
const anchor = anchors.get( element );
|
||||||
@@ -168,7 +170,7 @@
|
|||||||
} else if ( ! mobile && focusWasMenuButton ) {
|
} else if ( ! mobile && focusWasMenuButton ) {
|
||||||
navLink.focus();
|
navLink.focus();
|
||||||
} else if ( ! mobile && focusWasThemeButton ) {
|
} else if ( ! mobile && focusWasThemeButton ) {
|
||||||
checkedRadio.focus();
|
currentThemeRadio().focus();
|
||||||
} else if ( ! mobile && ( focusWasNav || focusWasThemePanel ) ) {
|
} else if ( ! mobile && ( focusWasNav || focusWasThemePanel ) ) {
|
||||||
focused.focus();
|
focused.focus();
|
||||||
} else if ( mobile && focusWasNav ) {
|
} else if ( mobile && focusWasNav ) {
|
||||||
@@ -177,11 +179,6 @@
|
|||||||
themeButton.focus();
|
themeButton.focus();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const closeForOutsideTarget = ( target ) => {
|
|
||||||
if ( activePanel === PANEL.theme && ! theme.contains( target ) ) {
|
|
||||||
setActivePanel( null );
|
|
||||||
}
|
|
||||||
};
|
|
||||||
const focusSafely = ( element ) => {
|
const focusSafely = ( element ) => {
|
||||||
try {
|
try {
|
||||||
element.focus( { preventScroll: true } );
|
element.focus( { preventScroll: true } );
|
||||||
@@ -189,6 +186,20 @@
|
|||||||
element.focus();
|
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 = () => {
|
const onMenuClick = () => {
|
||||||
focusSafely( menuButton );
|
focusSafely( menuButton );
|
||||||
setActivePanel( activePanel === PANEL.menu ? null : PANEL.menu );
|
setActivePanel( activePanel === PANEL.menu ? null : PANEL.menu );
|
||||||
@@ -198,7 +209,7 @@
|
|||||||
setActivePanel( activePanel === PANEL.theme ? null : PANEL.theme );
|
setActivePanel( activePanel === PANEL.theme ? null : PANEL.theme );
|
||||||
};
|
};
|
||||||
const onSearchSubmit = ( event ) => {
|
const onSearchSubmit = ( event ) => {
|
||||||
if ( searchInput.value.trim() ) {
|
if ( activePanel === PANEL.search && searchInput.value.trim() ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
@@ -223,8 +234,8 @@
|
|||||||
themeButton.addEventListener( 'click', onThemeClick );
|
themeButton.addEventListener( 'click', onThemeClick );
|
||||||
search.addEventListener( 'submit', onSearchSubmit );
|
search.addEventListener( 'submit', onSearchSubmit );
|
||||||
doc.addEventListener( 'keydown', onKeydown );
|
doc.addEventListener( 'keydown', onKeydown );
|
||||||
doc.addEventListener( 'pointerdown', onOutside );
|
|
||||||
doc.addEventListener( 'focusin', onOutside );
|
doc.addEventListener( 'focusin', onOutside );
|
||||||
|
doc.addEventListener( 'click', onOutside );
|
||||||
media.addEventListener( 'change', onMediaChange );
|
media.addEventListener( 'change', onMediaChange );
|
||||||
doc.documentElement.classList.add( 'chrnx-navigation-enhanced' );
|
doc.documentElement.classList.add( 'chrnx-navigation-enhanced' );
|
||||||
applyViewport( media.matches );
|
applyViewport( media.matches );
|
||||||
@@ -234,8 +245,8 @@
|
|||||||
themeButton.removeEventListener( 'click', onThemeClick );
|
themeButton.removeEventListener( 'click', onThemeClick );
|
||||||
search.removeEventListener( 'submit', onSearchSubmit );
|
search.removeEventListener( 'submit', onSearchSubmit );
|
||||||
doc.removeEventListener( 'keydown', onKeydown );
|
doc.removeEventListener( 'keydown', onKeydown );
|
||||||
doc.removeEventListener( 'pointerdown', onOutside );
|
|
||||||
doc.removeEventListener( 'focusin', onOutside );
|
doc.removeEventListener( 'focusin', onOutside );
|
||||||
|
doc.removeEventListener( 'click', onOutside );
|
||||||
media.removeEventListener( 'change', onMediaChange );
|
media.removeEventListener( 'change', onMediaChange );
|
||||||
restoreOriginalOrder();
|
restoreOriginalOrder();
|
||||||
anchors.forEach( ( anchor ) => anchor.remove() );
|
anchors.forEach( ( anchor ) => anchor.remove() );
|
||||||
|
|||||||
+104
-8
@@ -54,6 +54,7 @@ function shellMarkup() {
|
|||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
<button data-outside>Outside</button>
|
<button data-outside>Outside</button>
|
||||||
|
<div data-outside-content>Outside content</div>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,6 +75,7 @@ function controls() {
|
|||||||
checkedRadio: document.querySelector( '[data-chrnx-theme-panel] input:checked' ),
|
checkedRadio: document.querySelector( '[data-chrnx-theme-panel] input:checked' ),
|
||||||
firstRadio: document.querySelector( '[data-chrnx-theme-panel] input' ),
|
firstRadio: document.querySelector( '[data-chrnx-theme-panel] input' ),
|
||||||
outside: document.querySelector( '[data-outside]' ),
|
outside: document.querySelector( '[data-outside]' ),
|
||||||
|
outsideContent: document.querySelector( '[data-outside-content]' ),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -271,6 +273,27 @@ describe( 'coordinated CHRNX header shell', () => {
|
|||||||
expect( submit( control.search ).defaultPrevented ).toBe( false );
|
expect( submit( control.search ).defaultPrevented ).toBe( false );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
test( 'retained query reopens closed Search before a second native submit', () => {
|
||||||
|
const { initialize } = require( modulePath );
|
||||||
|
initialize( { document, media: createMedia( true ) } );
|
||||||
|
const control = controls();
|
||||||
|
submit( control.search );
|
||||||
|
control.searchInput.value = 'retained cats';
|
||||||
|
press( control.searchInput, 'Escape' );
|
||||||
|
expect( control.searchPanel.hidden ).toBe( true );
|
||||||
|
|
||||||
|
const reopenEvent = submit( control.search );
|
||||||
|
expect( reopenEvent.defaultPrevented ).toBe( true );
|
||||||
|
expect( control.searchInput.value ).toBe( 'retained cats' );
|
||||||
|
expect( control.searchPanel.hidden ).toBe( false );
|
||||||
|
expect( control.searchButton.getAttribute( 'aria-expanded' ) ).toBe( 'true' );
|
||||||
|
expect( document.activeElement ).toBe( control.searchInput );
|
||||||
|
|
||||||
|
const nativeEvent = submit( control.search );
|
||||||
|
expect( nativeEvent.defaultPrevented ).toBe( false );
|
||||||
|
expect( control.searchInput.value ).toBe( 'retained cats' );
|
||||||
|
} );
|
||||||
|
|
||||||
test( 'Escape closes Search and restores its trigger', () => {
|
test( 'Escape closes Search and restores its trigger', () => {
|
||||||
const { initialize } = require( modulePath );
|
const { initialize } = require( modulePath );
|
||||||
initialize( { document, media: createMedia( true ) } );
|
initialize( { document, media: createMedia( true ) } );
|
||||||
@@ -314,24 +337,66 @@ describe( 'coordinated CHRNX header shell', () => {
|
|||||||
expect( document.activeElement ).toBe( control[ buttonName ] );
|
expect( document.activeElement ).toBe( control[ buttonName ] );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
test( 'outside pointer and focus close Theme without stealing destination focus', () => {
|
test( 'real outside focusable click order closes Theme after focus transfers', () => {
|
||||||
const { initialize } = require( modulePath );
|
const { initialize } = require( modulePath );
|
||||||
initialize( { document, media: createMedia( true ) } );
|
initialize( { document, media: createMedia( true ) } );
|
||||||
const control = controls();
|
const control = controls();
|
||||||
|
|
||||||
control.themeButton.click();
|
control.themeButton.click();
|
||||||
control.outside.focus();
|
|
||||||
|
let hidden = control.themePanel.hidden;
|
||||||
|
let focusAtHide = null;
|
||||||
|
Object.defineProperty( control.themePanel, 'hidden', {
|
||||||
|
configurable: true,
|
||||||
|
get: () => hidden,
|
||||||
|
set: ( value ) => {
|
||||||
|
if ( value ) {
|
||||||
|
focusAtHide = document.activeElement;
|
||||||
|
}
|
||||||
|
hidden = value;
|
||||||
|
},
|
||||||
|
} );
|
||||||
|
|
||||||
control.outside.dispatchEvent( new Event( 'pointerdown', { bubbles: true } ) );
|
control.outside.dispatchEvent( new Event( 'pointerdown', { bubbles: true } ) );
|
||||||
expect( control.themePanel.hidden ).toBe( true );
|
expect( control.themePanel.hidden ).toBe( false );
|
||||||
expect( document.activeElement ).toBe( control.outside );
|
|
||||||
|
|
||||||
control.themeButton.click();
|
|
||||||
control.outside.focus();
|
control.outside.focus();
|
||||||
control.outside.dispatchEvent( new Event( 'focusin', { bubbles: true } ) );
|
control.outside.dispatchEvent( new Event( 'focusin', { bubbles: true } ) );
|
||||||
|
control.outside.dispatchEvent( new Event( 'click', { bubbles: true } ) );
|
||||||
expect( control.themePanel.hidden ).toBe( true );
|
expect( control.themePanel.hidden ).toBe( true );
|
||||||
|
expect( focusAtHide ).toBe( control.outside );
|
||||||
expect( document.activeElement ).toBe( control.outside );
|
expect( document.activeElement ).toBe( control.outside );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
test( 'outside nonfocusable click closes Theme without hiding focused content', () => {
|
||||||
|
const { initialize } = require( modulePath );
|
||||||
|
initialize( { document, media: createMedia( true ) } );
|
||||||
|
const control = controls();
|
||||||
|
control.themeButton.click();
|
||||||
|
control.checkedRadio.focus();
|
||||||
|
|
||||||
|
let hidden = control.themePanel.hidden;
|
||||||
|
let focusAtHide = null;
|
||||||
|
Object.defineProperty( control.themePanel, 'hidden', {
|
||||||
|
configurable: true,
|
||||||
|
get: () => hidden,
|
||||||
|
set: ( value ) => {
|
||||||
|
if ( value ) {
|
||||||
|
focusAtHide = document.activeElement;
|
||||||
|
}
|
||||||
|
hidden = value;
|
||||||
|
},
|
||||||
|
} );
|
||||||
|
|
||||||
|
control.outsideContent.dispatchEvent( new Event( 'pointerdown', { bubbles: true } ) );
|
||||||
|
expect( control.themePanel.hidden ).toBe( false );
|
||||||
|
control.checkedRadio.blur();
|
||||||
|
expect( document.activeElement ).toBe( document.body );
|
||||||
|
control.outsideContent.dispatchEvent( new Event( 'click', { bubbles: true } ) );
|
||||||
|
expect( control.themePanel.hidden ).toBe( true );
|
||||||
|
expect( focusAtHide ).toBe( control.themeButton );
|
||||||
|
expect( document.activeElement ).toBe( control.themeButton );
|
||||||
|
expect( document.activeElement.closest( '[hidden]' ) ).toBeNull();
|
||||||
|
} );
|
||||||
|
|
||||||
test( 'Tab is never prevented', () => {
|
test( 'Tab is never prevented', () => {
|
||||||
const { initialize } = require( modulePath );
|
const { initialize } = require( modulePath );
|
||||||
initialize( { document, media: createMedia( true ) } );
|
initialize( { document, media: createMedia( true ) } );
|
||||||
@@ -363,6 +428,37 @@ describe( 'coordinated CHRNX header shell', () => {
|
|||||||
expect( document.activeElement ).toBe( control.checkedRadio );
|
expect( document.activeElement ).toBe( control.checkedRadio );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
test.each( [ 'light', 'dark' ] )(
|
||||||
|
'599 to 600 focuses the current %s radio selected after initialization',
|
||||||
|
( value ) => {
|
||||||
|
const { initialize } = require( modulePath );
|
||||||
|
const media = createMedia( true );
|
||||||
|
initialize( { document, media } );
|
||||||
|
const control = controls();
|
||||||
|
const currentRadio = control.themePanel.querySelector( `input[value="${ value }"]` );
|
||||||
|
control.checkedRadio.checked = false;
|
||||||
|
currentRadio.checked = true;
|
||||||
|
control.themeButton.focus();
|
||||||
|
|
||||||
|
media.change( false );
|
||||||
|
expect( document.activeElement ).toBe( currentRadio );
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
test( '599 to 600 safely falls back to the first radio when none is checked', () => {
|
||||||
|
const { initialize } = require( modulePath );
|
||||||
|
const media = createMedia( true );
|
||||||
|
initialize( { document, media } );
|
||||||
|
const control = controls();
|
||||||
|
control.themePanel.querySelectorAll( 'input[type="radio"]' ).forEach( ( radio ) => {
|
||||||
|
radio.checked = false;
|
||||||
|
} );
|
||||||
|
control.themeButton.focus();
|
||||||
|
|
||||||
|
media.change( false );
|
||||||
|
expect( document.activeElement ).toBe( control.firstRadio );
|
||||||
|
} );
|
||||||
|
|
||||||
test( '599 to 600 preserves focus on a navigation link or visible radio', () => {
|
test( '599 to 600 preserves focus on a navigation link or visible radio', () => {
|
||||||
const { initialize } = require( modulePath );
|
const { initialize } = require( modulePath );
|
||||||
const media = createMedia( true );
|
const media = createMedia( true );
|
||||||
@@ -458,7 +554,7 @@ describe( 'coordinated CHRNX header shell', () => {
|
|||||||
test.each( [
|
test.each( [
|
||||||
[ 'brand', '.chrnx-site-brand' ],
|
[ 'brand', '.chrnx-site-brand' ],
|
||||||
[ 'navigation link', '[data-part="nav"] a' ],
|
[ 'navigation link', '[data-part="nav"] a' ],
|
||||||
[ 'checked theme radio', '[data-chrnx-theme-panel] input:checked' ],
|
[ 'theme radio', '[data-chrnx-theme-panel] input' ],
|
||||||
] )( 'fails closed without the required %s', ( _name, selector ) => {
|
] )( 'fails closed without the required %s', ( _name, selector ) => {
|
||||||
const { initialize } = require( modulePath );
|
const { initialize } = require( modulePath );
|
||||||
document.querySelectorAll( selector ).forEach( ( element ) => element.remove() );
|
document.querySelectorAll( selector ).forEach( ( element ) => element.remove() );
|
||||||
|
|||||||
Reference in new issue
Block a user