From b15b8fd522109ae96a26fb869ed663bcf6b39bf7 Mon Sep 17 00:00:00 2001 From: Mitchell Austin Date: Tue, 8 Oct 2024 09:05:58 -0700 Subject: [PATCH] Post editor: fix meta boxes accessibility (#65466) * Replace details element with custom toggle * Consolidate details of updating height into function * Reduce conditionals for toggle vs. resizable * Fix style in RTL * Remove unneeded overflow style * Maintain focus even with caret browsing on * Use `ResizableBox` again * Add aria-expanded to toggle and remove aria-label * Fix styles for Windows high contrast mode * Add missing "border" in Windows high contrast mode * Move `flex-shrink` rule to toggle only * Fix rule preserved for non-iframe canvas * Make meta boxes container a navigable region * Try hiding meta box region contents when not expanded * Fix hidden conditional * Rename a couple functions Co-authored-by: DaniGuardiola Co-authored-by: joedolson Co-authored-by: t-hamano Co-authored-by: tyxla Co-authored-by: afercia --- .../edit-post/src/components/layout/index.js | 200 +++++++++--------- .../src/components/layout/style.scss | 126 +++++++---- .../src/components/navigable-region/index.js | 43 ++-- 3 files changed, 206 insertions(+), 163 deletions(-) diff --git a/packages/edit-post/src/components/layout/index.js b/packages/edit-post/src/components/layout/index.js index 20ef22b5f0eb2..88d0cd1588f03 100644 --- a/packages/edit-post/src/components/layout/index.js +++ b/packages/edit-post/src/components/layout/index.js @@ -31,6 +31,7 @@ import { useRef, useState, } from '@wordpress/element'; +import { chevronDown, chevronUp } from '@wordpress/icons'; import { store as noticesStore } from '@wordpress/notices'; import { store as preferencesStore } from '@wordpress/preferences'; import { @@ -43,6 +44,7 @@ import { addQueryArgs } from '@wordpress/url'; import { decodeEntities } from '@wordpress/html-entities'; import { store as coreStore } from '@wordpress/core-data'; import { + Icon, ResizableBox, SlotFillProvider, Tooltip, @@ -76,7 +78,7 @@ import useNavigateToEntityRecord from '../../hooks/use-navigate-to-entity-record const { getLayoutStyles } = unlock( blockEditorPrivateApis ); const { useCommands } = unlock( coreCommandsPrivateApis ); const { useCommandContext } = unlock( commandsPrivateApis ); -const { Editor, FullscreenMode } = unlock( editorPrivateApis ); +const { Editor, FullscreenMode, NavigableRegion } = unlock( editorPrivateApis ); const { BlockKeyboardShortcuts } = unlock( blockLibraryPrivateApis ); const DESIGN_POST_TYPES = [ 'wp_template', @@ -163,7 +165,7 @@ function MetaBoxesMain( { isLegacy } ) { ]; }, [] ); const { set: setPreference } = useDispatch( preferencesStore ); - const resizableBoxRef = useRef(); + const metaBoxesMainRef = useRef(); const isShort = useMediaQuery( '(max-height: 549px)' ); const [ { min, max }, setHeightConstraints ] = useState( () => ( {} ) ); @@ -178,9 +180,9 @@ function MetaBoxesMain( { isLegacy } ) { ':scope > .components-notice-list' ); const resizeHandle = container.querySelector( - '.edit-post-meta-boxes-main__resize-handle' + '.edit-post-meta-boxes-main__presenter' ); - const actualize = () => { + const deriveConstraints = () => { const fullHeight = container.offsetHeight; let nextMax = fullHeight; for ( const element of noticeLists ) { @@ -189,7 +191,7 @@ function MetaBoxesMain( { isLegacy } ) { const nextMin = resizeHandle.offsetHeight; setHeightConstraints( { min: nextMin, max: nextMax } ); }; - const observer = new window.ResizeObserver( actualize ); + const observer = new window.ResizeObserver( deriveConstraints ); observer.observe( container ); for ( const element of noticeLists ) { observer.observe( element ); @@ -201,12 +203,33 @@ function MetaBoxesMain( { isLegacy } ) { const separatorHelpId = useId(); const [ isUntouched, setIsUntouched ] = useState( true ); + const applyHeight = ( candidateHeight, isPersistent, isInstant ) => { + const nextHeight = Math.min( max, Math.max( min, candidateHeight ) ); + if ( isPersistent ) { + setPreference( + 'core/edit-post', + 'metaBoxesMainOpenHeight', + nextHeight + ); + } else { + separatorRef.current.ariaValueNow = getAriaValueNow( nextHeight ); + } + if ( isInstant ) { + metaBoxesMainRef.current.updateSize( { + height: nextHeight, + // Oddly, when the event that triggered this was not from the mouse (e.g. keydown), + // if `width` is left unspecified a subsequent drag gesture applies a fixed + // width and the pane fails to widen/narrow with parent width changes from + // sidebars opening/closing or window resizes. + width: 'auto', + } ); + } + }; if ( ! hasAnyVisible ) { return; } - const className = 'edit-post-meta-boxes-main'; const contents = (