Fix auto focus in accordion block#72021
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @byteninjaa0! In case you missed it, we'd love to have you join us in our Slack community. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. |
mikachan
left a comment
There was a problem hiding this comment.
Thanks for working on this! It's testing well for me 🙌
I've left some inline comments with some possible improvements, let me know what you think.
| const titleRef = useRef(); | ||
|
|
||
| // Set icon attributes. | ||
|
|
There was a problem hiding this comment.
I don't think we need this extra line here.
|
Thanks for the PR! I understand the approach using the After inserting the Accordion block, select it and press the "Add" button. The title field should no longer be focused: 7cbd6a7d394478b3342125eea843e019.mp4I haven't been able to look into it enough yet, but it might be worth looking a bit more into why the problem occurs rather than simply fixing the symptoms. |
|
@mikachan I was able to fix this problem by applying the following changes to trunk branch, but what do you think? git diff
diff --git a/packages/block-library/src/accordion-heading/edit.js b/packages/block-library/src/accordion-heading/edit.js
index 8818e9cc4f..ceddfb4ae7 100644
--- a/packages/block-library/src/accordion-heading/edit.js
+++ b/packages/block-library/src/accordion-heading/edit.js
@@ -33,7 +33,7 @@ export default function Edit( { attributes, setAttributes, context } ) {
return (
<TagName { ...blockProps }>
- <button
+ <div
className="wp-block-accordion-heading__toggle"
style={ spacingProps.style }
>
@@ -64,7 +64,7 @@ export default function Edit( { attributes, setAttributes, context } ) {
+
</span>
) }
- </button>
+ </div>
</TagName>
);
}
diff --git a/packages/block-library/src/accordion/edit.js b/packages/block-library/src/accordion/edit.js
index f628c93e9b..43fdae369e 100644
--- a/packages/block-library/src/accordion/edit.js
+++ b/packages/block-library/src/accordion/edit.js
@@ -56,7 +56,7 @@ export default function Edit( {
const isContentOnlyMode = blockEditingMode === 'contentOnly';
const innerBlocksProps = useInnerBlocksProps( blockProps, {
- template: [ [ ACCORDION_BLOCK_NAME ], [ ACCORDION_BLOCK_NAME ] ],
+ template: [ [ ACCORDION_BLOCK_NAME ] ],
defaultBlock: ACCORDION_BLOCK,
directInsert: true,
templateInsertUpdatesSelection: true,
In my tests, it works fine in most cases, except when I press the Add button, the RichText doesn't get focus. 260a98f5d649a55460d1bb1a7bcf41ca.mp4 |
|
@t-hamano yes it works for me too but don't you think changing the button tag to div will cause regressions? |
We used a button in the heading element so we could follow these examples as closely as possible: https://www.w3.org/WAI/ARIA/apg/patterns/accordion/examples/accordion/ and https://designsystem.digital.gov/components/accordion/. I believe the button helps describe the open/close action of the accordions. I'm not suggesting that we can't change the markup, but we should be careful not to reduce the accessibility. |
|
How about adding |
Good idea! I've just tried this out, and it works nicely when combined with reducing the number of accordions to one. I think this is a better solution as it's simpler and addresses the underlying issue. diff --git a/packages/block-library/src/accordion-header/edit.js b/packages/block-library/src/accordion-header/edit.js
index fbd5a4e3005..682fb79c898 100644
--- a/packages/block-library/src/accordion-header/edit.js
+++ b/packages/block-library/src/accordion-header/edit.js
@@ -52,6 +52,7 @@ export default function Edit( { attributes, setAttributes, context } ) {
style={ {
...spacingProps.style,
} }
+ tabIndex="-1"
>
{ showIcon && iconPosition === 'left' && (
<span
diff --git a/packages/block-library/src/accordion/edit.js b/packages/block-library/src/accordion/edit.js
index 7ed2693067f..52eced629b6 100644
--- a/packages/block-library/src/accordion/edit.js
+++ b/packages/block-library/src/accordion/edit.js
@@ -41,7 +41,7 @@ export default function Edit( {
const { insertBlock } = useDispatch( blockEditorStore );
const innerBlocksProps = useInnerBlocksProps( blockProps, {
- template: [ [ ACCORDION_BLOCK_NAME ], [ ACCORDION_BLOCK_NAME ] ],
+ template: [ [ ACCORDION_BLOCK_NAME ] ],
defaultBlock: ACCORDION_BLOCK,
: |
|
@byteninjaa0, could you try this approach? #72021 (comment) |
|
Yes sure |
|
it works for me too 2025-10-03.22-07-43.mp4 |
|
@t-hamano should i send a commit with the changes? |
|
@byteninjaa0 Yes 👍 |
Co-authored-by: byteninjaa0 <byteninjaa0@git.wordpress.org> Co-authored-by: mikachan <mikachan@git.wordpress.org> Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: hanneslsm <hanneslsm@git.wordpress.org>
Co-authored-by: byteninjaa0 <byteninjaa0@git.wordpress.org> Co-authored-by: mikachan <mikachan@git.wordpress.org> Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: hanneslsm <hanneslsm@git.wordpress.org>
|
I believe the failing E2e test is not related to this PR. See #71630 (comment) |
What?
Closes #71933
Fix: Add auto-focus to accordion heading on block selection
Why?
Automatically focus the accordion heading title field when the block
is selected, making it consistent with other Gutenberg blocks like
paragraphs and headings. This allows users to start typing immediately
after inserting an accordion block without requiring a manual click.
How?
The implementation uses useRef to reference the RichText component and
useEffect to focus it when isSelected becomes true. A setTimeout with
cleanup ensures the DOM is ready before focusing and prevents memory
leaks.The implementation uses useRef to reference the RichText component and
useEffect to focus it when isSelected becomes true. A setTimeout with
cleanup ensures the DOM is ready before focusing and prevents memory
leaks.
Testing Instructions
Testing Instructions for Keyboard
Screenshots or screencast
before
beforee.mp4
after
afterrr.mp4