Skip to main content
cngx-src documentation

CngxAsyncClick

DirectivePrimaryv0.1.0WCAG AA

projects/common/interactive/async-click/async-click.directive.ts

Import#

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

Description#

Async action handler with loading state, auto-disable, and success/error feedback.

Place on any clickable element (button, link, div). Executes the provided async action on click, tracks the full lifecycle as a state machine, auto-disables during execution, announces state changes to screen readers, and guards against double-clicks.

Basic usage

<button [cngxAsyncClick]="saveAction" #btn="cngxAsyncClick">
  @switch (btn.status()) {
    @case ('pending')   { Saving... }
    @case ('success')   { Saved! }
    @case ('error')     { Failed }
    @default            { Save }
  }
</button>

With Material

<button mat-raised-button [cngxAsyncClick]="submitForm" #btn="cngxAsyncClick">
  @if (btn.pending()) { <mat-spinner diameter="20" /> Submitting... }
  @else { Submit }
</button>

On any element

<a role="button" [cngxAsyncClick]="navigate" #btn="cngxAsyncClick">Go</a>

https://cngxjs.github.io/cngx/examples/#/common/interactive/retry/optimistic-instant-like-toggle https://cngxjs.github.io/cngx/examples/#/common/interactive/retry/withretry-cngxasyncclick

Metadata#

Host#

Relationships

Index#

Inputs#

input()Required

The async action to execute on click.

default { alias: 'cngxAsyncClick' }
enabled#boolean
input()

When false, clicks are ignored (does not set disabled attribute).

default true
failedAnnouncement#string
input()

Label announced to screen readers on failure.

default 'Action failed'
feedbackDuration#number
input()

Duration in ms to show success/error state before reset.

default 2000
succeededAnnouncement#string
input()

Label announced to screen readers on success.

default 'Action succeeded'

Instance Properties#

error#Signal
Readonly

The error value from a failed action. Cleared on reset.

this.errorState.asReadonly()
failed#Signal
Readonly

true for feedbackDuration ms after a failed action.

this.failedState.asReadonly()
pending#Signal
Readonly

true while the action is executing.

this.pendingState.asReadonly()
Readonly

Full CngxAsyncState view of this directive's lifecycle.

Bind to any state consumer ([state]="btn.state") to connect the feedback system - toasts, alerts, skeletons, async containers.

buildAsyncStateView<unknown>({
  status: this.status,
  data: computed(() => undefined),
  error: this.error,
  lastUpdated: this.lastUpdatedState.asReadonly(),
})
succeeded#Signal
Readonly

true for feedbackDuration ms after a successful action.

this.succeededState.asReadonly()

HostBindings#

BindingExpression
[class.cngx-async--pending]pending()
[class.cngx-async--success]succeeded()
[class.cngx-async--error]failed()
[attr.aria-busy]pending() || null
[attr.aria-disabled]pending() || null
[attr.disabled]shouldDisable()

HostListeners#

EventHandler
(click)click()