Skip to content

fix(Modal): focus returns to the toggle on close, initial focus through the focus trap - #285

Merged
xidedix merged 7 commits into
v5.7from
fix/modal-focus-return
Oct 8, 2026
Merged

xidedix merged 7 commits into
v5.7from
fix/modal-focus-return

Conversation

@xidedix

@xidedix xidedix commented Oct 8, 2026

Copy link
Copy Markdown
Member

Modal returned focus on close to the element focused when it opened. Safari does not focus a button on click, so after a modal opened with the mouse that element was the body and focus was lost on close.

cModalToggle now passes its host as the trigger in the modal service action. The modal remembers the trigger only for an action that opens it, so a cModalToggle close button inside the modal does not replace it. On close, and when an open modal is destroyed, focus returns to that toggle, else to the element focused when the modal opened, skipping a target that is detached or cannot take focus. Focus moves only while it is inside the modal or on the body. The focus trap no longer auto-captures focus. On open the modal hands initial focus to the focus trap: the element marked cdkFocusInitial, otherwise the first tabbable element, as auto-capture did; when nothing in the modal can take focus, the modal itself takes it (the vanilla edition always focuses the modal itself). This replaces a hand-written selector that also matched elements that cannot take focus (a hidden input, an element that is not displayed), which left focus outside an open modal once auto-capture was gone, and drops FocusMonitor from Modal. A closed modal is hidden with inert only, as Offcanvas is: aria-hidden on the host made Chrome block it and warn whenever focus was still inside. Focus is also returned when the modal is closed and destroyed in the same change detection. A modal closed because another one opens hands its focus targets to that action, and the opened modal falls back to them on close; the helper skips targets inside the closing overlay. Switching to a second modal and back to the first no longer loses focus to the body when the first one closes.

Offcanvas and Modal now share one restoreFocus helper in shared/focus.utils.ts, and Offcanvas gets the same same-tick destroy handling in its ngOnDestroy. Focus is returned with focus({ preventScroll: true }) in both, as in the vanilla edition, instead of FocusMonitor.focusVia(…, 'keyboard') in Modal.

Docs: coreui/coreui-angular-docs branch fix/modal-focus-return.

cModalToggle passes its host as the trigger; on close or destroy while open, focus goes to that toggle, else to the element focused at open, only while focus is inside the modal or on body. The focus trap no longer auto-captures. Offcanvas and Modal share restoreFocus from shared/focus.utils.
@xidedix
xidedix merged commit 97fd9f9 into v5.7 Oct 8, 2026
9 checks passed
@xidedix
xidedix deleted the fix/modal-focus-return branch October 8, 2026 00:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant