gitoria

components

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchvl27-baseline489afcf1virtual-list: name the scroll-position gap hybriel#445Atlasvl27-baseline/components/virtual-list/virtual-list.hl

11.2 KB

  1. // virtual-list.hl — THE VIRTUAL LIST: a very long list (tens of thousands of rows) that scrolls smoothly on a phone because only the rows
  2. // near the viewport exist in the page. Every row has the same fixed height, so the list knows where row N is without measuring: a spacer
  3. // as tall as the whole list (`itemCount * rowHeight`) makes the scroll bar honest, and the few rows that are near the viewport are placed in it.
  4. // The component does not fetch anything. The HOST owns the data: it hands in the rows it has loaded (`loaded`, the first of them is row number
  5. // `loadedFrom`), and the component calls the host's `onRange(first, count)` when the rows near the viewport are not all there yet; the host loads
  6. // them (usually page by page from its server) and hands in the new `loaded` / `loadedFrom`. Until they arrive the missing rows are blank rows of the same
  7. // height, so the list never jumps. A row is `{ id, title, sub, aside, img }`: a thumbnail (`img`, loaded lazily; a plain grey square when it is empty),
  8. // the title with a second line under it, and a short text at the right (a play count, a length). A tap calls `onPick(id, index)`.
  9. //
  10. // VirtualList { itemCount = 13449 rowHeight = 56 loaded = chunk loadedFrom = chunkFrom onRange = loadRange onPick = picked selectedId = playingId }
  11. //
  12. // Anything written inside the tag (`VirtualList { … h1 { "Title" } }`) stands above the rows and scrolls away with them.
  13. // The host gives the list a height (it scrolls inside its own box): `virtual-list { height: 70dvh }` or a flex child. Changing `resetKey` scrolls back to the
  14. // first row after the host changes resetKey (or to the supplied scrollTop).
  15. // Each composed list otherwise owns its state; parameters and bound callbacks connect it to its host.
  16. import Icon from '../icons/icons.hl'
  17. Number itemCount = 0
  18. Number rowHeight = 56
  19. Number overscan = 10
  20. Hybrid loaded = []
  21. Number loadedFrom = 0
  22. String selectedId = ''
  23. String centerId = ''
  24. Number scrollTop = null
  25. String resetKey = ''
  26. String vlResetKey = ''
  27. vlBox = null
  28. Boolean vlResetPending = false
  29. get resetKey() { return vlResetKey }
  30. set resetKey(String v) {
  31. if (v == vlResetKey) { return null }
  32. vlResetKey = v
  33. // the first key (given before the list stands) is the baseline: scrollTop and centerId place the list at mount
  34. if (v == '' || vlBox == null) { return null }
  35. vlResetPending = true
  36. vlFirst = 0
  37. vlApplyReset()
  38. }
  39. // F20 gap hybriel#445: hl:web has no form to set a scroll container's position from a member, so the reset writes the
  40. // scroller it took at mount; replace with `scrollTop = member` on the scroller once #445 is built
  41. vlApplyReset() {
  42. if (!vlResetPending || vlBox == null) { return null }
  43. vlResetPending = false
  44. vlBox.scrollTop = scrollTop == null ? 0 : scrollTop
  45. vlSync(vlBox)
  46. }
  47. onScroll = null
  48. Boolean vlRestored = false
  49. Boolean vlScrollPending = false
  50. String listLabel = 'List'
  51. String emptyText = ''
  52. String thumbs = 'true'
  53. onRange = null
  54. onPick = null
  55. slot = null
  56. // what the scroll position says: the first row that exists in the page and how many exist
  57. Number vlFirst = 0
  58. Number vlCount = 30
  59. String spacerStyle = 'height:' + (itemCount * rowHeight) + 'px;--vl-row:' + rowHeight + 'px'
  60. String windowStyle = 'transform:translateY(' + (vlFirst * rowHeight) + 'px)'
  61. String rootClass = thumbs == 'true' ? 'vl-thumbs' : 'vl-plain'
  62. Boolean isEmpty = itemCount == 0 && emptyText != ''
  63. Hybrid shown = shownOf(loaded, loadedFrom, vlFirst, vlCount, itemCount, selectedId)
  64. // the rows that exist in the page: from the host's loaded rows, a blank row where the host has not delivered one yet
  65. shownOf(rows, from0, first0, count0, n, sel) {
  66. out = []
  67. let i = first0
  68. let end = first0 + count0
  69. if (end > n) { end = n }
  70. while (i < end) {
  71. let k = i - from0
  72. if (k >= 0 && k < rows.length) {
  73. r = rows[k]
  74. out.push({ idx = '' + i id = r.id title = r.title sub = r.sub aside = r.aside img = r.img hasImg = r.img != '' cls = r.id == sel ? 'vl-row current' : 'vl-row' pos = i + 1 })
  75. } else {
  76. out.push({ idx = '' + i id = '' title = '' sub = '' aside = '' img = '' hasImg = false cls = 'vl-row blank' pos = i + 1 })
  77. }
  78. i = i + 1
  79. }
  80. return out
  81. }
  82. Style {
  83. virtualList {
  84. display = 'block'
  85. position = 'relative'
  86. height = '100%'
  87. minHeight = '0'
  88. color = 'var(--color-text, #000)'
  89. // Class set by the View or row data; quoted compound selector.
  90. '.vl-scroller' {
  91. position = 'absolute'
  92. inset = '0'
  93. overflowY = 'auto'
  94. overflowX = 'hidden'
  95. overscrollBehavior = 'contain'
  96. animation = 'virtual-list-mount 100ms linear infinite'
  97. focusVisible {
  98. outline = '2px solid var(--color-accent, #555)'
  99. outlineOffset = '-2px'
  100. }
  101. }
  102. // Class set by the View or row data; quoted compound selector.
  103. '.vl-head' {
  104. display = 'block'
  105. empty {
  106. display = 'none'
  107. }
  108. }
  109. // Class set by the View or row data; quoted compound selector.
  110. '.vl-spacer' {
  111. position = 'relative'
  112. width = '100%'
  113. }
  114. // Class set by the View or row data; quoted compound selector.
  115. '.vl-window' {
  116. position = 'absolute'
  117. left = '0'
  118. right = '0'
  119. top = '0'
  120. willChange = 'transform'
  121. }
  122. // Class set by the View or row data; quoted compound selector.
  123. '.vl-row' {
  124. display = 'flex'
  125. alignItems = 'center'
  126. gap = '.7rem'
  127. boxSizing = 'border-box'
  128. width = '100%'
  129. height = 'var(--vl-row)'
  130. margin = '0'
  131. padding = '0 .6rem'
  132. font = 'inherit'
  133. fontWeight = '400'
  134. textAlign = 'left'
  135. color = 'var(--color-text, #000)'
  136. background = 'transparent'
  137. border = '0'
  138. borderRadius = 'var(--radius-small, 5px)'
  139. cursor = 'pointer'
  140. overflow = 'hidden'
  141. hover {
  142. background = 'var(--color-surface, #f2f2f2)'
  143. }
  144. focusVisible {
  145. outline = '2px solid var(--color-accent, #555)'
  146. outlineOffset = '-2px'
  147. }
  148. }
  149. // Class set by the View or row data; quoted compound selector.
  150. '.vl-row.current' {
  151. color = 'var(--color-accent, #000)'
  152. background = 'var(--color-surface, #f2f2f2)'
  153. // Class set by the View or row data; quoted compound selector.
  154. '.vl-thumb' {
  155. background = 'var(--color-background-sunken, #ccc)'
  156. }
  157. // Class set by the View or row data; quoted compound selector.
  158. '.vl-sub' {
  159. color = 'inherit'
  160. }
  161. // Class set by the View or row data; quoted compound selector.
  162. '.vl-aside' {
  163. color = 'inherit'
  164. }
  165. }
  166. // Class set by the View or row data; quoted compound selector.
  167. '.vl-row.blank' {
  168. cursor = 'default'
  169. pointerEvents = 'none'
  170. // Class set by the View or row data; quoted compound selector.
  171. '.vl-thumb' {
  172. background = 'transparent'
  173. }
  174. }
  175. // Class set by the View or row data; quoted compound selector.
  176. '.vl-thumb' {
  177. display = 'block'
  178. flex = 'none'
  179. width = 'calc(var(--vl-row) - 1rem)'
  180. height = 'calc(var(--vl-row) - 1rem)'
  181. borderRadius = 'var(--radius-small, 5px)'
  182. background = 'var(--color-surface, #ddd)'
  183. overflow = 'hidden'
  184. img {
  185. display = 'block'
  186. width = '100%'
  187. height = '100%'
  188. objectFit = 'cover'
  189. }
  190. }
  191. // Class set by the View or row data; quoted compound selector.
  192. '.vl-text' {
  193. display = 'flex'
  194. flexDirection = 'column'
  195. justifyContent = 'center'
  196. flex = '1 1 auto'
  197. minWidth = '0'
  198. }
  199. // Class set by the View or row data; quoted compound selector.
  200. '.vl-title' {
  201. display = 'block'
  202. overflow = 'hidden'
  203. textOverflow = 'ellipsis'
  204. whiteSpace = 'nowrap'
  205. lineHeight = '1.3'
  206. }
  207. // Class set by the View or row data; quoted compound selector.
  208. '.vl-sub' {
  209. display = 'block'
  210. overflow = 'hidden'
  211. textOverflow = 'ellipsis'
  212. whiteSpace = 'nowrap'
  213. lineHeight = '1.3'
  214. fontSize = '.85rem'
  215. color = 'var(--color-text-muted, #555)'
  216. }
  217. // Class set by the View or row data; quoted compound selector.
  218. '.vl-aside' {
  219. display = 'block'
  220. flex = 'none'
  221. fontSize = '.85rem'
  222. fontVariantNumeric = 'tabular-nums'
  223. color = 'var(--color-text-muted, #555)'
  224. }
  225. // Class set by the View or row data; quoted compound selector.
  226. '.vl-empty' {
  227. margin = '0'
  228. padding = '1rem .6rem'
  229. color = 'var(--color-text-muted, #555)'
  230. }
  231. }
  232. '@keyframes virtual-list-mount' {
  233. 'from' {
  234. opacity = '.999'
  235. }
  236. to {
  237. opacity = '1'
  238. }
  239. }
  240. // Class set by the View or row data; quoted compound selector.
  241. 'virtual-list.vl-plain' {
  242. // Class set by the View or row data; quoted compound selector.
  243. '.vl-thumb' {
  244. display = 'none'
  245. }
  246. }
  247. }
  248. View {
  249. virtualList { class = rootClass
  250. div { class = "vl-scroller" role = "list" tabindex = "0" 'aria-label' = listLabel 'aria-rowcount' = itemCount
  251. on scroll(e) { emit vlScroll(e) } on animationiteration(e) { emit vlMount(e) }
  252. div { class = "vl-head" slot }
  253. div { class = "vl-spacer" style = spacerStyle
  254. div { class = "vl-window" style = windowStyle
  255. for (r of shown) {
  256. button { type = "button" class = r.cls role = "listitem" 'data-index' = r.idx 'data-id' = r.id 'aria-posinset' = r.pos 'aria-setsize' = itemCount title = r.title
  257. span { class = "vl-thumb" if (r.hasImg) { img { src = r.img alt = "" loading = "lazy" decoding = "async" } } }
  258. span { class = "vl-text"
  259. span { class = "vl-title" r.title }
  260. span { class = "vl-sub" r.sub }
  261. }
  262. span { class = "vl-aside" r.aside }
  263. on click(e) { emit vlPick(e) }
  264. }
  265. }
  266. }
  267. }
  268. }
  269. if (isEmpty) { p { class = "vl-empty" emptyText } }
  270. }
  271. }
  272. // ---- the scroll position -> the rows that exist ------------------------------------------------------------------------------
  273. // look at the scroll box: which rows should exist, and ask the host for them when it has not delivered all
  274. vlSync(box) {
  275. // what the host put in front of the rows (a title, a picture) scrolls away with them: the rows start below it
  276. head = box.querySelector('.vl-head')
  277. let top = head == null ? box.scrollTop : box.scrollTop - head.offsetHeight
  278. if (top < 0) { top = 0 }
  279. idx = (top - top % rowHeight) / rowHeight
  280. let first = idx - overscan
  281. if (first < 0) { first = 0 }
  282. fit = box.clientHeight > 0 ? box.clientHeight : window.innerHeight
  283. count = (fit - fit % rowHeight) / rowHeight + 2 + 2 * overscan
  284. if (first != vlFirst) { vlFirst = first }
  285. if (count != vlCount) { vlCount = count }
  286. let last = first + count
  287. if (last > itemCount) { last = itemCount }
  288. if (onRange != null && itemCount > 0 && (first < loadedFrom || last > loadedFrom + loaded.length)) { onRange(first, last - first) }
  289. }
  290. on vlScroll(e) {
  291. vlSync(e.target)
  292. if (!vlScrollPending && onScroll != null) {
  293. vlScrollPending = true
  294. box = e.target
  295. window.setTimeout(() => {
  296. vlScrollPending = false
  297. onScroll(box.scrollTop)
  298. }, 100)
  299. }
  300. return null
  301. }
  302. // the first tick of the mount animation: the page is live, read the real size and position, then stop the animation
  303. on vlMount(e) {
  304. vlBox = e.target
  305. e.target.style.animationName = 'none'
  306. if (!vlRestored) {
  307. vlRestored = true
  308. box = e.target
  309. if (scrollTop != null) { box.scrollTop = scrollTop }
  310. if (centerId != '') {
  311. let i = 0
  312. for (row of loaded) {
  313. if (row.id == centerId) {
  314. head = box.querySelector('.vl-head')
  315. height = head == null ? 0 : head.offsetHeight
  316. box.scrollTop = height + (loadedFrom + i) * rowHeight + rowHeight / 2 - box.clientHeight / 2
  317. }
  318. i = i + 1
  319. }
  320. }
  321. }
  322. vlApplyReset()
  323. vlSync(e.target)
  324. return null
  325. }
  326. on vlPick(e) {
  327. b = e.target.closest('button')
  328. if (b == null) { return null }
  329. id = b.getAttribute('data-id')
  330. if (id == '' || onPick == null) { return null }
  331. onPick(id, toNumber(b.getAttribute('data-index')))
  332. return null
  333. }

