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

restingComposerControlsMeasurement.ts

77 lines3.3 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 const hiddenLabelWidth = label ? Math.max(0, label.scrollWidth - label.clientWidth) : 0;
28 const maxWidth = Number.parseFloat(style.maxWidth);
29 const naturalWidth = Math.min(
30 renderedWidth + hiddenLabelWidth,
31 Number.isFinite(maxWidth) ? maxWidth : Number.POSITIVE_INFINITY,
32 );
33 return naturalWidth + elementInlineMarginWidth(picker);
35​
36function providerModelPickerMinimumWidth(picker: HTMLElement): number {
37 const minWidth = Number.parseFloat(getComputedStyle(picker).minWidth) || 0;
38 return minWidth + elementInlineMarginWidth(picker);
40​
41/**
42 * Read the natural widths of the resting composer controls from the DOM.
43 *
44 * Both the composer (deciding which blocks move into overflow) and the
45 * context strip (deciding whether its labels may expand) read the same
46 * numbers, so neither decision depends on what the other one hid last render.
47 *
48 * Hidden blocks and the unused overflow trigger stay mounted out of flow at
49 * full size. The picker is the one flexible item: its intended width is
50 * recovered from the truncated label.
51 */
52export function measureRestingComposerControls(
53 controls: HTMLElement,
54): RestingComposerControlsMeasurement | null {
55 const gap = Number.parseFloat(getComputedStyle(controls).columnGap) || 0;
56 const picker = controls.querySelector<HTMLElement>("[data-chat-provider-model-picker]");
57 const leadingControl =
58 picker ?? controls.querySelector<HTMLElement>('[data-chat-provider-unavailable="true"]');
59 if (!leadingControl) return null;
60 // Separators are display:none on phone widths; a hidden one takes no gap.
61 const separator = controls.querySelector<HTMLElement>("[data-resting-controls-separator]");
62 const separatorWidth = separator ? elementOuterWidth(separator) : 0;
63 const overflow = controls.querySelector<HTMLElement>("[data-resting-controls-overflow]");
64 const separatorAndGapWidth = separatorWidth > 0 ? separatorWidth + gap : 0;
65 const blocks = Array.from(controls.querySelectorAll<HTMLElement>("[data-resting-block]"));
66 return {
67 gap,
68 naturalFixedWidth:
69 (picker ? providerModelPickerNaturalWidth(picker) : elementOuterWidth(leadingControl)) +
70 separatorAndGapWidth,
71 minimumFixedWidth:
72 (picker ? providerModelPickerMinimumWidth(picker) : elementOuterWidth(leadingControl)) +
73 separatorAndGapWidth,
74 blockWidths: blocks.map(elementOuterWidth),
75 overflowWidth: overflow ? elementOuterWidth(overflow) : 0,
76 };