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

composerFooterLayout.ts

206 lines6.8 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 timelineOverflows: boolean;
33}): boolean {
34 return (
35 input.isExistingThread &&
36 !input.isMobileViewport &&
37 input.timelineOverflows &&
38 input.isScrollCollapsed &&
39 !input.hasMultilinePrompt &&
40 !input.hasExpandedChrome
41 );
43​
44/**
45 * How much taller the empty expanded composer is than its resting row on
46 * desktop widths, from the layout classes in ChatComposer: the body loses
47 * 8px of top padding, the prompt clamps from min-h-17.5 (70px) to 32px, and
48 * the 48px footer leaves flow.
49 */
50export const COMPOSER_RESTING_EXPANSION_MIN_PX = 94;
51​
52/**
53 * Reserving only the resting height lets a scroll to the end land flush against the short
54 * composer, and the expansion that follows then covers the last rows because the timeline never
55 * moves for footer growth.
56 */
57export function resolveComposerTimelineInset(input: {
58 currentInset: number;
59 overlayHeight: number;
60 isResting: boolean;
61 restingOnlyHeight?: number;
62 expandedNoticesHeight?: number;
63}): number {
64 // Expanded notices temporarily cover the timeline. Remove their growth before
65 // rounding so fractional measurements reserve exactly the collapsed height.
66 const overlayHeight = Math.ceil(input.overlayHeight - (input.expandedNoticesHeight ?? 0));
67 return input.isResting
68 ? Math.max(
69 input.currentInset,
70 overlayHeight +
71 (input.currentInset === 0
72 ? COMPOSER_RESTING_EXPANSION_MIN_PX - (input.restingOnlyHeight ?? 0)
73 : 0),
74 )
75 : overlayHeight;
77​
78export function overlayComposerIsResting(input: {
79 composerMounted: boolean;
80 composerReportedResting: boolean;
81}): boolean {
82 return input.composerMounted && input.composerReportedResting;
84​
85export function shouldAnimateComposerRestingTransition(input: {
86 hasCompletedInitialLayout: boolean;
87 stateChanged: boolean;
88 hasInterruptedAnimation: boolean;
89}): boolean {
90 return input.hasCompletedInitialLayout && (input.stateChanged || input.hasInterruptedAnimation);
92​
93export function shouldUseCompactComposerPrimaryActions(
94 width: number | null,
95 options?: { hasWideActions?: boolean },
96): boolean {
97 if (!options?.hasWideActions) {
98 return false;
99 }
100 return width !== null && width < COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX;
102​
103export interface RestingComposerControlsMeasurement {
104 gap: number;
105 naturalFixedWidth: number;
106 minimumFixedWidth: number;
107 blockWidths: readonly number[];
108 overflowWidth: number;
109 iconOnlyBlockWidths?: readonly number[];
111​
112function restingComposerControlsWidth(
113 input: RestingComposerControlsMeasurement,
114 hiddenCount: number,
115 fixedWidth = input.naturalFixedWidth,
116 iconOnlyCount = 0,
117): number {
118 const { blockWidths, gap } = input;
119 const visibleCount = blockWidths.length - hiddenCount;
120 return (
121 fixedWidth +
122 blockWidths
123 .slice(0, visibleCount)
124 .reduce(
125 (sum, width, index) =>
126 sum +
127 (index >= blockWidths.length - iconOnlyCount
128 ? (input.iconOnlyBlockWidths?.[index] ?? width)
129 : width),
130 0,
131 ) +
132 (hiddenCount > 0 ? input.overflowWidth : 0) +
133 gap * (visibleCount + (hiddenCount > 0 ? 1 : 0))
134 );
136​
137/**
138 * The context strip reserves this much for the composer before deciding whether its own labels
139 * may expand. Judging against the currently visible controls instead lets the strip expand into
140 * space the composer just gave up, which shrinks the host, hides the controls again, and
141 * repeats.
142 */
143export function resolveRestingComposerControlsNaturalWidth(
144 input: RestingComposerControlsMeasurement,
145): number {
146 return restingComposerControlsWidth(input, 0);
148​
149const RESTING_CONTROLS_SLACK_PX = 1;
150​
151export function resolveRestingComposerControlsLayout(
152 input: RestingComposerControlsMeasurement & {
153 hostWidth: number;
154 previous?: { hiddenCount: number; iconOnlyCount?: number; visible: boolean };
155 },
156): { hiddenCount: number; iconOnlyCount?: number; visible: boolean } {
157 const { blockWidths, hostWidth, previous } = input;
158 const iconSteps = input.iconOnlyBlockWidths ? blockWidths.length : 0;
159 const previousStep = previous
160 ? previous.hiddenCount > 0
161 ? iconSteps + Math.min(previous.hiddenCount, blockWidths.length)
162 : Math.min(previous.iconOnlyCount ?? 0, iconSteps)
163 : 0;
164 let step = 0;
165 const widthAtStep = (candidate: number, fixedWidth = input.naturalFixedWidth) =>
166 restingComposerControlsWidth(
167 input,
168 Math.max(0, candidate - iconSteps),
169 fixedWidth,
170 Math.min(candidate, iconSteps),
171 );
172 // Promotions need a pixel of slack: recovering a flexible picker's natural
173 // width can jitter by a fraction of a pixel across renders. Demotions are
174 // immediate so a threshold cannot clip or flip React between layouts.
175 while (
176 step < iconSteps + blockWidths.length &&
177 widthAtStep(step) > hostWidth - (step < previousStep ? RESTING_CONTROLS_SLACK_PX : 0)
178 ) {
179 step += 1;
180 }
181 const hiddenCount = Math.max(0, step - iconSteps);
182 const iconOnlyCount = Math.min(step, iconSteps);
183 const minimumWidth = widthAtStep(step, input.minimumFixedWidth);
184 const visible =
185 previous && !previous.visible
186 ? minimumWidth <= hostWidth - RESTING_CONTROLS_SLACK_PX
187 : minimumWidth <= hostWidth;
188 return { hiddenCount, ...(input.iconOnlyBlockWidths ? { iconOnlyCount } : {}), visible };
190​
191export function resolveScrollToEndClearance(input: {
192 overlayHeight: number;
193 mainSurfaceTop: number;
194 button: { left: number; right: number };
195 attachments: ReadonlyArray<{ top: number; left: number; right: number }>;
196}): number {
197 let contentTop = input.mainSurfaceTop;
198 let top = contentTop;
199 for (const attachment of input.attachments) {
200 contentTop = Math.min(contentTop, attachment.top);
201 if (attachment.left < input.button.right && attachment.right > input.button.left) {
202 top = Math.min(top, attachment.top);
203 }
204 }
205 return Math.ceil(input.overlayHeight - (top - contentTop));