0
0
Angularframework~20 mins

Keyboard navigation support in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Keyboard Navigation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does this Angular component handle keyboard navigation?
Consider this Angular standalone component that manages focus on a list of buttons using keyboard arrows. What happens when the user presses the 'ArrowDown' key?
Angular
import { Component, signal } from '@angular/core';

@Component({
  selector: 'app-keyboard-nav',
  standalone: true,
  template: `
    <div role="list" tabindex="0" (keydown)="onKeydown($event)">
      <button *ngFor="let item of items; let i = index"
              [attr.aria-selected]="i === focusedIndex()"
              [class.focused]="i === focusedIndex()"
              (click)="select(i)"
              tabindex="-1">
        {{item}}
      </button>
    </div>
  `,
  styles: [`.focused { outline: 2px solid blue; }`]
})
export class KeyboardNavComponent {
  items = ['One', 'Two', 'Three'];
  focusedIndex = signal(0);

  onKeydown(event: KeyboardEvent) {
    const maxIndex = this.items.length - 1;
    switch(event.key) {
      case 'ArrowDown':
        this.focusedIndex.set((this.focusedIndex() < maxIndex) ? this.focusedIndex() + 1 : 0);
        event.preventDefault();
        break;
      case 'ArrowUp':
        this.focusedIndex.set((this.focusedIndex() > 0) ? this.focusedIndex() - 1 : maxIndex);
        event.preventDefault();
        break;
    }
  }

  select(index: number) {
    this.focusedIndex.set(index);
  }
}
AThe focus moves to the next button in the list, cycling back to the first after the last.
BThe focus moves to the previous button in the list, cycling back to the last after the first.
CThe component throws an error because the focusedIndex signal is not updated correctly.
DNothing happens because the keydown event is not handled.
Attempts:
2 left
💡 Hint
Look at how the focusedIndex signal changes when 'ArrowDown' is pressed.
📝 Syntax
intermediate
1:30remaining
Identify the syntax error in this Angular keyboard event handler
Which option contains a syntax error in this Angular keyboard event handler method?
Angular
onKeydown(event: KeyboardEvent) {
  switch(event.key) {
    case 'ArrowLeft':
      this.moveFocusLeft();
      break;
    case 'ArrowRight':
      this.moveFocusRight();
      break;
    default:
      break;
  }
}
AMissing curly braces around switch cases.
BIncorrect use of parentheses in switch statement.
CMissing semicolon after break in case 'ArrowLeft'.
DUsing event.key instead of event.code causes syntax error.
Attempts:
2 left
💡 Hint
Check punctuation after each case block.
state_output
advanced
1:30remaining
What is the value of focusedIndex after pressing ArrowUp twice?
Given this Angular component with 3 items and initial focusedIndex 0, what is the focusedIndex value after pressing 'ArrowUp' key two times in a row?
Angular
items = ['A', 'B', 'C'];
focusedIndex = signal(0);

onKeydown(event: KeyboardEvent) {
  const maxIndex = this.items.length - 1;
  switch(event.key) {
    case 'ArrowUp':
      this.focusedIndex.set((this.focusedIndex() > 0) ? this.focusedIndex() - 1 : maxIndex);
      break;
  }
}
A2
B0
CThrows an error
D1
Attempts:
2 left
💡 Hint
Remember the index cycles to maxIndex when at 0 and ArrowUp is pressed.
🔧 Debug
advanced
2:00remaining
Why does this Angular keyboard navigation not update focus visually?
This Angular component updates focusedIndex on ArrowDown key but the focused button does not show the blue outline. What is the likely cause?
Angular
template:
<div role="list" tabindex="0" (keydown)="onKeydown($event)">
  <button *ngFor="let item of items; let i = index"
          [attr.aria-selected]="i === focusedIndex()"
          [class.focused]="i === focusedIndex"
          tabindex="-1">
    {{item}}
  </button>
</div>

component:
focusedIndex = signal(0);

onKeydown(event: KeyboardEvent) {
  if(event.key === 'ArrowDown') {
    this.focusedIndex.set((this.focusedIndex() + 1) % this.items.length);
    event.preventDefault();
  }
}
AThe tabindex on buttons should be 0 to receive focus.
BThe class binding uses 'focusedIndex' instead of 'focusedIndex()' causing no update.
CThe event.preventDefault() is missing, so default scrolling overrides focus.
DThe signal focusedIndex is not initialized.
Attempts:
2 left
💡 Hint
Check how the signal is accessed in the template.
🧠 Conceptual
expert
2:30remaining
Which ARIA role and attributes best support keyboard navigation in a custom list component?
You build a custom list component with keyboard navigation support. Which ARIA roles and attributes should you use to ensure accessibility and proper keyboard interaction?
AUse role="list" on the container and role="listitem" on each item, with tabindex="-1" on items and manage focus programmatically.
BUse role="menu" on the container and role="menuitem" on each item, with tabindex="0" on all items.
CUse no ARIA roles and rely on native <ul> and <li> elements only.
DUse role="grid" on the container and role="gridcell" on each item, with tabindex="0" on the container only.
Attempts:
2 left
💡 Hint
Think about semantic roles for lists and how focus should be managed on items.