Skip to content
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: simplify focus logic in Tab keydown handler #7989

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions packages/grid/src/vaadin-grid-keyboard-navigation-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ export const KeyboardNavigationMixin = (superClass) =>

/** @private */
_onTabKeyDown(e) {
const focusTarget = this._predictFocusStepTarget(e.composedPath()[0], e.shiftKey ? -1 : 1);
let focusTarget = this._predictFocusStepTarget(e.composedPath()[0], e.shiftKey ? -1 : 1);

// Can be undefined if grid has tabindex
if (!focusTarget) {
Expand All @@ -722,24 +722,24 @@ export const KeyboardNavigationMixin = (superClass) =>
// Prevent focus-trap logic from intercepting the event.
e.stopPropagation();

if (focusTarget === this.$.table) {
// The focus is about to exit the grid to the top.
this.$.table.focus();
} else if (focusTarget === this.$.focusexit) {
// The focus is about to exit the grid to the bottom.
this.$.focusexit.focus();
} else if (focusTarget === this._itemsFocusable) {
if (focusTarget === this._itemsFocusable) {
this.__ensureFlatIndexInViewport(this._focusedItemIndex);

// Ensure the correct element is set as focusable after scrolling.
// The virtualizer may use a different element to render the item.
this.__updateItemsFocusable();

focusTarget = this._itemsFocusable;
}

focusTarget.focus();

// If the next element is the table or focusexit, it indicates the user
// intends to leave the grid. In this case, we move focus to these elements
// without preventing the default Tab behavior. The default behavior then
// starts from these elements and moves focus outside the grid.
if (focusTarget !== this.$.table && focusTarget !== this.$.focusexit) {
e.preventDefault();
this._itemsFocusable.focus();
} else {
e.preventDefault();
focusTarget.focus();
}

this.toggleAttribute('navigating', true);
Expand Down