-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
feat: focus [autofocus]
element in a popup
#2736
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -221,22 +221,50 @@ const setupTimer = (globalState, innerParams, dismissWith) => { | |
} | ||
|
||
/** | ||
* Initialize focus in the popup: | ||
* | ||
* 1. If `toast` is `true`, don't steal focus from the document. | ||
* 2. Else if there is an [autofocus] element, focus it. | ||
* 3. Else if `focusConfirm` is `true` and confirm button is visible, focus it. | ||
* 4. Else if `focusDeny` is `true` and deny button is visible, focus it. | ||
* 5. Else if `focusCancel` is `true` and cancel button is visible, focus it. | ||
* 6. Else focus the first focusable element in a popup (if any). | ||
* | ||
* @param {DomCache} domCache | ||
* @param {SweetAlertOptions} innerParams | ||
*/ | ||
const initFocus = (domCache, innerParams) => { | ||
if (innerParams.toast) { | ||
return | ||
} | ||
|
||
// TODO: this is dumb, remove `allowEnterKey` param in the next major version | ||
if (!callIfFunction(innerParams.allowEnterKey)) { | ||
blurActiveElement() | ||
return | ||
} | ||
|
||
if (!focusButton(domCache, innerParams)) { | ||
setFocus(-1, 1) | ||
if (focusAutofocus(domCache)) { | ||
return | ||
} | ||
|
||
if (focusButton(domCache, innerParams)) { | ||
return | ||
} | ||
|
||
setFocus(-1, 1) | ||
} | ||
|
||
/** | ||
* @param {DomCache} domCache | ||
* @returns {boolean} | ||
*/ | ||
const focusAutofocus = (domCache) => { | ||
const autofocusElement = domCache.popup.querySelector('[autofocus]') | ||
if (autofocusElement instanceof HTMLElement && dom.isVisible(autofocusElement)) { | ||
autofocusElement.focus() | ||
return true | ||
} | ||
return false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure proper accessibility handling when focusing elements. The |
||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactor
initFocus
to improve readability and maintainability.The
initFocus
function has several conditional checks to determine where the focus should be set. To improve readability and maintainability, consider restructuring this function to reduce nesting and clarify the logic flow:This refactoring uses an array of functions that encapsulate the focus logic, making it easier to add or remove focus strategies and improving the code's testability.
Committable suggestion