File

projects/storefrontlib/layout/a11y/keyboard-focus/escape/escape-focus.service.ts

Extends

PersistFocusService

Index

Properties
Methods

Constructor

constructor(selectFocusUtil: SelectFocusUtility)
Parameters :
Name Type Optional
selectFocusUtil SelectFocusUtility No

Methods

handleEscape
handleEscape(host: HTMLElement, config: EscapeFocusConfig, event: KeyboardEvent)
Parameters :
Name Type Optional
host HTMLElement No
config EscapeFocusConfig No
event KeyboardEvent No
Returns : void
shouldFocus
shouldFocus(config: EscapeFocusConfig)
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 { EscapeFocusConfig } from '../keyboard-focus.model';
import { PersistFocusService } from '../persist/persist-focus.service';
import { SelectFocusUtility } from '../services/select-focus.util';

@Injectable({
  providedIn: 'root',
})
export class EscapeFocusService extends PersistFocusService {
  constructor(protected selectFocusUtil: SelectFocusUtility) {
    super();
  }

  shouldFocus(config: EscapeFocusConfig): boolean {
    return !!config?.focusOnEscape;
  }

  handleEscape(
    host: HTMLElement,
    config: EscapeFocusConfig,
    event: KeyboardEvent
  ): void {
    if (this.shouldFocus(config)) {
      if (host !== event.target) {
        host.focus({ preventScroll: true });
        event.preventDefault();
        event.stopPropagation();
      } else {
        if (config?.focusOnDoubleEscape) {
          this.selectFocusUtil
            .findFirstFocusable(host, { autofocus: true })
            ?.focus();
        }
      }
    }
  }
}

result-matching ""

    No results matching ""