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
| Tier | Condition (units) | Tag form | Copy policy |
|---|---|---|---|
| In stock | > 100 | type="success" "In stock" | Plenty (N units) — no artificial scarcity |
| Low | 20 < n ≤ 100 | type="warning" "Only N left" | Stock running low, only N left |
| Critical | 0 < n ≤ 20 | type="danger" "Only N left" | Only N left, act fast |
| Sold out | = 0 | type="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)
Plenty in stock (528 units)
Change the stock to see the tiers: >100 in stock / ≤100 low / ≤20 critical / 0 sold out.
Key points:
- Components have no stock concept:
oas-pricehandles price,oas-taghandles the semantic badge, and the tier function is a dozen lines of host code — the abstraction you need is a policy, not a container. - Semantic colors switch only
type, never hard-coded values: the four tiers map to theme tokens (dark variants included) and survive theme switches automatically. - Announcements ride
role="status": on tier switches screen-reader users hear "only 3 left, act fast" — stock hints are accessibility information, not just decoration. - Sold out is a behavior tier, not just a color tier: in real business a sellout also disables the buy entry (
disabledon 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
Key points:
- The
tagsslot is free content — drop in theoas-tagproduced by the tier function and you have a stock slot; one tier function serves both the detail page and card walls. - 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
- 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. toLocaleStringfor thousands: keep the stock number formatting consistent with the price (thousands built intooas-price).- 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).
- Dark and high-contrast themes: all four semantic colors ride tokens and switch automatically in dark mode; do not override
typesemantic colors with arbitrary custom colors unless you have re-checked contrast.