@link-foundation/ui-components 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (402) hide show
  1. package/CONFIGURATIONS.md +74 -0
  2. package/ICONS.md +76 -0
  3. package/LICENSE +24 -0
  4. package/README.md +1499 -2
  5. package/bin/lino-network.mjs +71 -0
  6. package/dist/icons/ATTRIBUTION.md +602 -0
  7. package/dist/icons/NOTICE.lino +1261 -0
  8. package/dist/icons/brand.js +31 -0
  9. package/dist/icons/brand.svg +3 -0
  10. package/dist/icons/licenses/brand/DISCLAIMER.md +51 -0
  11. package/dist/icons/licenses/brand/LICENSE.md +30 -0
  12. package/dist/icons/licenses/lucide/LICENSE +43 -0
  13. package/dist/icons/licenses/material-symbols/LICENSE +202 -0
  14. package/dist/icons/licenses/material-symbols/NOTICE +12 -0
  15. package/dist/icons/licenses/tabler/LICENSE +21 -0
  16. package/dist/icons/lucide.js +681 -0
  17. package/dist/icons/lucide.svg +34 -0
  18. package/dist/icons/material-symbols.js +681 -0
  19. package/dist/icons/material-symbols.svg +34 -0
  20. package/dist/icons/own.js +521 -0
  21. package/dist/icons/own.svg +34 -0
  22. package/dist/icons/tabler.js +681 -0
  23. package/dist/icons/tabler.svg +34 -0
  24. package/dist/network-layout-NOTICE.md +6 -0
  25. package/dist/network-layout-d3-dispatch-LICENSE.txt +13 -0
  26. package/dist/network-layout-d3-force-LICENSE.txt +13 -0
  27. package/dist/network-layout-d3-quadtree-LICENSE.txt +13 -0
  28. package/dist/network-layout-d3-timer-LICENSE.txt +13 -0
  29. package/dist/network-layout-dagre-LICENSE.txt +19 -0
  30. package/dist/network-layout.worker.mjs +2 -0
  31. package/package.json +246 -4
  32. package/src/a11y/announce.js +88 -0
  33. package/src/a11y/bind-machine.js +181 -0
  34. package/src/a11y/focus.js +169 -0
  35. package/src/a11y/index.d.ts +89 -0
  36. package/src/a11y/index.js +19 -0
  37. package/src/a11y/keys.js +182 -0
  38. package/src/a11y/roving.js +167 -0
  39. package/src/a11y/typeahead.js +82 -0
  40. package/src/behavior/actions.js +235 -0
  41. package/src/behavior/diagnostics.js +73 -0
  42. package/src/behavior/dom.d.ts +38 -0
  43. package/src/behavior/dom.js +420 -0
  44. package/src/behavior/index.d.ts +211 -0
  45. package/src/behavior/index.js +29 -0
  46. package/src/behavior/interpret.js +454 -0
  47. package/src/behavior/machines/disclosure.lino +46 -0
  48. package/src/behavior/machines/index.js +91 -0
  49. package/src/behavior/machines/toggle.lino +25 -0
  50. package/src/behavior/parse-machine.js +367 -0
  51. package/src/charts/README.md +78 -0
  52. package/src/charts/activity-layout.js +350 -0
  53. package/src/charts/activity-model.js +130 -0
  54. package/src/charts/activity-validation.js +31 -0
  55. package/src/charts/chart-model.js +667 -0
  56. package/src/charts/chart.js +518 -0
  57. package/src/charts/echarts-adapter.d.ts +23 -0
  58. package/src/charts/echarts-adapter.js +240 -0
  59. package/src/charts/element.js +330 -0
  60. package/src/charts/errors.js +62 -0
  61. package/src/charts/gauge.js +27 -0
  62. package/src/charts/index.d.ts +344 -0
  63. package/src/charts/index.js +59 -0
  64. package/src/charts/layout.js +542 -0
  65. package/src/charts/lino.js +158 -0
  66. package/src/charts/mark-options.js +78 -0
  67. package/src/charts/read.js +101 -0
  68. package/src/charts/renderer-features.js +8 -0
  69. package/src/charts/scales.js +223 -0
  70. package/src/charts/small-model.js +223 -0
  71. package/src/charts/small-svg.js +269 -0
  72. package/src/charts/sparkline.js +27 -0
  73. package/src/charts/stat-tile.js +37 -0
  74. package/src/charts/summary.js +193 -0
  75. package/src/charts/svg-renderer.js +226 -0
  76. package/src/charts/svg.js +69 -0
  77. package/src/charts/tag-cloud.js +129 -0
  78. package/src/charts/tokens.js +171 -0
  79. package/src/charts/tooltip.js +47 -0
  80. package/src/charts/values.js +146 -0
  81. package/src/chat/README.md +142 -0
  82. package/src/chat/actions.js +75 -0
  83. package/src/chat/chat-contract.js +95 -0
  84. package/src/chat/chat-list.js +42 -0
  85. package/src/chat/chat-model.js +125 -0
  86. package/src/chat/chat.css +228 -0
  87. package/src/chat/composer.js +131 -0
  88. package/src/chat/day-separator.js +12 -0
  89. package/src/chat/dom.js +33 -0
  90. package/src/chat/follow.js +49 -0
  91. package/src/chat/index.d.ts +142 -0
  92. package/src/chat/index.js +13 -0
  93. package/src/chat/message-bubble.js +191 -0
  94. package/src/chat/message-stream.js +331 -0
  95. package/src/chat/reasoning-trace.js +70 -0
  96. package/src/chat/selection.js +39 -0
  97. package/src/chat/skins/bubbles.lino +2 -0
  98. package/src/chat/skins/cards.lino +4 -0
  99. package/src/chat/skins/compact.lino +4 -0
  100. package/src/chat/skins/signal.lino +25 -0
  101. package/src/chat/skins/t3.lino +25 -0
  102. package/src/chat/skins/telegram.lino +31 -0
  103. package/src/chat/skins/whatsapp.lino +25 -0
  104. package/src/chat/stream-keys.js +57 -0
  105. package/src/chat/stream-window.js +159 -0
  106. package/src/chat/vocabulary.js +19 -0
  107. package/src/controls/accordion.js +160 -0
  108. package/src/controls/avatar.js +80 -0
  109. package/src/controls/badge.js +48 -0
  110. package/src/controls/button-template.js +32 -0
  111. package/src/controls/button.js +97 -0
  112. package/src/controls/catalog.lino +84 -0
  113. package/src/controls/checkbox.js +112 -0
  114. package/src/controls/chip.js +140 -0
  115. package/src/controls/choices.js +45 -0
  116. package/src/controls/color-field.js +217 -0
  117. package/src/controls/color-popover.js +135 -0
  118. package/src/controls/color-rgb.js +80 -0
  119. package/src/controls/color.js +67 -0
  120. package/src/controls/combobox.js +192 -0
  121. package/src/controls/dom.js +261 -0
  122. package/src/controls/icon-button.js +33 -0
  123. package/src/controls/index.d.ts +163 -0
  124. package/src/controls/index.js +182 -0
  125. package/src/controls/kinds-text.js +358 -0
  126. package/src/controls/kinds.lino +350 -0
  127. package/src/controls/label.js +52 -0
  128. package/src/controls/link.js +101 -0
  129. package/src/controls/listbox.js +144 -0
  130. package/src/controls/machines/button.lino +20 -0
  131. package/src/controls/machines/checkbox.lino +17 -0
  132. package/src/controls/machines/chip.lino +20 -0
  133. package/src/controls/machines/color-field.lino +23 -0
  134. package/src/controls/machines/combobox.lino +82 -0
  135. package/src/controls/machines/icon-button.lino +20 -0
  136. package/src/controls/machines/index.js +586 -0
  137. package/src/controls/machines/number-field.lino +71 -0
  138. package/src/controls/machines/radio-group.lino +17 -0
  139. package/src/controls/machines/select.lino +122 -0
  140. package/src/controls/machines/slider.lino +99 -0
  141. package/src/controls/machines/switch.lino +19 -0
  142. package/src/controls/machines/text-area.lino +26 -0
  143. package/src/controls/machines/text-field.lino +26 -0
  144. package/src/controls/number-field.js +239 -0
  145. package/src/controls/numeric-keys.js +125 -0
  146. package/src/controls/option.js +54 -0
  147. package/src/controls/progress.js +76 -0
  148. package/src/controls/radio-group.js +234 -0
  149. package/src/controls/select.js +229 -0
  150. package/src/controls/slider.js +234 -0
  151. package/src/controls/stories.js +172 -0
  152. package/src/controls/styles.css +1060 -0
  153. package/src/controls/switch.js +95 -0
  154. package/src/controls/text-area.js +39 -0
  155. package/src/controls/text-field.js +131 -0
  156. package/src/controls/vocabulary.js +164 -0
  157. package/src/data/data.css +95 -0
  158. package/src/data/index.d.ts +83 -0
  159. package/src/data/index.js +10 -0
  160. package/src/data/keyboard.js +189 -0
  161. package/src/data/list.js +215 -0
  162. package/src/data/render-data.js +116 -0
  163. package/src/data/selection-dom.js +39 -0
  164. package/src/data/selection-model.js +43 -0
  165. package/src/data/sort-model.js +42 -0
  166. package/src/data/source.js +45 -0
  167. package/src/data/table.js +160 -0
  168. package/src/data/tree-model.js +41 -0
  169. package/src/data/tree.js +55 -0
  170. package/src/data/viewport.js +216 -0
  171. package/src/data/virtual-range.js +92 -0
  172. package/src/data/vocabulary.js +67 -0
  173. package/src/diagrams/diagram-model.js +511 -0
  174. package/src/diagrams/diagram.js +204 -0
  175. package/src/diagrams/index.d.ts +225 -0
  176. package/src/diagrams/index.js +48 -0
  177. package/src/diagrams/layout-dagre.js +164 -0
  178. package/src/diagrams/layout-plugins.js +131 -0
  179. package/src/diagrams/mermaid-export.js +232 -0
  180. package/src/diagrams/mermaid-view.js +229 -0
  181. package/src/diagrams/svg-diagram.js +215 -0
  182. package/src/editor/code-editor.js +206 -0
  183. package/src/editor/documents-model.js +170 -0
  184. package/src/editor/documents-persistence.js +85 -0
  185. package/src/editor/editor-engine.js +22 -0
  186. package/src/editor/editor-ui.js +203 -0
  187. package/src/editor/editor.css +106 -0
  188. package/src/editor/engines/codemirror.js +227 -0
  189. package/src/editor/engines/monaco.js +219 -0
  190. package/src/editor/index.d.ts +111 -0
  191. package/src/editor/index.js +14 -0
  192. package/src/editor/lino-highlight.js +106 -0
  193. package/src/editor/lino-structure.js +85 -0
  194. package/src/editor/validate-lino.js +43 -0
  195. package/src/embed/README.md +161 -0
  196. package/src/embed/controller.js +223 -0
  197. package/src/embed/element.js +141 -0
  198. package/src/embed/index.d.ts +75 -0
  199. package/src/embed/index.js +9 -0
  200. package/src/embed/providers/telegram.d.ts +3 -0
  201. package/src/embed/providers/telegram.js +38 -0
  202. package/src/embed/providers/vk.d.ts +3 -0
  203. package/src/embed/providers/vk.js +42 -0
  204. package/src/embed/providers/x.d.ts +3 -0
  205. package/src/embed/providers/x.js +31 -0
  206. package/src/embed/registry.js +35 -0
  207. package/src/embed/script.js +73 -0
  208. package/src/embed/view.js +122 -0
  209. package/src/icons/adapters/lucide.d.ts +15 -0
  210. package/src/icons/adapters/lucide.js +142 -0
  211. package/src/icons/adapters/material-symbols.d.ts +13 -0
  212. package/src/icons/adapters/material-symbols.js +113 -0
  213. package/src/icons/adapters/shared.d.ts +38 -0
  214. package/src/icons/adapters/shared.js +157 -0
  215. package/src/icons/adapters/simple-icons.d.ts +9 -0
  216. package/src/icons/adapters/simple-icons.js +38 -0
  217. package/src/icons/adapters/tabler.d.ts +9 -0
  218. package/src/icons/adapters/tabler.js +86 -0
  219. package/src/icons/icon-registry.js +215 -0
  220. package/src/icons/icon.js +356 -0
  221. package/src/icons/index.d.ts +123 -0
  222. package/src/icons/index.js +30 -0
  223. package/src/icons/names.js +51 -0
  224. package/src/icons/own-icons.js +521 -0
  225. package/src/icons/presentation.js +55 -0
  226. package/src/icons/svg.js +136 -0
  227. package/src/index.d.ts +14 -0
  228. package/src/index.js +40 -0
  229. package/src/layout/README.md +102 -0
  230. package/src/layout/app-shell.js +150 -0
  231. package/src/layout/catalog.lino +27 -0
  232. package/src/layout/dock-model.js +576 -0
  233. package/src/layout/dock-view.js +312 -0
  234. package/src/layout/dock.js +352 -0
  235. package/src/layout/dom.js +312 -0
  236. package/src/layout/grid.js +192 -0
  237. package/src/layout/index.d.ts +267 -0
  238. package/src/layout/index.js +60 -0
  239. package/src/layout/layer-manager.js +120 -0
  240. package/src/layout/layout.css +760 -0
  241. package/src/layout/menu.js +141 -0
  242. package/src/layout/nav-strip.js +174 -0
  243. package/src/layout/page-links.js +166 -0
  244. package/src/layout/panel.js +26 -0
  245. package/src/layout/pref-switches.js +209 -0
  246. package/src/layout/split-model.js +270 -0
  247. package/src/layout/split.js +430 -0
  248. package/src/layout/stack.js +86 -0
  249. package/src/layout/surfaces.js +119 -0
  250. package/src/layout/tab-group.js +151 -0
  251. package/src/layout/tab-strip.js +277 -0
  252. package/src/layout/vocabulary.js +229 -0
  253. package/src/layout/window-frame.js +222 -0
  254. package/src/layout/window.js +146 -0
  255. package/src/links-view/animator.js +144 -0
  256. package/src/links-view/configuration-classes.js +150 -0
  257. package/src/links-view/configuration-model.js +191 -0
  258. package/src/links-view/configuration-notations.js +230 -0
  259. package/src/links-view/configurations.d.ts +73 -0
  260. package/src/links-view/configurations.js +33 -0
  261. package/src/links-view/create-layout-runner.js +79 -0
  262. package/src/links-view/doublet-curves.js +239 -0
  263. package/src/links-view/doublet-layout.js +148 -0
  264. package/src/links-view/doublet-markers.js +109 -0
  265. package/src/links-view/doublet-options.js +369 -0
  266. package/src/links-view/doublet-svg.js +156 -0
  267. package/src/links-view/doublet.js +230 -0
  268. package/src/links-view/export.js +146 -0
  269. package/src/links-view/geometry.js +409 -0
  270. package/src/links-view/index.d.ts +1029 -0
  271. package/src/links-view/index.js +140 -0
  272. package/src/links-view/labels.js +28 -0
  273. package/src/links-view/layout-worker-entry.mjs +2 -0
  274. package/src/links-view/layout-worker.js +28 -0
  275. package/src/links-view/layouts/circle.js +7 -0
  276. package/src/links-view/layouts/force.js +76 -0
  277. package/src/links-view/layouts/grid.js +7 -0
  278. package/src/links-view/layouts/hex.js +8 -0
  279. package/src/links-view/layouts/index.js +15 -0
  280. package/src/links-view/layouts/layered.js +29 -0
  281. package/src/links-view/layouts/regular.js +27 -0
  282. package/src/links-view/layouts/spiral.js +10 -0
  283. package/src/links-view/link-view.js +494 -0
  284. package/src/links-view/links-gallery.js +306 -0
  285. package/src/links-view/links-network.js +496 -0
  286. package/src/links-view/lino-network-source.js +122 -0
  287. package/src/links-view/lino-network.js +233 -0
  288. package/src/links-view/lino-to-network.js +252 -0
  289. package/src/links-view/markers.js +192 -0
  290. package/src/links-view/model.js +233 -0
  291. package/src/links-view/network-editor/dom.js +36 -0
  292. package/src/links-view/network-editor/editor.js +202 -0
  293. package/src/links-view/network-editor/model.js +163 -0
  294. package/src/links-view/network-editor/options.js +265 -0
  295. package/src/links-view/network-editor/panels.js +423 -0
  296. package/src/links-view/network-editor/presets.js +76 -0
  297. package/src/links-view/network-editor/provider.js +93 -0
  298. package/src/links-view/network-editor/remap.js +45 -0
  299. package/src/links-view/network-editor/schema.js +224 -0
  300. package/src/links-view/network-editor/share.js +232 -0
  301. package/src/links-view/network-editor/styles.js +19 -0
  302. package/src/links-view/network-editor/url-sync.js +131 -0
  303. package/src/links-view/network-interaction.js +228 -0
  304. package/src/links-view/network-model.js +85 -0
  305. package/src/links-view/network-renderer.js +259 -0
  306. package/src/links-view/network-scene.js +245 -0
  307. package/src/links-view/network-source.js +82 -0
  308. package/src/links-view/network-vocabulary.js +18 -0
  309. package/src/links-view/presentation.js +199 -0
  310. package/src/links-view/render-canvas.js +102 -0
  311. package/src/links-view/render-svg.js +118 -0
  312. package/src/links-view/scene.js +233 -0
  313. package/src/links-view/solver-job.js +106 -0
  314. package/src/links-view/tokens.js +114 -0
  315. package/src/links-view/zoom-pan.js +203 -0
  316. package/src/overlays/catalog.lino +89 -0
  317. package/src/overlays/context-menu.js +110 -0
  318. package/src/overlays/dialog.js +117 -0
  319. package/src/overlays/dom.js +168 -0
  320. package/src/overlays/drawer.js +8 -0
  321. package/src/overlays/index.d.ts +80 -0
  322. package/src/overlays/index.js +25 -0
  323. package/src/overlays/kinds.lino +137 -0
  324. package/src/overlays/list-popover.js +129 -0
  325. package/src/overlays/machines/index.js +71 -0
  326. package/src/overlays/machines/surface.lino +11 -0
  327. package/src/overlays/machines/tabs.lino +7 -0
  328. package/src/overlays/machines/toast.lino +16 -0
  329. package/src/overlays/machines/tooltip.lino +29 -0
  330. package/src/overlays/menu.js +240 -0
  331. package/src/overlays/popover.js +194 -0
  332. package/src/overlays/position.js +84 -0
  333. package/src/overlays/radial-geometry.js +38 -0
  334. package/src/overlays/radial-menu.js +50 -0
  335. package/src/overlays/styles.css +155 -0
  336. package/src/overlays/tabs.js +125 -0
  337. package/src/overlays/toast-queue.js +105 -0
  338. package/src/overlays/toast.js +163 -0
  339. package/src/overlays/tooltip.js +110 -0
  340. package/src/overlays/vocabulary.js +3 -0
  341. package/src/settings/conditions.js +44 -0
  342. package/src/settings/control-for.js +62 -0
  343. package/src/settings/field-control.js +201 -0
  344. package/src/settings/index.d.ts +201 -0
  345. package/src/settings/index.js +3 -0
  346. package/src/settings/messages.js +112 -0
  347. package/src/settings/miniature.js +26 -0
  348. package/src/settings/panel-actions.js +234 -0
  349. package/src/settings/panel-model.js +49 -0
  350. package/src/settings/panel-refresh.js +56 -0
  351. package/src/settings/panel-rows.js +200 -0
  352. package/src/settings/preset-cards.js +43 -0
  353. package/src/settings/saved-looks.js +227 -0
  354. package/src/settings/section-layout.js +95 -0
  355. package/src/settings/section-tabs.js +175 -0
  356. package/src/settings/settings-panel.js +192 -0
  357. package/src/settings/settings.css +178 -0
  358. package/stories/accordion.lino +14 -0
  359. package/stories/alert-dialog.lino +4 -0
  360. package/stories/app-bar.lino +10 -0
  361. package/stories/avatar.lino +7 -0
  362. package/stories/badge.lino +12 -0
  363. package/stories/button.lino +17 -0
  364. package/stories/card.lino +10 -0
  365. package/stories/checkbox.lino +14 -0
  366. package/stories/chip.lino +19 -0
  367. package/stories/color-field.lino +9 -0
  368. package/stories/combobox.lino +10 -0
  369. package/stories/container.lino +11 -0
  370. package/stories/context-menu.lino +15 -0
  371. package/stories/dialog.lino +16 -0
  372. package/stories/drawer.lino +12 -0
  373. package/stories/icon-button.lino +13 -0
  374. package/stories/label.lino +7 -0
  375. package/stories/link-cards.lino +9 -0
  376. package/stories/link.lino +9 -0
  377. package/stories/menu-item.lino +6 -0
  378. package/stories/menu-separator.lino +7 -0
  379. package/stories/menu.lino +30 -0
  380. package/stories/nav-strip.lino +11 -0
  381. package/stories/number-field.lino +16 -0
  382. package/stories/option.lino +10 -0
  383. package/stories/paper.lino +11 -0
  384. package/stories/popover.lino +4 -0
  385. package/stories/pref-switches.lino +4 -0
  386. package/stories/progress.lino +14 -0
  387. package/stories/radial-menu.lino +19 -0
  388. package/stories/radio-group.lino +22 -0
  389. package/stories/radio.lino +10 -0
  390. package/stories/section-nav.lino +9 -0
  391. package/stories/select.lino +16 -0
  392. package/stories/slider.lino +14 -0
  393. package/stories/switch.lino +10 -0
  394. package/stories/tab-panel.lino +7 -0
  395. package/stories/tab.lino +7 -0
  396. package/stories/tabs.lino +25 -0
  397. package/stories/text-area.lino +12 -0
  398. package/stories/text-field.lino +23 -0
  399. package/stories/titled-section.lino +7 -0
  400. package/stories/toast.lino +3 -0
  401. package/stories/toaster.lino +2 -0
  402. package/stories/tooltip.lino +3 -0
