fix: unify mobile sheet animations

This commit is contained in:
7w1 committed 2026-07-23 21:19:14 -05:00
1 parent 936547facb
commit ea4d7259a3
6 files changed
+167 -92

No files matched your search

+25 -9
View File
@@ -3,6 +3,8 @@ import { createPortal } from 'react-dom';
import { Box } from 'folds';
import * as css from '$features/room/message/styles.css';
import { useDismissOnBack } from '$utils/androidBack';
import { getMobileSheetTiming, useMobileSheetAnimation } from './mobileSheetAnimation';
import * as animationCss from './mobileSheetAnimation.css';
interface MobileSwipeDownModalProps {
children: (
@@ -22,6 +24,8 @@ export function MobileSwipeDownModal({ children, requestClose }: MobileSwipeDown
const touchYDiff = useRef(0);
const startTime = useRef(0);
const [mounted, setMounted] = useState(false);
const resetAnimationRef = useRef<Animation>();
const { shouldReduceMotion } = useMobileSheetAnimation();
useEffect(() => {
setMounted(true);
@@ -43,9 +47,10 @@ export function MobileSwipeDownModal({ children, requestClose }: MobileSwipeDown
// Only allow pulling down
if (diff > 0) {
touchYDiff.current = diff;
resetAnimationRef.current?.cancel();
containerRef.current?.getAnimations().forEach((animation) => animation.cancel());
if (containerRef.current) {
containerRef.current.style.transform = `translateY(${diff}px)`;
containerRef.current.style.transition = 'none';
containerRef.current.style.transform = `translate3d(0, ${diff}px, 0)`;
}
}
};
@@ -58,10 +63,24 @@ export function MobileSwipeDownModal({ children, requestClose }: MobileSwipeDown
) {
requestClose();
} else {
const currentOffset = touchYDiff.current;
touchYDiff.current = 0;
if (containerRef.current) {
containerRef.current.style.transform = '';
containerRef.current.style.transition = 'transform 0.2s ease-out';
const container = containerRef.current;
resetAnimationRef.current = container.animate(
[
{ transform: `translate3d(0, ${currentOffset}px, 0)` },
{ transform: 'translate3d(0, 0, 0)' },
],
getMobileSheetTiming(shouldReduceMotion)
);
resetAnimationRef.current.addEventListener(
'finish',
() => {
container.style.transform = '';
},
{ once: true }
);
}
}
touchStartY.current = null;
@@ -92,11 +111,8 @@ export function MobileSwipeDownModal({ children, requestClose }: MobileSwipeDown
>
<Box
ref={containerRef}
className={css.MessageMobileOptionsContainer}
style={{
transform: touchYDiff.current > 0 ? `translateY(${touchYDiff.current}px)` : undefined,
transition: touchStartY.current === null ? 'transform 0.2s ease-out' : 'none',
}}
className={`${css.MessageMobileOptionsContainer} ${animationCss.SheetEntrance}`}
style={shouldReduceMotion ? { animation: 'none' } : undefined}
onClick={(e: React.MouseEvent) => e.stopPropagation()}
>
{children(dragHandleJSX, dragHandlers)}
@@ -1,6 +1,5 @@
import { type RefObject, useEffect, useRef } from 'react';
import { type RefObject, useLayoutEffect, useRef } from 'react';
import { createPortal } from 'react-dom';
import { AnimatePresence, animate, motion, useMotionValue, useReducedMotion } from 'framer-motion';
import { useDrag } from '@use-gesture/react';
import FocusTrap from 'focus-trap-react';
import { useSetting } from '$state/hooks/settings';
@@ -8,6 +7,8 @@ import { settingsAtom } from '$state/settings';
import { useAndroidBackHandler } from '$utils/androidBack';
import { stopPropagation } from '$utils/keyboard';
import { mobileOrTablet } from '$utils/user-agent';
import { getMobileSheetTiming, useMobileSheetAnimation } from '$components/mobileSheetAnimation';
import * as animationCss from '$components/mobileSheetAnimation.css';
import type { Icon } from '@phosphor-icons/react';
import {
Image as ImageIcon,
@@ -47,20 +48,21 @@ export function AttachmentSheet({
containerRef,
}: AttachmentSheetProps) {
const [mobileGestures] = useSetting(settingsAtom, 'mobileGestures');
const [reducedMotion] = useSetting(settingsAtom, 'reducedMotion');
const containerEl = containerRef.current;
const sheetRef = useRef<HTMLDivElement>(null);
const skipReturnFocusRef = useRef(false);
const y = useMotionValue(0);
const prefersReducedMotion = useReducedMotion() ?? false;
const shouldReduceMotion = reducedMotion || prefersReducedMotion;
const dragYRef = useRef(0);
const resetAnimationRef = useRef<Animation>();
const { shouldReduceMotion } = useMobileSheetAnimation();
useEffect(() => {
useLayoutEffect(() => {
if (open) {
skipReturnFocusRef.current = false;
y.set(0);
dragYRef.current = 0;
resetAnimationRef.current?.cancel();
if (sheetRef.current) sheetRef.current.style.transform = '';
}
}, [open, y]);
}, [open]);
useAndroidBackHandler(() => {
onClose();
@@ -84,17 +86,36 @@ export function AttachmentSheet({
const val = Math.max(0, oy);
if (active) {
if (first) y.stop();
y.set(val);
if (first) {
resetAnimationRef.current?.cancel();
sheetRef.current?.getAnimations().forEach((animation) => animation.cancel());
}
dragYRef.current = val;
if (sheetRef.current) {
sheetRef.current.style.transform = `translate3d(0, ${val}px, 0)`;
}
} else {
const swipedDown = val > SWIPE_THRESHOLD || (vy > VELOCITY_THRESHOLD && dy > 0);
if (swipedDown) {
onClose();
} else if (shouldReduceMotion) {
y.set(0);
} else {
animate(y, 0, { type: 'spring', stiffness: 400, damping: 40 });
} else if (sheetRef.current) {
const sheet = sheetRef.current;
resetAnimationRef.current = sheet.animate(
[
{ transform: `translate3d(0, ${dragYRef.current}px, 0)` },
{ transform: 'translate3d(0, 0, 0)' },
],
getMobileSheetTiming(shouldReduceMotion)
);
resetAnimationRef.current.addEventListener(
'finish',
() => {
dragYRef.current = 0;
sheet.style.transform = '';
},
{ once: true }
);
}
}
},
@@ -104,7 +125,7 @@ export function AttachmentSheet({
rubberband: true,
filterTaps: true,
pointer: { capture: true },
from: () => [0, y.get()],
from: () => [0, dragYRef.current],
}
);
@@ -173,64 +194,48 @@ export function AttachmentSheet({
</>
);
const sheetElement = (
<AnimatePresence>
{open && (
<>
<motion.div
className={css.Backdrop}
initial={shouldReduceMotion ? false : { opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: shouldReduceMotion ? 0 : 0.18 }}
onClick={onClose}
onPointerDown={(event) => event.stopPropagation()}
data-gestures="ignore"
aria-hidden="true"
/>
const sheetElement = open ? (
<>
<div
className={`${css.Backdrop} ${animationCss.BackdropEntrance}`}
style={shouldReduceMotion ? { animation: 'none' } : undefined}
onClick={onClose}
onPointerDown={(event) => event.stopPropagation()}
data-gestures="ignore"
aria-hidden="true"
/>
<FocusTrap
focusTrapOptions={{
initialFocus: () => sheetRef.current ?? containerEl,
fallbackFocus: () => sheetRef.current ?? containerEl,
returnFocusOnDeactivate: true,
setReturnFocus: (previousActiveElement: HTMLElement) =>
skipReturnFocusRef.current ? false : previousActiveElement,
allowOutsideClick: true,
clickOutsideDeactivates: false,
escapeDeactivates: (event: KeyboardEvent) => {
if (!stopPropagation(event)) return false;
onClose();
return false;
},
}}
>
<motion.div
ref={sheetRef}
className={css.Sheet}
initial={shouldReduceMotion ? false : { y: '100%' }}
animate={{ y: 0 }}
exit={{ y: '100%' }}
transition={
shouldReduceMotion
? { duration: 0 }
: { type: 'spring', damping: 32, stiffness: 340 }
}
role="dialog"
aria-modal="true"
aria-labelledby="attachment-sheet-title"
tabIndex={-1}
onPointerDown={(event) => event.stopPropagation()}
>
<motion.div style={{ y, display: 'flex', flexDirection: 'column', flexGrow: 1 }}>
{sheetContent}
</motion.div>
</motion.div>
</FocusTrap>
</>
)}
</AnimatePresence>
);
<FocusTrap
focusTrapOptions={{
initialFocus: () => sheetRef.current ?? containerEl,
fallbackFocus: () => sheetRef.current ?? containerEl,
returnFocusOnDeactivate: true,
setReturnFocus: (previousActiveElement: HTMLElement) =>
skipReturnFocusRef.current ? false : previousActiveElement,
allowOutsideClick: true,
clickOutsideDeactivates: false,
escapeDeactivates: (event: KeyboardEvent) => {
if (!stopPropagation(event)) return false;
onClose();
return false;
},
}}
>
<div
ref={sheetRef}
className={`${css.Sheet} ${animationCss.SheetEntrance}`}
style={shouldReduceMotion ? { animation: 'none' } : undefined}
role="dialog"
aria-modal="true"
aria-labelledby="attachment-sheet-title"
tabIndex={-1}
onPointerDown={(event) => event.stopPropagation()}
>
{sheetContent}
</div>
</FocusTrap>
</>
) : null;
// Never render inline: without the active pane as a portal target, the sheet
// could briefly anchor to the room layout and cover the sidebar.
@@ -0,0 +1,32 @@
import { keyframes, style } from '@vanilla-extract/css';
import { MOBILE_SHEET_DURATION_MS, MOBILE_SHEET_EASING } from './mobileSheetAnimationConstants';
const slideIn = keyframes({
from: { transform: 'translate3d(0, 100%, 0)' },
to: { transform: 'translate3d(0, 0, 0)' },
});
const fadeIn = keyframes({
from: { opacity: 0 },
to: { opacity: 1 },
});
export const SheetEntrance = style({
animation: `${slideIn} ${MOBILE_SHEET_DURATION_MS}ms ${MOBILE_SHEET_EASING}`,
willChange: 'transform',
'@media': {
'(prefers-reduced-motion: reduce)': {
animation: 'none',
},
},
});
export const BackdropEntrance = style({
animation: `${fadeIn} 180ms ease-out`,
willChange: 'opacity',
'@media': {
'(prefers-reduced-motion: reduce)': {
animation: 'none',
},
},
});
@@ -0,0 +1,29 @@
import { useEffect, useState } from 'react';
import { useSetting } from '$state/hooks/settings';
import { settingsAtom } from '$state/settings';
import { MOBILE_SHEET_DURATION_MS, MOBILE_SHEET_EASING } from './mobileSheetAnimationConstants';
export const getMobileSheetTiming = (reducedMotion: boolean): KeyframeAnimationOptions => ({
duration: reducedMotion ? 0 : MOBILE_SHEET_DURATION_MS,
easing: MOBILE_SHEET_EASING,
fill: 'forwards',
});
export function useMobileSheetAnimation() {
const [reducedMotion] = useSetting(settingsAtom, 'reducedMotion');
const [prefersReducedMotion, setPrefersReducedMotion] = useState(
() => window.matchMedia?.('(prefers-reduced-motion: reduce)').matches ?? false
);
useEffect(() => {
const query = window.matchMedia?.('(prefers-reduced-motion: reduce)');
if (!query) return undefined;
const handleChange = () => setPrefersReducedMotion(query.matches);
query.addEventListener('change', handleChange);
return () => query.removeEventListener('change', handleChange);
}, []);
return {
shouldReduceMotion: reducedMotion || prefersReducedMotion,
};
}
@@ -0,0 +1,2 @@
export const MOBILE_SHEET_DURATION_MS = 240;
export const MOBILE_SHEET_EASING = 'cubic-bezier(0.32, 0.72, 0, 1)';
+1 -10
View File
@@ -1,4 +1,4 @@
import { keyframes, style } from '@vanilla-extract/css';
import { style } from '@vanilla-extract/css';
import { DefaultReset, FocusOutline, color, config, toRem } from 'folds';
export const MessageBase = style({
@@ -106,15 +106,6 @@ export const MessageMobileOptionsContainer = style({
flexDirection: 'column',
justifyContent: 'flex-end',
overflow: 'visible',
animation: `${keyframes({
from: { transform: 'translateY(100%)' },
to: { transform: 'translateY(0)' },
})} 250ms cubic-bezier(0.32, 0.72, 0, 1)`,
'@media': {
'(prefers-reduced-motion: reduce)': {
animation: 'none',
},
},
});
export const MessageMobileDragHandle = style({