projects/storefrontlib/layout/a11y/keyboard-focus/autofocus/auto-focus.service.ts
Properties |
|
Methods |
| findFirstFocusable | ||||||||||||
findFirstFocusable(host: HTMLElement, config: AutoFocusConfig)
|
||||||||||||
|
Returns the first focusable child element of the host element.
Parameters :
Returns :
HTMLElement
|
| Protected getPersisted | ||||||||||||
getPersisted(host: HTMLElement, group?: string)
|
||||||||||||
|
Returns the element that has a persisted focus state. groups and remain the persistence
Parameters :
Returns :
HTMLElement
|
| hasPersistedFocus | |||||||||
hasPersistedFocus(host: HTMLElement, config: PersistFocusConfig)
|
|||||||||
|
Indicates whether any of the focusable child elements is focused.
Parameters :
Returns :
boolean
|
| handleEscape | ||||||||||||
handleEscape(host: HTMLElement, config: EscapeFocusConfig, event: KeyboardEvent)
|
||||||||||||
|
Inherited from
EscapeFocusService
|
||||||||||||
|
Defined in
EscapeFocusService:18
|
||||||||||||
|
Parameters :
Returns :
void
|
| shouldFocus | ||||||
shouldFocus(config: EscapeFocusConfig)
|
||||||
|
Inherited from
EscapeFocusService
|
||||||
|
Defined in
EscapeFocusService:14
|
||||||
|
Parameters :
Returns :
boolean
|
| clear | ||||||
clear(group?: string)
|
||||||
|
Inherited from
PersistFocusService
|
||||||
|
Defined in
PersistFocusService:38
|
||||||
|
Clears the persisted keyboard focus state globally or for the given group.
Parameters :
Returns :
void
|
| get | ||||||
get(group?: string)
|
||||||
|
Inherited from
PersistFocusService
|
||||||
|
Defined in
PersistFocusService:21
|
||||||
|
Parameters :
Returns :
string
|
| getPersistenceGroup | |||||||||
getPersistenceGroup(host: HTMLElement, config?: PersistFocusConfig)
|
|||||||||
|
Inherited from
PersistFocusService
|
|||||||||
|
Defined in
PersistFocusService:46
|
|||||||||
|
Returns the group for the host element based on the configured group or
by the
Parameters :
Returns :
string
|
| set |
set(key: string, group?: string)
|
|
Inherited from
PersistFocusService
|
|
Defined in
PersistFocusService:29
|
|
Persist the keyboard focus state for the given key. The focus is stored globally or for the given group.
Returns :
void
|
| Protected focus |
Default value : new Map<string, string>()
|
|
Inherited from
PersistFocusService
|
|
Defined in
PersistFocusService:19
|
import { Injectable } from '@angular/core';
import { EscapeFocusService } from '../escape/escape-focus.service';
import {
AutoFocusConfig,
FOCUS_ATTR,
PersistFocusConfig,
} from '../keyboard-focus.model';
@Injectable({
providedIn: 'root',
})
export class AutoFocusService extends EscapeFocusService {
/**
* Returns the first focusable child element of the host element.
*/
findFirstFocusable(
host: HTMLElement,
config: AutoFocusConfig = { autofocus: true }
): HTMLElement {
if (config?.autofocus === ':host') {
return host;
} else if (this.hasPersistedFocus(host, config)) {
return this.getPersisted(host, this.getPersistenceGroup(host, config));
} else {
return this.selectFocusUtil.findFirstFocusable(host, config) || host;
}
}
/**
* Indicates whether any of the focusable child elements is focused.
*/
hasPersistedFocus(host: HTMLElement, config: PersistFocusConfig): boolean {
return !!this.getPersisted(host, this.getPersistenceGroup(host, config));
}
/**
* Returns the element that has a persisted focus state.
*
* @param host the `HTMLElement` used to query for focusable children
* @param group the optional group for the persistent state, to separate different focus
* groups and remain the persistence
*/
protected getPersisted(host: HTMLElement, group?: string): HTMLElement {
if (!this.get(group)) {
return;
}
const focussed = Array.from(
host.querySelectorAll(
`[${FOCUS_ATTR}='${this.get(group)}']`
) as NodeListOf<HTMLElement>
);
return focussed.length > 0 ? focussed[0] : null;
}
}