Branches

Latest commits

  • 489afcf1virtual-list: name the scroll-position gap hybriel#445Atlas
  • b7e11e3bvirtual-list: the first resetKey is the baseline, so centerId and scrollTop keep their place at mount; gate for itAtlas
  • 7e428ba6Re-vendor Hybriel day72 (day73 withdrawn: minifier breaks write sets)Atlas
  • a06fc778Reset virtual-list window when the host changes resetKeyAtlas
  • 3aab598cRe-vendor complete Hybriel day73 set for virtual-list resetAtlas
  • 337ca075MD1 gate check measures frames in the page, not milliseconds from outside, and takes the middle load (components#30)mre
  • f5f131c5Record AP29 autoplay comparisons and passing gatesmre
  • 51c38187Gate media autoplay on first load and after client mountmre
  • a3f2223dRecord successful B44 deployment and live proofmre
  • 06c1e021Record passing B44 browser rerunmre
  • c9861533Record B44 component proof and browser gate blockermre
  • 1b7413beIgnore detached media events and space demo option outputsmre
  • ca3b4959Re-vendor complete Hybriel plugins and runtime to day44mre
  • df9e7df9Last-item checks run in the browser gate (components#26 fixed on day41); media-player loop is a plain Booleanmre
  • 895a5afcRe-vendor complete Hybriel plugins and runtime to day41mre
  • 16006bb3Backup tar: --hard-dereference, so mpackdb sidecars are never stored as hard links (Tracker's finding)mre
  • 88397fc3Re-vendor complete Hybriel plugins and runtime to day39mre
  • 9b872cedRecord IC28 deployment and live icon verificationmre
  • 46fefaa7Count the icons entry in the offline home-page checkmre
  • c7e14cabRecord IC28 rerun and unchanged-main baseline comparisonmre