File

projects/storefrontlib/layout/a11y/keyboard-focus/autofocus/auto-focus.service.ts

Extends

EscapeFocusService

Index

Properties
Methods

Methods

findFirstFocusable
findFirstFocusable(host: HTMLElement, config: AutoFocusConfig)

Returns the first focusable child element of the host element.

Parameters :
Name Type Optional Default value
host HTMLElement No
config AutoFocusConfig No { autofocus: true }
Returns : HTMLElement
Protected getPersisted
getPersisted(host: HTMLElement, group?: string)

Returns the element that has a persisted focus state.

groups and remain the persistence

Parameters :
Name Type Optional Description
host HTMLElement No

the HTMLElement used to query for focusable children

group string Yes

the optional group for the persistent state, to separate different focus groups and remain the persistence

Returns : HTMLElement
hasPersistedFocus
hasPersistedFocus(host: HTMLElement, config: PersistFocusConfig)

Indicates whether any of the focusable child elements is focused.

Parameters :
Name Type Optional
host HTMLElement No
config PersistFocusConfig No
Returns : boolean
handleEscape
handleEscape(host: HTMLElement, config: EscapeFocusConfig, event: KeyboardEvent)
Inherited from EscapeFocusService
Parameters :
Name Type Optional
host HTMLElement No
config EscapeFocusConfig No
event KeyboardEvent No
Returns : void
shouldFocus
shouldFocus(config: EscapeFocusConfig)
Inherited from EscapeFocusService
Parameters :
Name Type Optional
config EscapeFocusConfig No
Returns : boolean
clear
clear(group?: string)
Inherited from PersistFocusService

Clears the persisted keyboard focus state globally or for the given group.

Parameters :
Name Type Optional
group string Yes
Returns : void
get
get(group?: string)
Inherited from PersistFocusService
Parameters :
Name Type Optional
group string Yes
Returns : string
getPersistenceGroup
getPersistenceGroup(host: HTMLElement, config?: PersistFocusConfig)
Inherited from PersistFocusService

Returns the group for the host element based on the configured group or by the data-cx-focus-group attribute stored on the host.

Parameters :
Name Type Optional
host HTMLElement No
config PersistFocusConfig Yes
Returns : string
set
set(key: string, group?: string)
Inherited from PersistFocusService

Persist the keyboard focus state for the given key. The focus is stored globally or for the given group.

Parameters :
Name Type Optional
key string No
group string Yes
Returns : void

Properties

Protected focus
Default value : new Map<string, string>()
Inherited from PersistFocusService
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;
  }
}

result-matching ""

    No results matching ""