|
| 1 | +/** |
| 2 | + * Collapses context chips that do not fit the current row width behind a |
| 3 | + * "+N more" pill, so narrow sidebars degrade gracefully instead of |
| 4 | + * clipping chips. Clicking the pill expands the row (wrapped) so hidden |
| 5 | + * chips stay reachable; clicking again collapses it. |
| 6 | + */ |
| 7 | +export class ContextRowOverflowController { |
| 8 | + private readonly rowEl: HTMLElement; |
| 9 | + private readonly hostEl: HTMLElement; |
| 10 | + private readonly pillEl: HTMLElement; |
| 11 | + private readonly measureEl: HTMLElement; |
| 12 | + private readonly resizeObserver: ResizeObserver; |
| 13 | + private readonly mutationObserver: MutationObserver; |
| 14 | + private layoutScheduled = false; |
| 15 | + private expanded = false; |
| 16 | + private destroyed = false; |
| 17 | + |
| 18 | + constructor(rowEl: HTMLElement) { |
| 19 | + this.rowEl = rowEl; |
| 20 | + // Measurement surface lives outside the observed subtree so measuring |
| 21 | + // never re-triggers the mutation observer. |
| 22 | + const host = rowEl.parentElement; |
| 23 | + if (!host) throw new Error('ContextRowOverflowController requires an attached context row'); |
| 24 | + this.hostEl = host; |
| 25 | + |
| 26 | + this.pillEl = rowEl.createDiv({ cls: 'qoderian-context-overflow-pill qoderian-hidden' }); |
| 27 | + this.pillEl.setAttribute('role', 'button'); |
| 28 | + this.pillEl.setAttribute('tabindex', '0'); |
| 29 | + this.pillEl.addEventListener('click', () => this.toggleExpanded()); |
| 30 | + this.pillEl.addEventListener('keydown', (event) => { |
| 31 | + if (event.key === 'Enter' || event.key === ' ') { |
| 32 | + event.preventDefault(); |
| 33 | + this.toggleExpanded(); |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + this.measureEl = this.hostEl.createDiv({ cls: 'qoderian-context-overflow-measure' }); |
| 38 | + |
| 39 | + this.resizeObserver = new ResizeObserver(() => this.scheduleLayout()); |
| 40 | + this.resizeObserver.observe(rowEl); |
| 41 | + |
| 42 | + this.mutationObserver = new MutationObserver(() => this.scheduleLayout()); |
| 43 | + this.mutationObserver.observe(rowEl, { |
| 44 | + childList: true, |
| 45 | + subtree: true, |
| 46 | + attributes: true, |
| 47 | + attributeFilter: ['class'], |
| 48 | + characterData: true, |
| 49 | + }); |
| 50 | + |
| 51 | + this.scheduleLayout(); |
| 52 | + } |
| 53 | + |
| 54 | + destroy(): void { |
| 55 | + this.destroyed = true; |
| 56 | + this.resizeObserver.disconnect(); |
| 57 | + this.mutationObserver.disconnect(); |
| 58 | + this.pillEl.remove(); |
| 59 | + this.measureEl.remove(); |
| 60 | + } |
| 61 | + |
| 62 | + private scheduleLayout(): void { |
| 63 | + if (this.layoutScheduled) return; |
| 64 | + this.layoutScheduled = true; |
| 65 | + window.requestAnimationFrame(() => { |
| 66 | + this.layoutScheduled = false; |
| 67 | + if (!this.destroyed) this.layout(); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + /** Content items are row children that are currently meant to be visible. */ |
| 72 | + private contentItems(): HTMLElement[] { |
| 73 | + return Array.from(this.rowEl.children).filter( |
| 74 | + (el): el is HTMLElement => |
| 75 | + el.instanceOf(HTMLElement) && el !== this.pillEl && !el.hasClass('qoderian-hidden') |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + private layout(): void { |
| 80 | + const items = this.contentItems(); |
| 81 | + |
| 82 | + if (items.length === 0 || !this.rowEl.hasClass('has-content')) { |
| 83 | + this.applyState(items, items.length, false); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + const rowStyles = getComputedStyle(this.rowEl); |
| 88 | + const available = |
| 89 | + this.rowEl.clientWidth - |
| 90 | + parseFloat(rowStyles.paddingLeft) - |
| 91 | + parseFloat(rowStyles.paddingRight); |
| 92 | + // Row not rendered (e.g. inactive tab): skip, ResizeObserver re-runs once visible. |
| 93 | + if (available <= 0) return; |
| 94 | + const gap = parseFloat(rowStyles.columnGap) || 0; |
| 95 | + |
| 96 | + const widths = this.measureWidths(items); |
| 97 | + const total = widths.reduce((sum, width) => sum + width, 0) + gap * (items.length - 1); |
| 98 | + |
| 99 | + if (this.expanded) { |
| 100 | + // Auto-collapse once everything fits on a single line again. |
| 101 | + this.applyState(items, items.length, total > available); |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + if (total <= available) { |
| 106 | + this.applyState(items, items.length, false); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + // Keep as many leading chips as fit alongside the pill; when even one |
| 111 | + // chip cannot fit, collapse everything behind the pill if the pill fits. |
| 112 | + let visibleCount = 0; |
| 113 | + for (let k = items.length - 1; k >= 1; k--) { |
| 114 | + const pillWidth = this.measurePillWidth(items.length - k); |
| 115 | + const used = |
| 116 | + widths.slice(0, k).reduce((sum, width) => sum + width, 0) + |
| 117 | + gap * (k - 1) + |
| 118 | + gap + |
| 119 | + pillWidth; |
| 120 | + if (used <= available) { |
| 121 | + visibleCount = k; |
| 122 | + break; |
| 123 | + } |
| 124 | + } |
| 125 | + if (visibleCount === 0 && this.measurePillWidth(items.length) > available) { |
| 126 | + // Even the pill does not fit: show one chip rather than nothing. |
| 127 | + visibleCount = 1; |
| 128 | + } |
| 129 | + |
| 130 | + this.applyState(items, visibleCount, false); |
| 131 | + } |
| 132 | + |
| 133 | + private toggleExpanded(): void { |
| 134 | + this.expanded = !this.expanded; |
| 135 | + this.layout(); |
| 136 | + } |
| 137 | + |
| 138 | + /** Natural single-line widths, measured on clones so hidden items work too. */ |
| 139 | + private measureWidths(items: HTMLElement[]): number[] { |
| 140 | + this.measureEl.empty(); |
| 141 | + const clones = items.map((item) => { |
| 142 | + const clone = item.cloneNode(true) as HTMLElement; |
| 143 | + clone.classList.remove('qoderian-context-overflow-hidden'); |
| 144 | + this.measureEl.appendChild(clone); |
| 145 | + return clone; |
| 146 | + }); |
| 147 | + const widths = clones.map(clone => clone.offsetWidth); |
| 148 | + this.measureEl.empty(); |
| 149 | + return widths; |
| 150 | + } |
| 151 | + |
| 152 | + private measurePillWidth(hiddenCount: number): number { |
| 153 | + this.measureEl.empty(); |
| 154 | + const clone = this.pillEl.cloneNode(false) as HTMLElement; |
| 155 | + clone.classList.remove('qoderian-hidden'); |
| 156 | + clone.setText(this.pillLabel(hiddenCount)); |
| 157 | + this.measureEl.appendChild(clone); |
| 158 | + const width = clone.offsetWidth; |
| 159 | + this.measureEl.empty(); |
| 160 | + return width; |
| 161 | + } |
| 162 | + |
| 163 | + private pillLabel(hiddenCount: number): string { |
| 164 | + return this.expanded ? 'Show less' : `+${hiddenCount} more`; |
| 165 | + } |
| 166 | + |
| 167 | + private applyState(items: HTMLElement[], visibleCount: number, expanded: boolean): void { |
| 168 | + this.expanded = expanded && items.length > 0; |
| 169 | + |
| 170 | + items.forEach((el, index) => { |
| 171 | + const shouldHide = !this.expanded && index >= visibleCount; |
| 172 | + if (shouldHide !== el.hasClass('qoderian-context-overflow-hidden')) { |
| 173 | + el.toggleClass('qoderian-context-overflow-hidden', shouldHide); |
| 174 | + } |
| 175 | + }); |
| 176 | + |
| 177 | + this.rowEl.toggleClass('qoderian-context-row--expanded', this.expanded); |
| 178 | + |
| 179 | + const hiddenCount = items.length - (this.expanded ? items.length : visibleCount); |
| 180 | + const showPill = this.expanded || hiddenCount > 0; |
| 181 | + |
| 182 | + if (showPill) { |
| 183 | + const label = this.pillLabel(hiddenCount); |
| 184 | + if (this.pillEl.textContent !== label) this.pillEl.setText(label); |
| 185 | + if (this.pillEl.hasClass('qoderian-hidden')) this.pillEl.removeClass('qoderian-hidden'); |
| 186 | + // The pill always trails the chips. |
| 187 | + if (this.pillEl.nextElementSibling) this.rowEl.appendChild(this.pillEl); |
| 188 | + } else if (!this.pillEl.hasClass('qoderian-hidden')) { |
| 189 | + this.pillEl.addClass('qoderian-hidden'); |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments