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

composerFooterLayout.ts

182 lines6.9 KB

1export const COMPOSER_FOOTER_COMPACT_BREAKPOINT_PX = 620;
2export const COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX = 780;
3export const 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 isFocused: boolean;
30 isScrollCollapsed: boolean;
31 hasExpandedChrome: boolean;
32 collapseOnBlur: boolean;
33}): boolean {
34 // Passive draft content is deliberately absent here. Resting only clamps
35 // the prompt row and overlays its actions; non-image attachment and context
36 // rows keep their natural height above it while image previews move inline.
37 // Banners and the tasks badge dock above the surface, so they are absent
38 // too. Whether the context strip can host the relocated controls is
39 // deliberately absent here: resting reclaims vertical space at every
40 // desktop width, and where the strip is missing or too narrow the controls
41 // simply return when the composer is focused.
42 //
43 // A scroll collapse rests the composer regardless of the blur preference:
44 // the user asked for it with the gesture, and it lifts on the next
45 // composer interaction. With blur collapse off, losing focus alone never
46 // rests the composer.
47 const collapsed = input.isScrollCollapsed || (input.collapseOnBlur && !input.isFocused);
48 return input.isExistingThread && !input.isMobileViewport && collapsed && !input.hasExpandedChrome;
50​
51/**
52 * How much taller the empty expanded composer is than its resting row on
53 * desktop widths, from the layout classes in ChatComposer: the body loses
54 * 8px of top padding, the prompt clamps from min-h-17.5 (70px) to 32px, and
55 * the 48px footer leaves flow.
56 */
57export const COMPOSER_RESTING_EXPANSION_MIN_PX = 94;
58​
59/**
60 * The space the timeline reserves at its end for the composer overlay.
61 *
62 * The overlay is measured live, but a resting composer is much shorter than
63 * an expanded one. Reserving only the resting height lets a scroll to the end
64 * land flush against the short composer, and the expansion that follows then
65 * covers the last rows because the timeline never moves for footer growth.
66 * While resting, the reservation keeps the last expanded height, or at least
67 * the resting height plus the empty expansion, so expanding again changes
68 * nothing above the composer. An expanded measurement is authoritative and
69 * may shrink it.
70 */
71export function resolveComposerTimelineInset(input: {
72 currentInset: number;
73 overlayHeight: number;
74 isResting: boolean;
75}): number {
76 return input.isResting
77 ? Math.max(input.currentInset, input.overlayHeight + COMPOSER_RESTING_EXPANSION_MIN_PX)
78 : input.overlayHeight;
80​
81export function shouldAnimateComposerRestingTransition(input: {
82 hasCompletedInitialLayout: boolean;
83 stateChanged: boolean;
84 hasInterruptedAnimation: boolean;
85}): boolean {
86 return input.hasCompletedInitialLayout && (input.stateChanged || input.hasInterruptedAnimation);
88​
89export function shouldUseCompactComposerPrimaryActions(
90 width: number | null,
91 options?: { hasWideActions?: boolean },
92): boolean {
93 if (!options?.hasWideActions) {
94 return false;
95 }
96 return width !== null && width < COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX;
98​
99export interface RestingComposerControlsMeasurement {
100 gap: number;
101 naturalFixedWidth: number;
102 minimumFixedWidth: number;
103 blockWidths: readonly number[];
104 overflowWidth: number;
106​
107function restingComposerControlsWidth(
108 input: RestingComposerControlsMeasurement,
109 hiddenCount: number,
110 fixedWidth = input.naturalFixedWidth,
111): number {
112 const { blockWidths, gap } = input;
113 const visibleCount = blockWidths.length - hiddenCount;
114 return (
115 fixedWidth +
116 blockWidths.slice(0, visibleCount).reduce((sum, width) => sum + width, 0) +
117 (hiddenCount > 0 ? input.overflowWidth : 0) +
118 gap * (visibleCount + (hiddenCount > 0 ? 1 : 0))
119 );
121​
122/**
123 * The width the resting controls take with nothing moved into overflow.
124 *
125 * The context strip reserves this much for the composer before deciding
126 * whether its own labels may expand. Judging against the currently visible
127 * controls instead lets the strip expand into space the composer just gave
128 * up, which shrinks the host, hides the controls again, and repeats.
129 */
130export function resolveRestingComposerControlsNaturalWidth(
131 input: RestingComposerControlsMeasurement,
132): number {
133 return restingComposerControlsWidth(input, 0);
135​
136/**
137 * Decide how many trailing resting control blocks move into the overflow
138 * menu, and whether the cluster can show at all, from natural widths.
139 *
140 * Trailing blocks hide before the model picker shrinks. Once they are all in
141 * the overflow menu, the picker may contract to its minimum readable width;
142 * below that the whole cluster hides rather than clipping.
143 */
144const RESTING_CONTROLS_SLACK_PX = 1;
145​
146export function resolveRestingComposerControlsLayout(
147 input: RestingComposerControlsMeasurement & {
148 hostWidth: number;
149 previous?: { hiddenCount: number; visible: boolean };
150 },
151): { hiddenCount: number; visible: boolean } {
152 const { blockWidths, hostWidth, previous } = input;
153 let hiddenCount = 0;
154 while (
155 hiddenCount < blockWidths.length &&
156 restingComposerControlsWidth(input, hiddenCount) > hostWidth
157 ) {
158 hiddenCount += 1;
159 }
160 // Growing the overflow menu is unconditional, or the controls would clip.
161 // Shrinking it has to earn a pixel of slack first: the picker is flexible,
162 // so its natural width is recovered from a truncated label whose
163 // scrollWidth is integral while the rendered box is fractional. The
164 // composer re-measures on every render, so without that margin a host
165 // sitting exactly on a threshold flips a block in and out until React
166 // gives up with "Maximum update depth exceeded".
167 if (previous) {
168 const previousHiddenCount = Math.min(previous.hiddenCount, blockWidths.length);
169 while (
170 hiddenCount < previousHiddenCount &&
171 restingComposerControlsWidth(input, hiddenCount) > hostWidth - RESTING_CONTROLS_SLACK_PX
172 ) {
173 hiddenCount += 1;
174 }
175 }
176 const minimumWidth = restingComposerControlsWidth(input, hiddenCount, input.minimumFixedWidth);
177 const visible =
178 previous && !previous.visible
179 ? minimumWidth <= hostWidth - RESTING_CONTROLS_SLACK_PX
180 : minimumWidth <= hostWidth;
181 return { hiddenCount, visible };