Skip to content

Templates: Add back button & fix focus loss when navigating through template creation flow #70091

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

Merged
merged 4 commits into from
Jun 12, 2025

Conversation

Mayank-Tripathi32
Copy link
Contributor

@Mayank-Tripathi32 Mayank-Tripathi32 commented May 9, 2025

Added by @t-hamano

Note

Don't forget to add @Sourav61, who worked in #69711, as a co-author when merging this PR.


What?

Closes #69711

Adds a Back button to the template creation flow and sets focus on the name input field when creating a custom template.

Why?

When clicking the "Custom template" button, the focus is currently lost, causing accessibility issues for screen reader users. This PR fixes the focus management and improves the navigation flow by adding a Back button that allows users to return to the previous step without closing the entire modal.

How?

  • Added a Back button to the custom template modal that returns users to the template selection screen
  • Implemented focus management using requestAnimationFrame to set focus on the name input field when the custom template modal opens
  • Removed the redundant Cancel button to simplify the UI and make navigation more intuitive

Testing Instructions

  1. Open the Site Editor
  2. Click on "Templates" in the navigation menu
  3. Click the "Add Template" button
  4. Click the "Custom template" button
  5. Verify that focus is set on the Name input field
  6. Click the "Back" button
  7. Verify that you return to the template selection screen

Testing Instructions for Keyboard

  1. Open the Site Editor
  2. Use Tab to navigate to the "Templates" option in the navigation menu and press Enter
  3. Tab to the "Add Template" button and press Enter
  4. Tab to the "Custom template" button and press Enter
  5. Verify that focus is automatically set on the Name input field
  6. Tab to the "Back" button and press Enter
  7. Verify that you return to the template selection screen

Screencast

https://q7utzrengv.ufs.sh/f/Wgl9eBAmTj29r0ltzJ9bvXLPnVf0wHAxqleM96uGDdWoCa3T

@Mayank-Tripathi32 Mayank-Tripathi32 marked this pull request as ready for review May 13, 2025 11:23
Copy link

github-actions bot commented May 13, 2025

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: Mayank-Tripathi32 <mayanktripathi32@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: Sourav61 <sourav08@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@Mayank-Tripathi32
Copy link
Contributor Author

I would like some feedback on PR and on issue with other focus loss:

Here is video, that better shows the focus loss when going to post based templates
https://q7utzrengv.ufs.sh/f/Wgl9eBAmTj29DqvG0WCE9O3LjigMnKlJx2AvwtoUc0hWd7yz

Let me know if we should fix these in this PR itself

@carolinan carolinan added [Type] Bug An existing feature does not function as intended [Feature] Site Editor Related to the overarching Site Editor (formerly "full site editing") labels May 15, 2025
Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I think this PR is a great a11y improvement, but we need to address two more things:

First, we need the back button in the "Add template: Post" modal as well:

image

Second, when the Back button is pressed, focus is lost. We should focus either the modal itself or the first focusable element within the modal.

@t-hamano t-hamano added [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Feature] Templates API Related to API powering block template functionality in the Site Editor labels May 29, 2025
@Mayank-Tripathi32
Copy link
Contributor Author

Hey @t-hamano, I have added the back button in all steps and fixed focus loss issue.

https://q7utzrengv.ufs.sh/f/Wgl9eBAmTj29pojunySmx9PaNdTZpH58C7GnvcBY3RjewJus

Can you check? Also should we focus heading or first tab item? I feel that currently it just loops 3 times "Add template" when creating posts/page templates.

Comment on lines +179 to +188
// Focus on the first focusable element when the modal opens.
// We handle focus management in the parent modal, just need to focus on the first focusable element.
useEffect( () => {
if ( containerRef.current ) {
const [ firstFocusable ] = focus.focusable.find(
containerRef.current
);
firstFocusable?.focus();
}
}, [ showSearchEntities ] );
Copy link
Contributor

@t-hamano t-hamano Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking that we could centralize all focus management in the modal, i.e. NewTemplateModal. I tried the following code.

Details
diff --git a/packages/edit-site/src/components/add-new-template/add-custom-generic-template-modal-content.js b/packages/edit-site/src/components/add-new-template/add-custom-generic-template-modal-content.js
index dd8ccce86f..e5b21c8633 100644
--- a/packages/edit-site/src/components/add-new-template/add-custom-generic-template-modal-content.js
+++ b/packages/edit-site/src/components/add-new-template/add-custom-generic-template-modal-content.js
@@ -6,7 +6,7 @@ import { paramCase as kebabCase } from 'change-case';
 /**
  * WordPress dependencies
  */
