Skip to content

FormatForAriaController

Defined in: controllers/format-for-aria.ts:59

A Lit ReactiveController whose format is formatForAria with the controller's platform option, or the detected platform without one. A platform passed to format wins. Use its value for aria-keyshortcuts with Lit's ifDefined, so Lit omits the attribute for a multi-step sequence, no hotkey, or a hotkey that can't fire right now (one that is disabled, in a suspended scope, a suspended character key, or not registered). The host updates when a registration changes.

Physical bindings such as [KeyQ] name the key the user presses in Chromium browsers, once the keyboard layout has loaded. The host updates when the layout loads or changes. A layoutMap passed to format wins.

Lit has no provider, so the options are the only defaults. Pass a getter to read them from host properties.

Example

ts
class SearchButton extends LitElement {
  private formatForAria = new FormatForAriaController(this)

  render() {
    return html`
      <button aria-keyshortcuts=${ifDefined(this.formatForAria.format('Mod+K'))}>
        Search
      </button>
    `
  }
}

Implements

  • ReactiveController

Constructors

Constructor

ts
new FormatForAriaController(_host, _options?): FormatForAriaController;

Defined in: controllers/format-for-aria.ts:83

Parameters

_host

ReactiveControllerHost

The Lit component that owns this controller. The controller adds itself to it.

_options?

MaybeGetter<FormatForAriaControllerOptions> = {}

Options or a getter. platform is read for each value. keyboardLayout is read on connection and before host updates.

Returns

FormatForAriaController

Properties

format

ts
format: (hotkey, options) => string | undefined;

Defined in: controllers/format-for-aria.ts:70

Formats a hotkey for aria-keyshortcuts, with the controller's platform and the user's keyboard layout filled in. It takes the same options as formatForAria and needs no this, so it can be passed around.

Formats a hotkey for an aria-keyshortcuts attribute so screen readers can announce the shortcut. Mod+K becomes Meta+K on macOS and Control+K elsewhere.

Modifiers use the ARIA names (Control, Alt, Shift, Meta) and come before the key. Keys use KeyboardEvent.key values, such as K, /, Escape, or ArrowUp, with Space for the space bar and Plus for the plus key. A physical binding names the key at that position, so [KeyQ] becomes Q, or the key from layoutMap such as A on a French AZERTY keyboard. Logical punctuation stays as written, so '?' stays ? even where it takes Shift to type.

Returns undefined when the hotkey has no ARIA form, so the attribute is omitted. This applies to empty or null hotkeys, multi-step sequences, standalone modifiers, and physical codes with no key value such as [NumpadMemoryAdd]. A one-step sequence formats like its chord.

For a control with two shortcuts, join both formatted values with a space. In a server-rendered app, use your adapter's formatter or pass platform. Without a platform option, formatForAria uses 'linux' on the server, like formatForDisplay.

Parameters

hotkey

DisplayHotkey | null | undefined

A hotkey string, raw object, parsed chord, or nothing

options?

FormatAriaOptions = {}

The platform and keyboard layout

Returns

string | undefined

The attribute value, or undefined when there is none

Example

ts
formatForAria('Mod+K', { platform: 'mac' }) // 'Meta+K'
formatForAria('Mod+Shift+P', { platform: 'windows' }) // 'Control+Shift+P'
formatForAria('Shift+Space') // 'Shift+Space'
formatForAria(['G', 'H']) // undefined

Methods

dispose()

ts
dispose(): void;

Defined in: controllers/format-for-aria.ts:110

Releases the controller now and removes it from its host, with the layout controller it added. Use this method to remove a controller early, because Lit's removeController alone leaves it active.

Returns

void


hostConnected()

ts
hostConnected(): void;

Defined in: controllers/format-for-aria.ts:96

Starts reading the layout, unless keyboardLayout is false.

Returns

void

Implementation of

ts
ReactiveController.hostConnected

hostUpdate()

ts
hostUpdate(): void;

Defined in: controllers/format-for-aria.ts:101

Follows a keyboardLayout option that a host property changed.

Returns

void

Implementation of

ts
ReactiveController.hostUpdate