Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): restore reduced motion in compatibility action sheet

DavidShenfac82107c629
diff --git a/apps/mobile/src/__tests__/sessionActionSheetMotion.test.tsx b/apps/mobile/src/__tests__/sessionActionSheetMotion.test.tsx
new file mode 100644
index 0000000000..3ebb2eaca9
--- /dev/null
+++ b/apps/mobile/src/__tests__/sessionActionSheetMotion.test.tsx
@@ -0,0 +1,110 @@
+// @vitest-environment jsdom
+import React, { act } from 'react';
+import { createRoot, type Root } from 'react-dom/client';
+import { afterEach, beforeEach, expect, it, vi } from 'vitest';
+import { motionDuration, motionEasing } from '@/theme/tokens';
+
+const motion = vi.hoisted(() => ({
+ reduced: null as boolean | null,
+ setValue: vi.fn(),
+ timing: vi.fn(),
+ finish: undefined as undefined | ((result: { finished: boolean }) => void),
+}));
+vi.mock('@/hooks/useReduceMotion', () => ({
+ useReduceMotionEnabled: () => motion.reduced,
+}));
+vi.mock('react-native', () => ({
+ Animated: {
+ Value: class {
+ setValue = motion.setValue;
+ interpolate() { return 0; }
+ },
+ timing: motion.timing,
+ View: ({ children }: any) => <div>{children}</div>,
+ },
+ Easing: { bezier: (...values: number[]) => values },
+ Modal: ({ visible, children }: any) => visible ? <div data-testid="modal">{children}</div> : null,
+ Pressable: () => null,
+ StyleSheet: { create: (value: any) => value, absoluteFill: {} },
+}));
+vi.mock('react-native-safe-area-context', () => ({
+ useSafeAreaInsets: () => ({ bottom: 0 }),
+}));
+vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: (key: string) => key }) }));
+vi.mock('@/platform/gestureHandler', () => ({
+ GestureHandlerRootView: ({ children }: any) => children,
+}));
+vi.mock('@/session/BlurBackdrop', () => ({ BlurBackdrop: () => null }));
+
+import { SessionActionSheetFrame } from '@/session/SessionActionSheetFrame';
+
+let root: Root;
+let container: HTMLDivElement;
+beforeEach(() => {
+ (globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;
+ motion.reduced = null;
+ motion.finish = undefined;
+ motion.setValue.mockClear();
+ motion.timing.mockReset().mockImplementation(() => ({
+ start: (callback: typeof motion.finish) => { motion.finish = callback; },
+ }));
+ container = document.createElement('div');
+ document.body.append(container);
+ root = createRoot(container);
+});
+afterEach(() => {
+ act(() => root.unmount());
+ container.remove();
+});
+function render(visible: boolean, onClosed = vi.fn()) {
+ act(() => root.render(
+ <SessionActionSheetFrame visible={visible} onClose={vi.fn()} onClosed={onClosed}>
+ actions
+ </SessionActionSheetFrame>,
+ ));
+}
+
+it.each([true, null])('opens and closes without animation when reduced motion is %s', (reduced) => {
+ motion.reduced = reduced;
+ const closed = vi.fn(() => expect(container.querySelector('[data-testid="modal"]')).toBeNull());
+ render(false, closed);
+ expect(closed).not.toHaveBeenCalled();
+ render(true, closed);
+ expect(container.textContent).toBe('actions');
+ expect(motion.setValue).toHaveBeenLastCalledWith(1);
+ render(false, closed);
+ expect(motion.setValue).toHaveBeenLastCalledWith(0);
+ expect(motion.timing).not.toHaveBeenCalled();
+ expect(closed).toHaveBeenCalledOnce();
+});
+
+it('uses motion tokens and waits for a completed exit before notifying onClosed', () => {
+ motion.reduced = false;
+ const closed = vi.fn(() => expect(container.querySelector('[data-testid="modal"]')).toBeNull());
+ render(true, closed);
+ expect(motion.timing).toHaveBeenLastCalledWith(expect.anything(), {
+ duration: motionDuration.enter, easing: motionEasing.out, toValue: 1, useNativeDriver: true,
+ });
+ render(false, closed);
+ expect(motion.timing).toHaveBeenLastCalledWith(expect.anything(), {
+ duration: motionDuration.exit, easing: motionEasing.in, toValue: 0, useNativeDriver: true,
+ });
+ expect(closed).not.toHaveBeenCalled();
+ act(() => motion.finish?.({ finished: false }));
+ expect(container.textContent).toBe('actions');
+ expect(closed).not.toHaveBeenCalled();
+ act(() => motion.finish?.({ finished: true }));
+ expect(closed).toHaveBeenCalledOnce();
+});
+
+it('honors a live change to reduced motion on the next close', () => {
+ motion.reduced = false;
+ render(true);
+ motion.reduced = true;
+ render(true);
+ expect(motion.setValue).toHaveBeenLastCalledWith(1);
+ motion.timing.mockClear();
+ render(false);
+ expect(container.textContent).toBe('');
+ expect(motion.timing).not.toHaveBeenCalled();
+});
diff --git a/apps/mobile/src/session/SessionActionSheetFrame.tsx b/apps/mobile/src/session/SessionActionSheetFrame.tsx
index f06c687854..2342d2abf5 100644
--- a/apps/mobile/src/session/SessionActionSheetFrame.tsx
+++ b/apps/mobile/src/session/SessionActionSheetFrame.tsx
@@ -4,7 +4,8 @@ import { useSafeAreaInsets } from "react-native-safe-area-context";
import { useTranslation } from "react-i18next";
import { GestureHandlerRootView } from "@/platform/gestureHandler";
import { BlurBackdrop } from "@/session/BlurBackdrop";
-import { spacing } from "@/theme/tokens";
+import { motionDuration, motionEasing, spacing } from "@/theme/tokens";
+import { useReduceMotionEnabled } from "@/hooks/useReduceMotion";
export interface SessionActionSheetFrameProps {
visible: boolean;
@@ -24,27 +25,38 @@ export function SessionActionSheetFrame({
const insets = useSafeAreaInsets();
const [mounted, setMounted] = useState(visible);
const progress = useRef(new Animated.Value(visible ? 1 : 0)).current;
+ // Unknown system preference must also avoid motion on the first frame.
+ const animate = useReduceMotionEnabled() === false;
useEffect(() => {
if (visible) {
setMounted(true);
+ if (!animate) {
+ progress.setValue(1);
+ return;
+ }
Animated.timing(progress, {
- duration: 220,
- easing: Easing.out(Easing.cubic),
+ duration: motionDuration.enter,
+ easing: Easing.bezier(...motionEasing.out),
toValue: 1,
useNativeDriver: true,
}).start();
} else {
+ if (!animate) {
+ progress.setValue(0);
+ setMounted(false);
+ return;
+ }
Animated.timing(progress, {
- duration: 160,
- easing: Easing.in(Easing.quad),
+ duration: motionDuration.exit,
+ easing: Easing.bezier(...motionEasing.in),
toValue: 0,
useNativeDriver: true,
}).start(({ finished }) => {
if (finished) setMounted(false);
});
}
- }, [visible, progress]);
+ }, [animate, visible, progress]);
// onClosed 等 Modal 真正从树上卸载后再触发(同 DeviceMenuModal:动画回调里同步挂
// 第二个 Modal 会和本 Modal 的卸载挤进同一个 commit,iOS 可能吞掉新弹窗)。