gitoria

layouts

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit04d36fcf04d36fcf|||PR Type function parameters and drop unneeded let (HL-STANDARD L2, L10)Atlas04d36fcf/components/modal/modal.hl

8.0 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. // No language form for direct-child combinator, :not/:has/:where condition.
  23. 'html:has(modal-dialog > dialog[open])' { overflow = 'hidden' }
  24. modalDialog {
  25. display = 'contents'
  26. header {
  27. display = 'flex'
  28. alignItems = 'center'
  29. gap = '.5rem'
  30. padding = '.6rem .6rem .6rem 1rem'
  31. borderBottom = '0'
  32. }
  33. h2 {
  34. flex = '1'
  35. margin = '0'
  36. fontSize = '1.1rem'
  37. lineHeight = '1.3'
  38. color = 'var(--color-text-strong, #000)'
  39. overflowWrap = 'anywhere'
  40. }
  41. button {
  42. font = 'inherit'
  43. fontWeight = '600'
  44. minHeight = '2.75rem'
  45. padding = '.45rem .9rem'
  46. color = 'var(--color-text, #000)'
  47. background = 'transparent'
  48. border = '0'
  49. borderRadius = '.4rem'
  50. cursor = 'pointer'
  51. hover { background = 'var(--color-surface, #f2f2f2)' }
  52. focusVisible {
  53. outline = '2px solid var(--color-accent, #555)'
  54. outlineOffset = '1px'
  55. }
  56. }
  57. // Class or attribute assigned by the View or code; no compound-selector form.
  58. 'button.modal-x' {
  59. flex = 'none'
  60. width = '2.75rem'
  61. height = '2.75rem'
  62. padding = '0'
  63. fontSize = '1.5rem'
  64. lineHeight = '1'
  65. color = 'var(--color-text-muted, #555)'
  66. hover { color = 'var(--color-text-strong, #000)' }
  67. }
  68. modalBody {
  69. display = 'block'
  70. flex = '1'
  71. minHeight = '0'
  72. overflow = 'auto'
  73. padding = '1rem'
  74. overflowWrap = 'anywhere'
  75. }
  76. }
  77. // No language form for direct-child combinator.
  78. 'modal-dialog > dialog' {
  79. boxSizing = 'border-box'
  80. width = 'min(34rem, calc(100vw - 2rem))'
  81. maxWidth = 'none'
  82. maxHeight = 'calc(100dvh - 2rem)'
  83. margin = 'auto'
  84. padding = '0'
  85. color = 'var(--color-text, #000)'
  86. background = 'var(--color-background, #fff)'
  87. border = '0'
  88. borderRadius = '.6rem'
  89. overscrollBehavior = 'contain'
  90. }
  91. // No language form for direct-child combinator.
  92. 'modal-dialog > dialog[open]' {
  93. display = 'flex'
  94. flexDirection = 'column'
  95. }
  96. // No language form for direct-child combinator, pseudo-element.
  97. 'modal-dialog > dialog::backdrop' {
  98. background = 'var(--color-background-sunken, #555)'
  99. opacity = '.75'
  100. }
  101. // No language form for direct-child combinator.
  102. 'modal-dialog > dialog:focus' { outline = 'none' }
  103. // No language form for direct-child combinator.
  104. 'modal-dialog modal-body > :first-child' { marginTop = '0' }
  105. // No language form for direct-child combinator, :not/:has/:where condition.
  106. 'modal-dialog modal-body > :last-child:not(footer)' { marginBottom = '0' }
  107. // No language form for direct-child combinator.
  108. 'modal-dialog modal-body > footer' {
  109. position = 'sticky'
  110. bottom = '-1rem'
  111. display = 'flex'
  112. flexWrap = 'wrap'
  113. justifyContent = 'flex-end'
  114. gap = '.5rem'
  115. margin = '1rem -1rem -1rem'
  116. padding = '.75rem 1rem'
  117. background = 'var(--color-background-sunken, #f2f2f2)'
  118. borderTop = '0'
  119. }
  120. // No language form for direct-child combinator.
  121. 'modal-dialog modal-body > footer button:last-child' {
  122. color = 'var(--color-accent-text, #fff)'
  123. background = 'var(--color-accent, #000)'
  124. }
  125. // No language form for direct-child combinator.
  126. 'modal-dialog modal-body > footer button:last-child:hover' { filter = 'brightness(1.1)' }
  127. }
  128. View {
  129. modalDialog {
  130. 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) }
  131. header {
  132. h2 { title }
  133. button { type = "button" class = "modal-x" title = closeLabel 'aria-label' = closeLabel "×" on click(e) { emit modalX(e) } }
  134. }
  135. modalBody { slot }
  136. }
  137. }
  138. }
  139. dialogOf(node) { return node.closest('modal-dialog').querySelector('dialog') }
  140. // what can take the focus inside the window, in Tab order (enough for forms and buttons; no positive tabindex)
  141. focusables(d) {
  142. all = Array.from(d.querySelectorAll('button, [href], input, select, textarea, summary, [tabindex], [contenteditable="true"]'))
  143. out = []
  144. for (el of all) {
  145. if (!el.disabled && el.getAttribute('tabindex') != '-1' && el.getClientRects().length > 0) { out.push(el) }
  146. }
  147. return out
  148. }
  149. // every way out ends here: the browser's `close` event, with the reason in the dialog's returnValue ('' = the browser closed it by
  150. // itself, which only Esc does); the reason goes out as a bubbling `close` on <modal-dialog> (the dialog's own `close` does not bubble;
  151. // a modal inside this one's content sends its bubbling one past here, so only this dialog's own counts)
  152. on modalClosed(e) {
  153. d = e.target
  154. if (d != e.currentTarget) { return }
  155. let reason = d.returnValue != '' ? d.returnValue : 'escape'
  156. d.returnValue = ''
  157. d.parentNode.dispatchEvent(new CustomEvent('close', { bubbles = true detail = { id = d.id reason = reason } }))
  158. }
  159. // Esc: the browser cancels and closes; the reason is set before it does
  160. on modalCancel(e) {
  161. if (e.target.tagName == 'DIALOG') { e.target.returnValue = 'escape' }
  162. }
  163. on modalX(e) { dialogOf(e.target).close('close') }
  164. // Tab / Shift+Tab go round inside the window (without this the focus would leave to the browser's own bar)
  165. on modalKey(e) {
  166. if (e.key != 'Tab') { return }
  167. d = dialogOf(e.target)
  168. f = focusables(d)
  169. if (f.length == 0) { e.preventDefault() d.focus() return }
  170. first = f[0]
  171. last = f[f.length - 1]
  172. at = document.activeElement
  173. if (e.shiftKey && (at == first || at == d || !d.contains(at))) { e.preventDefault() last.focus() }
  174. if (!e.shiftKey && (at == last || !d.contains(at))) { e.preventDefault() first.focus() }
  175. }
  176. // a press on the dim background lands on the dialog element itself (its content covers the rest of it); the press must also start
  177. // there, so a text selection dragged out of the window does not close it
  178. on modalDown(e) {
  179. d = dialogOf(e.target)
  180. d.modalDownOut = e.target == d
  181. }
  182. on modalClick(e) {
  183. d = dialogOf(e.target)
  184. if (e.target == d) {
  185. if (d.modalDownOut && d.getAttribute('data-backdrop') != 'false') { d.close('backdrop') }
  186. return
  187. }
  188. // a button in the content's own footer (a direct child of the body: an editor's footer inside the content is not it) closes it; its value (or its text) is the reason
  189. b = e.target.closest('footer button')
  190. if (b != null && d.contains(b) && b.closest('footer').parentElement.tagName == 'MODAL-BODY') {
  191. r = b.value != '' ? b.value : b.textContent.trim()
  192. d.close(r)
  193. }
  194. }

