index.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Statum Demo</title>
  7. <style>
  8. :root {
  9. --bg: #f6f7f9;
  10. --card: #fff;
  11. --line: #e3e6ea;
  12. --accent: #3a6ea5;
  13. --muted: #6b7280;
  14. }
  15. * {
  16. box-sizing: border-box;
  17. }
  18. body {
  19. margin: 0;
  20. font-family: system-ui, Segoe UI, Roboto, sans-serif;
  21. background: var(--bg);
  22. color: #1f2933;
  23. line-height: 1.5;
  24. }
  25. header {
  26. background: var(--accent);
  27. color: #fff;
  28. padding: 1rem 1.5rem;
  29. display: flex;
  30. align-items: baseline;
  31. gap: 1rem;
  32. flex-wrap: wrap;
  33. }
  34. header h1 {
  35. margin: 0;
  36. font-size: 1.25rem;
  37. }
  38. header .role {
  39. margin-left: auto;
  40. opacity: .9;
  41. }
  42. main {
  43. max-width: 900px;
  44. margin: 0 auto;
  45. padding: 1.5rem;
  46. }
  47. section {
  48. background: var(--card);
  49. border: 1px solid var(--line);
  50. border-radius: 10px;
  51. padding: 1rem 1.25rem;
  52. margin-bottom: 1.25rem;
  53. }
  54. h2 {
  55. margin-top: 0;
  56. font-size: 1.05rem;
  57. color: var(--muted);
  58. text-transform: uppercase;
  59. letter-spacing: .04em;
  60. }
  61. button {
  62. font: inherit;
  63. padding: .35rem .8rem;
  64. border: 1px solid var(--line);
  65. border-radius: 6px;
  66. background: #fff;
  67. cursor: pointer;
  68. }
  69. button:hover {
  70. border-color: var(--accent);
  71. }
  72. button:disabled {
  73. opacity: .55;
  74. cursor: progress;
  75. }
  76. /* stm-class targets */
  77. .high {
  78. color: #b00020;
  79. font-weight: 700;
  80. }
  81. .featured {
  82. border-color: #f0a500;
  83. box-shadow: 0 0 0 2px #f0a50033;
  84. }
  85. .out {
  86. opacity: .5;
  87. }
  88. /* stm-busy-class */
  89. .busy {
  90. outline: 2px solid var(--accent);
  91. outline-offset: 2px;
  92. }
  93. /* stm-on-error */
  94. .error {
  95. border-color: #b00020;
  96. background: #fde8ea;
  97. }
  98. .ok {
  99. background: #e6f4ea;
  100. border: 1px solid #b7deca;
  101. padding: .5rem .75rem;
  102. border-radius: 6px;
  103. }
  104. /* stm-markdown output */
  105. .md {
  106. border: 1px solid var(--line);
  107. border-radius: 6px;
  108. padding: .75rem 1rem;
  109. background: #fbfcfd;
  110. }
  111. .md pre {
  112. background: #eef2f7;
  113. border-radius: 6px;
  114. padding: .5rem .75rem;
  115. overflow-x: auto;
  116. }
  117. .md code {
  118. font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  119. font-size: .9em;
  120. }
  121. .md > :first-child {
  122. margin-top: 0;
  123. }
  124. .md > :last-child {
  125. margin-bottom: 0;
  126. }
  127. .counter {
  128. display: inline-flex;
  129. align-items: center;
  130. gap: .75rem;
  131. }
  132. .counter .value {
  133. min-width: 2.5ch;
  134. text-align: center;
  135. font-size: 1.5rem;
  136. }
  137. .grid {
  138. display: grid;
  139. grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  140. gap: 1rem;
  141. }
  142. .card {
  143. border: 1px solid var(--line);
  144. border-radius: 8px;
  145. padding: .75rem;
  146. }
  147. .card h3 {
  148. margin: 0 0 .25rem;
  149. }
  150. .price {
  151. color: var(--muted);
  152. }
  153. .pill {
  154. display: inline-block;
  155. font-size: .75rem;
  156. padding: .1rem .5rem;
  157. border-radius: 999px;
  158. background: #eef2f7;
  159. color: var(--muted);
  160. }
  161. .pill.stock {
  162. background: #e6f4ea;
  163. color: #1e7e34;
  164. }
  165. .pill.soldout {
  166. background: #fde8ea;
  167. color: #b00020;
  168. }
  169. ul.lines {
  170. padding-left: 1.1rem;
  171. }
  172. .muted {
  173. color: var(--muted);
  174. }
  175. /* Toasts rendered by the custom onNotifyHandler/onErrorHandler overrides. */
  176. #toasts {
  177. position: fixed;
  178. right: 1rem;
  179. bottom: 1rem;
  180. display: flex;
  181. flex-direction: column;
  182. gap: .5rem;
  183. z-index: 50;
  184. max-width: 22rem;
  185. }
  186. .toast {
  187. background: var(--card);
  188. border: 1px solid var(--line);
  189. border-left: 4px solid var(--accent);
  190. border-radius: 6px;
  191. padding: .5rem .75rem;
  192. box-shadow: 0 2px 8px rgba(0, 0, 0, .12);
  193. font-size: .9rem;
  194. transition: opacity .4s ease;
  195. }
  196. .toast.success { border-left-color: #1e7e34; }
  197. .toast.error { border-left-color: #b00020; }
  198. .toast.info { border-left-color: var(--accent); }
  199. .toast.hide { opacity: 0; }
  200. </style>
  201. </head>
  202. <!--
  203. Body-level overrides (optional): stm-entrypoint / stm-slots point the library
  204. at this server's endpoints. They default to /_statum/entrypoint and
  205. /_statum/slots, so they are shown here only to demonstrate the overrides.
  206. -->
  207. <body stm-entrypoint="/_statum/entrypoint" stm-slots="/_statum/slots">
  208. <header>
  209. <h1>Statum Demo</h1>
  210. <!-- stm-text -->
  211. <span>Role: <span stm-text="user.name">Guest</span></span>
  212. <span class="role">
  213. <!-- stm-if / stm-else-if / stm-else chain -->
  214. <span stm-if="user.role === 'admin'">administrator tools enabled</span>
  215. <span stm-else-if="user.role === 'member'">member tools</span>
  216. <span stm-else>read-only guest</span>
  217. </span>
  218. <button stm-action="user.toggleAction">Toggle role</button>
  219. </header>
  220. <main>
  221. <!-- stm-preloader is shown until the entrypoint resolves; stm-content after. -->
  222. <div stm-preloader>Loading application state…</div>
  223. <!--
  224. stm-content is pre-hidden in the HTML so the un-bound template never
  225. flashes before the script runs; statum removes `hidden` once ready.
  226. -->
  227. <div stm-content hidden>
  228. <!--
  229. Intentional leftover: this pstm-* markup should have been resolved
  230. during server-side pre-rendering. The library logs a single console
  231. warning about it on load to demonstrate that detection.
  232. -->
  233. <div pstm-component="header" hidden aria-hidden="true"></div>
  234. <!-- Counter: stm-text, stm-class.x, stm-if, stm-action, stm-data-*, stm-attribute, stm-disabled -->
  235. <section>
  236. <h2>Counter <span class="muted">(stm-text · stm-class · stm-if · stm-data · stm-attribute · stm-disabled)</span></h2>
  237. <div class="counter">
  238. <!-- stm-disabled disables the element + descendants when the predicate is true -->
  239. <button stm-action="counter.decrement" stm-disabled="counter.value &lt;= 0">−</button>
  240. <!-- stm-class.high toggles when value > 5; stm-attribute.data-value sets a data-* attr -->
  241. <span class="value" stm-text="counter.value" stm-class.high="counter.value > 5" stm-attribute.data-value="counter.value">0</span>
  242. <button stm-action="counter.increment">+</button>
  243. <button stm-action="counter.reset">Reset</button>
  244. <!-- stm-data-amount sends a literal value with the request -->
  245. <button stm-action="counter.add" stm-data-amount="5">+5</button>
  246. <!-- stm-attribute.checked toggles a boolean attribute from a boolean result -->
  247. <label class="pill"><input type="checkbox" stm-attribute.checked="counter.high"> high flag</label>
  248. </div>
  249. <!-- stm-if on a derived boolean -->
  250. <p stm-if="counter.high">That is a high number!</p>
  251. </section>
  252. <!-- Products: stm-for-..-in, stm-key, stm-class, stm-if/else, stm-action, stm-busy-class -->
  253. <section>
  254. <h2>Products <span class="muted">(stm-for · stm-key · stm-class · stm-action · stm-busy-class)</span></h2>
  255. <div class="grid">
  256. <div class="card"
  257. stm-for-product-in="products.items"
  258. stm-key="product.id"
  259. stm-class.featured="product.featured"
  260. stm-class.out="!product.inStock">
  261. <h3 stm-text="product.name">Product</h3>
  262. <div class="price">$<span stm-text="product.price">0</span></div>
  263. <p>
  264. <span class="pill stock" stm-if="product.inStock">In stock</span>
  265. <span class="pill soldout" stm-else>Sold out</span>
  266. <span class="pill" stm-if="product.featured">featured</span>
  267. </p>
  268. <!--
  269. Per-item context (the product id) is baked into the action's
  270. `private` field server-side, so no stm-data expression is needed.
  271. stm-busy-class applies while the request is in flight.
  272. -->
  273. <button stm-action="product.addToCartAction"
  274. stm-busy-class="busy"
  275. stm-data-confirm="true">Add to cart</button>
  276. </div>
  277. </div>
  278. </section>
  279. <!-- Cart: stm-text, stm-if/else, nested stm-for, and the always-send slot -->
  280. <section>
  281. <h2>Cart <span class="muted">(stm-text · stm-if/else · nested stm-for)</span></h2>
  282. <p stm-if="cart.empty">Your cart is empty.</p>
  283. <div stm-else>
  284. <p><strong>Items:</strong> <span stm-text="cart.count">0</span>
  285. &nbsp; <strong>Total:</strong> $<span stm-text="cart.total">0</span></p>
  286. <ul class="lines">
  287. <!-- nested loop inside an stm-else branch -->
  288. <li stm-for-line-in="cart.items" stm-key="line.id">
  289. <span stm-text="line.name"></span> ×
  290. <span stm-text="line.qty"></span>
  291. ($<span stm-text="line.price"></span>)
  292. </li>
  293. </ul>
  294. <button stm-action="cart.checkoutAction" stm-disabled="cart.empty">Checkout</button>
  295. <button stm-action="cart.clearAction">Clear cart</button>
  296. </div>
  297. </section>
  298. <!-- Notice: stm-html, and an error directive to demonstrate stm-on-error -->
  299. <section>
  300. <h2>Notice <span class="muted">(stm-html · stm-on-error)</span></h2>
  301. <!-- stm-html renders server-provided HTML content -->
  302. <div stm-html="notice.html">No notice set.</div>
  303. <p>
  304. <button stm-action="notice.setAction">Set HTML notice</button>
  305. <!-- stm-on-error applies these classes when the action fails -->
  306. <button stm-action="notice.failAction" stm-on-error="error">Trigger error</button>
  307. </p>
  308. </section>
  309. <!--
  310. Markdown: stm-markdown renders the state value as CommonMark. The
  311. sample deliberately contains the cases the engine is regression-tested
  312. for: emphasis spanning a line wrap, soft line breaks (no new paragraph),
  313. a wrapped bullet item, a fenced code block, reference + autolinks, and
  314. raw HTML / javascript: URLs that must come out inert.
  315. -->
  316. <section>
  317. <h2>Markdown <span class="muted">(stm-markdown · CommonMark, source HTML escaped)</span></h2>
  318. <div class="md" stm-markdown="markdown.body">Rendering markdown…</div>
  319. </section>
  320. <!--
  321. Profile: stm-prefill writes state into control PROPERTIES (never
  322. attributes), covering the whole control matrix: text input, select,
  323. checkbox (checked), and a radio group (every member carries the same
  324. expression; the one whose value matches is checked). Controls the user
  325. has edited are dirty and skipped until the form resets.
  326. stm-clear-on-success resets this form when its own stm-action succeeds
  327. (the reset also un-dirties the controls, so the fresh prefill below can
  328. re-apply), and stm-on-success marks the submit button for the duration
  329. of the next attempt — the success classes mirror stm-on-error (a
  330. failure clears them and applies the error classes instead).
  331. The stm-text line echoes the saved state back.
  332. -->
  333. <section>
  334. <h2>Profile <span class="muted">(stm-prefill · dirty tracking · stm-clear-on-success · stm-on-success)</span></h2>
  335. <form stm-action="profile.saveAction" stm-clear-on-success>
  336. <p>
  337. <label>Name <input type="text" name="name" stm-prefill="profile.name"></label>
  338. </p>
  339. <p>
  340. <label>Theme
  341. <select name="theme" stm-prefill="profile.theme">
  342. <option value="light">Light</option>
  343. <option value="dark">Dark</option>
  344. <option value="auto">Auto</option>
  345. </select>
  346. </label>
  347. </p>
  348. <p>
  349. <label><input type="checkbox" name="news" value="1" stm-prefill="profile.news"> Email me news</label>
  350. </p>
  351. <p>
  352. Digest:
  353. <label><input type="radio" name="cadence" value="daily" stm-prefill="profile.cadence"> Daily</label>
  354. <label><input type="radio" name="cadence" value="weekly" stm-prefill="profile.cadence"> Weekly</label>
  355. <label><input type="radio" name="cadence" value="never" stm-prefill="profile.cadence"> Never</label>
  356. </p>
  357. <p><button type="submit" stm-on-success="ok" stm-on-error="error">Save profile</button></p>
  358. </form>
  359. <p class="muted">
  360. Saved:
  361. <span stm-text="profile.name">…</span>
  362. · theme <span stm-text="profile.theme">…</span>
  363. · news <span stm-text="profile.news ? 'on' : 'off'">…</span>
  364. · digest <span stm-text="profile.cadence">…</span>
  365. </p>
  366. <!--
  367. stm-clear-on-error: the mirror of stm-clear-on-success. This form's
  368. action always fails, so submitting it resets the form to its
  369. defaults (and un-dirties it). A declined confirm would NOT clear —
  370. decline is cancellation, not error — but this button has no
  371. stm-confirm; try it with the DevTools confirm-cancelled case.
  372. -->
  373. <form stm-action="profile.failAction" stm-clear-on-error>
  374. <p>
  375. <label>Nickname <input type="text" name="nick" value="untyped"></label>
  376. </p>
  377. <p><button type="submit" stm-on-error="error">Try a failing save</button></p>
  378. </form>
  379. </section>
  380. <!--
  381. Live channel: subscribe/unsubscribe directives drive a realtime
  382. Server-Sent Events connection; broadcasting pushes a `set` to every
  383. subscribed tab and also returns a `notify` directive. stm-def-{name}-as
  384. defines a local variable for this element and its descendants.
  385. Open a second tab and broadcast to see the update arrive live.
  386. -->
  387. <section>
  388. <h2>Live channel <span class="muted">(subscribe · realtime · notify · stm-def)</span></h2>
  389. <div stm-def-stamp-as="live.at ? 'last updated ' + live.at : ''">
  390. <p>
  391. <strong stm-text="live.text">…</strong>
  392. <span class="muted" stm-text="stamp"></span>
  393. </p>
  394. <form stm-action="live.sendAction">
  395. <input type="text" name="msg" placeholder="Broadcast to every open tab" style="min-width:260px">
  396. <button type="submit">Broadcast</button>
  397. </form>
  398. <p class="muted">
  399. <button stm-action="live.muteAction">Stop live updates</button>
  400. <button stm-action="live.resumeAction">Resume live updates</button>
  401. </p>
  402. </div>
  403. </section>
  404. <section>
  405. <h2>Session <span class="muted">(stm-confirm)</span></h2>
  406. <p>
  407. <!-- stm-confirm shows a confirm() dialog before the action runs. -->
  408. <button stm-action="user.resetAllAction" stm-confirm="Reset all session state?">Reset everything</button>
  409. </p>
  410. </section>
  411. </div>
  412. </main>
  413. <!-- Container for the toast notifications rendered by the handlers below. -->
  414. <div id="toasts"></div>
  415. <!-- The library auto-initialises on DOMContentLoaded. Four modules, in load
  416. order: core first (it installs the seams the later modules register
  417. into), then document, markdown, forms. -->
  418. <script src="/statum-core.js"></script>
  419. <script src="/statum-document.js"></script>
  420. <script src="/statum-markdown.js"></script>
  421. <script>
  422. // Override the default UI handlers so `notify` directives (and errors)
  423. // render as styled toasts instead of using the browser's native alert().
  424. // These run before DOMContentLoaded fires, so they are in place before the
  425. // entrypoint is processed.
  426. statum.onNotifyHandler = function (kind, message) {
  427. toast((kind ? '[' + kind + '] ' : '') + (message || ''), kind || 'info');
  428. };
  429. statum.onErrorHandler = function (err, element) {
  430. toast('Error: ' + (err && err.message ? err.message : 'something went wrong'), 'error');
  431. return true; // keep the default behaviour (stm-on-error classes + rejection)
  432. };
  433. function toast(msg, kind) {
  434. var box = document.getElementById('toasts');
  435. if (!box) return;
  436. var el = document.createElement('div');
  437. el.className = 'toast ' + kind;
  438. el.textContent = msg;
  439. box.appendChild(el);
  440. setTimeout(function () {
  441. el.classList.add('hide');
  442. setTimeout(function () { el.remove(); }, 400);
  443. }, 3500);
  444. }
  445. </script>
  446. </body>
  447. </html>