gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitcb78a1a0cb78a1a0HM65 report missing heap diagnostic package with repromrecb78a1a0/components/modal.hl

7.1 KB

  1. // modal.hl — THE MODAL DIALOG, pure Hybriel (no JavaScript file): a window over the page with a title, a × and any content; the
  2. // page behind is dimmed, blocked (inert) and does not scroll. `Modal { id = "confirm" title = "Delete it?" p { "…" } }`.
  3. // Built on the browser's own `<dialog>` opened with `showModal()`: the browser makes the page behind inert, puts the focus
  4. // inside and gives it back to the opener when it closes. This file adds what the browser leaves open: Tab / Shift+Tab wrap
  5. // inside the window, Esc, a click on the dim background (switchable: `backdropClose = "false"`), footer buttons, no scrolling
  6. // behind, and ONE close signal with the reason: a bubbling `close` event on `<modal-dialog>` (hl:web binds standard DOM event names
  7. // only, ticket #31), `e.detail.reason` =
  8. // "close" (×), "escape", "backdrop", a footer button's `value` (or its text), or what the host passed to `close(reason)`.
  9. // The host opens and closes it by the dialog's id: `document.getElementById('confirm').showModal()` / `.close('saved')`.
  10. // Several modals may stand on one page: every member is assigned a literal per reference, and the handlers read the
  11. // options from the element (a composed component's members are the page's state — never write them in a handler).
  12. String id = 'modal'
  13. String title = ''
  14. String backdropClose = 'true'
  15. String closeLabel = 'Close'
  16. slot = null
  17. // Look: black, white and the greys #555 #999 #ddd #f2f2f2 (the library's base, named in shared/base.hl; README "Look"); every colour is a
  18. // semantic token with its base value as fallback, so a bare copy looks right and an app that declares the tokens (layouts.worldapi.org's theme)
  19. // restyles it. No frames: the grey dimmed page around the white window is the separation. Buttons have no border (the creator's rule); the footer's last button is the main one (accent). The
  20. // footer sticks to the bottom of a long content.
  21. Style {
  22. 'html:has(modal-dialog > dialog[open])' {
  23. overflow = 'hidden'
  24. }
  25. modalDialog {
  26. display = 'contents'
  27. '>' {
  28. dialog {
  29. boxSizing = 'border-box'
  30. width = 'min(34rem, calc(100vw - 2rem))'
  31. maxWidth = 'none'
  32. maxHeight = 'calc(100dvh - 2rem)'
  33. margin = 'auto'
  34. padding = '0'
  35. color = 'var(--color-text, #000)'
  36. background = 'var(--color-background, #fff)'
  37. border = '0'
  38. borderRadius = '.6rem'
  39. overscrollBehavior = 'contain'
  40. focus {
  41. outline = 'none'
  42. }
  43. }
  44. 'dialog[open]' {
  45. display = 'flex'
  46. flexDirection = 'column'
  47. }
  48. 'dialog::backdrop' {
  49. background = 'var(--color-background-sunken, #555)'
  50. opacity = '.75'
  51. }
  52. }
  53. header {
  54. display = 'flex'
  55. alignItems = 'center'
  56. gap = '.5rem'
  57. padding = '.6rem .6rem .6rem 1rem'
  58. borderBottom = '0'
  59. }
  60. h2 {
  61. flex = '1'
  62. margin = '0'
  63. fontSize = '1.1rem'
  64. lineHeight = '1.3'
  65. color = 'var(--color-text-strong, #000)'
  66. overflowWrap = 'anywhere'
  67. }
  68. button {
  69. font = 'inherit'
  70. fontWeight = '600'
  71. minHeight = '2.75rem'
  72. padding = '.45rem .9rem'
  73. color = 'var(--color-text, #000)'
  74. background = 'transparent'
  75. border = '0'
  76. borderRadius = '.4rem'
  77. cursor = 'pointer'
  78. hover {
  79. background = 'var(--color-surface, #f2f2f2)'
  80. }
  81. focusVisible {
  82. outline = '2px solid var(--color-accent, #555)'
  83. outlineOffset = '1px'
  84. }
  85. }
  86. 'button.modal-x' {
  87. flex = 'none'
  88. width = '2.75rem'
  89. height = '2.75rem'
  90. padding = '0'
  91. fontSize = '1.5rem'
  92. lineHeight = '1'
  93. color = 'var(--color-text-muted, #555)'
  94. hover {
  95. color = 'var(--color-text-strong, #000)'
  96. }
  97. }
  98. modalBody {
  99. display = 'block'
  100. flex = '1'
  101. minHeight = '0'
  102. overflow = 'auto'
  103. padding = '1rem'
  104. overflowWrap = 'anywhere'
  105. '>' {
  106. ':first-child' {
  107. marginTop = '0'
  108. }
  109. ':last-child:not(footer)' {
  110. marginBottom = '0'
  111. }
  112. }
  113. footer {
  114. position = 'sticky'
  115. bottom = '-1rem'
  116. display = 'flex'
  117. flexWrap = 'wrap'
  118. justifyContent = 'flex-end'
  119. gap = '.5rem'
  120. margin = '1rem -1rem -1rem'
  121. padding = '.75rem 1rem'
  122. background = 'var(--color-background-sunken, #f2f2f2)'
  123. borderTop = '0'
  124. button {
  125. lastChild {
  126. color = 'var(--color-accent-text, #fff)'
  127. background = 'var(--color-accent, #000)'
  128. hover {
  129. filter = 'brightness(1.1)'
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }
  137. View {
  138. modalDialog {
  139. dialog { id = id 'aria-label' = title 'data-backdrop' = backdropClose on close(e) { emit modalClosed(e) } on cancel(e) { emit modalCancel(e) } on keydown(e) { emit modalKey(e) } on pointerdown(e) { emit modalDown(e) } on click(e) { emit modalClick(e) }
  140. header {
  141. h2 { title }
  142. button { type = "button" class = "modal-x" title = closeLabel 'aria-label' = closeLabel "×" on click(e) { emit modalX(e) } }
  143. }
  144. modalBody { slot }
  145. }
  146. }
  147. }
  148. dialogOf(node) { return node.closest('modal-dialog').querySelector('dialog') }
  149. // what can take the focus inside the window, in Tab order (enough for forms and buttons; no positive tabindex)
  150. focusables(d) {
  151. all = Array.from(d.querySelectorAll('button, [href], input, select, textarea, summary, [tabindex], [contenteditable="true"]'))
  152. out = []
  153. for (el of all) {
  154. if (!el.disabled && el.getAttribute('tabindex') != '-1' && el.getClientRects().length > 0) { out.push(el) }
  155. }
  156. return out
  157. }
  158. // every way out ends here: the browser's `close` event, with the reason in the dialog's returnValue ('' = the browser closed it by
  159. // itself, which only Esc does); the reason goes out as a bubbling `close` on <modal-dialog> (the dialog's own `close` does not bubble;
  160. // a modal inside this one's content sends its bubbling one past here, so only this dialog's own counts)
  161. on modalClosed(e) {
  162. d = e.target
  163. if (d != e.currentTarget) { return }
  164. reason = d.returnValue != '' ? d.returnValue : 'escape'
  165. d.returnValue = ''
  166. d.parentNode.dispatchEvent(new CustomEvent('close', { bubbles = true detail = { id = d.id reason = reason } }))
  167. }
  168. // Esc: the browser cancels and closes; the reason is set before it does
  169. on modalCancel(e) {
  170. if (e.target.tagName == 'DIALOG') { e.target.returnValue = 'escape' }
  171. }
  172. on modalX(e) { dialogOf(e.target).close('close') }
  173. // Tab / Shift+Tab go round inside the window (without this the focus would leave to the browser's own bar)
  174. on modalKey(e) {
  175. if (e.key != 'Tab') { return }
  176. d = dialogOf(e.target)
  177. f = focusables(d)
  178. if (f.length == 0) { e.preventDefault() d.focus() return }
  179. first = f[0]
  180. last = f[f.length - 1]
  181. at = e.target
  182. if (e.shiftKey && (at == first || at == d || !d.contains(at))) { e.preventDefault() last.focus() }
  183. if (!e.shiftKey && (at == last || !d.contains(at))) { e.preventDefault() first.focus() }
  184. }
  185. // a press on the dim background lands on the dialog element itself (its content covers the rest of it); the press must also start
  186. // there, so a text selection dragged out of the window does not close it
  187. on modalDown(e) {
  188. d = dialogOf(e.target)
  189. d.modalDownOut = e.target == d
  190. }
  191. on modalClick(e) {
  192. d = dialogOf(e.target)
  193. if (e.target == d) {
  194. if (d.modalDownOut && d.getAttribute('data-backdrop') != 'false') { d.close('backdrop') }
  195. return
  196. }
  197. // a button in the content's footer closes it; its value (or its text) is the reason
  198. b = e.target.closest('footer button')
  199. if (b != null && d.contains(b)) {
  200. r = b.value != '' ? b.value : b.textContent.trim()
  201. d.close(r)
  202. }
  203. }

Branches

Latest commits

  • cb78a1a0HM65 report missing heap diagnostic package with repromre
  • 89606a5cHM65 briefmre
  • 096ce0d4log: DP69 live on day48mre
  • b3d8a80bDP69 brief, RV48 mergedmre
  • ce6d7d32Merge rv48: Hybriel day48mre
  • 3da2ea86RV48: report day48 cold starts, gates and cleanupmre
  • 8d9ecdfeRV48: re-vendor Hybriel set day48 (source b6a8f429, bin 660a9524)mre
  • 4a802ae7status: RV48 runningmre
  • 0a5412b6RV48 brief, day48 outmre
  • 56665efelog: CB69 mergedmre
  • 05448a98Merge cb69-imports: appTitle in a dependency-free topicmre
  • e55b379dKeep shared app title independent of the route import graphmre
  • 9a99659blog: hybriel#334, batch waits for day48mre
  • 85fa6c9blog: CB68 result, CB69 startedmre
  • 2f07c1b7log: DP68 boot gate failed, CB68mre
  • 9652782dlog: HM59 merged, DP68 startedmre
  • 29867a75Merge hm59-logic: tracker#59 page logic moved to libmre
  • f7bfe90bMove remaining page builders into their topicsmre
  • 7c3991f9log: day46 from Antonmre
  • ec54fc35Move show page logic into the shows topicmre