Repository navigation
fix(Modal): focus returns to the toggle on close, initial focus through the focus trap - #285
Merged
Merged
Conversation
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.
…e-tick destroy and inside shadow roots
…en first tabbable), else the modal itself
…, so focus survives switching back
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
cModalTogglenow passes its host as the trigger in the modal service action. The modal remembers the trigger only for an action that opens it, so acModalToggleclose 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 markedcdkFocusInitial, 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 dropsFocusMonitorfrom Modal. A closed modal is hidden withinertonly, as Offcanvas is:aria-hiddenon 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
restoreFocushelper inshared/focus.utils.ts, and Offcanvas gets the same same-tick destroy handling in itsngOnDestroy. Focus is returned withfocus({ preventScroll: true })in both, as in the vanilla edition, instead ofFocusMonitor.focusVia(…, 'keyboard')in Modal.Docs: coreui/coreui-angular-docs branch fix/modal-focus-return.