Skip to main content
cngx-src documentation

CngxSpeak

DirectivePrimaryv0.1.0WCAG AAA

projects/common/interactive/speak/speak.directive.ts

Import#

import { CngxSpeak } from '@cngx/common/interactive'

Description#

Headless text-to-speech directive using the browser's SpeechSynthesis API.

A read-aloud feature for cognitive accessibility - dyslexia support, reading assistance, or convenience. Not a screen reader replacement (use CngxLiveRegion for ARIA live regions).

The directive is purely behavioral: it exposes speaking() and supported() signals plus speak() / cancel() methods. The consumer renders their own UI (button, icon, link) and calls the methods via the template reference.

Pair with CngxSpeakButton (@cngx/ui) for a ready-made speaker button.

Headless - consumer-owned button

<p [cngxSpeak]="text" #tts="cngxSpeak">
  {{ text }}
  <button (click)="tts.toggle()">{{ tts.speaking() ? 'Stop' : 'Listen' }}</button>
</p>

Auto-speak on dynamic value change

<div [cngxSpeak]="notification()">{{ notification() }}</div>

With CngxSpeakButton (from @cngx/ui)

<span [cngxSpeak]="text" #tts="cngxSpeak">{{ text }}</span>
<cngx-speak-button [speakRef]="tts" />

Suppress auto-speak for dynamic values

<div [cngxSpeak]="liveCounter()" [enabled]="false" #tts="cngxSpeak">
  {{ liveCounter() }}
  <button (click)="tts.speak(liveCounter().toString())">Read current value</button>
</div>

https://cngxjs.github.io/cngx/examples/#/common/interactive/speak/form-error-read-aloud-on-demand https://cngxjs.github.io/cngx/examples/#/common/interactive/speak/headless-read-aloud https://cngxjs.github.io/cngx/examples/#/ui/speak/speak-button/material-integration-theme-scss-mat-icon-button https://cngxjs.github.io/cngx/examples/#/ui/speak/speak-button/styled-speaker-icon https://cngxjs.github.io/cngx/examples/#/ui/speak/speak-button/theming-css-custom-properties

Metadata#

Index#

Inputs#

enabled#
input()

Controls auto-speak on text changes. Does NOT affect speak() or cancel().

default true
lang#
input()

BCP 47 language tag (e.g. 'de-DE'). Empty string uses the browser default.

default ''
pitch#
input()

Speech pitch (0–2, default 1).

default 1
rate#
input()

Speech rate (0.1–10, default 1).

default 1
input()Required

Text to speak. When enabled is true, speech triggers on every change to a non-empty value.

default { alias: 'cngxSpeak' }
volume#
input()

Speech volume (0–1, default 1).

default 1

Instance Properties#

speaking#unknown
Readonly

true while an utterance is being spoken.

this.speakingState.asReadonly()
supported#boolean
Readonly

Whether the browser supports the SpeechSynthesis API.

Methods#

cancel#void

Cancel any ongoing speech.

speak#void
speak(text: string)

Speak arbitrary text. Always works regardless of enabled.

@paramtextstring
toggle#void

Toggle speech: speak text if idle, cancel if speaking.