1import { inlineObservation } from"./inline-observation.ts";2import {3compositionMembers,4compositionAnchorName,5} from"./composition-elements.ts";67/**8 * What a rendered composition reports about itself in any host: which9 * Switchers are rows now, and whether a Shelf has more than it can show.10 * An overflowing Shelf also becomes reachable from the keyboard.11 *12 * Teaching chrome observes only the boxes whose sizes it displays. It never13 * determines the arrangement of a primitive.14 */15exportclassCompositionReadouts {16protectedreadonlyobserver:ResizeObserver;17privatereadonlyshelves=newSet<HTMLElement>();18privatereadonlyshelfInputs=newSet<Element>();19privatereadonlyshelfObservations:ReturnType<typeofinlineObservation>[] =20 [];21privatescrollable=false;22privatereadonlynote:HTMLElement;23privatereadonlydirections: () =>readonlystring[];2425constructor(note:HTMLElement, directions: () =>readonlystring[]) {26this.note=note;27this.directions=directions;28this.observer=newResizeObserver((entries) =>this.receive(entries));29 }3031protectedreceive(entries:readonlyResizeObserverEntry[]) {32if (entries.some((entry) =>this.shelfInputs.has(entry.target)))33this.refreshOverflow();34 }3536/** Call after a composition edit; content-sized children expose overflow changes. */37observeComposition(rendered:HTMLElement) {38for (constinputofthis.shelfInputs) this.observer.unobserve(input);39this.shelfInputs.clear();40this.shelves.clear();41for (constobservationofthis.shelfObservations) observation.dispose();42this.shelfObservations.length=0;43for (constmarkerofrendered.querySelectorAll(44'[data-decoration="overflow-extent"]',45 ))46marker.remove();47for (constshelfofrendered.querySelectorAll<HTMLElement>(48'[data-kind="Shelf"]',49 )) {50this.shelves.add(shelf);51constwidth=inlineObservation(shelf);52this.shelfObservations.push(width);53this.shelfInputs.add(width.target);54constmembers=compositionMembers(shelf);55if (members.length) {56constextent=shelf.ownerDocument.createElement("span");57extent.className="overflow-extent";58extent.dataset.decoration="overflow-extent";59extent.setAttribute("aria-hidden", "true");60extent.style.setProperty(61"--extent-first",62compositionAnchorName(members[0]!.dataset.path!),63 );64extent.style.setProperty(65"--extent-last",66compositionAnchorName(members.at(-1)!.dataset.path!),67 );68shelf.append(extent);69this.shelfInputs.add(extent);70 }71 }72for (constinputofthis.shelfInputs) {73this.observer.observe(input);74 }75this.scrollable=false;76this.refreshModes();77 }7879refreshOverflow() {80this.scrollable=false;81for (constshelfofthis.shelves) {82// Scroll extent is the input to keyboard accessibility, not a fit decision.83constoverflow=shelf.scrollWidth>shelf.clientWidth+1;84consttabIndex=overflow?0:-1;85if (shelf.tabIndex!==tabIndex) shelf.tabIndex=tabIndex;86if (overflow) {87if (shelf.getAttribute("role") !=="group")88shelf.setAttribute("role", "group");89if (shelf.getAttribute("aria-label") !=="Scrollable row")90shelf.setAttribute("aria-label", "Scrollable row");91this.scrollable=true;92 } else {93shelf.removeAttribute("role");94shelf.removeAttribute("aria-label");95 }96 }97this.refreshModes();98 }99100refreshModes() {101// Naming the primitive keeps this apart from a selected group's caption.102constmodes=this.directions().map((direction) =>103direction==="row"?"one row":"one column",104 );105constnote=this.scrollable106?"Scroll the row to read all its members."107:modes.length108?`${modes.length>1? "Switchers" : "Switcher"}: ${modes.join(" ยท ")}`109:"";110if (this.note.textContent!==note) this.note.textContent=note;111 }112113disconnect() {114this.observer.disconnect();115for (constobservationofthis.shelfObservations) observation.dispose();116this.shelfObservations.length=0;117this.shelves.clear();118this.shelfInputs.clear();119 }120}121122/** Adds the width a resizable example has, and the widest it may request. */123exportclassExampleReadoutsextendsCompositionReadouts {124privateactualWidth=0;125privatemaximumWidth=720;126privatereadonlystage:ReturnType<typeofinlineObservation>;127privatereadonlyviewport:ReturnType<typeofinlineObservation>;128privatereadonlyinput:HTMLInputElement;129130constructor(131stage:HTMLElement,132viewport:HTMLElement,133input:HTMLInputElement,134note:HTMLElement,135directions: () =>readonlystring[],136 ) {137super(note, directions);138this.stage=inlineObservation(stage);139this.viewport=inlineObservation(viewport);140this.input=input;141this.observer.observe(this.stage.target);142this.observer.observe(this.viewport.target);143 }144145protectedoverridereceive(entries:readonlyResizeObserverEntry[]) {146for (constentryofentries) {147constsize=entry.contentBoxSize[0]!.inlineSize;148if (entry.target===this.stage.target)149this.maximumWidth=Math.max(1, Math.min(720, size));150if (entry.target===this.viewport.target)151this.actualWidth=Math.round(size);152 }153this.refreshWidth();154super.receive(entries);155 }156157getmaximum():number {158returnthis.maximumWidth;159 }160161refreshWidth() {162this.input.min=String(Math.min(160, this.maximumWidth));163this.input.max=String(Math.floor(this.maximumWidth));164constroot=this.input.getRootNode();165if (rootinstanceofShadowRoot&&root.activeElement===this.input) return;166if (this.actualWidth) this.input.value=String(this.actualWidth);167 }168169overridedisconnect() {170super.disconnect();171this.stage.dispose();172this.viewport.dispose();173 }174}