Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

restingComposerControlsMeasurement.ts

81 lines3.6 KB

1import type { RestingComposerControlsMeasurement } from "../composerFooterLayout";
2​
3function elementOuterWidth(element: HTMLElement): number {
4 const width = element.getBoundingClientRect().width;
5 if (width === 0) return 0;
6 const style = getComputedStyle(element);
7 return (
8 width +
9 (Number.parseFloat(style.marginInlineStart) || 0) +
10 (Number.parseFloat(style.marginInlineEnd) || 0)
11 );
13​
14function elementInlineMarginWidth(element: HTMLElement): number {
15 const style = getComputedStyle(element);
16 return (
17 (Number.parseFloat(style.marginInlineStart) || 0) +
18 (Number.parseFloat(style.marginInlineEnd) || 0)
19 );
21​
22function providerModelPickerNaturalWidth(picker: HTMLElement): number {
23 const renderedWidth = picker.getBoundingClientRect().width;
24 if (renderedWidth === 0) return 0;
25 const style = getComputedStyle(picker);
26 const label = picker.querySelector<HTMLElement>('[data-chat-provider-model-picker-label="true"]');
27 // Narrow composers deliberately collapse the label with w-0 and flex-none.
28 // A flexible label squeezed to zero still needs its natural width recovered.
29 const labelIsCollapsed = label?.clientWidth === 0 && getComputedStyle(label).flexGrow === "0";
30 const hiddenLabelWidth =
31 label && !labelIsCollapsed ? Math.max(0, label.scrollWidth - label.clientWidth) : 0;
32 const maxWidth = Number.parseFloat(style.maxWidth);
33 const naturalWidth = Math.min(
34 renderedWidth + hiddenLabelWidth,
35 Number.isFinite(maxWidth) ? maxWidth : Number.POSITIVE_INFINITY,
36 );
37 return naturalWidth + elementInlineMarginWidth(picker);
39​
40function providerModelPickerMinimumWidth(picker: HTMLElement): number {
41 const minWidth = Number.parseFloat(getComputedStyle(picker).minWidth) || 0;
42 return minWidth + elementInlineMarginWidth(picker);
44​
45/**
46 * Read the natural widths of the resting composer controls from the DOM.
47 *
48 * Both the composer (deciding which blocks move into overflow) and the
49 * context strip (deciding whether its labels may expand) read the same
50 * numbers, so neither decision depends on what the other one hid last render.
51 *
52 * Hidden blocks and the unused overflow trigger stay mounted out of flow at
53 * full size. The picker is the one flexible item: its intended width is
54 * recovered from the truncated label.
55 */
56export function measureRestingComposerControls(
57 controls: HTMLElement,
58): RestingComposerControlsMeasurement | null {
59 const gap = Number.parseFloat(getComputedStyle(controls).columnGap) || 0;
60 const picker = controls.querySelector<HTMLElement>("[data-chat-provider-model-picker]");
61 const leadingControl =
62 picker ?? controls.querySelector<HTMLElement>('[data-chat-provider-unavailable="true"]');
63 if (!leadingControl) return null;
64 // Separators are display:none on phone widths; a hidden one takes no gap.
65 const separator = controls.querySelector<HTMLElement>("[data-resting-controls-separator]");
66 const separatorWidth = separator ? elementOuterWidth(separator) : 0;
67 const overflow = controls.querySelector<HTMLElement>("[data-resting-controls-overflow]");
68 const separatorAndGapWidth = separatorWidth > 0 ? separatorWidth + gap : 0;
69 const blocks = Array.from(controls.querySelectorAll<HTMLElement>("[data-resting-block]"));
70 return {
71 gap,
72 naturalFixedWidth:
73 (picker ? providerModelPickerNaturalWidth(picker) : elementOuterWidth(leadingControl)) +
74 separatorAndGapWidth,
75 minimumFixedWidth:
76 (picker ? providerModelPickerMinimumWidth(picker) : elementOuterWidth(leadingControl)) +
77 separatorAndGapWidth,
78 blockWidths: blocks.map(elementOuterWidth),
79 overflowWidth: overflow ? elementOuterWidth(overflow) : 0,
80 };