Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Keyboard navigation support
📖 Scenario: You are building a simple Angular component that displays a list of items. You want to make sure users can navigate the list using the keyboard for better accessibility.
🎯 Goal: Create an Angular standalone component that renders a list of three items. Add keyboard navigation support so users can move focus up and down the list using the arrow keys.
📋 What You'll Learn
Create a standalone Angular component named KeyboardNavComponent.
Use a signal to track the currently focused item index.
Render a list of three items: Item 1, Item 2, and Item 3.
Add keyboard event handling to move focus up and down with arrow keys.
Use the *ngFor directive to render the list items.
Add tabindex attributes to make items focusable.
Ensure the focused item has a visible outline for accessibility.
💡 Why This Matters
🌍 Real World
Keyboard navigation is essential for accessibility in web apps. Users who cannot use a mouse rely on keyboard controls to interact with lists, menus, and other UI elements.
💼 Career
Many frontend developer roles require knowledge of accessibility best practices, including keyboard navigation support in Angular applications.
Progress0 / 4 steps
1
Create the list data
Create a standalone Angular component named KeyboardNavComponent. Inside the component, create a readonly signal called items that holds an array with the strings 'Item 1', 'Item 2', and 'Item 3'.
Angular
Hint
Use signal from @angular/core to create a reactive array named items.
2
Add focused index signal
Inside the KeyboardNavComponent, add a signal named focusedIndex initialized to 0. This will track which item is currently focused.
Angular
Hint
Use signal(0) to create a reactive number named focusedIndex.
3
Render list with keyboard navigation
Update the component template to render a <ul> with a *ngFor directive over items(). For each item, render a <li> with tabindex set to 0 if the index equals focusedIndex(), otherwise -1. Add a (keydown) event handler on the <ul> that calls a method onKeydown($event).
Angular
Hint
Use *ngFor to loop over items(). Use [attr.tabindex] to set focusable attribute. Add (keydown) event on <ul>.
4
Implement keyboard navigation logic
Complete the onKeydown(event: KeyboardEvent) method to update focusedIndex when the user presses the ArrowDown or ArrowUp keys. Increase focusedIndex by 1 on ArrowDown unless at the last item, and decrease by 1 on ArrowUp unless at the first item. Use event.preventDefault() to stop page scrolling.
Angular
Hint
Check event.key for 'ArrowDown' and 'ArrowUp'. Use this.focusedIndex.set() to update the focused index.
Practice
(1/5)
1. What is the main purpose of adding keyboard navigation support in an Angular app?
easy
A. To allow users to navigate the app using keyboard keys
B. To improve the app's loading speed
C. To change the app's color scheme automatically
D. To add animations when clicking buttons
Solution
Step 1: Understand keyboard navigation
Keyboard navigation lets users move through app elements using keys like arrows or tab.
Step 2: Identify the purpose in Angular apps
Adding keyboard navigation improves accessibility and user experience by enabling key-based movement.
Final Answer:
To allow users to navigate the app using keyboard keys -> Option A
Quick Check:
Keyboard navigation = user key movement [OK]
Hint: Keyboard navigation means moving with keys, not visuals [OK]
Common Mistakes:
Confusing keyboard navigation with app speed
Thinking it changes colors or animations
Assuming it only works with mouse clicks
2. Which Angular syntax correctly listens for the 'ArrowDown' key press on a button?
easy
A. Click
B. Click
C. Click
D. Click
Solution
Step 1: Recall Angular event binding syntax
Angular uses parentheses for events and lowercase event names with dots for key names.
Step 2: Match correct key event syntax
The correct syntax is (keydown.arrowdown) with lowercase and dot notation.
Final Answer:
<button (keydown.arrowdown)="onArrowDown()">Click</button> -> Option B
Quick Check:
Angular key event = (keydown.arrowdown) [OK]
Hint: Use lowercase and dot for key events in Angular [OK]
Common Mistakes:
Using uppercase letters in event names
Replacing dot with dash or camelCase
Missing parentheses around event
3. Given this Angular snippet, what happens when the user presses the 'ArrowDown' key?
The code uses nativeElement.focus without parentheses, so the focus method is not called.
Step 2: Understand method invocation
To move focus, focus() must be called with parentheses to execute it.
Final Answer:
Missing parentheses after focus method call -> Option D
Quick Check:
Call focus() with () to move focus [OK]
Hint: Remember to call focus() with parentheses [OK]
Common Mistakes:
Forgetting parentheses on method calls
Confusing @ViewChild and @ViewChildren usage
Assuming toArray() is invalid on QueryList
5. You want to create a keyboard navigation system in Angular where pressing 'ArrowDown' moves focus to the next item, and pressing 'ArrowUp' moves focus to the previous item in a list of buttons. Which approach correctly supports this behavior and handles edge cases (start and end of list)?
hard
A. Use @ViewChildren to get buttons, then in keydown handlers call focusNext(index) or focusPrev(index) with checks to wrap focus from last to first and vice versa
B. Use @ViewChild for each button separately and manually set focus without index checks
C. Use (keydown) event without specifying keys and call focus() on the first button always
D. Use native JavaScript event listeners outside Angular to manage focus without Angular bindings
Solution
Step 1: Understand Angular focus management
@ViewChildren collects all buttons, allowing indexed access to manage focus programmatically.
Step 2: Handle keydown events with wrapping logic
Using keydown handlers for ArrowUp and ArrowDown with index checks lets you move focus up/down and wrap from last to first or first to last.
Step 3: Compare options for correctness
Use @ViewChildren to get buttons, then in keydown handlers call focusNext(index) or focusPrev(index) with checks to wrap focus from last to first and vice versa uses Angular patterns and handles edge cases correctly. Others miss index checks or Angular integration.
Final Answer:
Use @ViewChildren to get buttons, then in keydown handlers call focusNext(index) or focusPrev(index) with checks to wrap focus from last to first and vice versa -> Option A
Quick Check:
Indexed focus with wrapping = correct keyboard navigation [OK]
Hint: Use @ViewChildren and wrap focus indexes for smooth navigation [OK]
Common Mistakes:
Not handling focus wrap at list ends
Using @ViewChild for multiple elements incorrectly
Ignoring Angular event bindings and using native listeners