Tracking Focus After Closing a Confirmation Dialog | dialog

プログラミング・Web開発カテゴリを表すパンダのイラスト Programming / Web Development

About This Article
This article was created using an automated generation workflow powered by generative AI. Official documentation and fixed instructional materials were reviewed to organize operational and technical constraints.

Verification Status: 📘 Official Information and Implementation Cross-Check. Browser on-device testing not performed.

Losing track of the input position after closing a confirmation screen makes keyboard navigation difficult. We examine where the focus returns after pressing Esc or canceling.

Open Live Demo

Key Terms to Know First

  • A modalis a screen that prevents interaction with the background while it is open.

  • Focusis the element that receives keyboard input.

Thinking via State Transition Diagrams

Arrows represent triggers for focus movement. We check whether focus returns to the origin element regardless of the closure method.

stateDiagram-v2
    state "起点のボタン" as Launch
    state "確認画面の中" as Dialog
    [*] --> Launch
    Launch --> Dialog: 開く
    Dialog --> Launch: キャンセル
    Dialog --> Launch: Esc

Operating the Live Demo

Open the confirmation screen. Press Tab and Esc.

How to Read Results

Check the results section to see whether focus returns to the originating button upon closing.

Press Tab immediately after opening the confirmation screen. Verify which button focus moves to. Check that the origin point is the same whether closed via Esc or a click.

Key Code Highlights

Display it using showModal(), then read document.activeElement after cancellation.

You can check the implementation in the "Implementation Code" section of the demo screen.

When using it in production

Also verify whether data is prevented from being lost upon cancellation inside forms.

What to verify

Pressing the execute button creates a button that opens a confirmation screen. Navigate to this button using the keyboard Tab key, and open it with Enter or Space. Expect a message asking whether to continue with the changes and a cancel button to appear in the center of the screen. The primary point of interest is where the keyboard input target moves immediately after opening. Press Tab to check the destination, and press Esc to close. Check whether the phrase "returned to the starting button" appears in the result field. If it moves to a different element, the tag name of that element is displayed in the result field.

Next, open it again using the same starting point, and close it by pressing the cancel button instead of Esc. Compare the results for both closing methods. This is not a tutorial material for deleting or modifying actual business data through cancellation. It is strictly focused on observing opening/closing and focus.

ActionWhat to observe
Open via keyboardInitially selected element
Press TabNavigation inside the confirmation screen
Close with EscDoes it return to the starting point?
Press CancelFocus after closing

Modal display and input positioning

showModal is a method that opens a dialog as a modal. It can be used to restrict interaction with the document behind it and direct attention into the confirmation screen. It is different from simply displaying a rectangle on top of the screen. Focus is the current keyboard operation target and is distinct from the mouse pointer position. If users do not know where they will return after closing the screen, it becomes difficult for them to resume input.

MDN documentation explains that when opened with showModal, an internal focusable element is selected. If you want to specify the initial position explicitly, consider using autofocus or similar attributes. There is one cancel button within this screen, which can be used to close it. The implementation listens for the dialog's close event and compares whether document.activeElement is the same as the starting button. This tutorial material observes the behavior performed by the browser rather than processing that manually sets focus to the starting point.

It may not always return to the starting point.

When a button is clicked with the mouse, it does not necessarily receive focus. Behavior may vary depending on browser or device settings. It is easier to identify the cause by testing separately when the starting point is explicitly selected with the keyboard before opening versus when it is opened via a click. If the starting point button is deleted or disabled during processing, an alternative return destination must be considered.

In an actual application screen, verify whether the entered content and operational position can be maintained when returning from the confirmation screen. Some designs remove input fields after a successful submission, in which case focus should move to a completion message or the next appropriate action. Returning to the starting point is not always necessary; it should align with what the user does next. Also verify whether headings, descriptions, and button roles within the confirmation screen can be understood via screen readers.

When the implementation differs from expectations

First, verify whether the button created after execution is being clicked. If only the opening operation fails, check browser compatibility and the console. If it cannot be closed with the Esc key, check alternative key handling and cancel event management. Do not determine focus position solely by visual borders; cross-reference it with activeElement in the result field. Conduct tests for supported browsers, keyboard operations, magnification, and screen readers separately.

Published learning materials do not cover actual application contents or deletion operations. When introducing this into business workflows, clearly separate the confirmation before executing critical processing from the display of processing results. Make cancellations and approvals explicit so that closing the modal itself does not imply the finalization of the business task.

Verification Scope

Target: HTML dialog / showModal / close. Static verification of MDN dialog and fixed HTML showModal, close, onclose, activeElement. Keyboard and actual DOM operations not executed. Browser compatibility, display, accessibility, and Mermaid rendering are unverified.

References

GitHub Sample

Implementation and Experiment Procedure

Document information

Article title
Tracking Focus After Closing a Confirmation Dialog | dialog
Published
Updated
Source
https://papanda925.com/?p=19135&lang=en

License: Text and original figures for which this site holds the relevant rights are available under CC BY 4.0 , unless otherwise noted. This article may include content created or edited with generative AI. If code has a separate license notice or a linked GitHub repository license, that license takes precedence for the code. Quotations, third-party materials, images, and trademarks are excluded from this license. Usage policy

Copied title and URL