Skip to content

Commit 50f1602

Browse files
committed
fix(web): respect panel motion in composer transitions
1 parent fb3d165 commit 50f1602

5 files changed

Lines changed: 93 additions & 36 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,6 @@ import {
367367
import { deriveLatestContextWindowSnapshot, formatContextWindowTokens } from "../lib/contextWindow";
368368
import {
369369
DRAFT_HERO_TRANSITION_ANIMATION_ID,
370-
DRAFT_HERO_TRANSITION_DURATION_MS,
371370
DRAFT_HERO_TRANSITION_EASING,
372371
MOBILE_COMPOSER_VIEW_TRANSITION_NAME,
373372
MOBILE_DRAFT_HEADLINE_VIEW_TRANSITION_NAME,
@@ -490,7 +489,11 @@ const EMPTY_PROVIDERS: ServerProvider[] = [];
490489
const EMPTY_USAGE_LIMIT_SOURCES: UsageLimitSourceSnapshots = [];
491490
const EMPTY_PROVIDER_SKILLS: ServerProvider["skills"] = [];
492491
const EMPTY_PENDING_USER_INPUT_ANSWERS: Record<string, PendingUserInputDraftAnswer> = {};
493-
function useDraftHeroLayoutTransition(isDraftHeroState: boolean) {
492+
function useDraftHeroLayoutTransition(
493+
isDraftHeroState: boolean,
494+
animationsActive: boolean,
495+
animationDurationMs: number,
496+
) {
494497
const transitionGroupRef = useRef<HTMLDivElement | null>(null);
495498
const composerAnchorRef = useRef<HTMLDivElement | null>(null);
496499
const previousStateRef = useRef(isDraftHeroState);
@@ -510,9 +513,6 @@ function useDraftHeroLayoutTransition(isDraftHeroState: boolean) {
510513
const transitionGroup = transitionGroupRef.current;
511514
const nextComposerRect = composerAnchorRef.current?.getBoundingClientRect() ?? null;
512515
const stateChanged = previousStateRef.current !== isDraftHeroState;
513-
const prefersReducedMotion =
514-
typeof window !== "undefined" &&
515-
window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
516516
const mobileComposerTransitionActive =
517517
typeof document !== "undefined" &&
518518
document.documentElement.dataset.mobileComposerRouteTransition === "true";
@@ -523,7 +523,7 @@ function useDraftHeroLayoutTransition(isDraftHeroState: boolean) {
523523
const previousComposerRect = previousComposerRectRef.current;
524524
if (
525525
stateChanged &&
526-
!prefersReducedMotion &&
526+
animationsActive &&
527527
!mobileComposerTransitionActive &&
528528
transitionGroup &&
529529
previousComposerRect &&
@@ -539,7 +539,7 @@ function useDraftHeroLayoutTransition(isDraftHeroState: boolean) {
539539
{ transform: "translate3d(0, 0, 0)" },
540540
],
541541
{
542-
duration: DRAFT_HERO_TRANSITION_DURATION_MS,
542+
duration: animationDurationMs,
543543
easing: DRAFT_HERO_TRANSITION_EASING,
544544
},
545545
);
@@ -558,7 +558,7 @@ function useDraftHeroLayoutTransition(isDraftHeroState: boolean) {
558558

559559
previousStateRef.current = isDraftHeroState;
560560
previousComposerRectRef.current = nextComposerRect;
561-
}, [isDraftHeroState]);
561+
}, [animationDurationMs, animationsActive, isDraftHeroState]);
562562

563563
return [attachTransitionGroupRef, attachComposerAnchorRef, captureComposerRect] as const;
564564
}
@@ -3275,7 +3275,11 @@ export default function ChatView(props: ChatViewProps) {
32753275
attachDraftHeroTransitionGroupRef,
32763276
attachDraftHeroComposerAnchorRef,
32773277
captureDraftHeroComposerRect,
3278-
] = useDraftHeroLayoutTransition(isDraftHeroState);
3278+
] = useDraftHeroLayoutTransition(
3279+
isDraftHeroState,
3280+
panelAnimationsActive,
3281+
panelAnimationDurationMs,
3282+
);
32793283

32803284
const gitCwd = activeProject
32813285
? projectScriptCwd({
@@ -6996,13 +7000,19 @@ export default function ChatView(props: ChatViewProps) {
69967000
const dockStarted = new Promise<void>((resolve) => {
69977001
resolveDockStarted = resolve;
69987002
});
6999-
const dockTransition = runMobileComposerTransition(() => {
7000-
flushSync(() => {
7001-
captureDraftHeroComposerRect();
7002-
setDockedDraftHeroThreadKey(activeThreadKey);
7003-
});
7004-
resolveDockStarted?.();
7005-
});
7003+
const dockTransition = runMobileComposerTransition(
7004+
() => {
7005+
flushSync(() => {
7006+
captureDraftHeroComposerRect();
7007+
setDockedDraftHeroThreadKey(activeThreadKey);
7008+
});
7009+
resolveDockStarted?.();
7010+
},
7011+
{
7012+
active: panelAnimationsActive,
7013+
durationMs: panelAnimationDurationMs,
7014+
},
7015+
);
70067016
void dockTransition.catch(() => resolveDockStarted?.());
70077017
await dockStarted;
70087018
}

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,6 @@ function SnapShotAttachmentFrame({
312312

313313
const COMPOSER_SCROLL_COLLAPSE_THRESHOLD_PX = 24;
314314
const COMPOSER_SCROLL_GESTURE_RESET_MS = 120;
315-
const COMPOSER_RESTING_TRANSITION_DURATION_MS = 280;
316315
const COMPOSER_RESTING_TRANSITION_CLEANUP_BUFFER_MS = 50;
317316
const COMPOSER_RESTING_TRANSITION_EASING = "cubic-bezier(0.32, 0.72, 0, 1)";
318317
const COMPOSER_RESTING_CONTROLS_ARRIVAL_DRIFT_PX = 4;
@@ -322,6 +321,8 @@ function useComposerRestingTransition(
322321
isResting: boolean,
323322
restingControlsRef: React.RefObject<HTMLDivElement | null>,
324323
onOverlayHeightChange: (height: number) => void,
324+
animationsActive: boolean,
325+
animationDurationMs: number,
325326
) {
326327
const elementRef = useRef<HTMLDivElement>(null);
327328
const isCollapsedRef = useRef(isCollapsed);
@@ -440,7 +441,6 @@ function useComposerRestingTransition(
440441
const previousHeight = interruptedHeight ?? previousHeightRef.current;
441442
const targetChanged =
442443
interruptedTargetHeight === null || Math.abs(interruptedTargetHeight - nextHeight) >= 0.5;
443-
const prefersReducedMotion = window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
444444
const shouldAnimate = shouldAnimateComposerRestingTransition({
445445
hasCompletedInitialLayout: hasCompletedInitialLayoutRef.current,
446446
stateChanged,
@@ -449,18 +449,16 @@ function useComposerRestingTransition(
449449

450450
if (
451451
shouldAnimate &&
452-
!prefersReducedMotion &&
452+
animationsActive &&
453453
previousHeight !== null &&
454454
Math.abs(previousHeight - nextHeight) >= 0.5
455455
) {
456456
const remainingDuration =
457457
typeof interruptedDuration === "number" && interruptedCurrentTime !== null
458458
? Math.max(1, interruptedDuration - interruptedCurrentTime)
459-
: COMPOSER_RESTING_TRANSITION_DURATION_MS;
459+
: animationDurationMs;
460460
const duration =
461-
interruptedHeight !== null && !targetChanged
462-
? remainingDuration
463-
: COMPOSER_RESTING_TRANSITION_DURATION_MS;
461+
interruptedHeight !== null && !targetChanged ? remainingDuration : animationDurationMs;
464462
element.style.overflow = "clip";
465463
surface.style.height = "100%";
466464

@@ -660,7 +658,13 @@ function useComposerRestingTransition(
660658
actionFromBottom: nextActionTop === null ? null : nextRect.bottom - nextActionTop,
661659
};
662660
},
663-
[clearTransitionStyles, onOverlayHeightChange, restingControlsRef],
661+
[
662+
animationDurationMs,
663+
animationsActive,
664+
clearTransitionStyles,
665+
onOverlayHeightChange,
666+
restingControlsRef,
667+
],
664668
);
665669

666670
useLayoutEffect(() => {
@@ -3946,6 +3950,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
39463950
isComposerResting,
39473951
restingComposerControlsRef,
39483952
onComposerOverlayHeightChange,
3953+
panelAnimationsActive,
3954+
panelAnimationDurationMs,
39493955
);
39503956
const canTrackComposerScrollGesture =
39513957
routeKind === "server" && activeThreadId !== null && !isMobileViewport;

‎apps/web/src/components/chat/draftHeroTransition.test.ts‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ describe("runMobileComposerTransition", () => {
5656
finishTransition = resolve;
5757
});
5858
const dataset: Record<string, string> = {};
59+
const style = { setProperty: vi.fn(), removeProperty: vi.fn() };
5960
vi.stubGlobal("document", {
60-
documentElement: { dataset },
61+
documentElement: { dataset, style },
6162
getAnimations: () => [],
6263
startViewTransition: (update: () => void | Promise<void>) => {
6364
void update();
@@ -68,7 +69,10 @@ describe("runMobileComposerTransition", () => {
6869
matchMedia: (query: string) => ({ matches: query === "(max-width: 639px)" }),
6970
});
7071

71-
const transition = runMobileComposerTransition(() => undefined);
72+
const transition = runMobileComposerTransition(() => undefined, {
73+
active: true,
74+
durationMs: 240,
75+
});
7276
await Promise.resolve();
7377

7478
let handoffComplete = false;
@@ -85,25 +89,48 @@ describe("runMobileComposerTransition", () => {
8589

8690
it("uses a scoped view transition on mobile", async () => {
8791
const dataset: Record<string, string> = {};
92+
const style = { setProperty: vi.fn(), removeProperty: vi.fn() };
8893
const startViewTransition = vi.fn((update: () => void | Promise<void>) => ({
8994
finished: Promise.resolve(update()).then(() => undefined),
9095
}));
9196
vi.stubGlobal("document", {
92-
documentElement: { dataset },
97+
documentElement: { dataset, style },
9398
startViewTransition,
9499
});
95100
vi.stubGlobal("window", {
96101
matchMedia: (query: string) => ({ matches: query === "(max-width: 639px)" }),
97102
});
98103
const update = vi.fn();
99104

100-
await runMobileComposerTransition(update);
105+
await runMobileComposerTransition(update, { active: true, durationMs: 360 });
101106

102107
expect(startViewTransition).toHaveBeenCalledOnce();
103108
expect(update).toHaveBeenCalledOnce();
109+
expect(style.setProperty).toHaveBeenCalledWith(
110+
"--mobile-composer-transition-duration",
111+
"360ms",
112+
);
113+
expect(style.removeProperty).toHaveBeenCalledWith("--mobile-composer-transition-duration");
104114
expect(dataset).not.toHaveProperty("mobileComposerRouteTransition");
105115
});
106116

117+
it("updates without a view transition when panel animations are inactive", async () => {
118+
const startViewTransition = vi.fn();
119+
vi.stubGlobal("document", {
120+
documentElement: { dataset: {} },
121+
startViewTransition,
122+
});
123+
vi.stubGlobal("window", {
124+
matchMedia: () => ({ matches: true }),
125+
});
126+
const update = vi.fn();
127+
128+
await runMobileComposerTransition(update, { active: false, durationMs: 360 });
129+
130+
expect(startViewTransition).not.toHaveBeenCalled();
131+
expect(update).toHaveBeenCalledOnce();
132+
});
133+
107134
it("updates without a view transition when reduced motion is preferred", async () => {
108135
const startViewTransition = vi.fn();
109136
vi.stubGlobal("document", {
@@ -115,7 +142,7 @@ describe("runMobileComposerTransition", () => {
115142
});
116143
const update = vi.fn();
117144

118-
await runMobileComposerTransition(update);
145+
await runMobileComposerTransition(update, { active: true, durationMs: 360 });
119146

120147
expect(startViewTransition).not.toHaveBeenCalled();
121148
expect(update).toHaveBeenCalledOnce();

‎apps/web/src/components/chat/draftHeroTransition.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export const DRAFT_HERO_TRANSITION_ANIMATION_ID = "t3-draft-hero-transition";
2-
export const DRAFT_HERO_TRANSITION_DURATION_MS = 180;
32
export const DRAFT_HERO_TRANSITION_EASING = "cubic-bezier(0.4, 0, 0.2, 1)";
43
export const MOBILE_COMPOSER_VIEW_TRANSITION_NAME = "t3-mobile-composer";
54
export const MOBILE_DRAFT_HEADLINE_VIEW_TRANSITION_NAME = "t3-mobile-draft-headline";
5+
const MOBILE_COMPOSER_TRANSITION_DURATION_PROPERTY = "--mobile-composer-transition-duration";
66

77
type ComposerViewTransition = {
88
readonly finished: Promise<void>;
@@ -39,6 +39,7 @@ export async function waitForDraftHeroTransition(): Promise<void> {
3939

4040
export async function runMobileComposerTransition(
4141
update: () => void | Promise<void>,
42+
options: { active: boolean; durationMs: number },
4243
): Promise<void> {
4344
if (typeof document === "undefined" || typeof window === "undefined") {
4445
await update();
@@ -49,7 +50,12 @@ export async function runMobileComposerTransition(
4950
const mobileViewport = window.matchMedia?.("(max-width: 639px)").matches ?? false;
5051
const prefersReducedMotion =
5152
window.matchMedia?.("(prefers-reduced-motion: reduce)").matches ?? false;
52-
if (!mobileViewport || prefersReducedMotion || !transitionDocument.startViewTransition) {
53+
if (
54+
!options.active ||
55+
!mobileViewport ||
56+
prefersReducedMotion ||
57+
!transitionDocument.startViewTransition
58+
) {
5359
await update();
5460
return;
5561
}
@@ -61,6 +67,10 @@ export async function runMobileComposerTransition(
6167
await update();
6268
};
6369
let transitionFinished: Promise<void> | null = null;
70+
transitionDocument.documentElement.style.setProperty(
71+
MOBILE_COMPOSER_TRANSITION_DURATION_PROPERTY,
72+
`${String(options.durationMs)}ms`,
73+
);
6474
transitionDocument.documentElement.dataset.mobileComposerRouteTransition = "true";
6575
try {
6676
const transition = transitionDocument.startViewTransition(runUpdate);
@@ -78,5 +88,8 @@ export async function runMobileComposerTransition(
7888
activeMobileComposerTransition = null;
7989
}
8090
delete transitionDocument.documentElement.dataset.mobileComposerRouteTransition;
91+
transitionDocument.documentElement.style.removeProperty(
92+
MOBILE_COMPOSER_TRANSITION_DURATION_PROPERTY,
93+
);
8194
}
8295
}

‎apps/web/src/index.css‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ html[data-mobile-composer-route-transition="true"]::view-transition-new(root) {
1313
}
1414

1515
html[data-mobile-composer-route-transition="true"]::view-transition-group(t3-mobile-composer) {
16-
animation-duration: 180ms;
16+
animation-duration: var(--mobile-composer-transition-duration);
1717
animation-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
1818
}
1919

@@ -22,12 +22,12 @@ html[data-mobile-composer-route-transition="true"]::view-transition-image-pair(t
2222
}
2323

2424
html[data-mobile-composer-route-transition="true"]::view-transition-old(t3-mobile-composer) {
25-
animation: t3-mobile-composer-old 180ms linear both;
25+
animation: t3-mobile-composer-old var(--mobile-composer-transition-duration) linear both;
2626
mix-blend-mode: normal;
2727
}
2828

2929
html[data-mobile-composer-route-transition="true"]::view-transition-new(t3-mobile-composer) {
30-
animation: t3-mobile-composer-new 180ms linear both;
30+
animation: t3-mobile-composer-new var(--mobile-composer-transition-duration) linear both;
3131
mix-blend-mode: normal;
3232
}
3333

@@ -57,11 +57,12 @@ html[data-mobile-composer-route-transition="true"]::view-transition-new(t3-mobil
5757
}
5858

5959
html[data-mobile-composer-route-transition="true"]::view-transition-group(t3-mobile-draft-headline) {
60-
animation-duration: 130ms;
60+
animation-duration: var(--mobile-composer-transition-duration);
6161
}
6262

6363
html[data-mobile-composer-route-transition="true"]::view-transition-old(t3-mobile-draft-headline) {
64-
animation: t3-mobile-draft-headline-exit 130ms cubic-bezier(0.4, 0, 1, 1) both;
64+
animation: t3-mobile-draft-headline-exit var(--mobile-composer-transition-duration)
65+
cubic-bezier(0.4, 0, 1, 1) both;
6566
mix-blend-mode: normal;
6667
}
6768

0 commit comments

Comments
 (0)