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

restingComposerControlsMeasurement.ts

99 lines4.5 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 labelIsCollapsed = label?.clientWidth === 0 && getComputedStyle(label).flexGrow === "0";
28 const hiddenLabelWidth =
29 label && !labelIsCollapsed ? Math.max(0, label.scrollWidth - label.clientWidth) : 0;
30 const maxWidth = Number.parseFloat(style.maxWidth);
31 const naturalWidth = Math.min(
32 renderedWidth + hiddenLabelWidth,
33 Number.isFinite(maxWidth) ? maxWidth : Number.POSITIVE_INFINITY,
34 );
35 return naturalWidth + elementInlineMarginWidth(picker);
37​
38function providerModelPickerMinimumWidth(picker: HTMLElement): number {
39 const minWidth = Number.parseFloat(getComputedStyle(picker).minWidth) || 0;
40 return minWidth + elementInlineMarginWidth(picker);
42​
43function controlBlockWidths(block: HTMLElement): { natural: number; iconOnly: number } {
44 const compact = block.dataset.composerBlockIconOnly === "true";
45 let natural = elementOuterWidth(block);
46 let iconOnly = natural;
47 for (const label of block.querySelectorAll<HTMLElement>("[data-composer-control-label]")) {
48 const labelStyle = getComputedStyle(label);
49 const inFlow = labelStyle.position !== "absolute";
50 // Phone widths already hide the build label with sr-only. Its clipping
51 // remains in effect even when compact styles replace its one-pixel width.
52 if (!inFlow && (!compact || labelStyle.clip !== "auto")) continue;
53 const labelWidth = label.scrollWidth;
54 const renderedWidth = inFlow ? label.getBoundingClientRect().width : 0;
55 const gap = Number.parseFloat(getComputedStyle(label.parentElement!).columnGap) || 0;
56 natural += labelWidth - renderedWidth + (inFlow ? 0 : gap);
57 iconOnly -= renderedWidth + (inFlow ? gap : 0);
58 }
59 for (const icon of block.querySelectorAll<HTMLElement>("[data-composer-control-compact-icon]")) {
60 const width = elementOuterWidth(icon);
61 const gap = Number.parseFloat(getComputedStyle(icon.parentElement!).columnGap) || 0;
62 natural -= compact ? width + gap : 0;
63 iconOnly += compact ? 0 : width + gap;
64 }
65 return { natural, iconOnly: Math.min(natural, iconOnly) };
67​
68/**
69 * Both the composer (deciding which blocks move into overflow) and the
70 * context strip (deciding whether its labels may expand) read the same
71 * numbers, so neither decision depends on what the other one hid last render.
72 */
73export function measureRestingComposerControls(
74 controls: HTMLElement,
75): RestingComposerControlsMeasurement | null {
76 const gap = Number.parseFloat(getComputedStyle(controls).columnGap) || 0;
77 const picker = controls.querySelector<HTMLElement>("[data-chat-provider-model-picker]");
78 const leadingControl =
79 picker ?? controls.querySelector<HTMLElement>('[data-chat-provider-unavailable="true"]');
80 if (!leadingControl) return null;
81 const separator = controls.querySelector<HTMLElement>("[data-resting-controls-separator]");
82 const separatorWidth = separator ? elementOuterWidth(separator) : 0;
83 const overflow = controls.querySelector<HTMLElement>("[data-resting-controls-overflow]");
84 const separatorAndGapWidth = separatorWidth > 0 ? separatorWidth + gap : 0;
85 const blocks = Array.from(controls.querySelectorAll<HTMLElement>("[data-resting-block]"));
86 const widths = blocks.map(controlBlockWidths);
87 return {
88 gap,
89 naturalFixedWidth:
90 (picker ? providerModelPickerNaturalWidth(picker) : elementOuterWidth(leadingControl)) +
91 separatorAndGapWidth,
92 minimumFixedWidth:
93 (picker ? providerModelPickerMinimumWidth(picker) : elementOuterWidth(leadingControl)) +
94 separatorAndGapWidth,
95 blockWidths: widths.map((width) => width.natural),
96 iconOnlyBlockWidths: widths.map((width) => width.iconOnly),
97 overflowWidth: overflow ? elementOuterWidth(overflow) : 0,
98 };