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

restingComposerControlsMeasurement.ts

104 lines4.7 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 // A label with its own max width, such as a long model name, never grows past it.
29 const labelMaxWidth = label ? Number.parseFloat(getComputedStyle(label).maxWidth) : Number.NaN;
30 const hiddenLabelWidth =
31 label && !labelIsCollapsed
32 ? Math.max(
33 0,
34 Math.min(
35 label.scrollWidth,
36 Number.isFinite(labelMaxWidth) ? labelMaxWidth : Number.POSITIVE_INFINITY,
37 ) - label.clientWidth,
38 )
39 : 0;
40 const maxWidth = Number.parseFloat(style.maxWidth);
41 const naturalWidth = Math.min(
42 renderedWidth + hiddenLabelWidth,
43 Number.isFinite(maxWidth) ? maxWidth : Number.POSITIVE_INFINITY,
44 );
45 return naturalWidth + elementInlineMarginWidth(picker);
47​
48function providerModelPickerMinimumWidth(picker: HTMLElement): number {
49 const minWidth = Number.parseFloat(getComputedStyle(picker).minWidth) || 0;
50 return minWidth + elementInlineMarginWidth(picker);
52​
53function controlBlockWidths(block: HTMLElement): { natural: number; iconOnly: number } {
54 const compact = block.dataset.composerBlockIconOnly === "true";
55 let natural = elementOuterWidth(block);
56 let iconOnly = natural;
57 for (const label of block.querySelectorAll<HTMLElement>("[data-composer-control-label]")) {
58 const labelStyle = getComputedStyle(label);
59 const inFlow = labelStyle.position !== "absolute";
60 // Phone sr-only clipping survives compact styles overriding the label width.
61 if (!inFlow && (!compact || labelStyle.clip !== "auto")) continue;
62 const labelWidth = label.scrollWidth;
63 const renderedWidth = inFlow ? label.getBoundingClientRect().width : 0;
64 const gap = Number.parseFloat(getComputedStyle(label.parentElement!).columnGap) || 0;
65 natural += labelWidth - renderedWidth + (inFlow ? 0 : gap);
66 iconOnly -= renderedWidth + (inFlow ? gap : 0);
67 }
68 for (const icon of block.querySelectorAll<HTMLElement>("[data-composer-control-compact-icon]")) {
69 const width = elementOuterWidth(icon);
70 const gap = Number.parseFloat(getComputedStyle(icon.parentElement!).columnGap) || 0;
71 natural -= compact ? width + gap : 0;
72 iconOnly += compact ? 0 : width + gap;
73 }
74 return { natural, iconOnly: Math.min(natural, iconOnly) };
76​
77/** Measure independently of controls hidden by the previous render. */
78export function measureRestingComposerControls(
79 controls: HTMLElement,
80): RestingComposerControlsMeasurement | null {
81 const gap = Number.parseFloat(getComputedStyle(controls).columnGap) || 0;
82 const picker = controls.querySelector<HTMLElement>("[data-chat-provider-model-picker]");
83 const leadingControl =
84 picker ?? controls.querySelector<HTMLElement>('[data-chat-provider-unavailable="true"]');
85 if (!leadingControl) return null;
86 const separator = controls.querySelector<HTMLElement>("[data-resting-controls-separator]");
87 const separatorWidth = separator ? elementOuterWidth(separator) : 0;
88 const overflow = controls.querySelector<HTMLElement>("[data-resting-controls-overflow]");
89 const separatorAndGapWidth = separatorWidth > 0 ? separatorWidth + gap : 0;
90 const blocks = Array.from(controls.querySelectorAll<HTMLElement>("[data-resting-block]"));
91 const widths = blocks.map(controlBlockWidths);
92 return {
93 gap,
94 naturalFixedWidth:
95 (picker ? providerModelPickerNaturalWidth(picker) : elementOuterWidth(leadingControl)) +
96 separatorAndGapWidth,
97 minimumFixedWidth:
98 (picker ? providerModelPickerMinimumWidth(picker) : elementOuterWidth(leadingControl)) +
99 separatorAndGapWidth,
100 blockWidths: widths.map((width) => width.natural),
101 iconOnlyBlockWidths: widths.map((width) => width.iconOnly),
102 overflowWidth: overflow ? elementOuterWidth(overflow) : 0,
103 };