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
| Role | Component | Component provides | Host provides |
|---|---|---|---|
| Countdown | oas-countdown | Time display + oas-finish deadline signal | Feed value with endAt − Date.now() in milliseconds |
| Sale progress | oas-progress | Percent bar (percent, flowing stripes, label) | Convert "sold / total" to 0–100 |
| Buy bar | oas-submit-bar | Total + main button three states + oas-submit | Submit 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)
5 of 5 left · sold 0
Press "Buy now": each success advances the progress; on sellout or timeout the button switches automatically.
Key points:
- 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". - In-flight submits are protected for free: on
oas-submitthe host setsloading— 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, removeloadingand advance stock/progress. - Progress linkage is a one-line conversion:
percent = sold / total × 100;labelgives the bar an accessible name so screen-reader users perceive the heat too. - 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
| Signal | Source | Host action |
|---|---|---|
| Timeout | oas-finish from oas-countdown | disabled + action-text="Ended", state tag → "Ended" |
| Sold out | Host stock ledger (decrement on oas-submit success) | disabled + action-text="Sold out", state tag → "Sold out" |
| Submit in flight | Host sets loading on click | Built-in: loading is unclickable, no oas-submit fires |
| Restore / restock | Host writes state back | Remove 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 card: press "Notify me at start" for the host-side feedback.
Key points:
- 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. - "Notify me" is host business — the submit-bar stays disabled guarding the buy entry while a plain button takes the subscription.
value="0"parks the ended card's countdown at zero: a zero value finishes immediately, so component state and business state agree.
Key code
// 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
- Feed
valuea duration, not a future timestamp:countdown.value = String(endAt - Date.now()); for multi-day rounds useformat="Dd HH:mm:ss". oas-finishfires once; callreset()to reopen a round; reconnecting leaves no stray timer (built into the component'sonCleanup).- submit-bar defaults to
position: stickywith its placeholder in the document flow; if the host overrides tofixed, the placeholder is the host's job (see the submit-bar page's safe-area recipe). - The real stock truth lives on the backend: front-end progress and disables are experience layers only — whether
oas-submitsucceeded is decided by the server response; this demo simulates it with a delay.