diff --git a/assets/css/layout.css b/assets/css/layout.css index 9403443..6a4a8d0 100644 --- a/assets/css/layout.css +++ b/assets/css/layout.css @@ -311,26 +311,147 @@ html[data-theme="light"] footer.wp-block-template-part { @media (max-width: 599px) { + .chrnx-site-header__inner { + display: grid !important; + grid-template-columns: minmax(0, 1fr) repeat(3, 45px); + gap: 0.25rem; + align-items: start; + padding-block: 0.75rem; + } + + .chrnx-site-brand { + grid-row: 1; + grid-column: 1; + min-width: 0; + overflow: hidden; + } + + .chrnx-site-brand img { + max-width: 100%; + } + .chrnx-mobile-menu-toggle { - margin-left: auto; + grid-row: 1; + grid-column: 4; + width: 45px; + height: 45px; + margin-left: 0; } .chrnx-primary-navigation { - order: 3; + grid-column: 1 / -1; width: 100%; margin-left: 0; } + .chrnx-navigation-enhanced .chrnx-primary-navigation { + grid-row: 2; + } + + .chrnx-primary-navigation[hidden] { + display: none; + } + .chrnx-primary-navigation ul { flex-wrap: wrap; } .chrnx-header-search { - order: 4; + display: grid; + grid-template-rows: 45px auto; + grid-template-columns: minmax(0, 1fr) repeat(3, 45px); + grid-column: 1 / -1; + gap: 0.25rem; + width: 100%; + pointer-events: none; } - .chrnx-theme-toggle { - order: 5; + .chrnx-navigation-enhanced .chrnx-header-search { + grid-row: 1; + } + + .chrnx-header-search-row { + display: flex; + grid-row: 2; + grid-column: 1 / -1; + width: 100%; + pointer-events: auto; + } + + .chrnx-header-search-row[hidden] { + display: none; + } + + .chrnx-header-search input { + width: 100%; + } + + .chrnx-header-search button { + grid-row: 1; + grid-column: 2; + width: 45px; + height: 45px; + padding: 0; + pointer-events: auto; + } + + .chrnx-theme-control { + grid-column: 1 / -1; + min-width: 0; + } + + .chrnx-navigation-enhanced .chrnx-theme-control { + position: relative; + z-index: 2; + display: flex; + grid-row: 1; + grid-column: 3; + width: 45px; + height: 45px; + } + + .chrnx-theme-control > [data-chrnx-theme-button] { + display: inline-flex; + align-items: center; + justify-content: center; + width: 45px; + height: 45px; + padding: 0; + border: 1px solid currentcolor; + background: transparent; + color: inherit; + cursor: pointer; + } + + .chrnx-theme-control > [data-chrnx-theme-button][hidden], + .chrnx-navigation-enhanced .chrnx-theme-toggle[hidden] { + display: none; + } + + .chrnx-navigation-enhanced .chrnx-theme-toggle { + position: absolute; + top: calc(100% + 0.25rem); + right: 0; + z-index: 1; + flex-wrap: wrap; + width: max-content; + max-width: calc(100vw - 2rem); + min-width: 0; + height: auto; + padding: 0.5rem; + border: 1px solid currentcolor; + border-radius: 0.35rem; + background: var(--wp--preset--color--panel); + color: inherit; + } + + html[data-theme="light"].chrnx-navigation-enhanced .chrnx-theme-toggle { + background: var(--wp--preset--color--surface); + } + + .chrnx-navigation-enhanced .chrnx-theme-toggle label { + flex: 1 1 auto; + white-space: normal; } } diff --git a/tests/e2e/navigation.spec.mjs b/tests/e2e/navigation.spec.mjs index 6e4bfb2..191daa9 100644 --- a/tests/e2e/navigation.spec.mjs +++ b/tests/e2e/navigation.spec.mjs @@ -18,6 +18,51 @@ async function getHomeURL( page ) { return page.getByRole( 'banner' ).getByRole( 'link', { name: 'CHRNX' } ).getAttribute( 'href' ); } +async function getBox( locator ) { + const box = await locator.boundingBox(); + expect( box ).not.toBeNull(); + return box; +} + +async function getContentBox( locator ) { + return locator.evaluate( ( element ) => { + const box = element.getBoundingClientRect(); + const style = window.getComputedStyle( element ); + const paddingLeft = Number.parseFloat( style.paddingLeft ); + const paddingRight = Number.parseFloat( style.paddingRight ); + return { + x: box.x + paddingLeft, + width: box.width - paddingLeft - paddingRight, + }; + } ); +} + +function expectNoOverlap( boxes ) { + for ( let first = 0; first < boxes.length; first += 1 ) { + for ( let second = first + 1; second < boxes.length; second += 1 ) { + const a = boxes[ first ]; + const b = boxes[ second ]; + const overlaps = + a.x < b.x + b.width && + a.x + a.width > b.x && + a.y < b.y + b.height && + a.y + a.height > b.y; + expect( overlaps ).toBe( false ); + } + } +} + +async function expectNoHorizontalOverflow( page, header ) { + expect( + await header.evaluate( ( element ) => element.scrollWidth <= element.clientWidth ), + ).toBe( true ); + expect( + await page.evaluate( + () => document.documentElement.scrollWidth <= document.documentElement.clientWidth, + ), + ).toBe( true ); +} + test.describe( 'site navigation shell', () => { test( 'desktop has a stable accessible header, approved local brand, and compact footer', async ( { page, @@ -111,6 +156,14 @@ test.describe( 'site navigation shell', () => { await page.goto( '/' ); const header = page.getByRole( 'banner' ); const open = header.locator( '[data-chrnx-menu-toggle]' ); + const compactSearch = header.locator( '[data-chrnx-search-button]' ); + const theme = header.locator( '[data-chrnx-theme-button]' ); + const focusKey = testInfo.project.name === 'webkit' ? 'Alt+Tab' : 'Tab'; + await compactSearch.focus(); + await page.keyboard.press( focusKey ); + await expect( theme ).toBeFocused(); + await page.keyboard.press( focusKey ); + await expect( open ).toBeFocused(); await expect( open ).toBeVisible(); await expect( open ).toHaveAccessibleName( 'Open primary navigation' ); await expectTargetSize( open ); @@ -126,21 +179,15 @@ test.describe( 'site navigation shell', () => { ); } await open.focus(); - const focusKey = testInfo.project.name === 'webkit' ? 'Alt+Tab' : 'Tab'; for ( const [ name ] of destinations ) { await page.keyboard.press( focusKey ); await expect( nav.getByRole( 'link', { name, exact: true } ) ).toBeFocused(); } - await page.keyboard.press( focusKey ); - await expect( header.getByRole( 'button', { name: 'Open search' } ) ).toBeFocused(); - await page.keyboard.press( focusKey ); - await expect( header.getByRole( 'radio', { name: 'System', exact: true } ) ).toBeFocused(); await nav.getByRole( 'link', { name: 'Music', exact: true } ).focus(); await page.keyboard.press( 'Escape' ); await expect( open ).toBeFocused(); await expect( open ).toHaveAccessibleName( 'Open primary navigation' ); await expect( nav ).toBeHidden(); - const compactSearch = header.locator( '[data-chrnx-search-button]' ); await expect( compactSearch ).toHaveAccessibleName( 'Open search' ); await expectTargetSize( compactSearch ); await compactSearch.click(); @@ -151,6 +198,161 @@ test.describe( 'site navigation shell', () => { await expect( open ).toHaveAttribute( 'aria-expanded', 'false' ); } ); + for ( const width of [ 375, 599 ] ) { + test( `compact mobile header keeps one closed bar at ${ width }px`, async ( { page } ) => { + await page.setViewportSize( { width, height: 844 } ); + await page.goto( '/' ); + const header = page.getByRole( 'banner' ); + const brandLink = header.getByRole( 'link', { name: 'CHRNX home' } ); + const brand = header.getByRole( 'img', { name: 'CHRNX' } ); + const search = header.getByRole( 'button', { name: 'Open search', exact: true } ); + const theme = header.getByRole( 'button', { name: 'Choose color theme', exact: true } ); + const menu = header.getByRole( 'button', { + name: 'Open primary navigation', + exact: true, + } ); + + await expect( search ).toHaveCount( 1 ); + await expect( theme ).toHaveCount( 1 ); + await expect( menu ).toHaveCount( 1 ); + await expect( search ).toHaveAttribute( 'aria-expanded', 'false' ); + await expect( theme ).toHaveAttribute( 'aria-expanded', 'false' ); + await expect( menu ).toHaveAttribute( 'aria-expanded', 'false' ); + + const boxes = await Promise.all( + [ brandLink, search, theme, menu ].map( ( locator ) => getBox( locator ) ), + ); + const [ brandBox, ...controlBoxes ] = boxes; + for ( const box of controlBoxes ) { + expect( box.width ).toBeGreaterThanOrEqual( 44 ); + expect( box.width ).toBeLessThanOrEqual( 45 ); + expect( box.height ).toBeGreaterThanOrEqual( 44 ); + expect( box.height ).toBeLessThanOrEqual( 45 ); + } + for ( const box of boxes.slice( 1 ) ) { + expect( Math.abs( box.y - brandBox.y ) ).toBeLessThanOrEqual( 1 ); + } + expectNoOverlap( boxes ); + + const headerBox = await getBox( header ); + expect( headerBox.height ).toBeGreaterThanOrEqual( 68 ); + expect( headerBox.height ).toBeLessThanOrEqual( 76 ); + await expectNoHorizontalOverflow( page, header ); + + const imageGeometry = await brand.evaluate( ( image ) => { + const imageBox = image.getBoundingClientRect(); + const linkBox = image.parentElement.getBoundingClientRect(); + return { + renderedRatio: imageBox.width / imageBox.height, + intrinsicRatio: + Number.parseFloat( image.getAttribute( 'width' ) ) / + Number.parseFloat( image.getAttribute( 'height' ) ), + fits: + imageBox.left >= linkBox.left && + imageBox.right <= linkBox.right && + imageBox.top >= linkBox.top && + imageBox.bottom <= linkBox.bottom, + }; + } ); + expect( imageGeometry.fits ).toBe( true ); + expect( Math.abs( imageGeometry.renderedRatio - imageGeometry.intrinsicRatio ) ).toBeLessThan( + 0.02, + ); + } ); + + test( `compact mobile header exposes one usable panel at ${ width }px`, async ( { page } ) => { + await page.setViewportSize( { width, height: 844 } ); + await page.goto( '/' ); + const header = page.getByRole( 'banner' ); + const inner = header.locator( '.chrnx-site-header__inner' ); + const searchButton = header.locator( '[data-chrnx-search-button]' ); + const themeButton = header.locator( '[data-chrnx-theme-button]' ); + const menuButton = header.locator( '[data-chrnx-menu-toggle]' ); + const searchPanel = header.locator( '[data-chrnx-search-panel]' ); + const themePanel = header.locator( '[data-chrnx-theme-panel]' ); + const nav = header.getByRole( 'navigation', { name: 'Primary navigation' } ); + + await searchButton.click(); + await expect( searchButton ).toHaveAccessibleName( 'Search' ); + await expect( searchButton ).toHaveAttribute( 'aria-expanded', 'true' ); + await expect( menuButton ).toHaveAttribute( 'aria-expanded', 'false' ); + await expect( themeButton ).toHaveAttribute( 'aria-expanded', 'false' ); + await expect( searchPanel ).toBeVisible(); + await expect( nav ).toBeHidden(); + await expect( themePanel ).toBeHidden(); + const innerBox = await getContentBox( inner ); + const searchBox = await getBox( searchPanel ); + const searchTriggerBox = await getBox( searchButton ); + expect( Math.abs( searchBox.x - innerBox.x ) ).toBeLessThanOrEqual( 1 ); + expect( Math.abs( searchBox.width - innerBox.width ) ).toBeLessThanOrEqual( 1 ); + expect( searchBox.y ).toBeGreaterThanOrEqual( searchTriggerBox.y + searchTriggerBox.height ); + await expectNoHorizontalOverflow( page, header ); + + await menuButton.click(); + await expect( menuButton ).toHaveAccessibleName( 'Close primary navigation' ); + await expect( menuButton ).toHaveAttribute( 'aria-expanded', 'true' ); + await expect( searchButton ).toHaveAccessibleName( 'Open search' ); + await expect( searchButton ).toHaveAttribute( 'aria-expanded', 'false' ); + await expect( themeButton ).toHaveAttribute( 'aria-expanded', 'false' ); + await expect( searchPanel ).toBeHidden(); + await expect( nav ).toBeVisible(); + await expect( themePanel ).toBeHidden(); + const navBox = await getBox( nav ); + const menuTriggerBox = await getBox( menuButton ); + expect( Math.abs( navBox.x - innerBox.x ) ).toBeLessThanOrEqual( 1 ); + expect( Math.abs( navBox.width - innerBox.width ) ).toBeLessThanOrEqual( 1 ); + expect( navBox.y ).toBeGreaterThanOrEqual( menuTriggerBox.y + menuTriggerBox.height ); + await expectNoHorizontalOverflow( page, header ); + + await themeButton.click(); + await expect( themeButton ).toHaveAccessibleName( 'Choose color theme' ); + await expect( themeButton ).toHaveAttribute( 'aria-expanded', 'true' ); + await expect( menuButton ).toHaveAccessibleName( 'Open primary navigation' ); + await expect( menuButton ).toHaveAttribute( 'aria-expanded', 'false' ); + await expect( searchButton ).toHaveAttribute( 'aria-expanded', 'false' ); + await expect( searchPanel ).toBeHidden(); + await expect( nav ).toBeHidden(); + await expect( themePanel ).toBeVisible(); + const themeBox = await getBox( themePanel ); + expect( themeBox.x ).toBeGreaterThanOrEqual( 0 ); + expect( themeBox.x + themeBox.width ).toBeLessThanOrEqual( width ); + for ( const name of [ 'Light', 'Dark', 'System' ] ) { + const radio = header.getByRole( 'radio', { name, exact: true } ); + await expect( radio ).toBeVisible(); + const radioBox = await getBox( radio ); + expect( radioBox.width ).toBeGreaterThan( 0 ); + expect( radioBox.height ).toBeGreaterThan( 0 ); + } + await expectNoHorizontalOverflow( page, header ); + } ); + } + + test( 'compact mobile header stops at the exact 599px boundary', async ( { page } ) => { + for ( const width of [ 600, 1280 ] ) { + await page.setViewportSize( { width, height: 800 } ); + await page.goto( '/' ); + const header = page.getByRole( 'banner' ); + const nav = header.getByRole( 'navigation', { name: 'Primary navigation' } ); + const themePanel = header.locator( '[data-chrnx-theme-panel]' ); + await expect( nav ).toBeVisible(); + await expect( themePanel ).toBeVisible(); + await expect( header.getByRole( 'radio', { name: 'System', exact: true } ) ).toBeVisible(); + await expect( header.locator( '[data-chrnx-menu-toggle]' ) ).toBeHidden(); + await expect( header.locator( '[data-chrnx-theme-button]' ) ).toBeHidden(); + await expect( header.getByRole( 'button', { name: 'Open search' } ) ).toBeVisible(); + await expect( header.getByRole( 'searchbox' ) ).toBeHidden(); + await expectNoHorizontalOverflow( page, header ); + + const headerBox = await getBox( header ); + if ( width === 600 ) { + expect( headerBox.height ).toBeGreaterThan( 100 ); + } else { + expect( headerBox.height ).toBeGreaterThanOrEqual( 65 ); + expect( headerBox.height ).toBeLessThanOrEqual( 75 ); + } + } + } ); + test( 'resize never leaves focus inside hidden navigation', async ( { page } ) => { await page.setViewportSize( { width: 1280, height: 800 } ); await page.goto( '/' ); @@ -183,6 +385,9 @@ test.describe( 'site navigation shell', () => { const submit = header.getByRole( 'button', { name: 'Search', exact: true } ); await expect( search ).toBeVisible(); await expect( submit ).toBeVisible(); + await expect( header.getByRole( 'img', { name: 'CHRNX' } ) ).toBeVisible(); + await expect( header.locator( '.chrnx-theme-noscript' ) ).toBeVisible(); + await expectNoHorizontalOverflow( page, header ); await search.fill( 'no js cats' ); await submit.click(); await expect( page ).toHaveURL( new RegExp( `[?&]s=no(?:\\+|%20)js(?:\\+|%20)cats(?:&|$)` ) );