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

composerFooterLayout.ts

215 lines7.9 KB

1export const COMPOSER_FOOTER_COMPACT_BREAKPOINT_PX = 620;
2export const COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX = 780;
3const RESTING_COMPOSER_IMAGE_THUMBNAIL_LIMIT = 3;
4​
5export function getRestingComposerImagePreviewCounts(imageCount: number): {
6 visibleCount: number;
7 overflowCount: number;
8} {
9 const visibleCount = Math.min(imageCount, RESTING_COMPOSER_IMAGE_THUMBNAIL_LIMIT);
10 return {
11 visibleCount,
12 overflowCount: Math.max(0, imageCount - visibleCount),
13 };
15​
16export function shouldUseCompactComposerFooter(
17 width: number | null,
18 options?: { hasWideActions?: boolean },
19): boolean {
20 const breakpoint = options?.hasWideActions
21 ? COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX
22 : COMPOSER_FOOTER_COMPACT_BREAKPOINT_PX;
23 return width !== null && width < breakpoint;
25​
26export function shouldUseRestingComposerLayout(input: {
27 isExistingThread: boolean;
28 isMobileViewport: boolean;
29 isScrollCollapsed: boolean;
30 hasExpandedChrome: boolean;
31 hasMultilinePrompt: boolean;
32 /** Whether the timeline has more content than fits above the composer. */
33 timelineOverflows: boolean;
34}): boolean {
35 // Multiline drafts stay readable. Resting only clamps a single prompt
36 // line and overlays its actions; non-image attachment and context
37 // rows keep their natural height above it while image previews move inline.
38 // Banners and the tasks badge dock above the surface, so they are absent
39 // too. The context strip is optional: collapsed controls use it when
40 // present and otherwise occupy a compact row inside the composer.
41 //
42 // Only a timeline scroll rests the composer: the user asked for it with the
43 // gesture, and it lifts on the next composer interaction. Losing focus never
44 // rests it, so clicking a message, copying output, or selecting text for a
45 // citation leaves the composer where it was.
46 //
47 // Resting exists to give reading space back to the timeline. A thread that
48 // fits above the composer has nothing to reclaim, so it stays expanded and
49 // never shows the collapsed row that a fresh thread would otherwise open on.
50 return (
51 input.isExistingThread &&
52 !input.isMobileViewport &&
53 input.timelineOverflows &&
54 input.isScrollCollapsed &&
55 !input.hasMultilinePrompt &&
56 !input.hasExpandedChrome
57 );
59​
60/**
61 * How much taller the empty expanded composer is than its resting row on
62 * desktop widths, from the layout classes in ChatComposer: the body loses
63 * 8px of top padding, the prompt clamps from min-h-17.5 (70px) to 32px, and
64 * the 48px footer leaves flow.
65 */
66export const COMPOSER_RESTING_EXPANSION_MIN_PX = 94;
67​
68/**
69 * The space the timeline reserves at its end for the composer overlay.
70 *
71 * The overlay is measured live, but a resting composer is much shorter than
72 * an expanded one. Reserving only the resting height lets a scroll to the end
73 * land flush against the short composer, and the expansion that follows then
74 * covers the last rows because the timeline never moves for footer growth.
75 * While resting, keep the measured expanded height. Estimate the empty
76 * expansion only before that measurement exists: strip mounting can otherwise
77 * inflate the estimate mid-transition and move the timeline. An expanded
78 * measurement is authoritative and may shrink the reservation.
79 */
80export function resolveComposerTimelineInset(input: {
81 currentInset: number;
82 overlayHeight: number;
83 isResting: boolean;
84 restingOnlyHeight?: number;
85}): number {
86 return input.isResting
87 ? Math.max(
88 input.currentInset,
89 input.overlayHeight +
90 (input.currentInset === 0
91 ? COMPOSER_RESTING_EXPANSION_MIN_PX - (input.restingOnlyHeight ?? 0)
92 : 0),
93 )
94 : input.overlayHeight;
96​
97export function shouldAnimateComposerRestingTransition(input: {
98 hasCompletedInitialLayout: boolean;
99 stateChanged: boolean;
100 hasInterruptedAnimation: boolean;
101}): boolean {
102 return input.hasCompletedInitialLayout && (input.stateChanged || input.hasInterruptedAnimation);
104​
105export function shouldUseCompactComposerPrimaryActions(
106 width: number | null,
107 options?: { hasWideActions?: boolean },
108): boolean {
109 if (!options?.hasWideActions) {
110 return false;
111 }
112 return width !== null && width < COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX;
114​
115export interface RestingComposerControlsMeasurement {
116 gap: number;
117 naturalFixedWidth: number;
118 minimumFixedWidth: number;
119 blockWidths: readonly number[];
120 overflowWidth: number;
122​
123function restingComposerControlsWidth(
124 input: RestingComposerControlsMeasurement,
125 hiddenCount: number,
126 fixedWidth = input.naturalFixedWidth,
127): number {
128 const { blockWidths, gap } = input;
129 const visibleCount = blockWidths.length - hiddenCount;
130 return (
131 fixedWidth +
132 blockWidths.slice(0, visibleCount).reduce((sum, width) => sum + width, 0) +
133 (hiddenCount > 0 ? input.overflowWidth : 0) +
134 gap * (visibleCount + (hiddenCount > 0 ? 1 : 0))
135 );
137​
138/**
139 * The width the resting controls take with nothing moved into overflow.
140 *
141 * The context strip reserves this much for the composer before deciding
142 * whether its own labels may expand. Judging against the currently visible
143 * controls instead lets the strip expand into space the composer just gave
144 * up, which shrinks the host, hides the controls again, and repeats.
145 */
146export function resolveRestingComposerControlsNaturalWidth(
147 input: RestingComposerControlsMeasurement,
148): number {
149 return restingComposerControlsWidth(input, 0);
151​
152/**
153 * Decide how many trailing resting control blocks move into the overflow
154 * menu, and whether the cluster can show at all, from natural widths.
155 *
156 * Trailing blocks hide before the model picker shrinks. Once they are all in
157 * the overflow menu, the picker may contract to its minimum readable width;
158 * below that the whole cluster hides rather than clipping.
159 */
160const RESTING_CONTROLS_SLACK_PX = 1;
161​
162export function resolveRestingComposerControlsLayout(
163 input: RestingComposerControlsMeasurement & {
164 hostWidth: number;
165 previous?: { hiddenCount: number; visible: boolean };
166 },
167): { hiddenCount: number; visible: boolean } {
168 const { blockWidths, hostWidth, previous } = input;
169 let hiddenCount = 0;
170 while (
171 hiddenCount < blockWidths.length &&
172 restingComposerControlsWidth(input, hiddenCount) > hostWidth
173 ) {
174 hiddenCount += 1;
175 }
176 // Growing the overflow menu is unconditional, or the controls would clip.
177 // Shrinking it has to earn a pixel of slack first: the picker is flexible,
178 // so its natural width is recovered from a truncated label whose
179 // scrollWidth is integral while the rendered box is fractional. The
180 // composer re-measures on every render, so without that margin a host
181 // sitting exactly on a threshold flips a block in and out until React
182 // gives up with "Maximum update depth exceeded".
183 if (previous) {
184 const previousHiddenCount = Math.min(previous.hiddenCount, blockWidths.length);
185 while (
186 hiddenCount < previousHiddenCount &&
187 restingComposerControlsWidth(input, hiddenCount) > hostWidth - RESTING_CONTROLS_SLACK_PX
188 ) {
189 hiddenCount += 1;
190 }
191 }
192 const minimumWidth = restingComposerControlsWidth(input, hiddenCount, input.minimumFixedWidth);
193 const visible =
194 previous && !previous.visible
195 ? minimumWidth <= hostWidth - RESTING_CONTROLS_SLACK_PX
196 : minimumWidth <= hostWidth;
197 return { hiddenCount, visible };
199​
200export function resolveScrollToEndClearance(input: {
201 overlayHeight: number;
202 mainSurfaceTop: number;
203 button: { left: number; right: number };
204 attachments: ReadonlyArray<{ top: number; left: number; right: number }>;
205}): number {
206 let contentTop = input.mainSurfaceTop;
207 let top = contentTop;
208 for (const attachment of input.attachments) {
209 contentTop = Math.min(contentTop, attachment.top);
210 if (attachment.left < input.button.right && attachment.right > input.button.left) {
211 top = Math.min(top, attachment.top);
212 }
213 }
214 return Math.ceil(input.overlayHeight - (top - contentTop));