Skip to content

Flash sale (scenario recipe) ​

A host-side assembly of three existing components — countdown, progress and the submit bar. This page introduces no new component and no new component API; the flash-sale business rules (when to disable, when to switch behavior) live entirely in the host.

Roles ​

RoleComponentComponent providesHost provides
Countdownoas-countdownTime display + oas-finish deadline signalFeed value with endAt − Date.now() in milliseconds
Sale progressoas-progressPercent bar (percent, flowing stripes, label)Convert "sold / total" to 0–100
Buy baroas-submit-barTotal + main button three states + oas-submitSubmit queue, stock decrement, disable decisions

The countdown value is a duration in milliseconds, not an absolute timestamp — the host does the subtraction; drift correction under background-tab throttling is built in (timestamp-based bookkeeping, auto-corrected on the first frame after returning to the foreground).

Full flash-sale loop ​

60-second round: buy → progress advances → auto switch on sellout / timeout (resettable)

Flagship noise-cancelling headphones (glossy white)

Live

5 of 5 left · sold 0

Press "Buy now": each success advances the progress; on sellout or timeout the button switches automatically.

Reset demo

Key points:

  1. Disable timing is two independent conditions, first wins: timeout (oas-finish) → disabled + action-text="Ended"; stock reaches zero (host ledger) → disabled + action-text="Sold out". Both are inputs to the host state machine — the component knows nothing about "campaigns".
  2. In-flight submits are protected for free: on oas-submit the host sets loading — while loading the button is unclickable and no further events fire (built-in semantics), so no hand-rolled debounce is needed; after the async response resolves, remove loading and advance stock/progress.
  3. Progress linkage is a one-line conversion: percent = sold / total × 100; label gives the bar an accessible name so screen-reader users perceive the heat too.
  4. The demo is resettable: countdown.reset() restarts from the initial value (component method) while the host rolls back stock / button / copy — the real "restock / reopen" path.

Disable-timing decision table ​

SignalSourceHost action
Timeoutoas-finish from oas-countdowndisabled + action-text="Ended", state tag → "Ended"
Sold outHost stock ledger (decrement on oas-submit success)disabled + action-text="Sold out", state tag → "Sold out"
Submit in flightHost sets loading on clickBuilt-in: loading is unclickable, no oas-submit fires
Restore / restockHost writes state backRemove disabled, restore action-text, countdown.reset()

Three states side by side: preview / live / ended ​

One component set, three campaign states (preview card subscribes a reminder)

Preview

Starts today 20:00 · 8,241 subscribed

Notify me at start
Live
Ended

Preview card: press "Notify me at start" for the host-side feedback.

Key points:

  1. All three states share the same components — only attribute values differ: preview = button disabled, countdown runs toward the start time; live = button active, progress flows; ended = both disabled, progress full at status="success" green.
  2. "Notify me" is host business — the submit-bar stays disabled guarding the buy entry while a plain button takes the subscription.
  3. value="0" parks the ended card's countdown at zero: a zero value finishes immediately, so component state and business state agree.

Key code ​

ts
// Host state machine (pseudo code, copy-ready)
let stock = TOTAL_STOCK
let ended = false

countdown.addEventListener('oas-finish', () => {
  ended = true
  sealBar('Ended', 'Ended', 'default')
})

bar.addEventListener('oas-submit', () => {
  if (ended || stock <= 0) return // belt and braces (no event fires while disabled / loading)
  bar.setAttribute('loading', '')
  queueSubmit().then(() => {
    bar.removeAttribute('loading')
    stock--
    renderProgress() // percent = sold / total × 100
    if (stock === 0) sealBar('Sold out', 'Sold out', 'default')
  })
})

function sealBar(text: string, tag: string, type: string) {
  bar.setAttribute('disabled', '')
  bar.setAttribute('action-text', text)
  stateTag.setAttribute('type', type)
  // Timeout / sold-out are weak states: fall back to soft (the solid "Live" belongs to the live state only, consistent with static weak states)
  stateTag.removeAttribute('variant')
  stateTag.textContent = tag
}

Notes ​

  1. Feed value a duration, not a future timestamp: countdown.value = String(endAt - Date.now()); for multi-day rounds use format="Dd HH:mm:ss".
  2. oas-finish fires once; call reset() to reopen a round; reconnecting leaves no stray timer (built into the component's onCleanup).
  3. submit-bar defaults to position: sticky with its placeholder in the document flow; if the host overrides to fixed, the placeholder is the host's job (see the submit-bar page's safe-area recipe).
  4. The real stock truth lives on the backend: front-end progress and disables are experience layers only — whether oas-submit succeeded is decided by the server response; this demo simulates it with a delay.

Released under the MIT OR Apache-2.0 License.