fix: scrolling, closing, and animations in bottom sheets

This commit is contained in:
7w1 committed 2026-07-26 18:40:40 -05:00
1 parent f5650a3a65
commit a5ca9dfd04
4 files changed
+78 -9

No files matched your search

+48 -5
View File
@@ -1,4 +1,11 @@
import React, { useRef, useState, useEffect } from 'react';
import React, {
createContext,
useCallback,
useContext,
useRef,
useState,
useEffect,
} from 'react';
import { createPortal } from 'react-dom';
import { Box } from 'folds';
import * as css from '$features/room/message/styles.css';
@@ -18,12 +25,20 @@ interface MobileSwipeDownModalProps {
requestClose: () => void;
}
const MobileSheetCloseContext = createContext<(() => void) | null>(null);
export function useMobileSheetClose() {
return useContext(MobileSheetCloseContext);
}
export function MobileSwipeDownModal({ children, requestClose }: MobileSwipeDownModalProps) {
const containerRef = useRef<HTMLDivElement>(null);
const touchStartY = useRef<number | null>(null);
const touchYDiff = useRef(0);
const startTime = useRef(0);
const [mounted, setMounted] = useState(false);
const [closing, setClosing] = useState(false);
const closingRef = useRef(false);
const resetAnimationRef = useRef<Animation>();
const { shouldReduceMotion } = useMobileSheetAnimation();
@@ -31,8 +46,33 @@ export function MobileSwipeDownModal({ children, requestClose }: MobileSwipeDown
setMounted(true);
}, []);
const closeWithAnimation = useCallback(() => {
if (closingRef.current) return;
closingRef.current = true;
setClosing(true);
const container = containerRef.current;
if (!container) {
requestClose();
return;
}
container.getAnimations().forEach((animation) => animation.cancel());
const animation = container.animate(
[
{ transform: 'translate3d(0, 0, 0)' },
{ transform: 'translate3d(0, 100%, 0)' },
],
{
...getMobileSheetTiming(shouldReduceMotion),
duration: shouldReduceMotion ? 0 : 140,
}
);
animation.addEventListener('finish', requestClose, { once: true });
}, [requestClose, shouldReduceMotion]);
// Android back closes the overlay instead of navigating away.
useDismissOnBack(requestClose);
useDismissOnBack(closeWithAnimation);
const handleTouchStart = (e: React.TouchEvent) => {
touchStartY.current = e.touches[0]?.clientY ?? null;
@@ -61,7 +101,7 @@ export function MobileSwipeDownModal({ children, requestClose }: MobileSwipeDown
touchYDiff.current > 100 ||
(endTime - startTime.current < 600 && touchYDiff.current > 20)
) {
requestClose();
closeWithAnimation();
} else {
const currentOffset = touchYDiff.current;
touchYDiff.current = 0;
@@ -104,7 +144,8 @@ export function MobileSwipeDownModal({ children, requestClose }: MobileSwipeDown
<Box
className={css.MessageMobileOptionsWrapped}
data-gestures="ignore"
onClick={requestClose}
style={closing ? { opacity: 0, transition: 'opacity 100ms ease-out' } : undefined}
onClick={closeWithAnimation}
onTouchStart={(e: React.TouchEvent) => e.stopPropagation()}
onTouchMove={(e: React.TouchEvent) => e.stopPropagation()}
onTouchEnd={(e: React.TouchEvent) => e.stopPropagation()}
@@ -115,7 +156,9 @@ export function MobileSwipeDownModal({ children, requestClose }: MobileSwipeDown
style={shouldReduceMotion ? { animation: 'none' } : undefined}
onClick={(e: React.MouseEvent) => e.stopPropagation()}
>
{children(dragHandleJSX, dragHandlers)}
<MobileSheetCloseContext.Provider value={closeWithAnimation}>
{children(dragHandleJSX, dragHandlers)}
</MobileSheetCloseContext.Provider>
</Box>
</Box>,
document.body
+2 -1
View File
@@ -54,7 +54,8 @@ export function ResponsiveMenu({
fallbackFocus: () => document.body,
returnFocusOnDeactivate,
onDeactivate: requestClose,
clickOutsideDeactivates: true,
clickOutsideDeactivates: !isMobile,
allowOutsideClick: isMobile,
isKeyForward,
isKeyBackward,
escapeDeactivates: stopPropagation,