-import { useState, useEffect, useRef } from '@wordpress/element';
+import { useState, useRef } from '@wordpress/element';
 import { __ } from '@wordpress/i18n';
 import {
        Button,
@@ -21,13 +21,6 @@ function AddCustomGenericTemplateModalContent( { createTemplate, onBack } ) {
        const [ isBusy, setIsBusy ] = useState( false );
        const inputRef = useRef();
 
-       // Set focus to the name input when the component mounts
-       useEffect( () => {
-               if ( inputRef.current ) {
-                       inputRef.current.focus();
-               }
-       }, [] );
-
        async function onCreateTemplate( event ) {
                event.preventDefault();
                if ( isBusy ) {
diff --git a/packages/edit-site/src/components/add-new-template/add-custom-template-modal-content.js b/packages/edit-site/src/components/add-new-template/add-custom-template-modal-content.js
index 7a846985c8..3b0d9b998c 100644
--- a/packages/edit-site/src/components/add-new-template/add-custom-template-modal-content.js
+++ b/packages/edit-site/src/components/add-new-template/add-custom-template-modal-content.js
@@ -16,7 +16,6 @@ import {
 import { useEntityRecords } from '@wordpress/core-data';
 import { decodeEntities } from '@wordpress/html-entities';
 import { useDebouncedInput } from '@wordpress/compose';
-import { focus } from '@wordpress/dom';
 
 /**
  * Internal dependencies
@@ -170,23 +169,11 @@ function AddCustomTemplateModalContent( {
        onSelect,
        entityForSuggestions,
        onBack,
-       containerRef,
 } ) {
        const [ showSearchEntities, setShowSearchEntities ] = useState(
                entityForSuggestions.hasGeneralTemplate
        );
 
-       // Focus on the first focusable element when the modal opens.
-       // We handle focus management in the parent modal, just need to focus on the first focusable element.
-       useEffect( () => {
-               if ( containerRef.current ) {
-                       const [ firstFocusable ] = focus.focusable.find(
-                               containerRef.current
-                       );
-                       firstFocusable?.focus();
-               }
-       }, [ showSearchEntities ] );
-
        return (
                <VStack
                        spacing={ 4 }
diff --git a/packages/edit-site/src/components/add-new-template/index.js b/packages/edit-site/src/components/ad
d-new-template/index.js
index aa36fa2e59..5ad4f11ff0 100644
--- a/packages/edit-site/src/components/add-new-template/index.js
+++ b/packages/edit-site/src/components/add-new-template/index.js
@@ -184,10 +184,7 @@ function NewTemplateModal( { onClose } ) {
        useEffect( () => {
                // Focus the first focusable element when component mounts or UI changes
                // We don't want to focus on the other modals because they have their own focus management.
-               if (
-                       containerRef.current &&
-                       modalContent === modalContentMap.templatesList
-               ) {
+               if ( containerRef.current ) {
                        const [ firstFocusable ] = focus.focusable.find(
                                containerRef.current
                        );
@@ -339,7 +336,6 @@ function NewTemplateModal( { onClose } ) {
                                        onBack={ () =>
                                                setModalContent( modalContentMap.templatesList )
                                        }
-                                       containerRef={ containerRef }
                                />
                        ) }
                        { modalContent === modalContentMap.customGenericTemplate && (

This ensures that the modal dialog itself consistently gets focus when the modal content is switched. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two reasons, I did it this way:

  1. We wanted to focus on input field when its present, its related to this comment.

In my opinion, the solution to this would be to set focus to the "Name" input, and also add a 'Back' option, so that a user can cancel the Create Custom Template action without having to start over. This should be true for all templates: selecting "Category Archives", for example, should provide an option to return to the selection screen.

  1. This works fine but we will then need to move state of
	const [ showSearchEntities, setShowSearchEntities ] = useState(
		entityForSuggestions.hasGeneralTemplate
	);

to parent component, as there is also focus loss when going from "Select whether to create a single template for all items or a specific one." to "This template will be used only for the specific item chosen." and back. This happens as useEffect does not have it as dependancy in parent block.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, in the AddCustomTemplateModalContent component, the content switches depending on the showSearchEntities state, so this focus management is necessary.

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! It's working very well so far.

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my testing, it works very well and is a great accessibility improvement.

@t-hamano t-hamano merged commit fadd43b into WordPress:trunk Jun 12, 2025
75 of 76 checks passed
@github-actions github-actions bot added this to the Gutenberg 21.1 milestone Jun 12, 2025
@talldan talldan changed the title Enhance: Add back button & fix focus loss when navigating Templates: Add back button & fix focus loss when navigating through template creation flow Jun 25, 2025
chriszarate pushed a commit to chriszarate/gutenberg that referenced this pull request Jul 1, 2025
…70091)

* feat: add focus to name input and back navigation

* refactor: simplify focus handling for name input in template modal

* feat: add back navigation functionality to custom template modal

* feat: enhance focus management in create template modals

Co-authored-by: Mayank-Tripathi32 <mayanktripathi32@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: Sourav61 <sourav08@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Site Editor Related to the overarching Site Editor (formerly "full site editing") [Feature] Templates API Related to API powering block template functionality in the Site Editor [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants