Skip to content

Low-stock alerts (scenario recipe) ​

A pure host-logic demo for the price and stock hint slot: oas-price (price display) + oas-tag (semantic badge) + a small tier function. No stock component exists — and none is needed: stock is a number; copy and semantic color are the host's presentation policy.

Threshold tiers ​

TierCondition (units)Tag formCopy policy
In stock> 100type="success" "In stock"Plenty (N units) — no artificial scarcity
Low20 < n ≤ 100type="warning" "Only N left"Stock running low, only N left
Critical0 < n ≤ 20type="danger" "Only N left"Only N left, act fast
Sold out= 0type="default" "Sold out"Sold out, check similar items + buy entry disabled

The thresholds are business parameters — 100 / 20 above are examples; substitute your own operations policy.

Threshold linkage demo ​

Change the stock number and watch the hint slot react (stepper / quick tiers / typing all work)

Smartwatch Pro (45mm aluminium)

In stock

Plenty in stock (528 units)

528 (plenty)48 (low)3 (critical)0 (sold out)

Change the stock to see the tiers: >100 in stock / ≤100 low / ≤20 critical / 0 sold out.

Key points:

  1. Components have no stock concept: oas-price handles price, oas-tag handles the semantic badge, and the tier function is a dozen lines of host code — the abstraction you need is a policy, not a container.
  2. Semantic colors switch only type, never hard-coded values: the four tiers map to theme tokens (dark variants included) and survive theme switches automatically.
  3. Announcements ride role="status": on tier switches screen-reader users hear "only 3 left, act fast" — stock hints are accessibility information, not just decoration.
  4. Sold out is a behavior tier, not just a color tier: in real business a sellout also disables the buy entry (disabled on the submit-bar / goods-action button), as shown in the cards below.

Stock slots on product cards ​

product-card tags slot: in stock / only N left / sold out

In stockAdd to cartOnly 3 leftAdd to cartSold outSold out

Key points:

  1. The tags slot is free content — drop in the oas-tag produced by the tier function and you have a stock slot; one tier function serves both the detail page and card walls.
  2. The sold-out card's button uses disabled: visually readable (gray retained) but untriggerable, matching the "buy entry closed" business semantics.

Key code ​

ts
// tag / text are element references from the demo above (#sa-tag-en / #sa-text-en, getElementById on this page)
// Threshold tiers (host policy, copy-ready; replace 100 / 20 with your own policy)
function stockTier(n: number) {
  if (n <= 0) return { tag: 'Sold out', type: 'default', text: 'Sold out, check similar items', buyable: false }
  if (n <= 20) return { tag: `Only ${n} left`, type: 'danger', text: `Only ${n} left, act fast`, buyable: true }
  if (n <= 100) return { tag: `Only ${n} left`, type: 'warning', text: `Running low, ${n} left`, buyable: true }
  return { tag: 'In stock', type: 'success', text: `Plenty in stock (${n.toLocaleString()} units)`, buyable: true }
}

function renderStock(n: number) {
  const t = stockTier(n)
  tag.setAttribute('type', t.type)
  // Strong states (success / warning / danger) stay solid; the sold-out weak state falls back to soft, matching the static "Sold out" card below
  if (t.type === 'default') tag.removeAttribute('variant')
  else tag.setAttribute('variant', 'solid')
  tag.textContent = t.tag
  text.textContent = t.text
  // The host disables the buy entry on sold-out (submit-bar / goods-action button):
  // buyButton.toggleAttribute('disabled', !t.buyable) // presence attribute → toggleAttribute
}

Notes ​

  1. Tier switching is host logic: stock changes arrive from polling / push / API responses; the host calls renderStock(n) once per new number — components stay unaware.
  2. toLocaleString for thousands: keep the stock number formatting consistent with the price (thousands built into oas-price).
  3. Avoid flicker at boundaries: near a threshold (100 ↔ 101) copy and color can flip rapidly — add hysteresis host-side if needed (e.g. only return to "in stock" below 90 once "low" has engaged).
  4. Dark and high-contrast themes: all four semantic colors ride tokens and switch automatically in dark mode; do not override type semantic colors with arbitrary custom colors unless you have re-checked contrast.

Released under the MIT OR Apache-2.0 License.