-
Notifications
You must be signed in to change notification settings - Fork 13.5k
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
refactor(item): do not automatically delegate focus #29091
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c8871b0
refactor(item): do not automatically delegate focus
liamdebeasi 48e2e97
fix(menu): focus trap works correctly
liamdebeasi 2d5b7e7
remove only
liamdebeasi d81b8bb
chore(): add updated snapshots
Ionitron 5aee15f
improve comment
liamdebeasi 3f9f94c
chore: use consistent types
liamdebeasi 37ad77a
chore: use additional type parameter
liamdebeasi af6eaaf
Merge branch 'feature-8.0' into FW-4847
liamdebeasi b191746
refactor: focus descendant automatically uses ref as fallback
liamdebeasi f6116f0
Merge branch 'feature-8.0' into FW-4847
liamdebeasi bc0995e
fix(input, textarea): clicking padding focuses text field
liamdebeasi e6450bb
remove only
liamdebeasi 9ed7c62
Merge branch 'feature-8.0' into FW-4847
liamdebeasi 143fb7d
fix(item): input and textarea do not receive cursor
liamdebeasi cc88c37
Merge branch 'feature-8.0' into FW-4847
liamdebeasi 4362c3b
clarify name
liamdebeasi 0c9311c
a word
liamdebeasi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Binary file modified
BIN
+123 Bytes
(100%)
...menu.e2e.ts-snapshots/menu-basic-doc-dir-toggled-md-ltr-Mobile-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+212 Bytes
(100%)
...enu.e2e.ts-snapshots/menu-basic-doc-dir-toggled-md-ltr-Mobile-Firefox-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+383 Bytes
(100%)
...menu.e2e.ts-snapshots/menu-basic-doc-dir-toggled-md-ltr-Mobile-Safari-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+111 Bytes
(100%)
...ic/menu.e2e.ts-snapshots/menu-basic-side-toggled-md-ltr-Mobile-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+241 Bytes
(100%)
...c/menu.e2e.ts-snapshots/menu-basic-side-toggled-md-ltr-Mobile-Firefox-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+363 Bytes
(100%)
...ic/menu.e2e.ts-snapshots/menu-basic-side-toggled-md-ltr-Mobile-Safari-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-175 Bytes
(99%)
...st/basic/menu.e2e.ts-snapshots/menu-basic-start-ios-ltr-Mobile-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-195 Bytes
(99%)
...t/basic/menu.e2e.ts-snapshots/menu-basic-start-ios-ltr-Mobile-Firefox-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+551 Bytes
(100%)
...st/basic/menu.e2e.ts-snapshots/menu-basic-start-ios-ltr-Mobile-Safari-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-210 Bytes
(99%)
...st/basic/menu.e2e.ts-snapshots/menu-basic-start-ios-rtl-Mobile-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-37 Bytes
(100%)
...t/basic/menu.e2e.ts-snapshots/menu-basic-start-ios-rtl-Mobile-Firefox-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+533 Bytes
(100%)
...st/basic/menu.e2e.ts-snapshots/menu-basic-start-ios-rtl-Mobile-Safari-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+34 Bytes
(100%)
...est/basic/menu.e2e.ts-snapshots/menu-basic-start-md-ltr-Mobile-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+226 Bytes
(100%)
...st/basic/menu.e2e.ts-snapshots/menu-basic-start-md-ltr-Mobile-Firefox-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+319 Bytes
(100%)
...est/basic/menu.e2e.ts-snapshots/menu-basic-start-md-ltr-Mobile-Safari-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+123 Bytes
(100%)
...est/basic/menu.e2e.ts-snapshots/menu-basic-start-md-rtl-Mobile-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+212 Bytes
(100%)
...st/basic/menu.e2e.ts-snapshots/menu-basic-start-md-rtl-Mobile-Firefox-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+383 Bytes
(100%)
...est/basic/menu.e2e.ts-snapshots/menu-basic-start-md-rtl-Mobile-Safari-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { focusVisibleElement } from '@utils/helpers'; | ||
|
||
/** | ||
* This query string selects elements that | ||
* are eligible to receive focus. We select | ||
* interactive elements that meet the following | ||
* criteria: | ||
* 1. Element does not have a negative tabindex | ||
* 2. Element does not have `hidden` | ||
* 3. Element does not have `disabled` for non-Ionic components. | ||
* 4. Element does not have `disabled` or `disabled="true"` for Ionic components. | ||
* Note: We need this distinction because `disabled="false"` is | ||
* valid usage for the disabled property on ion-button. | ||
*/ | ||
export const focusableQueryString = | ||
'[tabindex]:not([tabindex^="-"]):not([hidden]):not([disabled]), input:not([type=hidden]):not([tabindex^="-"]):not([hidden]):not([disabled]), textarea:not([tabindex^="-"]):not([hidden]):not([disabled]), button:not([tabindex^="-"]):not([hidden]):not([disabled]), select:not([tabindex^="-"]):not([hidden]):not([disabled]), .ion-focusable:not([tabindex^="-"]):not([hidden]):not([disabled]), .ion-focusable[disabled="false"]:not([tabindex^="-"]):not([hidden])'; | ||
|
||
/** | ||
* Focuses the first descendant in a context | ||
* that can receive focus. If none exists, | ||
* a fallback element will be focused. | ||
* This fallback is typically an ancestor | ||
* container such as a menu or overlay so focus does not | ||
* leave the container we are trying to trap focus in. | ||
* | ||
* If no fallback is specified then we focus the container itself. | ||
*/ | ||
export const focusFirstDescendant = <R extends HTMLElement, T extends HTMLElement>(ref: R, fallbackElement?: T) => { | ||
const firstInput = ref.querySelector<HTMLElement>(focusableQueryString); | ||
|
||
focusElementInContext(firstInput, fallbackElement ?? ref); | ||
}; | ||
|
||
/** | ||
* Focuses the last descendant in a context | ||
* that can receive focus. If none exists, | ||
* a fallback element will be focused. | ||
* This fallback is typically an ancestor | ||
* container such as a menu or overlay so focus does not | ||
* leave the container we are trying to trap focus in. | ||
* | ||
* If no fallback is specified then we focus the container itself. | ||
*/ | ||
export const focusLastDescendant = <R extends HTMLElement, T extends HTMLElement>(ref: R, fallbackElement?: T) => { | ||
const inputs = Array.from(ref.querySelectorAll<HTMLElement>(focusableQueryString)); | ||
const lastInput = inputs.length > 0 ? inputs[inputs.length - 1] : null; | ||
|
||
focusElementInContext(lastInput, fallbackElement ?? ref); | ||
}; | ||
|
||
/** | ||
* Focuses a particular element in a context. If the element | ||
* doesn't have anything focusable associated with it then | ||
* a fallback element will be focused. | ||
* | ||
* This fallback is typically an ancestor | ||
* container such as a menu or overlay so focus does not | ||
* leave the container we are trying to trap focus in. | ||
* This should be used instead of the focus() method | ||
* on most elements because the focusable element | ||
* may not be the host element. | ||
* | ||
* For example, if an ion-button should be focused | ||
* then we should actually focus the native <button> | ||
* element inside of ion-button's shadow root, not | ||
* the host element itself. | ||
*/ | ||
const focusElementInContext = <T extends HTMLElement>( | ||
hostToFocus: HTMLElement | null | undefined, | ||
fallbackElement: T | ||
) => { | ||
let elementToFocus = hostToFocus; | ||
|
||
const shadowRoot = hostToFocus?.shadowRoot; | ||
if (shadowRoot) { | ||
// If there are no inner focusable elements, just focus the host element. | ||
elementToFocus = shadowRoot.querySelector<HTMLElement>(focusableQueryString) || hostToFocus; | ||
} | ||
|
||
if (elementToFocus) { | ||
focusVisibleElement(elementToFocus); | ||
} else { | ||
// Focus fallback element instead of letting focus escape | ||
fallbackElement.focus(); | ||
} | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
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.
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.
Nitpick: The padding around the input now has
cursor: pointer
, even though the rest of the control (label, input) doesn't; the inconsistency looks strange. For the other controls, it's consistent sincecursor: pointer
is used over the entire control.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.
Good catch. Any thoughts on how we could mitigate?
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.
That
cursor: pointer
is coming from theitem-has-interactive-control
class onion-item
, and then it gets overwritten bylabel
's default styles. Either we could addcursor: pointer
to the label ---- or make items with inputs/textareas not get
cursor: pointer
at all, likely by having it not add the class in that case. I'm not really sure which is best though 🤔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.
In 143fb7d I updated the code so that inputs and textures do not have the cursor effect. I updated the name of the class to be more descriptive of what it's doing, but I can change the name if you have other ideas.
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.
I could also do this with CSS and have it be
.item-has-interactive-control:not(:has(ion-input, ion-textarea))
but the specificity gets a little complicated, so I opted to just apply the class using logic in JS (since the class is already applied using JS logic)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.
Maybe something like
item-has-click-toggle-control
oritem-control-needs-pointer-cursor
? "Needs cursor" by itself is confusing to me since in theory everything needs a cursor, like initially clicking into the input.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.
Clarified in 4362c3b and 0c9311c