@@ -0,0 +1,312 @@
1
+ /**
2
+ * Helpers shared by the layout kinds: reading property values, unique DOM
3
+ * ids, the rendered children of a node, one controller per live node, and
4
+ * typed CSS values through ui-style.
5
+ *
6
+ * The kinds also render into the static document of ui-dom
7
+ * `renderToHTML`, which has no window: there they only write attributes and
8
+ * styles, and no controller is made (`isLive`).
9
+ */
10
+
11
+ import {
12
+ PROPERTIES,
13
+ declarationCss,
14
+ typeDeclaration,
15
+ } from '@link-foundation/ui-style';
16
+ import { cssVariable } from '@link-foundation/ui-tokens';
17
+ import { createLayerManager } from './layer-manager.js';
18
+
19
+ /** Every value of a property, nested lists flattened, as strings. */
20
+ export function valuesOf(el, name) {
21
+ const raw = el.props.get(name);
22
+ if (raw === undefined) {
23
+ return [];
24
+ }
25
+ return raw.flat(Infinity).filter((v) => typeof v === 'string');
26
+ }
27
+
28
+ /** The single text value of a property; `undefined` when it has none. */
29
+ export function atomOf(el, name) {
30
+ const raw = el.props.get(name);
31
+ return raw?.length === 1 && typeof raw[0] === 'string' ? raw[0] : undefined;
32
+ }
33
+
34
+ /** A finite number of a property, else `fallback`. */
35
+ export function numberOf(el, name, fallback) {
36
+ const value = Number(atomOf(el, name));
37
+ return atomOf(el, name) !== undefined && Number.isFinite(value)
38
+ ? value
39
+ : fallback;
40
+ }
41
+
42
+ /** Whether a boolean property is `true` (`fallback` when it is missing). */
43
+ export function flagOf(el, name, fallback = false) {
44
+ const value = atomOf(el, name);
45
+ return value === undefined ? fallback : value === 'true';
46
+ }
47
+
48
+ /**
49
+ * One limit of a split per pane: `(min 20)` is one value for every pane,
50
+ * `(min 20 30)` one value per pane.
51
+ */
52
+ export function listOf(el, name, parse) {
53
+ const items = valuesOf(el, name).map(parse);
54
+ if (items.length === 0) {
55
+ return undefined;
56
+ }
57
+ return items.length === 1 ? items[0] : items;
58
+ }
59
+
60
+ let counter = 0;
61
+
62
+ /** A document-unique id for helper nodes (`ui-layout-N`). */
63
+ export const uid = (prefix = 'ui-layout') => {
64
+ counter += 1;
65
+ return `${prefix}-${counter}`;
66
+ };
67
+
68
+ /** The id of a node, given one when it has none. */
69
+ export function idOf(node, prefix) {
70
+ if (!node.id) {
71
+ node.id = uid(prefix);
72
+ }
73
+ return node.id;
74
+ }
75
+
76
+ /** Whether the kind renders into a live document (with a window). */
77
+ export const isLive = (ctx) =>
78
+ ctx.document.defaultView !== null &&
79
+ ctx.document.defaultView !== undefined &&
80
+ typeof ctx.document.defaultView.MutationObserver === 'function';
81
+
82
+ /** The rendered children of a node: the elements of the store. */
83
+ export const renderedChildren = (node) =>
84
+ [...node.children].filter((child) => child.hasAttribute('data-link-id'));
85
+
86
+ /** The store id of a rendered node. */
87
+ export const linkId = (node) => node.getAttribute('data-link-id');
88
+
89
+ /** The text that names a rendered node (its title, label or id). */
90
+ export const titleOf = (node) =>
91
+ node.getAttribute('data-title') ??
92
+ node.getAttribute('aria-label') ??
93
+ linkId(node) ??
94
+ '';
95
+
96
+ const controllers = new WeakMap();
97
+
98
+ /**
99
+ * The controller of a live node: `make(node)` builds it on the first call,
100
+ * and `dropController` (the kind's `destroy`) disconnects it.
101
+ */
102
+ export function controllerOf(node, make) {
103
+ let controller = controllers.get(node);
104
+ if (controller === undefined) {
105
+ controller = make(node);
106
+ controllers.set(node, controller);
107
+ }
108
+ return controller;
109
+ }
110
+
111
+ /** The controller of a node, when it has one. */
112
+ export const findController = (node) => controllers.get(node);
113
+
114
+ /** Disconnect and forget the controller of a node. */
115
+ export function dropController(node) {
116
+ controllers.get(node)?.disconnect?.();
117
+ controllers.delete(node);
118
+ }
119
+
120
+ /**
121
+ * Call `refresh` once per task after the rendered children of `node`
122
+ * change (added, removed, moved, or one of `attributes` of a child set).
123
+ * The returned `observer.takeRecords()` drops the records of the kind's
124
+ * own changes.
125
+ */
126
+ export function watchChildren(node, refresh, attributes = []) {
127
+ const win = node.ownerDocument.defaultView;
128
+ let pending = false;
129
+ let active = true;
130
+ const observer = new win.MutationObserver((records) => {
131
+ const relevant = records.some(
132
+ (r) =>
133
+ r.type !== 'attributes' ||
134
+ (r.target.parentNode === node && r.target.hasAttribute('data-link-id'))
135
+ );
136
+ if (!active || !relevant || pending) {
137
+ return;
138
+ }
139
+ pending = true;
140
+ win.queueMicrotask(() => {
141
+ pending = false;
142
+ if (!active) {
143
+ return;
144
+ }
145
+ refresh();
146
+ observer.takeRecords();
147
+ });
148
+ });
149
+ const init = { childList: true };
150
+ if (attributes.length > 0) {
151
+ Object.assign(init, {
152
+ attributes: true,
153
+ subtree: true,
154
+ attributeFilter: attributes,
155
+ });
156
+ }
157
+ observer.observe(node, init);
158
+ return {
159
+ takeRecords: () => observer.takeRecords(),
160
+ disconnect() {
161
+ active = false;
162
+ observer.disconnect();
163
+ },
164
+ };
165
+ }
166
+
167
+ /** The CSS context of ui-style `declarationCss`: tokens → `var(--…)`. */
168
+ function cssContext(prefix = '') {
169
+ const refToVar = (ref, fallback) =>
170
+ `var(${cssVariable(ref.path, prefix)}${fallback === undefined ? '' : `, ${fallback}`})`;
171
+ return {
172
+ refToVar,
173
+ variable: (path) => refToVar({ type: 'token-ref', path }),
174
+ };
175
+ }
176
+
177
+ /**
178
+ * Set a typed style property (`gap`, `padding`) of the node from the
179
+ * element, through ui-style: an invalid value is reported and left out.
180
+ */
181
+ export function setTypedStyle(node, el, name, ctx) {
182
+ const css = PROPERTIES[name].css;
183
+ const raws = el.props.get(name);
184
+ if (raws === undefined) {
185
+ node.style.removeProperty(css);
186
+ return;
187
+ }
188
+ const decl = typeDeclaration(name, [...raws], {}, []);
189
+ if (decl === null) {
190
+ node.style.removeProperty(css);
191
+ ctx.diagnostic('invalid-value', 'error', { property: name });
192
+ return;
193
+ }
194
+ node.style.setProperty(
195
+ css,
196
+ declarationCss(decl, cssContext(ctx.options.prefix))
197
+ );
198
+ }
199
+
200
+ /** Set or remove an attribute. */
201
+ export function setAttr(node, name, value) {
202
+ if (value === undefined || value === null || value === false) {
203
+ node.removeAttribute(name);
204
+ } else {
205
+ node.setAttribute(name, value === true ? '' : String(value));
206
+ }
207
+ }
208
+
209
+ /** Replace the text of a node (no markup is ever parsed). */
210
+ export function setText(node, text) {
211
+ const value = text ?? '';
212
+ if (node.textContent === value && node.childNodes.length <= 1) {
213
+ return;
214
+ }
215
+ for (const child of [...node.childNodes]) {
216
+ node.removeChild(child);
217
+ }
218
+ if (value !== '') {
219
+ node.appendChild(node.ownerDocument.createTextNode(value));
220
+ }
221
+ }
222
+
223
+ /** The storage of the render (`options.layout.storage`), or `null`. */
224
+ export function storageOf(ctx) {
225
+ const storage = ctx.options?.layout?.storage;
226
+ return typeof storage?.getItem === 'function' &&
227
+ typeof storage?.setItem === 'function'
228
+ ? storage
229
+ : null;
230
+ }
231
+
232
+ /** Read a stored value; a failing storage reads as nothing. */
233
+ export function readStored(storage, key) {
234
+ try {
235
+ return storage?.getItem(key) ?? null;
236
+ } catch {
237
+ return null;
238
+ }
239
+ }
240
+
241
+ /** Write a stored value; a failing (full, denied) storage is ignored. */
242
+ export function writeStored(storage, key, value) {
243
+ try {
244
+ storage?.setItem(key, value);
245
+ } catch {
246
+ // Persistence is best effort.
247
+ }
248
+ }
249
+
250
+ const defaultLayers = new WeakMap();
251
+
252
+ /**
253
+ * The layer manager of the render (`options.layout.layers`), else one per
254
+ * document, shared by its windows and docks.
255
+ */
256
+ export function layersOf(ctx) {
257
+ const given = ctx.options?.layout?.layers;
258
+ if (given !== undefined) {
259
+ return given;
260
+ }
261
+ let layers = defaultLayers.get(ctx.document);
262
+ if (layers === undefined) {
263
+ layers = createLayerManager({ base: 10 });
264
+ defaultLayers.set(ctx.document, layers);
265
+ }
266
+ return layers;
267
+ }
268
+
269
+ /** Send a bubbling `CustomEvent` from a node. */
270
+ export function emit(node, type, detail) {
271
+ const win = node.ownerDocument.defaultView;
272
+ node.dispatchEvent(
273
+ new win.CustomEvent(type, { bubbles: true, composed: true, detail })
274
+ );
275
+ }
276
+
277
+ /** Make an element with attributes and optional text. */
278
+ export function make(document, tag, attributes = {}, text) {
279
+ const node = document.createElement(tag);
280
+ for (const [name, value] of Object.entries(attributes)) {
281
+ setAttr(node, name, value);
282
+ }
283
+ if (text !== undefined) {
284
+ node.appendChild(document.createTextNode(text));
285
+ }
286
+ return node;
287
+ }
288
+
289
+ /** Move a list of nodes so they appear in this order in `parent`. */
290
+ export function arrange(parent, nodes, before = null) {
291
+ let ref = before;
292
+ for (let i = nodes.length - 1; i >= 0; i -= 1) {
293
+ if (nodes[i].parentNode !== parent || nodes[i].nextSibling !== ref) {
294
+ parent.insertBefore(nodes[i], ref);
295
+ }
296
+ ref = nodes[i];
297
+ }
298
+ }
299
+
300
+ /** Whether the user asked for reduced motion. */
301
+ export const reducedMotion = (win) =>
302
+ win?.matchMedia?.('(prefers-reduced-motion: reduce)').matches === true;
303
+
304
+ /**
305
+ * A `label` property names the node as a group (`role="group"` and
306
+ * `aria-label`); without one the node has no role.
307
+ */
308
+ export function labelGroup(node, el) {
309
+ const label = atomOf(el, 'label');
310
+ setAttr(node, 'role', label === undefined ? undefined : 'group');
311
+ setAttr(node, 'aria-label', label);
312
+ }
@@ -0,0 +1,192 @@
1
+ /**
2
+ * `grid`: a thin wrapper of CSS grid.
3
+ *
4
+ * (page: grid (columns 16rem 1fr) (rows auto 1fr auto) (gap 8px)
5
+ * (areas 'nav header' 'nav main' 'nav footer')
6
+ * (header: text ...) (nav: stack ...) (main: stack ...) ...)
7
+ *
8
+ * `columns` and `rows` are track lists: lengths, `fr`, `auto`,
9
+ * `min-content`, `max-content` and the functions `(repeat N TRACK...)`,
10
+ * `(minmax MIN MAX)` and `(fit-content LENGTH)`. A single number `N`
11
+ * means N equal columns. `areas` are the rows of `grid-template-areas`; a
12
+ * child whose id names an area is placed in it. Every value is checked
13
+ * token by token: a value that is not a track is reported and left out,
14
+ * so no other CSS can be written through them.
15
+ */
16
+
17
+ import { defineKind } from '@link-foundation/ui-dom';
18
+ import {
19
+ controllerOf,
20
+ dropController,
21
+ isLive,
22
+ linkId,
23
+ renderedChildren,
24
+ setTypedStyle,
25
+ watchChildren,
26
+ } from './dom.js';
27
+
28
+ const KEYWORDS = new Set([
29
+ 'auto',
30
+ 'min-content',
31
+ 'max-content',
32
+ 'auto-fill',
33
+ 'auto-fit',
34
+ 'none',
35
+ ]);
36
+ const UNITS = ['px', 'rem', 'em', '%', 'fr', 'vw', 'vh', 'ch', 'dvh', 'dvw'];
37
+ const FUNCTIONS = { repeat: 2, minmax: 2, 'fit-content': 1 };
38
+
39
+ /** Whether a text is a plain number (`12`, `0.5`). */
40
+ function isNumber(text) {
41
+ return text !== '' && Number.isFinite(Number(text)) && !text.includes('e');
42
+ }
43
+
44
+ /** Whether a text is a length, a flex factor or a keyword (one track). */
45
+ export function isTrackAtom(text) {
46
+ if (KEYWORDS.has(text) || text === '0') {
47
+ return true;
48
+ }
49
+ const unit = UNITS.find((u) => text.endsWith(u));
50
+ return (
51
+ unit !== undefined &&
52
+ isNumber(text.slice(0, -unit.length).trim()) &&
53
+ !text.startsWith('-')
54
+ );
55
+ }
56
+
57
+ /** The CSS of one track (`null` when it is not one). */
58
+ function trackCss(value) {
59
+ if (typeof value === 'string') {
60
+ return isTrackAtom(value) ? value : null;
61
+ }
62
+ if (!Array.isArray(value) || typeof value[0] !== 'string') {
63
+ return null;
64
+ }
65
+ const [head, ...args] = value;
66
+ if (!Object.hasOwn(FUNCTIONS, head) || args.length < FUNCTIONS[head]) {
67
+ return null;
68
+ }
69
+ const first =
70
+ head === 'repeat' && (isNumber(args[0]) || KEYWORDS.has(args[0]))
71
+ ? args[0]
72
+ : trackCss(args[0]);
73
+ const rest = args.slice(1).map(trackCss);
74
+ if (first === null || rest.includes(null)) {
75
+ return null;
76
+ }
77
+ return rest.length === 0
78
+ ? `${head}(${first})`
79
+ : `${head}(${first}, ${rest.join(' ')})`;
80
+ }
81
+
82
+ /**
83
+ * The CSS track list of `columns` or `rows`: `null` when a value is not a
84
+ * track, `undefined` when the property is missing.
85
+ */
86
+ export function trackList(raws) {
87
+ if (raws === undefined || raws.length === 0) {
88
+ return undefined;
89
+ }
90
+ if (raws.length === 1 && typeof raws[0] === 'string' && isNumber(raws[0])) {
91
+ const count = Number(raws[0]);
92
+ return Number.isInteger(count) && count > 0
93
+ ? `repeat(${count}, minmax(0, 1fr))`
94
+ : null;
95
+ }
96
+ const tracks = raws.map(trackCss);
97
+ return tracks.includes(null) ? null : tracks.join(' ');
98
+ }
99
+
100
+ const isAreaName = (name) =>
101
+ name === '.' ||
102
+ [...name].every(
103
+ (c) =>
104
+ (c >= 'a' && c <= 'z') ||
105
+ (c >= 'A' && c <= 'Z') ||
106
+ (c >= '0' && c <= '9') ||
107
+ c === '-' ||
108
+ c === '_'
109
+ );
110
+
111
+ /**
112
+ * The areas of the grid: `{ css, names }`, or `null` when a row has a
113
+ * name that is not an area name or rows of different lengths.
114
+ */
115
+ export function areaList(raws) {
116
+ if (raws === undefined || raws.length === 0) {
117
+ return undefined;
118
+ }
119
+ const rows = raws.map((row) =>
120
+ typeof row === 'string' ? row.split(' ').filter((n) => n !== '') : null
121
+ );
122
+ if (
123
+ rows.includes(null) ||
124
+ rows.some((row) => row.length !== rows[0].length || !row.every(isAreaName))
125
+ ) {
126
+ return null;
127
+ }
128
+ const names = new Set(rows.flat().filter((n) => n !== '.'));
129
+ return { css: rows.map((row) => `"${row.join(' ')}"`).join(' '), names };
130
+ }
131
+
132
+ /** Set a template property; a bad value is reported and left out. */
133
+ function setTemplate(node, css, property, value, ctx) {
134
+ if (value === null) {
135
+ ctx.diagnostic('invalid-value', 'error', { property });
136
+ }
137
+ if (value === null || value === undefined) {
138
+ node.style.removeProperty(css);
139
+ } else {
140
+ node.style.setProperty(css, value);
141
+ }
142
+ }
143
+
144
+ /** Place the children named after an area in it. */
145
+ function placeChildren(node, names) {
146
+ for (const child of renderedChildren(node)) {
147
+ if (names.has(linkId(child))) {
148
+ child.style.setProperty('grid-area', linkId(child));
149
+ } else if (child.hasAttribute('data-grid-area')) {
150
+ child.style.removeProperty('grid-area');
151
+ }
152
+ child.toggleAttribute('data-grid-area', names.has(linkId(child)));
153
+ }
154
+ }
155
+
156
+ const areaNames = new WeakMap();
157
+
158
+ defineKind('grid', {
159
+ tag: 'ui-grid',
160
+ create(el, ctx) {
161
+ const node = ctx.document.createElement('ui-grid');
162
+ areaNames.set(node, new Set());
163
+ return node;
164
+ },
165
+ update(node, el, changed, ctx) {
166
+ if (changed.includes('columns')) {
167
+ const value = trackList(el.props.get('columns'));
168
+ setTemplate(node, 'grid-template-columns', 'columns', value, ctx);
169
+ }
170
+ if (changed.includes('rows')) {
171
+ const value = trackList(el.props.get('rows'));
172
+ setTemplate(node, 'grid-template-rows', 'rows', value, ctx);
173
+ }
174
+ if (changed.includes('areas')) {
175
+ const areas = areaList(el.props.get('areas'));
176
+ const css = areas === null ? null : areas?.css;
177
+ setTemplate(node, 'grid-template-areas', 'areas', css, ctx);
178
+ areaNames.set(node, areas?.names ?? new Set());
179
+ }
180
+ for (const name of ['gap', 'padding']) {
181
+ if (changed.includes(name)) {
182
+ setTypedStyle(node, el, name, ctx);
183
+ }
184
+ }
185
+ if (isLive(ctx)) {
186
+ const refresh = () => placeChildren(node, areaNames.get(node));
187
+ controllerOf(node, () => watchChildren(node, refresh));
188
+ refresh();
189
+ }
190
+ },
191
+ destroy: (node) => dropController(node),
192
+ });