jukebox
All repositories: gitoria
9.2 KB
// 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// 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// 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.// 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// `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// 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// 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),// 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)`.//// VirtualList { itemCount = 13449 rowHeight = 56 loaded = chunk loadedFrom = chunkFrom onRange = loadRange onPick = picked selectedId = playingId }//// Anything written inside the tag (`VirtualList { … h1 { "Title" } }`) stands above the rows and scrolls away with them.// The host gives the list a height (it scrolls inside its own box): `virtual-list { height: 70dvh }` or a flex child. `emit vlTop()` scrolls back to the// first row (after the host changed the sort order or the filter). USE ONE PER PAGE: a composed component's members are the page's state, so the// public names below are chosen to stay clear of a host's own (no `rows`, `total`, `current`, `offset`).import Icon from '../icons/icons.hl'Number itemCount = 0Number rowHeight = 56Number overscan = 10Hybrid loaded = []Number loadedFrom = 0String selectedId = ''String listLabel = 'List'String emptyText = ''String thumbs = 'true'onRange = nullonPick = nullslot = null// what the scroll position says: the first row that exists in the page and how many existNumber vlFirst = 0Number vlCount = 30String spacerStyle = 'height:' + (itemCount * rowHeight) + 'px;--vl-row:' + rowHeight + 'px'String windowStyle = 'transform:translateY(' + (vlFirst * rowHeight) + 'px)'String rootClass = thumbs == 'true' ? 'vl-thumbs' : 'vl-plain'Boolean isEmpty = itemCount == 0 && emptyText != ''Hybrid shown = shownOf(loaded, loadedFrom, vlFirst, vlCount, itemCount, selectedId)// the rows that exist in the page: from the host's loaded rows, a blank row where the host has not delivered one yetshownOf(rows, from0, first0, count0, n, sel) {out = []let i = first0let end = first0 + count0if (end > n) { end = n }while (i < end) {let k = i - from0if (k >= 0 && k < rows.length) {r = rows[k]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 })} else {out.push({ idx = '' + i id = '' title = '' sub = '' aside = '' img = '' hasImg = false cls = 'vl-row blank' pos = i + 1 })}i = i + 1}return out}Style {'virtual-list' { display = 'block' position = 'relative' height = '100%' minHeight = '0' color = 'var(--color-text, #000)' }// the box that scrolls (the host gives virtual-list its height); the mount tick (the code editor's trick) reads the real size once the page is live'@keyframes virtual-list-mount' { 'from' { opacity = '.999' } to { opacity = '1' } }'virtual-list .vl-scroller' { position = 'absolute' inset = '0' overflowY = 'auto' overflowX = 'hidden' overscrollBehavior = 'contain' animation = 'virtual-list-mount 100ms linear infinite' }'virtual-list .vl-scroller:focus-visible' { outline = '2px solid var(--color-accent, #555)' outlineOffset = '-2px' }'virtual-list .vl-head' { display = 'block' }'virtual-list .vl-head:empty' { display = 'none' }'virtual-list .vl-spacer' { position = 'relative' width = '100%' }'virtual-list .vl-window' { position = 'absolute' left = '0' right = '0' top = '0' willChange = 'transform' }// a row: exactly rowHeight tall (the tap area is the row), thumbnail · title over sub · aside; no frames, no gaps between rows'virtual-list .vl-row' { display = 'flex' alignItems = 'center' gap = '.7rem' boxSizing = 'border-box' width = '100%' height = 'var(--vl-row)' margin = '0' padding = '0 .6rem' font = 'inherit' fontWeight = '400' textAlign = 'left' color = 'var(--color-text, #000)' background = 'transparent' border = '0' borderRadius = 'var(--radius-small, 5px)' cursor = 'pointer' overflow = 'hidden' }'virtual-list .vl-row:hover' { background = 'var(--color-surface, #f2f2f2)' }'virtual-list .vl-row:focus-visible' { outline = '2px solid var(--color-accent, #555)' outlineOffset = '-2px' }'virtual-list .vl-row.current' { color = 'var(--color-accent, #000)' background = 'var(--color-surface, #f2f2f2)' }'virtual-list .vl-row.blank' { cursor = 'default' pointerEvents = 'none' }'virtual-list .vl-thumb' { display = 'block' flex = 'none' width = 'calc(var(--vl-row) - 1rem)' height = 'calc(var(--vl-row) - 1rem)' borderRadius = 'var(--radius-small, 5px)' background = 'var(--color-surface, #ddd)' overflow = 'hidden' }// on the marked row the grey of the empty thumbnail would be the row's own surface: it takes the darker sunken colour'virtual-list .vl-row.current .vl-thumb' { background = 'var(--color-background-sunken, #ccc)' }'virtual-list .vl-row.blank .vl-thumb' { background = 'transparent' }'virtual-list .vl-thumb img' { display = 'block' width = '100%' height = '100%' objectFit = 'cover' }'virtual-list.vl-plain .vl-thumb' { display = 'none' }'virtual-list .vl-text' { display = 'flex' flexDirection = 'column' justifyContent = 'center' flex = '1 1 auto' minWidth = '0' }'virtual-list .vl-title' { display = 'block' overflow = 'hidden' textOverflow = 'ellipsis' whiteSpace = 'nowrap' lineHeight = '1.3' }'virtual-list .vl-sub' { display = 'block' overflow = 'hidden' textOverflow = 'ellipsis' whiteSpace = 'nowrap' lineHeight = '1.3' fontSize = '.85rem' color = 'var(--color-text-muted, #555)' }'virtual-list .vl-row.current .vl-sub' { color = 'inherit' }'virtual-list .vl-aside' { display = 'block' flex = 'none' fontSize = '.85rem' fontVariantNumeric = 'tabular-nums' color = 'var(--color-text-muted, #555)' }'virtual-list .vl-row.current .vl-aside' { color = 'inherit' }'virtual-list .vl-empty' { margin = '0' padding = '1rem .6rem' color = 'var(--color-text-muted, #555)' }}View {virtualList { class = rootClassdiv { class = "vl-scroller" role = "list" tabindex = "0" 'aria-label' = listLabel 'aria-rowcount' = itemCounton scroll(e) { emit vlScroll(e) } on animationiteration(e) { emit vlMount(e) }div { class = "vl-head" slot }div { class = "vl-spacer" style = spacerStylediv { class = "vl-window" style = windowStylefor (r of shown) {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.titlespan { class = "vl-thumb" if (r.hasImg) { img { src = r.img alt = "" loading = "lazy" decoding = "async" } } }span { class = "vl-text"span { class = "vl-title" r.title }span { class = "vl-sub" r.sub }}span { class = "vl-aside" r.aside }on click(e) { emit vlPick(e) }}}}}}if (isEmpty) { p { class = "vl-empty" emptyText } }}}// ---- the scroll position -> the rows that exist ------------------------------------------------------------------------------// look at the scroll box: which rows should exist, and ask the host for them when it has not delivered allvlSync(box) {// what the host put in front of the rows (a title, a picture) scrolls away with them: the rows start below ithead = box.querySelector('.vl-head')let top = head == null ? box.scrollTop : box.scrollTop - head.offsetHeightif (top < 0) { top = 0 }idx = (top - top % rowHeight) / rowHeightlet first = idx - overscanif (first < 0) { first = 0 }fit = box.clientHeight > 0 ? box.clientHeight : window.innerHeightcount = (fit - fit % rowHeight) / rowHeight + 2 + 2 * overscanif (first != vlFirst) { vlFirst = first }if (count != vlCount) { vlCount = count }let last = first + countif (last > itemCount) { last = itemCount }if (onRange != null && itemCount > 0 && (first < loadedFrom || last > loadedFrom + loaded.length)) { onRange(first, last - first) }}on vlScroll(e) {vlSync(e.target)return null}// the first tick of the mount animation: the page is live, read the real size and position, then stop the animationon vlMount(e) {e.target.style.animationName = 'none'vlSync(e.target)return null}// the host changed the order or the filter: back to the first rowon vlTop() {box = document.querySelector('virtual-list .vl-scroller')if (box != null) { box.scrollTop = 0 vlSync(box) }return null}on vlPick(e) {b = e.target.closest('button')if (b == null) { return null }id = b.getAttribute('data-id')if (id == '' || onPick == null) { return null }onPick(id, toNumber(b.getAttribute('data-index')))return null}
Branches
Latest commits
- 68aace7fRecords: mission 003 day38bmre
- d105d8a9Records: mpackdb integrity check cleanmre
- 6e980c53Records: mpackdb rule refinementmre
- 6d335ac4Records: storage copy rulingmre
- 7eb2898dRecords: mpackdb second-opener rule until day38mre
- b011f98eRecords: style form live on day35mre
- 60d90cb2Records: L1 woken for the restartmre
- 75ae544fRecords: restart waits for the connection to closemre
- e0b75951Records: L1 merged and pushed, restart waits for an open connectionmre
- f2b5db98styles: format styles with nested rules and bare queriesmre
- f036bab4search: format styles with nested rules and bare queriesmre
- ef9a9d4asongs: format styles with nested rules and bare queriesmre
- f22f9aacmain: format styles with nested rules and bare queriesmre
- f56cd7c7Sync icons from 8176eb1 and verify mixed component sourcesmre
- d56e5054Sync theme and shared tokens from layouts 17e0f03mre
- b1362547Re-vendor to Hybriel set day35mre
- 89e10f08Records: mission 002 rollback wordingmre
- 9485dee7Records: mission 002 brief (live step)mre
- 9ba68fe7Records: S1b reviewedmre
- 193dfff1Records: S1 result, S1b startedmre