diff --git a/src/noldova.ui.components.angular/src/app/components/site-header/site-header.component.css b/src/noldova.ui.components.angular/src/app/components/site-header/site-header.component.css index 1b5ff05e..717edd51 100644 --- a/src/noldova.ui.components.angular/src/app/components/site-header/site-header.component.css +++ b/src/noldova.ui.components.angular/src/app/components/site-header/site-header.component.css @@ -380,13 +380,11 @@ nol-site-theme-switch { } } -/* On a phone, or wherever the header has measured that its row doesn't fit, it takes the collapsed class, and the bar - * keeps the lockup, the theme switch, Download, a search button and the menu button; the links, the social links and, on a - * documentation site, Download move into the menu, and so does the theme switch where the bar can't hold it beside the - * product's whole name, as below. The bar keeps the height and gutter of its width, so its lockup stays at the page's - * edge. */ -:host(.nol-site-header-collapsed) .nol-site-header-bar { - display: grid; +/* The phone bar's grid, the places of its parts in it, the menu glyph's color and its buttons' taps, which show only + * where a rule below makes the bar a grid and shows the search and menu buttons: the lockup, the theme switch, Download, a + * search button and the menu button in its row, and under it the search field, the links, the theme switch, the social + * links and, on a documentation site, Download, as the menu opens. */ +.nol-site-header-bar { grid-template-areas: "brand theme download find menu" "search search search search search" @@ -396,72 +394,105 @@ nol-site-theme-switch { "action action action action action"; grid-template-columns: minmax(0, 1fr) auto auto auto auto; grid-template-rows: calc(var(--nol-site-header-bar-height) - 2 * var(--nol-space-3)); - gap: 0; - min-height: var(--nol-site-header-bar-height); - padding: var(--nol-space-3) var(--nol-site-header-gutter); } -:host(.nol-site-header-collapsed) .nol-site-header-brand { +.nol-site-header-brand { grid-area: brand; } -/* The lockup may narrow below its wordmark's full width, so the gap before the bar's next part holds at any width. */ -:host(.nol-site-header-collapsed) .nol-site-header-brand nol-site-lockup { - min-width: 0; +nol-site-theme-switch { + grid-area: theme; } -:host(.nol-site-header-collapsed) nol-site-theme-switch { - grid-area: theme; - margin-inline-start: var(--nol-site-actions-gap-phone); +:host(:is(.nol-site-header-compact, :not(.nol-site-header-measured))) nol-site-theme-switch { + grid-area: switch; } -:host(.nol-site-header-collapsed) .nol-site-download { +.nol-site-download { grid-area: download; - margin-inline-start: var(--nol-site-actions-gap-phone); +} + +:host(.nol-site-header-wide) .nol-site-download { + grid-area: action; +} + +.nol-site-header-search { + grid-area: search; +} + +.nol-site-search-button { + grid-area: find; } /* The menu glyph is in the text color, where the other icon buttons take the icon color. */ -:host(.nol-site-header-collapsed) .nol-site-menu-button { +.nol-site-menu-button { --nol-icon-color: var(--nol-text); - display: inline-grid; grid-area: menu; +} + +/* The bar's buttons take taps at least a menu row tall, and as wide or out to halfway to the next button, drawn + * nowhere, so a thumb that lands just beside one still presses it and never the one beside it; Download takes them in + * the phone bar alone. */ +.nol-site-search-button, +.nol-site-menu-button { + position: relative; +} + +:is(.nol-site-download, .nol-site-search-button, .nol-site-menu-button)::after { + position: absolute; + inset-block: min(0px, calc(50% - var(--nol-site-menu-row) / 2)); + inset-inline: max(min(0px, calc(50% - var(--nol-site-menu-row) / 2)), calc(var(--nol-site-actions-gap-phone) / -2)); +} + +.nol-site-search-button::after, +.nol-site-menu-button::after { + content: ""; +} + +/* On a phone, or wherever the header has measured that its row doesn't fit, it takes the collapsed class, and the bar + * becomes that grid, with the links, the social links and, on a documentation site, Download in the menu, and so the + * theme switch where the bar can't hold it beside the product's whole name, as below. The bar keeps the height and + * gutter of its width, so its lockup stays at the page's edge. The same rules, keyed on the width in place of the class, + * draw a phone's bar until the header has measured, at the end of this file; they change together. */ +:host(.nol-site-header-collapsed) .nol-site-header-bar { + display: grid; + gap: 0; + min-height: var(--nol-site-header-bar-height); + padding: var(--nol-space-3) var(--nol-site-header-gutter); +} + +:host(.nol-site-header-collapsed) .nol-site-header-brand nol-site-lockup { + min-width: 0; +} + +:host(.nol-site-header-collapsed) :is(nol-site-theme-switch, .nol-site-download, .nol-site-search-button, .nol-site-menu-button) { margin-inline-start: var(--nol-site-actions-gap-phone); } +:host(.nol-site-header-collapsed) .nol-site-menu-button, :host(.nol-site-header-collapsed.nol-site-header-with-search) .nol-site-search-button { display: inline-grid; - grid-area: find; - margin-inline-start: var(--nol-site-actions-gap-phone); } -/* The bar's buttons take taps at least a menu row tall, and as wide or out to halfway to the next button, drawn - * nowhere, so a thumb that lands just beside one still presses it and never the one beside it. */ -:host(.nol-site-header-collapsed) :is(.nol-site-download, .nol-site-search-button, .nol-site-menu-button) { +:host(.nol-site-header-collapsed) .nol-site-download { position: relative; } -:host(.nol-site-header-collapsed) :is(.nol-site-download, .nol-site-search-button, .nol-site-menu-button)::after { +:host(.nol-site-header-collapsed) .nol-site-download::after { content: ""; - position: absolute; - inset-block: min(0px, calc(50% - var(--nol-site-menu-row) / 2)); - inset-inline: max(min(0px, calc(50% - var(--nol-site-menu-row) / 2)), calc(var(--nol-site-actions-gap-phone) / -2)); } :host(.nol-site-header-collapsed.nol-site-header-with-search) .nol-site-header-search, :host(.nol-site-header-collapsed) .nol-site-header-nav, -:host(.nol-site-header-collapsed) .nol-site-header-social { - display: none; -} - -:host(.nol-site-header-collapsed.nol-site-header-wide) .nol-site-download { +:host(.nol-site-header-collapsed) .nol-site-header-social, +:host(.nol-site-header-collapsed.nol-site-header-wide) .nol-site-download, +:host(.nol-site-header-collapsed:is(.nol-site-header-compact, :not(.nol-site-header-measured))) nol-site-theme-switch { display: none; - grid-area: action; } :host(.nol-site-header-collapsed.nol-site-header-searching.nol-site-header-with-search) .nol-site-header-search { display: block; - grid-area: search; width: auto; margin-block-start: var(--nol-space-3); margin-inline-end: 0; @@ -530,11 +561,6 @@ nol-site-theme-switch { * and where they don't it takes the compact class: the switch moves into the menu, under the links, at the menu's row * size. Until it has measured, as on a prerendered page, whose switch can't be used yet, the switch is in the menu too, * so the first paint keeps the whole name. */ -:host(.nol-site-header-collapsed:is(.nol-site-header-compact, :not(.nol-site-header-measured))) nol-site-theme-switch { - display: none; - grid-area: switch; -} - :host(.nol-site-header-collapsed:is(.nol-site-header-compact, :not(.nol-site-header-measured)).nol-site-header-open) nol-site-theme-switch { --nol-site-theme-pill: var(--nol-site-menu-row); --nol-site-header-icon: var(--nol-site-menu-icon); @@ -545,83 +571,44 @@ nol-site-theme-switch { margin-inline-start: 0; } -/* Until the header has measured, as on a prerendered page, a phone's header draws the same bar with its menu closed, - * the theme switch in the menu, by its width alone; the rules are the closed menu's above, keyed on the width in place of - * the class, and change with them. They give way to the class wherever the header has it. */ +/* Until the header has measured, as on a prerendered page, a phone's header draws the same bar with its menu closed and + * the theme switch in the menu, by its width alone: the collapsed class's rules above, keyed on the width in place of the + * class, which change with them. They give way to the class wherever the header has it. */ @container nol-site-header (width <= 47.5rem) { :host(:not(.nol-site-header-measured, .nol-site-header-collapsed)) .nol-site-header-bar { display: grid; - grid-template-areas: - "brand theme download find menu" - "search search search search search" - "nav nav nav nav nav" - "switch switch switch switch switch" - "social social social social social" - "action action action action action"; - grid-template-columns: minmax(0, 1fr) auto auto auto auto; - grid-template-rows: calc(var(--nol-site-header-bar-height) - 2 * var(--nol-space-3)); gap: 0; min-height: var(--nol-site-header-bar-height); padding: var(--nol-space-3) var(--nol-site-header-gutter); } - :host(:not(.nol-site-header-measured, .nol-site-header-collapsed)) .nol-site-header-brand { - grid-area: brand; - } - :host(:not(.nol-site-header-measured, .nol-site-header-collapsed)) .nol-site-header-brand nol-site-lockup { min-width: 0; } - :host(:not(.nol-site-header-measured, .nol-site-header-collapsed)) nol-site-theme-switch { - grid-area: theme; - margin-inline-start: var(--nol-site-actions-gap-phone); - } - - :host(:not(.nol-site-header-measured, .nol-site-header-collapsed)) .nol-site-download { - grid-area: download; - margin-inline-start: var(--nol-site-actions-gap-phone); - } - - :host(:not(.nol-site-header-measured, .nol-site-header-collapsed)) .nol-site-menu-button { - --nol-icon-color: var(--nol-text); - - display: inline-grid; - grid-area: menu; + :host(:not(.nol-site-header-measured, .nol-site-header-collapsed)) :is(nol-site-theme-switch, .nol-site-download, .nol-site-search-button, .nol-site-menu-button) { margin-inline-start: var(--nol-site-actions-gap-phone); } + :host(:not(.nol-site-header-measured, .nol-site-header-collapsed)) .nol-site-menu-button, :host(:not(.nol-site-header-measured, .nol-site-header-collapsed).nol-site-header-with-search) .nol-site-search-button { display: inline-grid; - grid-area: find; - margin-inline-start: var(--nol-site-actions-gap-phone); } - :host(:not(.nol-site-header-measured, .nol-site-header-collapsed)) :is(.nol-site-download, .nol-site-search-button, .nol-site-menu-button) { + :host(:not(.nol-site-header-measured, .nol-site-header-collapsed)) .nol-site-download { position: relative; } - :host(:not(.nol-site-header-measured, .nol-site-header-collapsed)) :is(.nol-site-download, .nol-site-search-button, .nol-site-menu-button)::after { + :host(:not(.nol-site-header-measured, .nol-site-header-collapsed)) .nol-site-download::after { content: ""; - position: absolute; - inset-block: min(0px, calc(50% - var(--nol-site-menu-row) / 2)); - inset-inline: max(min(0px, calc(50% - var(--nol-site-menu-row) / 2)), calc(var(--nol-site-actions-gap-phone) / -2)); } :host(:not(.nol-site-header-measured, .nol-site-header-collapsed).nol-site-header-with-search) .nol-site-header-search, :host(:not(.nol-site-header-measured, .nol-site-header-collapsed)) .nol-site-header-nav, - :host(:not(.nol-site-header-measured, .nol-site-header-collapsed)) .nol-site-header-social { - display: none; - } - - :host(:not(.nol-site-header-measured, .nol-site-header-collapsed).nol-site-header-wide) .nol-site-download { - display: none; - grid-area: action; - } - + :host(:not(.nol-site-header-measured, .nol-site-header-collapsed)) .nol-site-header-social, + :host(:not(.nol-site-header-measured, .nol-site-header-collapsed).nol-site-header-wide) .nol-site-download, :host(:not(.nol-site-header-measured, .nol-site-header-collapsed)) nol-site-theme-switch { display: none; - grid-area: switch; } } diff --git a/src/noldova.ui.components.angular/tests/app/components/site-header/site-header-fit.spec.ts b/src/noldova.ui.components.angular/tests/app/components/site-header/site-header-fit.spec.ts index cf3f8e4d..63d85409 100644 --- a/src/noldova.ui.components.angular/tests/app/components/site-header/site-header-fit.spec.ts +++ b/src/noldova.ui.components.angular/tests/app/components/site-header/site-header-fit.spec.ts @@ -11,6 +11,7 @@ import { type ComponentFixture, TestBed } from "@angular/core/testing"; import { page } from "vitest/browser"; import { SiteHeaderComponent } from "../../../../src/app/components/site-header/site-header.component"; +import { SiteSearchDirective } from "../../../../src/app/components/site-header/site-search.directive"; import { SiteWordmarkSize } from "../../../../src/app/enums/site-wordmark-size"; import { ThemeMode } from "../../../../src/app/enums/theme-mode"; import { DefaultTheme } from "../../../../src/app/models/default-theme"; @@ -26,20 +27,23 @@ const formatWordmark = (width: number, height: number): string => const icon = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1 1'%3E%3Crect width='1' height='1'/%3E%3C/svg%3E"; const social = [new SiteSocialLink("GitHub", "#github", icon), new SiteSocialLink("X", "#x", icon)]; -// A site's header: its wordmark, in its proportions and size, its links, and whether it offers Download. +// A site's header: its wordmark, in its proportions and size, its links, whether it offers Download, and whether it is a +// documentation site, wide with a search. class HeaderSite { public readonly id: string; public readonly wordmarkRatio: number; public readonly wordmarkSize: SiteWordmarkSize; public readonly links: readonly SiteLink[]; public readonly downloadHref: string | null; + public readonly isDocs: boolean; - public constructor(id: string, wordmarkRatio: number, wordmarkSize: SiteWordmarkSize, links: readonly SiteLink[], downloadHref: string | null) { + public constructor(id: string, wordmarkRatio: number, wordmarkSize: SiteWordmarkSize, links: readonly SiteLink[], downloadHref: string | null, isDocs: boolean = false) { this.id = id; this.wordmarkRatio = wordmarkRatio; this.wordmarkSize = wordmarkSize; this.links = links; this.downloadHref = downloadHref; + this.isDocs = isDocs; } } @@ -48,10 +52,19 @@ const company = new HeaderSite("a company's site with six links", 1311.818 / 197 ["TeamRun", "Commitline", "UI Components", "Context", "About", "Contact"].map(t => new SiteLink(t, `#${t.toLowerCase().replaceAll(" ", "-")}`)), null); const product = new HeaderSite("a product's site with three links and Download", 4, SiteWordmarkSize.Large, ["Product", "Docs", "Changelog"].map(t => new SiteLink(t, `#${t.toLowerCase()}`)), "#download"); +// A documentation site, wide, with a search, three links and Download. +const docs = new HeaderSite("a documentation site with a search, three links and Download", 4, SiteWordmarkSize.Small, + ["Guide", "API", "Changelog"].map(t => new SiteLink(t, `#${t.toLowerCase()}`)), "#download", true); @Component({ - imports: [SiteHeaderComponent], - template: `` + imports: [SiteHeaderComponent, SiteSearchDirective], + template: ` + + @if (isDocs()) { + + } + + ` }) class SiteHeaderFitHostComponent { public readonly wordmark = signal(formatWordmark(800, 200)); @@ -60,6 +73,7 @@ class SiteHeaderFitHostComponent { public readonly links = signal([]); public readonly social: readonly SiteSocialLink[] = social; public readonly downloadHref = signal(null); + public readonly isDocs = signal(false); } // What a header shows in its bar at one width: its box, whether it draws its menu, the box of each part it shows, in order, @@ -108,6 +122,7 @@ describe("SiteHeaderComponent's fit", () => { host.size.set(site.wordmarkSize); host.links.set(site.links); host.downloadHref.set(site.downloadHref); + host.isDocs.set(site.isDocs); fixture.detectChanges(); await fixture.whenStable(); await Promise.all(DomFixture.allOf(DomFixture.host(fixture), "img", HTMLImageElement).map(t => t.decode())); @@ -125,6 +140,21 @@ describe("SiteHeaderComponent's fit", () => { return new HeaderReading(width, header().getBoundingClientRect(), header().classList.contains("nol-site-header-collapsed"), parts, shown.filter(t => t.classList.contains("nol-site-nav-link")).length); } + // Each part the phone bar lays out, with its box from the header's top left corner in rem and the tap area drawn after + // it, or that it is hidden. + function readPhoneBar(): readonly string[] { + const origin = header().getBoundingClientRect(); + const round = (value: number): string => String(Math.round(value / rem(1) * 1000) / 1000); + return [".nol-site-header-bar", ".nol-site-header-brand", "nol-site-lockup", ".nol-site-nav-link", "nol-site-theme-switch", ".nol-site-social", ".nol-site-download", + ".nol-site-header-search", ".nol-site-search-button", ".nol-site-menu-button"].flatMap(t => DomFixture.all(header(), t).map(u => { + if (u.getClientRects().length === 0) + return `${t} hidden`; + const box = u.getBoundingClientRect(); + const after = getComputedStyle(u, "::after"); + return `${t} ${[box.left - origin.left, box.top - origin.top, box.width, box.height].map(round).join(" ")} ${after.content} ${after.getPropertyValue("inset-block-start")} ${after.getPropertyValue("inset-inline-start")}`; + })); + } + const windowWidth = window.innerWidth; const windowHeight = window.innerHeight; @@ -193,6 +223,21 @@ describe("SiteHeaderComponent's fit", () => { expect([shown(".nol-site-menu-button"), shown(".nol-site-nav-link"), shown(".nol-site-social"), shown("nol-site-theme-switch")]).toEqual([1, 0, 0, 0]); }); + for (const site of [company, product, docs]) + for (const width of [20, 40, 47.5]) + for (const mode of AppearanceFixture.modes) + it(`draws the same phone bar at ${String(width)}rem before it has measured as once it has, with the theme switch, and a wide site's Download, in the menu, for ${site.id}, ${mode}`, async () => { + await renderAsync(site, mode); + const reading = readAt(rem(width)); + header().classList.add("nol-site-header-compact"); + const measured = readPhoneBar(); + header().classList.remove("nol-site-header-measured", "nol-site-header-collapsed", "nol-site-header-compact"); + + expect([reading.isCollapsed, readPhoneBar()]).toEqual([true, measured]); + expect(measured.filter(t => !t.endsWith(" hidden")).map(t => t.split(" ")[0])).toEqual([".nol-site-header-bar", ".nol-site-header-brand", "nol-site-lockup", + ...Object.isNull(site.downloadHref) || site.isDocs || width <= 30 ? [] : [".nol-site-download"], ...site.isDocs ? [".nol-site-search-button"] : [], ".nol-site-menu-button"]); + }); + it("draws the menu for six links at 768 pixels, where their row would run under the lockup and the theme switch, and the row once it fits", async () => { await renderAsync(company, ThemeMode.Light);