@link-foundation/ui-components 0.0.0-stage → 0.0.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,88 @@
1
+ /**
2
+ * Announcements for screen readers through one shared live region per
3
+ * document: a visually hidden container with a polite region
4
+ * (`role="status"`) and an assertive one (`role="alert"`). Each message is
5
+ * a new node, so the same text said twice is announced twice.
6
+ */
7
+
8
+ const REGION_ID = 'ui-live-region';
9
+
10
+ /** Visually hidden but read by screen readers. */
11
+ const VISUALLY_HIDDEN = [
12
+ 'position: absolute',
13
+ 'width: 1px',
14
+ 'height: 1px',
15
+ 'margin: -1px',
16
+ 'padding: 0',
17
+ 'border: 0',
18
+ 'overflow: hidden',
19
+ 'clip: rect(0 0 0 0)',
20
+ 'clip-path: inset(50%)',
21
+ 'white-space: nowrap',
22
+ ].join('; ');
23
+
24
+ const POLITENESS = {
25
+ polite: 'status',
26
+ assertive: 'alert',
27
+ };
28
+
29
+ /**
30
+ * The shared live region of a document, created on first use.
31
+ * @param {Document} document
32
+ * @returns {HTMLElement}
33
+ */
34
+ export function liveRegion(document) {
35
+ const found = document.getElementById(REGION_ID);
36
+ if (found) {
37
+ return found;
38
+ }
39
+ const container = document.createElement('div');
40
+ container.id = REGION_ID;
41
+ container.setAttribute('style', VISUALLY_HIDDEN);
42
+ for (const [politeness, role] of Object.entries(POLITENESS)) {
43
+ const region = document.createElement('div');
44
+ region.setAttribute('role', role);
45
+ region.setAttribute('aria-live', politeness);
46
+ region.setAttribute('aria-atomic', 'true');
47
+ region.dataset.politeness = politeness;
48
+ container.appendChild(region);
49
+ }
50
+ document.body.appendChild(container);
51
+ return container;
52
+ }
53
+
54
+ /**
55
+ * Announce a message.
56
+ * @param {string} message
57
+ * @param {{politeness?: 'polite'|'assertive', document?: Document}} [options]
58
+ * `politeness` (default `polite`); `document` (default the global one)
59
+ * @returns {HTMLElement} the node that holds the message
60
+ */
61
+ export function announce(message, options = {}) {
62
+ const { politeness = 'polite' } = options;
63
+ if (!Object.hasOwn(POLITENESS, politeness)) {
64
+ throw new TypeError(
65
+ `announce: politeness must be "polite" or "assertive", not ${JSON.stringify(politeness)}`
66
+ );
67
+ }
68
+ const document = options.document ?? globalThis.document;
69
+ const region = liveRegion(document).querySelector(
70
+ `[data-politeness="${politeness}"]`
71
+ );
72
+ const node = document.createElement('div');
73
+ node.textContent = String(message);
74
+ region.replaceChildren(node);
75
+ return node;
76
+ }
77
+
78
+ /**
79
+ * Remove the messages of the live region (the region itself stays, as
80
+ * screen readers track it).
81
+ * @param {Document} [document]
82
+ */
83
+ export function clearAnnouncements(document = globalThis.document) {
84
+ const container = document.getElementById(REGION_ID);
85
+ for (const region of container?.children ?? []) {
86
+ region.replaceChildren();
87
+ }
88
+ }
@@ -0,0 +1,181 @@
1
+ /**
2
+ * `bindMachine(root, service, parts)`: connect a running machine to DOM
3
+ * elements. Every part of the machine gets its `(attr ...)` values (kept
4
+ * in sync with the state and context), its `(key KEY EVENT)` bindings and
5
+ * its `(on EVENT SEND)` UI events, and the `focus` action of the machine
6
+ * focuses a part element.
7
+ */
8
+
9
+ import { diagnostic } from '../behavior/diagnostics.js';
10
+ import { firesInField, isTextField, keyOf, normalizeKey } from './keys.js';
11
+
12
+ /** UI events of a part → DOM events (as in ui-dom). */
13
+ const DOM_EVENTS = {
14
+ press: 'click',
15
+ focus: 'focusin',
16
+ blur: 'focusout',
17
+ hover: 'pointerenter',
18
+ };
19
+
20
+ /**
21
+ * The part elements: the `parts` given, else `[data-part="NAME"]` in the
22
+ * root; the part `root` defaults to the root itself.
23
+ */
24
+ function resolveParts(root, service, parts) {
25
+ const resolved = {};
26
+ for (const name of Object.keys(service.machine.parts)) {
27
+ const given = typeof parts === 'function' ? parts(name) : parts?.[name];
28
+ resolved[name] =
29
+ given ??
30
+ (name === 'root'
31
+ ? root
32
+ : root.querySelector(`[data-part="${CSS_ESCAPE(name)}"]`));
33
+ }
34
+ return resolved;
35
+ }
36
+
37
+ /** Escape a part name for an attribute selector (names are LiNo atoms). */
38
+ const CSS_ESCAPE = (name) => String(name).replace(/["\\]/g, '\\$&');
39
+
40
+ /**
41
+ * Set attributes to the current values, and remove the ones that are gone.
42
+ * @param {HTMLElement} element
43
+ * @param {Record<string, string>} attrs
44
+ * @param {Set<string>} owned the attributes set before
45
+ */
46
+ function applyAttrs(element, attrs, owned) {
47
+ for (const name of owned) {
48
+ if (!Object.hasOwn(attrs, name)) {
49
+ element.removeAttribute(name);
50
+ owned.delete(name);
51
+ }
52
+ }
53
+ for (const [name, value] of Object.entries(attrs)) {
54
+ if (name.toLowerCase().startsWith('on')) {
55
+ continue; // an on* attribute would run its value as code
56
+ }
57
+ if (element.getAttribute(name) !== value) {
58
+ element.setAttribute(name, value);
59
+ }
60
+ owned.add(name);
61
+ }
62
+ }
63
+
64
+ /**
65
+ * @param {HTMLElement} root the element of the component
66
+ * @param {import('../behavior/interpret.js').Service} service
67
+ * @param {Record<string, HTMLElement|null|undefined> |
68
+ * ((part: string) => HTMLElement|null|undefined)} [parts] the part
69
+ * elements by name (default: `[data-part]` in the root)
70
+ * @param {{onDiagnostic?: (d: object) => void, ignoreInFields?: boolean}}
71
+ * [options]
72
+ * @returns {() => void} unbind: removes the listeners and the effects
73
+ * (the attributes stay as they are)
74
+ */
75
+ export function bindMachine(root, service, parts, options = {}) {
76
+ const { ignoreInFields = true } = options;
77
+ const elements = resolveParts(root, service, parts);
78
+ const cleanups = [];
79
+ const owned = new Map();
80
+
81
+ for (const [name, spec] of Object.entries(service.machine.parts)) {
82
+ const element = elements[name];
83
+ if (!element) {
84
+ options.onDiagnostic?.(
85
+ diagnostic('unknown-part', 'warning', {
86
+ machine: service.machine.id,
87
+ part: name,
88
+ })
89
+ );
90
+ continue;
91
+ }
92
+ owned.set(name, new Set());
93
+ if (!element.hasAttribute('data-part')) {
94
+ element.setAttribute('data-part', name);
95
+ }
96
+
97
+ const keys = new Map(
98
+ spec.keys.map(({ key, event }) => [normalizeKey(key), event])
99
+ );
100
+ if (keys.size > 0) {
101
+ const onKey = (event) => {
102
+ const combo = keyOf(event);
103
+ if (!keys.has(combo) || event.defaultPrevented || event.isComposing) {
104
+ return;
105
+ }
106
+ if (
107
+ ignoreInFields &&
108
+ isTextField(event.target) &&
109
+ !firesInField(combo)
110
+ ) {
111
+ return;
112
+ }
113
+ // The machine handles the key: a native button must not also
114
+ // turn it into a click (Enter on keydown, Space on keyup).
115
+ event.preventDefault();
116
+ if (event.type === 'keydown') {
117
+ service.send({ type: keys.get(combo), key: combo, part: name });
118
+ }
119
+ };
120
+ element.addEventListener('keydown', onKey);
121
+ element.addEventListener('keyup', onKey);
122
+ cleanups.push(() => {
123
+ element.removeEventListener('keydown', onKey);
124
+ element.removeEventListener('keyup', onKey);
125
+ });
126
+ }
127
+
128
+ for (const { event, send } of spec.events) {
129
+ const type = DOM_EVENTS[event] ?? event;
130
+ const listener = () => service.send({ type: send, part: name });
131
+ element.addEventListener(type, listener);
132
+ cleanups.push(() => element.removeEventListener(type, listener));
133
+ }
134
+ }
135
+
136
+ const sync = () => {
137
+ for (const [name, attrs] of owned) {
138
+ applyAttrs(elements[name], service.attrs(name), attrs);
139
+ }
140
+ };
141
+ sync();
142
+ cleanups.push(service.subscribe(sync));
143
+
144
+ cleanups.push(
145
+ service.use({
146
+ focus(target) {
147
+ const element = elements[target] ?? null;
148
+ if (!element) {
149
+ options.onDiagnostic?.(
150
+ diagnostic('unknown-part', 'warning', {
151
+ machine: service.machine.id,
152
+ part: target,
153
+ })
154
+ );
155
+ return false;
156
+ }
157
+ // Make the part focusable by script without adding it to the Tab
158
+ // order (content regions, dialogs).
159
+ if (element.tabIndex < 0 && !element.hasAttribute('tabindex')) {
160
+ element.setAttribute('tabindex', '-1');
161
+ }
162
+ // Apply the attributes first: a part hidden a moment ago cannot
163
+ // take focus.
164
+ sync();
165
+ element.focus();
166
+ return true;
167
+ },
168
+ })
169
+ );
170
+
171
+ let bound = true;
172
+ return () => {
173
+ if (!bound) {
174
+ return;
175
+ }
176
+ bound = false;
177
+ for (const cleanup of cleanups.reverse()) {
178
+ cleanup();
179
+ }
180
+ };
181
+ }
@@ -0,0 +1,169 @@
1
+ /**
2
+ * Focus management: a focus trap for modal content and focus restoration.
3
+ * The trap moves focus itself on Tab and Shift+Tab, so it cycles in the
4
+ * same order in every browser (and in jsdom).
5
+ */
6
+
7
+ /** Elements that can take focus with the keyboard, before filtering. */
8
+ export const FOCUSABLE = [
9
+ 'a[href]',
10
+ 'area[href]',
11
+ 'button',
12
+ 'input',
13
+ 'select',
14
+ 'textarea',
15
+ 'iframe',
16
+ 'audio[controls]',
17
+ 'video[controls]',
18
+ 'summary',
19
+ '[contenteditable]',
20
+ '[tabindex]',
21
+ ].join(',');
22
+
23
+ /** Whether an element or an ancestor is hidden, inert or disabled. */
24
+ function isUnavailable(element) {
25
+ if (element.disabled === true || element.getAttribute('tabindex') === '-1') {
26
+ return true;
27
+ }
28
+ if (element.localName === 'input' && element.type === 'hidden') {
29
+ return true;
30
+ }
31
+ for (
32
+ let node = element;
33
+ node && node.nodeType === 1;
34
+ node = node.parentElement
35
+ ) {
36
+ if (node.hidden || node.inert || node.hasAttribute('inert')) {
37
+ return true;
38
+ }
39
+ if (node.getAttribute('aria-hidden') === 'true') {
40
+ return true;
41
+ }
42
+ }
43
+ return false;
44
+ }
45
+
46
+ /**
47
+ * The elements of a container that Tab reaches, in Tab order: positive
48
+ * `tabindex` first (ascending), then document order.
49
+ * @param {Element} container
50
+ * @returns {HTMLElement[]}
51
+ */
52
+ export function tabbables(container) {
53
+ const found = [...container.querySelectorAll(FOCUSABLE)].filter(
54
+ (element) =>
55
+ !isUnavailable(element) &&
56
+ element.getAttribute('contenteditable') !== 'false'
57
+ );
58
+ const order = (element) => Math.max(0, element.tabIndex || 0);
59
+ return found
60
+ .map((element, index) => ({ element, index }))
61
+ .sort((a, b) => {
62
+ const [x, y] = [order(a.element), order(b.element)];
63
+ if (x === y) {
64
+ return a.index - b.index;
65
+ }
66
+ if (x === 0 || y === 0) {
67
+ return x === 0 ? 1 : -1;
68
+ }
69
+ return x - y;
70
+ })
71
+ .map(({ element }) => element);
72
+ }
73
+
74
+ /**
75
+ * Focus an element if it is still in the document; returns whether it was
76
+ * focused.
77
+ * @param {Element|null|undefined} previous
78
+ */
79
+ export function restoreFocus(previous) {
80
+ if (
81
+ !previous ||
82
+ !previous.isConnected ||
83
+ typeof previous.focus !== 'function'
84
+ ) {
85
+ return false;
86
+ }
87
+ previous.focus();
88
+ return previous.ownerDocument.activeElement === previous;
89
+ }
90
+
91
+ /**
92
+ * Keep keyboard focus inside a container: Tab after the last tabbable
93
+ * element goes to the first one, Shift+Tab before the first goes to the
94
+ * last, and focus that leaves the container (a click outside, a script)
95
+ * comes back. Focus moves into the container at once.
96
+ * @param {HTMLElement} container
97
+ * @param {{initialFocus?: HTMLElement|null, restore?: boolean}} [options]
98
+ * `initialFocus` (default: the first tabbable element, else the
99
+ * container); `restore` (default `true`): `release` focuses the element
100
+ * that had focus before the trap
101
+ * @returns {() => void} release
102
+ */
103
+ export function trapFocus(container, options = {}) {
104
+ const document = container.ownerDocument;
105
+ const previous = document.activeElement;
106
+ const { restore = true } = options;
107
+
108
+ const focusEdge = (last) => {
109
+ const items = tabbables(container);
110
+ const target = last ? items.at(-1) : items[0];
111
+ if (target) {
112
+ target.focus();
113
+ } else {
114
+ if (!container.hasAttribute('tabindex')) {
115
+ container.setAttribute('tabindex', '-1');
116
+ }
117
+ container.focus();
118
+ }
119
+ };
120
+
121
+ const onKeyDown = (event) => {
122
+ if (event.key !== 'Tab' || event.ctrlKey || event.altKey || event.metaKey) {
123
+ return;
124
+ }
125
+ const items = tabbables(container);
126
+ event.preventDefault();
127
+ if (items.length === 0) {
128
+ focusEdge(false);
129
+ return;
130
+ }
131
+ const index = items.indexOf(document.activeElement);
132
+ const step = event.shiftKey ? -1 : 1;
133
+ const next =
134
+ index === -1
135
+ ? event.shiftKey
136
+ ? items.length - 1
137
+ : 0
138
+ : (index + step + items.length) % items.length;
139
+ items[next].focus();
140
+ };
141
+
142
+ const onFocusIn = (event) => {
143
+ if (!container.contains(event.target)) {
144
+ focusEdge(false);
145
+ }
146
+ };
147
+
148
+ container.addEventListener('keydown', onKeyDown);
149
+ document.addEventListener('focusin', onFocusIn);
150
+ const initial = options.initialFocus;
151
+ if (initial && container.contains(initial)) {
152
+ initial.focus();
153
+ } else if (!container.contains(document.activeElement)) {
154
+ focusEdge(false);
155
+ }
156
+
157
+ let released = false;
158
+ return () => {
159
+ if (released) {
160
+ return;
161
+ }
162
+ released = true;
163
+ container.removeEventListener('keydown', onKeyDown);
164
+ document.removeEventListener('focusin', onFocusIn);
165
+ if (restore) {
166
+ restoreFocus(previous);
167
+ }
168
+ };
169
+ }
@@ -0,0 +1,89 @@
1
+ import type {
2
+ Diagnostic,
3
+ Service,
4
+ } from '@link-foundation/ui-components/behavior';
5
+
6
+ export const FOCUSABLE: string;
7
+ export function tabbables(container: Element): HTMLElement[];
8
+ export function restoreFocus(previous: Element | null | undefined): boolean;
9
+ export function trapFocus(
10
+ container: HTMLElement,
11
+ options?: { initialFocus?: HTMLElement | null; restore?: boolean }
12
+ ): () => void;
13
+
14
+ export interface RovingTabindex {
15
+ readonly index: number;
16
+ readonly item: HTMLElement | null;
17
+ focus(index: number): boolean;
18
+ /** Make an item the tab stop without focusing it. */
19
+ select(index: number): boolean;
20
+ update(): void;
21
+ handleKeyDown(event: KeyboardEvent): boolean;
22
+ attach(container: HTMLElement): () => void;
23
+ }
24
+
25
+ export function createRovingTabindex(
26
+ items: HTMLElement[] | (() => HTMLElement[]),
27
+ options?: {
28
+ orientation?: 'horizontal' | 'vertical' | 'both';
29
+ loop?: boolean;
30
+ initial?: number;
31
+ rtl?: boolean;
32
+ onChange?: (index: number, item: HTMLElement) => void;
33
+ }
34
+ ): RovingTabindex;
35
+
36
+ export interface Typeahead {
37
+ readonly query: string;
38
+ handleKey(key: string, current?: number): number;
39
+ reset(): void;
40
+ }
41
+
42
+ export function createTypeahead(
43
+ getLabels: () => readonly string[],
44
+ options?: {
45
+ timeout?: number;
46
+ clock?: {
47
+ setTimeout(fn: () => void, ms: number): unknown;
48
+ clearTimeout(handle: unknown): void;
49
+ };
50
+ locale?: string;
51
+ }
52
+ ): Typeahead;
53
+
54
+ export interface KeyLike {
55
+ key: string;
56
+ ctrlKey?: boolean;
57
+ altKey?: boolean;
58
+ shiftKey?: boolean;
59
+ metaKey?: boolean;
60
+ }
61
+
62
+ export function keyOf(event: KeyLike): string;
63
+ export function normalizeKey(combo: string): string;
64
+ export function isTextField(element: EventTarget | null | undefined): boolean;
65
+ export function firesInField(combo: string): boolean;
66
+ export function bindKeys(
67
+ element: EventTarget,
68
+ map: Record<string, (event: KeyboardEvent) => unknown>,
69
+ options?: { ignoreInFields?: boolean; type?: 'keydown' | 'keyup' }
70
+ ): () => void;
71
+
72
+ export function liveRegion(document: Document): HTMLElement;
73
+ export function announce(
74
+ message: string,
75
+ options?: { politeness?: 'polite' | 'assertive'; document?: Document }
76
+ ): HTMLElement;
77
+ export function clearAnnouncements(document?: Document): void;
78
+
79
+ export function bindMachine(
80
+ root: HTMLElement,
81
+ service: Service,
82
+ parts?:
83
+ | Record<string, HTMLElement | null | undefined>
84
+ | ((part: string) => HTMLElement | null | undefined),
85
+ options?: {
86
+ onDiagnostic?: (diagnostic: Diagnostic) => void;
87
+ ignoreInFields?: boolean;
88
+ }
89
+ ): () => void;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * `@link-foundation/ui-components/a11y`: DOM accessibility helpers —
3
+ * focus trap and restoration, roving tabindex, typeahead, key bindings,
4
+ * a shared live region, and `bindMachine` to connect a behaviour machine
5
+ * to elements.
6
+ */
7
+
8
+ export { trapFocus, restoreFocus, tabbables, FOCUSABLE } from './focus.js';
9
+ export { createRovingTabindex } from './roving.js';
10
+ export { createTypeahead } from './typeahead.js';
11
+ export {
12
+ bindKeys,
13
+ keyOf,
14
+ normalizeKey,
15
+ isTextField,
16
+ firesInField,
17
+ } from './keys.js';
18
+ export { announce, clearAnnouncements, liveRegion } from './announce.js';
19
+ export { bindMachine } from './bind-machine.js';