Skip to content

useFormatForAria

ts
function useFormatForAria(scope?): (hotkey, options) => string | undefined;

Defined in: packages/ember-hotkeys/src/useFormatForAria.ts:38

Returns formatForAria with the scope's platform as the default. Use its value for aria-keyshortcuts, so screen readers announce the shortcut. It returns undefined 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), so Glimmer omits the attribute. The returned function works anywhere, including templates, and reads the scope's defaults when called. An explicit platform option wins.

Physical bindings such as [KeyQ] name the key the user presses in Chromium browsers, once the keyboard layout has loaded, the same way as useFormatForDisplay. A layoutMap passed to the returned function wins. To never ask the browser for the layout, set keyboardLayout: false in the scope's default options.

Server-rendered apps should set the scope's default platform from the request user agent, such as detectPlatform(fastboot.request.headers.get('user-agent')) with the FastBoot service.

Parameters

scope?

EmberHotkeysScope

Returns

ts
(hotkey, options?): string | undefined;

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

Example

plaintext
class SearchButton extends Component<{ Args: { hotkeys: EmberHotkeysScope } }> {
  formatForAria = useFormatForAria(this.args.hotkeys)

  <template>
    <button aria-keyshortcuts={{this.formatForAria "Mod+K"}}>Search</button>
  </template>
}