SearchCtrlK

  • ↑↓navigate
  • ↵open
  • escclose

Can’t find it? Contact us

optimisticToggleSync

Why JS.

tsx
import { optimisticToggleSync } from "@sdxc/ui/app";

Usage

Applied to the toggle's <form>: on submit it flips data-pressed, the button's aria-pressed, tone and name, and the hidden next-state field, then posts the form as it stood. A refused post flips it back, sets data-failed and writes the refused copy for its status, or failed, into the live region. The newest press wins over a slow answer to an older one, and only its answer is reported.

Signature

typescript
optimisticToggleSync(options: OptimisticToggleSync.Options): MixinDescriptor<HTMLFormElement>

Parameters

ParameterTypeRequiredDescription
optionsOptimisticToggleSync.OptionsYes

Examples

tsx
<form method="post" action={url} mix={[optimisticToggleSync({ failed: "Failed" })]}>…</form>

The events, constants and types its module publishes, each imported from @sdxc/ui/app too.

Event

OptimisticToggleEvent

Dispatched, bubbling, from a toggle's form at the press and again once the server answers, so a row can restyle at once and reload server-derived counts only when settled is true.

typescript
new OptimisticToggleEvent(pressed: boolean, outcome?: OptimisticToggleSync.Outcome)
PropertyTypeRequiredDescription
pressedreadonly booleanYes
failedreadonly booleanYesWhether the server refused the last change, which put the toggle back.
settledreadonly booleanYesWhether the server has answered the press: false for the optimistic flip, true after.
statusreadonly number | nullYesThe answer's HTTP status, for picking copy; null before the answer and for a network error.
Constant

OPTIMISTIC_TOGGLE_HEADER

Header a background post carries, so the server can answer with a status instead of a redirect.

typescript
const OPTIMISTIC_TOGGLE_HEADER = "x-optimistic-toggle"
Function

toneOf

Picks the button's tone: danger after a refusal, brand while on, neutral while off.

typescript
toneOf(pressed: boolean, failed: boolean): "danger" | "brand" | "neutral"
ParameterTypeRequiredDescription
pressedbooleanYesWhether the toggle is on.
failedbooleanYesWhether the last change was refused.
Interface

OptimisticToggleSync.Options

Configuration for optimisticToggleSync.

MemberTypeRequiredDescription
failedstringYesWhat the form's live region says once the server refuses a change, such as "Failed".
refusedRecord<number, string>NoWhat it says instead for a refusal with one of these statuses, such as a full shelf's 507.
labelstringNoThe button's title and, when it has one, aria-label while off; left as rendered when omitted.
pressedLabelstringNoThe button's title and aria-label while on; label when omitted.
Interface

OptimisticToggleSync.Outcome

Where a press stands, as OptimisticToggleEvent reports it.

MemberTypeRequiredDescription
failedbooleanNoThe server refused the change.
settledbooleanNoThe server answered, with success or a refusal.
statusnumber | nullNoThe answer's HTTP status; null before the answer and for a network error.
@sdxc/ui

Can’t find it? Contact us