documentation/components/source/inline-observation.ts

1/** An auxiliary consumer needs an element's content inline size, without
2 * subscribing to the block-size changes that layout can make in the same
3 * ResizeObserver delivery. The host must establish a containing block.
4 * CSS supplies the width; this helper never reads geometry.
5 *
6 * Observation contract: the unused axis stays zero. A target can depend on
7 * its host or an ancestor changing arrangement, but never on a descendant's
8 * changing height. Its two-level placement puts a direct member's target
9 * below the enclosing Switcher's slot/requirement, so ResizeObserver can
10 * redeliver it in that frame. Gap targets follow the same rule with one more
11 * level beneath their selected primitive (see inspection.css). */
12export function inlineObservation(host: HTMLElement): {
13  target: HTMLElement;
14  dispose(): void;
15} {
16  const wrapper = host.ownerDocument.createElement("span");
17  wrapper.dataset.observation = "inline-size";
18  wrapper.setAttribute("aria-hidden", "true");
19  const target = host.ownerDocument.createElement("span");
20  target.dataset.observation = "inline-target";
21  wrapper.append(target);
22  host.append(wrapper);
23  return { target, dispose: () => wrapper.remove() };
24}