perf(mobile): restore concurrent room nav and drawer animations

This commit is contained in:
7w1 committed 2026-07-22 20:33:36 -05:00
1 parent c03d7f0ed7
commit f40bf8cece
4 files changed
+41 -17

No files matched your search

+15 -4
View File
@@ -1,5 +1,6 @@
import type { ReactNode } from 'react';
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react';
import { startTransition, useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react';
import type { AnimationPlaybackControls } from 'framer-motion';
import { animate, motion, useDragControls, useMotionValue, useReducedMotion } from 'framer-motion';
import { useAtomValue, useSetAtom } from 'jotai';
import { matchPath, useLocation, useNavigate } from 'react-router-dom';
@@ -72,6 +73,7 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra
const [width, setWidth] = useState(0);
const x = useMotionValue(0);
const dragControls = useDragControls();
const settleAnimRef = useRef<AnimationPlaybackControls | null>(null);
const initialIntent = contentOpen ? 1 : 0;
const [panelIntent, setPanelIntent] = useState(initialIntent);
@@ -92,11 +94,14 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra
const settle = useCallback(
(target: number) => {
settleAnimRef.current?.stop();
settleAnimRef.current = null;
if (reduceMotion) {
x.jump(target);
return;
}
animate(x, target, {
settleAnimRef.current = animate(x, target, {
type: 'spring',
stiffness: SETTLE_STIFFNESS,
damping: SETTLE_DAMPING,
@@ -130,12 +135,16 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra
if (width > 0 && !reduceMotion) {
settle(target);
} else {
settleAnimRef.current?.stop();
settleAnimRef.current = null;
x.jump(target);
}
}
}, [contentOpen, width, x, settle, reduceMotion]);
useLayoutEffect(() => {
settleAnimRef.current?.stop();
settleAnimRef.current = null;
const target = panelIntentRef.current === 1 ? -width : 0;
x.jump(target);
}, [width, x]);
@@ -171,6 +180,8 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra
dragMomentum={false}
onDragStart={() => {
x.stop();
settleAnimRef.current?.stop();
settleAnimRef.current = null;
if (panelIntentRef.current === 0 && !roomArmed) setRoomArmed(true);
}}
onDragEnd={(_event, info) => {
@@ -187,7 +198,7 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra
if (section?.getRoomPath && matchedRoomId && isRoomRoute) {
setLastRoom((prev) => ({ ...prev, [section.key]: matchedRoomId }));
}
if (section) navigate(section.listPath);
if (section) startTransition(() => navigate(section.listPath));
} else {
settle(-width);
}
@@ -205,7 +216,7 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra
panelIntentRef.current = 1;
setPanelIntent(1);
settle(-width);
navigate(roomPath);
startTransition(() => navigate(roomPath));
} else {
settle(0);
}
+3 -7
View File
@@ -413,12 +413,8 @@ export function RoomNavItem({
navigateRoom(room.roomId);
}
} else {
if (isMobile) {
navigate(linkPath);
} else {
// Keep heavy room mounts off the urgent path on desktop.
startTransition(() => navigate(linkPath));
}
// Render the room off the urgent path so the tap doesn't freeze the UI on mount.
startTransition(() => navigate(linkPath));
}
};
@@ -441,7 +437,7 @@ export function RoomNavItem({
if (dx > 10 || dy > 10) return; // was a drag, not a tap
if (room.isCallRoom()) return; // call rooms use onClick
evt.preventDefault();
navigate(linkPath);
startTransition(() => navigate(linkPath));
};
const handleChatButtonClick = (evt: MouseEvent<HTMLButtonElement>) => {
+17 -6
View File
@@ -1,9 +1,10 @@
import { useAtomValue } from 'jotai';
import { useMemo } from 'react';
import { atom, useAtomValue } from 'jotai';
import { matchPath, useLocation, useParams } from 'react-router-dom';
import { getCanonicalAliasRoomId, isRoomAlias } from '$utils/matrix';
import { useMatrixClient } from '$hooks/useMatrixClient';
import { resolveSection } from '$pages/pathUtils';
import { lastVisitedRoomAtom } from '$state/room/lastRoom';
import { lastVisitedRoomSectionAtom } from '$state/room/lastRoom';
export const useSelectedRoom = (): string | undefined => {
const mx = useMatrixClient();
@@ -18,14 +19,24 @@ export const useSelectedRoom = (): string | undefined => {
return roomId;
};
const emptyLastRoomAtom = atom<string | undefined>(undefined);
export const useSelectedOrLastRoom = (): string | undefined => {
const selectedRoomId = useSelectedRoom();
const location = useLocation();
const lastVisitedRoom = useAtomValue(lastVisitedRoomAtom);
if (selectedRoomId) return selectedRoomId;
const section = resolveSection(location.pathname);
const listMatch = section && matchPath({ path: section.listPath, end: true }, location.pathname);
return listMatch ? lastVisitedRoom[section.key] : undefined;
const sectionKey = section?.key;
const sectionAtom = useMemo(
() => (sectionKey ? lastVisitedRoomSectionAtom(sectionKey) : emptyLastRoomAtom),
[sectionKey]
);
const lastRoomId = useAtomValue(sectionAtom);
if (selectedRoomId) return selectedRoomId;
return listMatch ? lastRoomId : undefined;
};
+6
View File
@@ -1,3 +1,4 @@
import { atom } from 'jotai';
import {
atomWithLocalStorage,
getLocalStorageItem,
@@ -13,3 +14,8 @@ const lastVisitedRoomBaseAtom = atomWithLocalStorage<Record<string, string>>(
);
export const lastVisitedRoomAtom = lastVisitedRoomBaseAtom;
/** Returns a read-only derived atom for a single section's last-visited room. */
export const lastVisitedRoomSectionAtom = (sectionKey: string) =>
atom((get) => get(lastVisitedRoomAtom)[sectionKey]);