-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accessibility improvements for Block Inserter #37357
Changes from all commits
38868d5
83fb9e0
ab9bd8d
5637346
cc51c95
1dbdad0
7975ec1
76962cb
701f930
1afc148
9764c59
0d9fdf2
2a97df2
64f0b8f
b975b40
c4715a1
ea055f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,15 @@ | |
* WordPress dependencies | ||
*/ | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { Button } from '@wordpress/components'; | ||
import { Button, VisuallyHidden } from '@wordpress/components'; | ||
import { __experimentalLibrary as Library } from '@wordpress/block-editor'; | ||
import { close } from '@wordpress/icons'; | ||
import { | ||
useViewportMatch, | ||
__experimentalUseDialog as useDialog, | ||
} from '@wordpress/compose'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { useEffect, useRef } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -23,23 +25,36 @@ export default function InserterSidebar() { | |
); | ||
|
||
const isMobile = useViewportMatch( 'medium', '<' ); | ||
const TagName = ! isMobile ? VisuallyHidden : 'div'; | ||
const [ inserterDialogRef, inserterDialogProps ] = useDialog( { | ||
onClose: () => setIsInserterOpened( false ), | ||
focusOnMount: null, | ||
} ); | ||
|
||
const inserterContentRef = useRef(); | ||
useEffect( () => { | ||
inserterContentRef.current | ||
.querySelector( '.block-editor-inserter__search input' ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we use pass a Or we can use If all of the above can't be done, then I'd prefer using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think @youknowriad wanted to avoid doing anything with the block-editor/inserter component because it could be used as a single element. E.g. keep all the implementation in the sidebar files and out of what could be a reusable stand-alone component. I actually used forwardRef() all the way to the Search component a few commits back. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep I noticed that, but I think my approach is different because it's not using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kevin940726 I think a ref with an added property is a sane approach indeed. I'm fine either way but we should be mindful of added complexity. |
||
.focus(); | ||
}, [] ); | ||
|
||
return ( | ||
<div | ||
ref={ inserterDialogRef } | ||
{ ...inserterDialogProps } | ||
className="edit-site-editor__inserter-panel" | ||
> | ||
<div className="edit-site-editor__inserter-panel-header"> | ||
<TagName className="edit-post-editor__inserter-panel-header"> | ||
<Button | ||
icon={ close } | ||
label={ __( 'Close block inserter' ) } | ||
onClick={ () => setIsInserterOpened( false ) } | ||
/> | ||
</div> | ||
<div className="edit-site-editor__inserter-panel-content"> | ||
</TagName> | ||
<div | ||
className="edit-post-editor__inserter-panel-content" | ||
ref={ inserterContentRef } | ||
> | ||
<Library | ||
showInserterHelpPanel | ||
shouldFocusBlock={ isMobile } | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Button } from '@wordpress/components'; | ||
import { Button, VisuallyHidden } from '@wordpress/components'; | ||
import { close } from '@wordpress/icons'; | ||
import { __experimentalLibrary as Library } from '@wordpress/block-editor'; | ||
import { | ||
useViewportMatch, | ||
__experimentalUseDialog as useDialog, | ||
} from '@wordpress/compose'; | ||
import { useCallback } from '@wordpress/element'; | ||
import { useCallback, useEffect, useRef } from '@wordpress/element'; | ||
import { useDispatch } from '@wordpress/data'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -24,23 +25,39 @@ export default function InserterSidebar() { | |
const { setIsInserterOpened } = useDispatch( editWidgetsStore ); | ||
|
||
const closeInserter = useCallback( () => { | ||
return () => setIsInserterOpened( false ); | ||
return setIsInserterOpened( false ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is indeed a good fix. |
||
}, [ setIsInserterOpened ] ); | ||
|
||
const TagName = ! isMobileViewport ? VisuallyHidden : 'div'; | ||
const [ inserterDialogRef, inserterDialogProps ] = useDialog( { | ||
onClose: closeInserter, | ||
focusOnMount: null, | ||
} ); | ||
|
||
const inserterContentRef = useRef(); | ||
useEffect( () => { | ||
inserterContentRef.current | ||
.querySelector( '.block-editor-inserter__search input' ) | ||
.focus(); | ||
}, [] ); | ||
|
||
return ( | ||
<div | ||
ref={ inserterDialogRef } | ||
{ ...inserterDialogProps } | ||
className="edit-widgets-layout__inserter-panel" | ||
> | ||
<div className="edit-widgets-layout__inserter-panel-header"> | ||
<Button icon={ close } onClick={ closeInserter } /> | ||
</div> | ||
<div className="edit-widgets-layout__inserter-panel-content"> | ||
<TagName className="edit-widgets-layout__inserter-panel-header"> | ||
<Button | ||
icon={ close } | ||
onClick={ closeInserter } | ||
label={ __( 'Close block inserter' ) } | ||
/> | ||
</TagName> | ||
<div | ||
className="edit-widgets-layout__inserter-panel-content" | ||
ref={ inserterContentRef } | ||
> | ||
<Library | ||
showInserterHelpPanel | ||
shouldFocusBlock={ isMobileViewport } | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had to set this to null to prevent the useFocusOnMount hook from firing. Now that useEffect is being used, it would cause duplicate focus events.