-
Notifications
You must be signed in to change notification settings - Fork 393
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Use proper keybaord navigation keys for facets (#19651)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
8297d98
commit b32ecac
Showing
5 changed files
with
85 additions
and
16 deletions.
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
30 changes: 30 additions & 0 deletions
30
projects/storefrontlib/layout/a11y/keyboard-focus/keyboard-focus.utils.spec.ts
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,30 @@ | ||
import { disableTabbingForTick } from './keyboard-focus.utils'; | ||
import { fakeAsync, tick } from '@angular/core/testing'; | ||
|
||
describe('disableTabbingForTick', () => { | ||
let elements: HTMLElement[]; | ||
|
||
beforeEach(() => { | ||
elements = [document.createElement('div'), document.createElement('div')]; | ||
elements.forEach((el) => document.body.appendChild(el)); | ||
}); | ||
|
||
afterEach(() => { | ||
elements.forEach((el) => document.body.removeChild(el)); | ||
}); | ||
|
||
it('should set tabIndex to -1 for each element', () => { | ||
disableTabbingForTick(elements); | ||
elements.forEach((el) => { | ||
expect(el.tabIndex).toBe(-1); | ||
}); | ||
}); | ||
|
||
it('should reset tabIndex to 0 after a tick', fakeAsync(() => { | ||
disableTabbingForTick(elements); | ||
tick(100); | ||
elements.forEach((el) => { | ||
expect(el.tabIndex).toBe(0); | ||
}); | ||
})); | ||
}); |
32 changes: 32 additions & 0 deletions
32
projects/storefrontlib/layout/a11y/keyboard-focus/keyboard-focus.utils.ts
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,32 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/** | ||
* Temporarily removes elements from the tabbing flow and restores them after a tick. | ||
* | ||
* This method sets the `tabIndex` of each element in the provided iterable to `-1` | ||
* and resets it back to `0` using `requestAnimationFrame`. While using `requestAnimationFrame` | ||
* may seem like a bad code smell, it is justified here as it ensures a natural tabbing flow | ||
* in cases where determining the next focusable element is complex, such as when directives | ||
* like `TrapFocusDirective` modify the DOM's focus behavior. | ||
* | ||
* This utility is especially useful for scenarios like menus, lists, or carousels where | ||
* `Tab` navigation is intentionally disabled, but other keyboard keys (e.g., `Arrow` keys) | ||
* are used for navigation. It helps prevent these elements from disrupting the tab order | ||
* while allowing other key-based interactions. | ||
* | ||
* @param elements - An iterable of `HTMLElement` objects to temporarily remove from tab navigation. | ||
*/ | ||
export const disableTabbingForTick = (elements: Iterable<HTMLElement>) => { | ||
for (const element of elements) { | ||
element.tabIndex = -1; | ||
} | ||
requestAnimationFrame(() => { | ||
for (const element of elements) { | ||
element.tabIndex = 0; | ||
} | ||
}); | ||
}; |
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