From 055d841798e2dcedc72da851f40fb1e0cdcf84c7 Mon Sep 17 00:00:00 2001 From: Rostislav Rotaru Date: Sun, 29 Nov 2026 17:08:20 -0800 Subject: [PATCH 1/3] Keep a code block as tall when its line scrolls as when it fits The code's bottom padding gave up the scrollbar token's 0.375rem whenever its line scrolled, whether or not the scrollbar took that room. Where it lies over the code, as on a phone or in a headless browser, a block whose line scrolled was 6 px shorter. The block now measures the room its sideways scrollbar takes, on each border-box resize of its body, and its bottom padding gives up exactly that. Issue #18 Agent: DEV1 (developer) Agent-Model: Anthropic Opus 5.5, Medium --- .../code-block/code-block-behavior.ts | 12 +++ .../code-block/code-block.component.css | 8 +- .../code-block/code-block.component.ts | 3 +- ...ode-block-code-size-dark.component.spec.ts | 3 +- .../code-block-code-size.component.spec.ts | 3 +- ...code-block-scroll-height.component.spec.ts | 80 +++++++++++++++++++ .../code-block-scrollbar.component.spec.ts | 4 +- 7 files changed, 99 insertions(+), 14 deletions(-) create mode 100644 src/noldova.ui.components.angular/tests/app/components/code-block/code-block-scroll-height.component.spec.ts diff --git a/src/noldova.ui.components.angular/src/app/components/code-block/code-block-behavior.ts b/src/noldova.ui.components.angular/src/app/components/code-block/code-block-behavior.ts index ec3f1f00..6a2acc48 100644 --- a/src/noldova.ui.components.angular/src/app/components/code-block/code-block-behavior.ts +++ b/src/noldova.ui.components.angular/src/app/components/code-block/code-block-behavior.ts @@ -77,6 +77,7 @@ export class CodeBlockBehavior { public readonly partResources: typeof CodeBlockResources = CodeBlockResources; public readonly runStates: typeof ShellRunState = ShellRunState; public readonly advanceRatio: WritableSignal = signal(null); + public readonly scrollbarHeight: WritableSignal = signal(null); public readonly isCopied: Signal = computed(() => this.copyState() === CopyState.Copied); public readonly copyGlyph: Signal = computed(() => Resources.copyGlyphs[this.copyState()]); public readonly copyLabel: Signal = computed(() => Resources.copyLabels[this.copyState()]); @@ -122,11 +123,15 @@ export class CodeBlockBehavior { afterNextRender(() => { const measureAfterLayout = this.observers.create(() => { this.measureAdvance(); + this.measureScrollbar(); this.followEnd(); }); this.destroyRef.onDestroy(() => measureAfterLayout.disconnect()); for (const element of [this.elements.text(), this.elements.advanceOne(), this.elements.advanceRun()]) measureAfterLayout.observe(element.nativeElement); + // A sideways scrollbar that takes room grows the body's border box as the line starts to scroll, and shrinks it as + // the line stops, so the block measures the bar again each time. + measureAfterLayout.observe(this.elements.body().nativeElement, { box: "border-box" }); }); } @@ -168,6 +173,13 @@ export class CodeBlockBehavior { this.advanceRatio.set(String(this.elements.advanceOne().nativeElement.getBoundingClientRect().width * CodeBlockResources.codeAdvanceSample.length / run)); } + // The room the body's sideways scrollbar takes: none while the line fits, or where the scrollbar lies over the code, as + // a phone's does. The code's bottom padding gives up as much, so a block is as tall whether its line fits or scrolls. + private measureScrollbar(): void { + const body = this.elements.body().nativeElement; + this.scrollbarHeight.set(`${String(body.offsetHeight - body.clientHeight)}px`); + } + private followEnd(): void { if (this.source.code().length > 0 && (this.grewStreaming || this.source.streaming())) this.follower.follow(this.elements.body().nativeElement); diff --git a/src/noldova.ui.components.angular/src/app/components/code-block/code-block.component.css b/src/noldova.ui.components.angular/src/app/components/code-block/code-block.component.css index 51836aaa..69bd32fe 100644 --- a/src/noldova.ui.components.angular/src/app/components/code-block/code-block.component.css +++ b/src/noldova.ui.components.angular/src/app/components/code-block/code-block.component.css @@ -51,7 +51,6 @@ .nol-code-block-body { margin: 0; overflow-x: auto; - container-type: scroll-state; font-family: var(--nol-font-mono); font-size: var(--nol-text-code); line-height: var(--nol-line-code); @@ -66,6 +65,7 @@ width: max-content; min-width: 100%; padding: var(--nol-code-block-padding); + padding-bottom: max(0px, var(--nol-code-block-padding) - var(--nol-code-scrollbar-height, 0px)); font: inherit; letter-spacing: calc((var(--nol-code-advance-ratio, 1) - 1) * 1ch); overflow: clip; @@ -98,12 +98,6 @@ } } -@container scroll-state(scrollable: inline) { - .nol-code-block-body > code { - padding-bottom: max(0px, var(--nol-code-block-padding) - var(--nol-scrollbar-size)); - } -} - .nol-code-block-body > code::highlight(nol-code-comment) { color: var(--nol-code-comment); } diff --git a/src/noldova.ui.components.angular/src/app/components/code-block/code-block.component.ts b/src/noldova.ui.components.angular/src/app/components/code-block/code-block.component.ts index 906cae7a..1920c128 100644 --- a/src/noldova.ui.components.angular/src/app/components/code-block/code-block.component.ts +++ b/src/noldova.ui.components.angular/src/app/components/code-block/code-block.component.ts @@ -29,7 +29,8 @@ import { CodeBlockBehavior } from "./code-block-behavior.js"; "class": "nol-code-block", "ngSkipHydration": "true", "[class.nol-code-block-wrapped]": "wrapped()", - "[style.--nol-code-advance-ratio]": "view.advanceRatio()" + "[style.--nol-code-advance-ratio]": "view.advanceRatio()", + "[style.--nol-code-scrollbar-height]": "view.scrollbarHeight()" } }) export class CodeBlockComponent { diff --git a/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-code-size-dark.component.spec.ts b/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-code-size-dark.component.spec.ts index 12360d1c..ebd39665 100644 --- a/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-code-size-dark.component.spec.ts +++ b/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-code-size-dark.component.spec.ts @@ -40,9 +40,8 @@ describe("CodeBlockComponent", () => { range.selectNodeContents(find(".nol-code-block-body > code")); const lines = [...range.getClientRects()]; const outer = body().getBoundingClientRect(); - const scrollbar = body().scrollWidth > body().clientWidth ? AppearanceFixture.measureLook("scrollbar-size") - (body().offsetHeight - body().clientHeight) : 0; const over = (lines[0] as DOMRect).top - outer.top; - const under = outer.bottom - (lines.at(-1) as DOMRect).bottom + scrollbar; + const under = outer.bottom - (lines.at(-1) as DOMRect).bottom; const space = AppearanceFixture.toPixels(0.875); expect([Math.abs(over - under) <= 0.5, Math.abs(over - space) <= 0.5, body().scrollHeight - body().clientHeight], `${over} px over, ${under} px under, ${space} px expected`) diff --git a/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-code-size.component.spec.ts b/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-code-size.component.spec.ts index 20d5d29c..2518c6e7 100644 --- a/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-code-size.component.spec.ts +++ b/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-code-size.component.spec.ts @@ -40,9 +40,8 @@ describe("CodeBlockComponent", () => { range.selectNodeContents(find(".nol-code-block-body > code")); const lines = [...range.getClientRects()]; const outer = body().getBoundingClientRect(); - const scrollbar = body().scrollWidth > body().clientWidth ? AppearanceFixture.measureLook("scrollbar-size") - (body().offsetHeight - body().clientHeight) : 0; const over = (lines[0] as DOMRect).top - outer.top; - const under = outer.bottom - (lines.at(-1) as DOMRect).bottom + scrollbar; + const under = outer.bottom - (lines.at(-1) as DOMRect).bottom; const space = AppearanceFixture.toPixels(0.875); expect([Math.abs(over - under) <= 0.5, Math.abs(over - space) <= 0.5, body().scrollHeight - body().clientHeight], `${over} px over, ${under} px under, ${space} px expected`) diff --git a/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-scroll-height.component.spec.ts b/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-scroll-height.component.spec.ts new file mode 100644 index 00000000..7d708812 --- /dev/null +++ b/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-scroll-height.component.spec.ts @@ -0,0 +1,80 @@ +/** + * @license + * Copyright (c) Noldova. + * + * This source code is licensed under the license found in the + * LICENSE file in the root directory of this source tree. + */ + +import { page } from "vitest/browser"; + +import { DefaultTheme } from "../../../../src/app/models/default-theme"; +import { AppearanceFixture } from "../../../../src/testing/services/appearance-fixture"; +import { CodeBlockHarness } from "../../../fixtures/code-block-harness.fixture"; + +// A one-line block's line, short enough to fit in a phone's column, and one that scrolls there. +const FITS = "sha256sum TeamRun-0.0.5-linux-x64.deb"; +const SCROLLS = "Get-FileHash .\\TeamRun-0.0.5-windows-x64.exe -Algorithm SHA256"; + +// A classic sideways scrollbar's height, as the kit draws one. The test browser runs headless, where no scrollbar takes +// room, so its block measures none; a classic bar's case sets the measured height on the body as the block would. +const CLASSIC_BAR = 0.375; + +describe("CodeBlockComponent", () => { + const harness = new CodeBlockHarness(); + const { renderAsync, changeAsync, find, body, settledAsync } = harness; + const windowWidth = window.innerWidth; + const windowHeight = window.innerHeight; + + beforeEach(harness.configure); + + afterEach(async () => { + await harness.resetAsync(); + await page.viewport(windowWidth, windowHeight); + }); + + const rem = (value: number): number => AppearanceFixture.toPixels(value); + const bar = (): number => body().offsetHeight - body().clientHeight; + const height = (): number => find("nol-code-block").getBoundingClientRect().height; + const padding = (): number => Number.parseFloat(getComputedStyle(find(".nol-code-block-body > code")).paddingBottom); + const shape = (): string => JSON.stringify([height(), body().scrollWidth, body().clientWidth, bar(), padding()]); + + async function renderLineAsync(width: number, mode: (typeof AppearanceFixture.modes)[number], code: string): Promise { + await page.viewport(width, 800); + AppearanceFixture.apply(DefaultTheme.theme, mode); + await renderAsync(); + await changeAsync(() => { + harness.host.width.set("calc(100vw - 2.5rem)"); + harness.host.language.set(null); + harness.host.code.set(code); + }); + await settledAsync("the line", shape); + } + + for (const width of [375, 1280]) + for (const mode of AppearanceFixture.modes) + it(`is as tall with one line that scrolls as with one that fits, where the scrollbar lies over the code, at ${String(width)} pixels, in ${mode.toLowerCase()} mode`, async () => { + await renderLineAsync(width, mode, FITS); + const fits = height(); + const fitsBar = bar(); + await changeAsync(() => harness.host.code.set(SCROLLS)); + await settledAsync("the line that scrolls", shape); + + expect([fitsBar, body().scrollWidth > body().clientWidth, bar()]).toEqual([0, width < 600, 0]); + AppearanceFixture.expectPixels(padding(), rem(0.875)); + AppearanceFixture.expectPixels(height(), fits); + }); + + for (const mode of AppearanceFixture.modes) + it(`gives up a classic scrollbar's measured height from its bottom padding, so with the bar's room it is as tall as a block whose line fits, at 375 pixels, in ${mode.toLowerCase()} mode`, async () => { + await renderLineAsync(375, mode, FITS); + const fits = height(); + await changeAsync(() => harness.host.code.set(SCROLLS)); + body().style.setProperty("--nol-code-scrollbar-height", `${String(CLASSIC_BAR)}rem`); + await settledAsync("the line that scrolls over a classic bar", shape); + + expect(body().scrollWidth).toBeGreaterThan(body().clientWidth); + AppearanceFixture.expectPixels(padding(), rem(0.875 - CLASSIC_BAR)); + AppearanceFixture.expectPixels(height() + rem(CLASSIC_BAR), fits); + }); +}); diff --git a/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-scrollbar.component.spec.ts b/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-scrollbar.component.spec.ts index f0a2c073..d8feb0d9 100644 --- a/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-scrollbar.component.spec.ts +++ b/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-scrollbar.component.spec.ts @@ -20,7 +20,7 @@ describe("CodeBlockComponent", () => { afterEach(harness.resetAsync); for (const language of [null, "TypeScript"]) - it(`takes a sideways scrollbar's height from its bottom padding only while its lines scroll, and keeps its end padding once scrolled to the end, in ${language ?? "plain"} code`, async () => { + it(`takes the room its sideways scrollbar takes from its bottom padding only while its lines scroll, and keeps its end padding once scrolled to the end, in ${language ?? "plain"} code`, async () => { AppearanceFixture.apply(); controlTokens(() => [new CodeToken(0, "const", CodeTokenKind.Keyword)]); await renderAsync(); @@ -41,7 +41,7 @@ describe("CodeBlockComponent", () => { expect(body().scrollWidth).toBeGreaterThan(body().clientWidth); expect(body().scrollWidth).toBe(Math.round(code().left + code().width) - Math.round(code().left)); AppearanceFixture.expectPixels(code().right - end(), start); - AppearanceFixture.expectPixels(bottom(), AppearanceFixture.toPixels(0.875) - AppearanceFixture.measureLook("scrollbar-size")); + AppearanceFixture.expectPixels(bottom(), AppearanceFixture.toPixels(0.875) - (body().offsetHeight - body().clientHeight)); body().scrollLeft = body().scrollWidth; From 65582bca2e4d19907f34bcedefa3d6cb35a50a53 Mon Sep 17 00:00:00 2001 From: Rostislav Rotaru Date: Sun, 29 Nov 2026 17:33:46 -0800 Subject: [PATCH 2/3] Start a mouse desktop's scrolling code block at its classic bar's height Until the block measures its sideways scrollbar, the padding gives up the scrollbar token while the line scrolls under a fine pointer, so prerendered pages and the first frame match the measured height. Agent: DEV1 (developer) Agent-Model: Anthropic Opus 5.5, Medium --- .../code-block/code-block.component.css | 12 +++++++++++- .../code-block-scroll-height.component.spec.ts | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/noldova.ui.components.angular/src/app/components/code-block/code-block.component.css b/src/noldova.ui.components.angular/src/app/components/code-block/code-block.component.css index 69bd32fe..a44908dd 100644 --- a/src/noldova.ui.components.angular/src/app/components/code-block/code-block.component.css +++ b/src/noldova.ui.components.angular/src/app/components/code-block/code-block.component.css @@ -51,6 +51,7 @@ .nol-code-block-body { margin: 0; overflow-x: auto; + container-type: scroll-state; font-family: var(--nol-font-mono); font-size: var(--nol-text-code); line-height: var(--nol-line-code); @@ -65,7 +66,7 @@ width: max-content; min-width: 100%; padding: var(--nol-code-block-padding); - padding-bottom: max(0px, var(--nol-code-block-padding) - var(--nol-code-scrollbar-height, 0px)); + padding-bottom: max(0px, var(--nol-code-block-padding) - var(--nol-code-scrollbar-height, var(--nol-code-scrollbar-guess, 0px))); font: inherit; letter-spacing: calc((var(--nol-code-advance-ratio, 1) - 1) * 1ch); overflow: clip; @@ -98,6 +99,15 @@ } } +/* Until the block measures its sideways scrollbar, a mouse desktop's line that scrolls likely has a classic bar. */ +@container scroll-state(scrollable: inline) { + @media (pointer: fine) { + .nol-code-block-body > code { + --nol-code-scrollbar-guess: var(--nol-scrollbar-size); + } + } +} + .nol-code-block-body > code::highlight(nol-code-comment) { color: var(--nol-code-comment); } diff --git a/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-scroll-height.component.spec.ts b/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-scroll-height.component.spec.ts index 7d708812..eef41e6f 100644 --- a/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-scroll-height.component.spec.ts +++ b/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-scroll-height.component.spec.ts @@ -77,4 +77,21 @@ describe("CodeBlockComponent", () => { AppearanceFixture.expectPixels(padding(), rem(0.875 - CLASSIC_BAR)); AppearanceFixture.expectPixels(height() + rem(CLASSIC_BAR), fits); }); + + for (const mode of AppearanceFixture.modes) + it(`gives up the kit's scrollbar size from its bottom padding before it has measured, under a fine pointer, only while its line scrolls, at 375 pixels, in ${mode.toLowerCase()} mode`, async () => { + await renderLineAsync(375, mode, SCROLLS); + const block = find("nol-code-block"); + const measured = block.style.getPropertyValue("--nol-code-scrollbar-height"); + block.style.removeProperty("--nol-code-scrollbar-height"); + await settledAsync("the line that scrolls before the block measures", shape); + const scrolls = padding(); + await changeAsync(() => harness.host.code.set(FITS)); + await settledAsync("the line that fits before the block measures", shape); + + expect([matchMedia("(pointer: fine)").matches, measured, block.style.getPropertyValue("--nol-code-scrollbar-height")]).toEqual([true, "0px", String.empty]); + expect(body().scrollWidth).toBe(body().clientWidth); + AppearanceFixture.expectPixels(scrolls, rem(0.875) - AppearanceFixture.measureLook("scrollbar-size")); + AppearanceFixture.expectPixels(padding(), rem(0.875)); + }); }); From 7f430222a673bd26ec84c8fb61f94d212bc02880 Mon Sep 17 00:00:00 2001 From: Rostislav Rotaru Date: Sun, 29 Nov 2026 17:37:19 -0800 Subject: [PATCH 3/3] Measure the site code block's scrollbar as the code block does The site code block shares the code block's behavior and stylesheet, but its host did not set the measured scrollbar height, so its padding kept the unmeasured guess. Agent: DEV1 (developer) Agent-Model: Anthropic Opus 5.5, Medium --- .../src/app/components/code-block/site-code-block.component.ts | 3 ++- .../components/code-block/site-code-block.component.spec.ts | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/noldova.ui.components.angular/src/app/components/code-block/site-code-block.component.ts b/src/noldova.ui.components.angular/src/app/components/code-block/site-code-block.component.ts index 8f9f469e..74d12710 100644 --- a/src/noldova.ui.components.angular/src/app/components/code-block/site-code-block.component.ts +++ b/src/noldova.ui.components.angular/src/app/components/code-block/site-code-block.component.ts @@ -31,7 +31,8 @@ import { CodeBlockBehavior } from "./code-block-behavior.js"; "class": "nol-code-block", "ngSkipHydration": "true", "[class.nol-code-block-wrapped]": "wrapped()", - "[style.--nol-code-advance-ratio]": "view.advanceRatio()" + "[style.--nol-code-advance-ratio]": "view.advanceRatio()", + "[style.--nol-code-scrollbar-height]": "view.scrollbarHeight()" } }) export class SiteCodeBlockComponent { diff --git a/src/noldova.ui.components.angular/tests/app/components/code-block/site-code-block.component.spec.ts b/src/noldova.ui.components.angular/tests/app/components/code-block/site-code-block.component.spec.ts index 9c1f5c98..451c53b0 100644 --- a/src/noldova.ui.components.angular/tests/app/components/code-block/site-code-block.component.spec.ts +++ b/src/noldova.ui.components.angular/tests/app/components/code-block/site-code-block.component.spec.ts @@ -72,6 +72,7 @@ describe("SiteCodeBlockComponent", () => { for (const mode of AppearanceFixture.modes) { AppearanceFixture.apply(DefaultTheme.theme, mode); await renderAsync(); + await DomChangeFixture.untilAsync(() => expect([block(), plain()].map(t => t.style.getPropertyValue("--nol-code-scrollbar-height"))).toEqual(["0px", "0px"])); expect([block().className, block().textContent]).toEqual([plain().className, plain().textContent]); expect(places(block())).toEqual(places(plain()));