When building a custom list widget on the Angular CDK, when do you choose `FocusKeyManager` over `ActiveDescendantKeyManager`, and what must each item implement?
answer
- where does DOM focus live
- focus() versus active styles
- an id on every item
- a text input keeps focus
- scrolling becomes your job
basics
~20 sFocusKeyManager moves DOM focus to the active item, which implements focus(). ActiveDescendantKeyManager keeps focus on the host and calls setActiveStyles() and setInactiveStyles(); the host's aria-activedescendant names the item's id. Use it when focus must stay in an input.
solid answer
~40 sBoth extend `ListKeyManager` and share its keyboard handling; they differ in what 'active' means. `FocusKeyManager` calls `focus()` on the active item, so items implement `FocusableOption`, and the browser scrolls and announces the focused element naturally; the component keeps a roving `tabindex`. `ActiveDescendantKeyManager` never moves focus: items implement `Highlightable` (`setActiveStyles()` and `setInactiveStyles()`), each needs an `id`, and the focused host binds `aria-activedescendant` to the active item's id. Choose it when focus must remain on one element while the user navigates, above all a text input that keeps receiving keystrokes; Material's select, autocomplete and timepicker do this, while its menu, chip set and selection list use `FocusKeyManager`. With active-descendant, scrolling the active item into view is the component's job.
code
ts · 23 linesimport {Directive, ElementRef, inject, input, signal} from '@angular/core';
import {FocusableOption, FocusOrigin, Highlightable} from '@angular/cdk/a11y';
// Item for FocusKeyManager: receives real focus.
@Directive({selector: '[appSavedFilter]', host: {role: 'option'}})
export class SavedFilter implements FocusableOption {
private el = inject<ElementRef<HTMLElement>>(ElementRef);
label = input.required<string>();
focus(origin?: FocusOrigin): void { this.el.nativeElement.focus(); }
getLabel(): string { return this.label(); }
}
// Item for ActiveDescendantKeyManager: only styled, needs an id.
@Directive({
selector: '[appSuggestion]',
host: {role: 'option', '[id]': 'id()', '[class.is-active]': 'active()'},
})
export class Suggestion implements Highlightable {
id = input.required<string>();
active = signal(false);
setActiveStyles(): void { this.active.set(true); }
setInactiveStyles(): void { this.active.set(false); }
}go deeper
Recall the one-line difference: FocusKeyManager moves focus to the item, ActiveDescendantKeyManager keeps focus on the host and highlights the item.
Name each item interface, FocusableOption and Highlightable, and the aria-activedescendant binding the second one depends on.
Justify the choice by where focus must live, cite Material's select and autocomplete versus its menu and chip set, and list the active-descendant costs: ids, roles, styling, scrolling.
Set a team rule for which manager custom widgets use and how its accessibility is verified with real screen readers, since support for aria-activedescendant varies.
## Two meanings of 'active' The Angular CDK's `@angular/cdk/a11y` package provides `ListKeyManager`, a class that tracks an *active item* in a list and updates it from keyboard events through `onKeydown(event)` or directly through `setActiveItem(indexOrItem)`. Two subclasses decide what happens when the active item changes: - **`FocusKeyManager`** calls `focus()` on the newly active item, so the item becomes `document.activeElement`. - **`ActiveDescendantKeyManager`** calls `setInactiveStyles()` on the old item and `setActiveStyles()` on the new one, and leaves DOM focus where it was. Everything else, including the arrow-key axes, wrapping, Home/End, typeahead and the `skipPredicate`, is inherited and configured identically. ## The item contracts | | `FocusKeyManager` | `ActiveDescendantKeyManager` | |---|---|---| | Item interface | `FocusableOption` | `Highlightable` | | Required methods | `focus(origin?)` | `setActiveStyles()`, `setInactiveStyles()` | | Optional members | `disabled`, `getLabel()` | `disabled`, `getLabel()` | | Where DOM focus sits | on the active item | on the host element | | What tells assistive technology | the focus itself | `aria-activedescendant` on the host, pointing at the item's `id` | | `tabindex` to manage | a roving tab stop: active item `0`, others `-1` | host focusable, items not in the tab order | | Scrolling the item into view | the browser does it on focus | the component must do it | `FocusKeyManager` also has `setFocusOrigin(origin)`, which sets the origin it passes to each item's `focus(origin)` call; the default is `'program'`. An item can hand that origin to the CDK's `FocusMonitor` so keyboard focus styling stays accurate. ## When to choose which Choose **`FocusKeyManager`** when each item is an independent interactive element and moving focus to it is natural: - menus, toolbars and chip rows, where each item is a button; - lists where items contain their own focusable controls; - cases where you want the browser's native scrolling and focus styling for free. Choose **`ActiveDescendantKeyManager`** when focus must stay on one element: - a text input with a suggestion list, where the user keeps typing while arrowing through options; - a trigger that owns the keyboard while its panel is open, as a select does; - a container that handles its own keys and should not lose focus to its children. Angular Material shows both choices in its own source. Its menu, chip set, selection list, tab header and expansion accordion build on `FocusKeyManager`. Its select, autocomplete and timepicker build on `ActiveDescendantKeyManager`, because the trigger or input must keep focus. ## What active-descendant costs you 1. **Ids.** Every item needs a stable, unique `id`, and the host must bind `[attr.aria-activedescendant]` to the active item's id, or nothing when no item is active. 2. **Roles.** The host and items need roles that support the pattern, such as a combobox input controlling a `listbox` of `option` elements; without them, screen readers may ignore the attribute. 3. **Visual state.** Because the item is never focused, `:focus` styles do not apply; `setActiveStyles()` must add a visible active state. 4. **Scrolling.** The manager does not scroll. Material's select and autocomplete scroll the active option into view themselves; a custom widget must do the same. 5. **Rendering.** The active item must exist in the DOM for `aria-activedescendant` to reference it. ## What focus management costs you - A roving tab stop that you maintain from the manager's `change` stream, since the manager never touches `tabindex`. - Focus moving away from any input, which is wrong for a combobox, where typing must continue. ## A senior way to frame the choice Ask one question first: *where must DOM focus be while the user navigates?* If the answer is 'on the item', use `FocusKeyManager`. If it is 'on this input or trigger', use `ActiveDescendantKeyManager` and budget for ids, roles, active styling and scrolling.
- Why does Angular Material's autocomplete use ActiveDescendantKeyManager rather than FocusKeyManager?The user keeps typing in the input while arrowing through options, so DOM focus must stay in the input. The autocomplete trigger exposes `aria-activedescendant` pointing at the active option's id while the panel is open, and the options only change their active styling. Moving focus to an option would stop the typing.
- What does FocusKeyManager.setFocusOrigin change?It sets the `FocusOrigin` passed to each item's `focus(origin)` call; the default is `'program'`. After `setFocusOrigin('keyboard')`, an item that forwards the value to `FocusMonitor.focusVia(element, origin)` records arrow-key moves as `'keyboard'`, so keyboard-only focus styles appear.
- What extra work does a custom ActiveDescendantKeyManager list need for long lists?Because no item receives focus, the browser never scrolls it into view. The component must scroll the active item into view when the manager's `change` stream emits, and the item must be rendered so `aria-activedescendant` can reference its id.
- ActiveDescendantKeyManager moves DOM focus to each option as you arrow through.
- FocusKeyManager items need setActiveStyles and setInactiveStyles.
- aria-activedescendant works without ids on the items.
- The two managers use different arrow-key and typeahead logic.
- ActiveDescendantKeyManager scrolls the active option into view for you.
- A combobox input should use FocusKeyManager so options get real focus.