Skip to content

Commit

Permalink
refactor: simplify focus logic in Tab keydown handler (#7989)
Browse files Browse the repository at this point in the history
  • Loading branch information
vursen authored Oct 23, 2024
1 parent 1de304b commit c318c01
Showing 1 changed file with 12 additions and 12 deletions.
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

0 comments on commit c318c01

Please sign in to comment.