index.html 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. .counter {
  105. display: inline-flex;
  106. align-items: center;
  107. gap: .75rem;
  108. }
  109. .counter .value {
  110. min-width: 2.5ch;
  111. text-align: center;
  112. font-size: 1.5rem;
  113. }
  114. .grid {
  115. display: grid;
  116. grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  117. gap: 1rem;
  118. }
  119. .card {
  120. border: 1px solid var(--line);
  121. border-radius: 8px;
  122. padding: .75rem;
  123. }
  124. .card h3 {
  125. margin: 0 0 .25rem;
  126. }
  127. .price {
  128. color: var(--muted);
  129. }
  130. .pill {
  131. display: inline-block;
  132. font-size: .75rem;
  133. padding: .1rem .5rem;
  134. border-radius: 999px;
  135. background: #eef2f7;
  136. color: var(--muted);
  137. }
  138. .pill.stock {
  139. background: #e6f4ea;
  140. color: #1e7e34;
  141. }
  142. .pill.soldout {
  143. background: #fde8ea;
  144. color: #b00020;
  145. }
  146. ul.lines {
  147. padding-left: 1.1rem;
  148. }
  149. .muted {
  150. color: var(--muted);
  151. }
  152. </style>
  153. </head>
  154. <!--
  155. Body-level overrides (optional): stm-entrypoint / stm-slots point the library
  156. at this server's endpoints. They default to /_statum/entrypoint and
  157. /_statum/slots, so they are shown here only to demonstrate the overrides.
  158. -->
  159. <body stm-entrypoint="/_statum/entrypoint" stm-slots="/_statum/slots">
  160. <header>
  161. <h1>Statum Demo</h1>
  162. <!-- stm-text -->
  163. <span>Role: <span stm-text="user.name">Guest</span></span>
  164. <span class="role">
  165. <!-- stm-if / stm-else-if / stm-else chain -->
  166. <span stm-if="user.role === 'admin'">administrator tools enabled</span>
  167. <span stm-else-if="user.role === 'member'">member tools</span>
  168. <span stm-else>read-only guest</span>
  169. </span>
  170. <button stm-action="user.toggleAction">Toggle role</button>
  171. </header>
  172. <main>
  173. <!-- stm-preloader is shown until the entrypoint resolves; stm-content after. -->
  174. <div stm-preloader>Loading application state…</div>
  175. <!--
  176. stm-content is pre-hidden in the HTML so the un-bound template never
  177. flashes before the script runs; statum removes `hidden` once ready.
  178. -->
  179. <div stm-content hidden>
  180. <!-- Counter: stm-text, stm-class.x, stm-if, stm-action, stm-data-*, stm-attribute, stm-disabled -->
  181. <section>
  182. <h2>Counter <span class="muted">(stm-text · stm-class · stm-if · stm-data · stm-attribute · stm-disabled)</span></h2>
  183. <div class="counter">
  184. <!-- stm-disabled disables the element + descendants when the predicate is true -->
  185. <button stm-action="counter.decrement" stm-disabled="counter.value &lt;= 0">−</button>
  186. <!-- stm-class.high toggles when value > 5; stm-attribute.data-value sets a data-* attr -->
  187. <span class="value" stm-text="counter.value" stm-class.high="counter.value > 5" stm-attribute.data-value="counter.value">0</span>
  188. <button stm-action="counter.increment">+</button>
  189. <button stm-action="counter.reset">Reset</button>
  190. <!-- stm-data-amount sends a literal value with the request -->
  191. <button stm-action="counter.add" stm-data-amount="5">+5</button>
  192. <!-- stm-attribute.checked toggles a boolean attribute from a boolean result -->
  193. <label class="pill"><input type="checkbox" stm-attribute.checked="counter.high"> high flag</label>
  194. </div>
  195. <!-- stm-if on a derived boolean -->
  196. <p stm-if="counter.high">That is a high number!</p>
  197. </section>
  198. <!-- Products: stm-for-..-in, stm-key, stm-class, stm-if/else, stm-action, stm-busy-class -->
  199. <section>
  200. <h2>Products <span class="muted">(stm-for · stm-key · stm-class · stm-action · stm-busy-class)</span></h2>
  201. <div class="grid">
  202. <div class="card"
  203. stm-for-product-in="products.items"
  204. stm-key="product.id"
  205. stm-class.featured="product.featured"
  206. stm-class.out="!product.inStock">
  207. <h3 stm-text="product.name">Product</h3>
  208. <div class="price">$<span stm-text="product.price">0</span></div>
  209. <p>
  210. <span class="pill stock" stm-if="product.inStock">In stock</span>
  211. <span class="pill soldout" stm-else>Sold out</span>
  212. <span class="pill" stm-if="product.featured">featured</span>
  213. </p>
  214. <!--
  215. Per-item context (the product id) is baked into the action's
  216. `private` field server-side, so no stm-data expression is needed.
  217. stm-busy-class applies while the request is in flight.
  218. -->
  219. <button stm-action="product.addToCartAction"
  220. stm-busy-class="busy"
  221. stm-data-confirm="true">Add to cart</button>
  222. </div>
  223. </div>
  224. </section>
  225. <!-- Cart: stm-text, stm-if/else, nested stm-for, and the always-send slot -->
  226. <section>
  227. <h2>Cart <span class="muted">(stm-text · stm-if/else · nested stm-for)</span></h2>
  228. <p stm-if="cart.empty">Your cart is empty.</p>
  229. <div stm-else>
  230. <p><strong>Items:</strong> <span stm-text="cart.count">0</span>
  231. &nbsp; <strong>Total:</strong> $<span stm-text="cart.total">0</span></p>
  232. <ul class="lines">
  233. <!-- nested loop inside an stm-else branch -->
  234. <li stm-for-line-in="cart.items" stm-key="line.id">
  235. <span stm-text="line.name"></span> ×
  236. <span stm-text="line.qty"></span>
  237. ($<span stm-text="line.price"></span>)
  238. </li>
  239. </ul>
  240. <button stm-action="cart.checkoutAction" stm-disabled="cart.empty">Checkout</button>
  241. <button stm-action="cart.clearAction">Clear cart</button>
  242. </div>
  243. </section>
  244. <!-- Notice: stm-html, and an error directive to demonstrate stm-on-error -->
  245. <section>
  246. <h2>Notice <span class="muted">(stm-html · stm-on-error)</span></h2>
  247. <!-- stm-html renders server-provided HTML content -->
  248. <div stm-html="notice.html">No notice set.</div>
  249. <p>
  250. <button stm-action="notice.setAction">Set HTML notice</button>
  251. <!-- stm-on-error applies these classes when the action fails -->
  252. <button stm-action="notice.failAction" stm-on-error="error">Trigger error</button>
  253. </p>
  254. </section>
  255. <section>
  256. <h2>Session <span class="muted">(stm-confirm)</span></h2>
  257. <p>
  258. <!-- stm-confirm shows a confirm() dialog before the action runs. -->
  259. <button stm-action="user.resetAllAction" stm-confirm="Reset all session state?">Reset everything</button>
  260. </p>
  261. </section>
  262. </div>
  263. </main>
  264. <!-- The library auto-initialises on DOMContentLoaded. -->
  265. <script src="/statum.js"></script>
  266. </body>
  267. </html>