Branches

Latest commits

  • 04d36fcf|||PR Type function parameters and drop unneeded let (HL-STANDARD L2, L10)Atlas
  • 220c8fa4Merge branch 'readme-audit' into mainAtlas
  • b78fb85a|||PR README matches the codemre
  • 9f351699Preserve comments and nest supported selectors without changing rendered outputmre
  • 97110ad9WIP: nest accepted layout selectors; stopped at startup parse failuremre
  • ce4a0baeKeep caller slot styles whole so minimal shell apps startmre
  • 12cd3992Re-vendor complete Hybriel plugins and runtime to day44mre
  • b05c20feBackup tar: --hard-dereference, so mpackdb sidecars are never stored as hard links (Tracker's finding)mre
  • 8d9ed0b8Re-vendor complete Hybriel plugins and runtime to day39mre
  • ebd00b6cNest caller slot styles in three-panemre
  • 009991efNest caller slot styles in list-firstmre
  • 494d75d4Nest caller slot styles in app-sidebarmre
  • e5571680Align all plugin sources with Hybriel day38bmre
  • 5d204c46Re-vendor layouts to Hybriel set day38bmre
  • 17e0f03eNest html-prefixed theme component overridesmre
  • e54e735eFormat plain theme elements with nested rules and bare mediamre
  • 547f834eRecord LY7 deployment and live verificationmre
  • fa4600b1Link caller-slot style exceptions to hybriel#270mre
  • 413b11e1Record corrected LY7 nesting, exact CSS and Chrome proofmre
  • b810453etracker-demo: nest child and compound selectors in Stylemre