diff --git a/src/app/components/MobileSwipeDownModal.tsx b/src/app/components/MobileSwipeDownModal.tsx index 636dc9f80..05f509042 100644 --- a/src/app/components/MobileSwipeDownModal.tsx +++ b/src/app/components/MobileSwipeDownModal.tsx @@ -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(); + 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 > 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)} diff --git a/src/app/components/attachment-sheet/AttachmentSheet.tsx b/src/app/components/attachment-sheet/AttachmentSheet.tsx index f6e77f9fc..aa25e10ad 100644 --- a/src/app/components/attachment-sheet/AttachmentSheet.tsx +++ b/src/app/components/attachment-sheet/AttachmentSheet.tsx @@ -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(null); const skipReturnFocusRef = useRef(false); - const y = useMotionValue(0); - const prefersReducedMotion = useReducedMotion() ?? false; - const shouldReduceMotion = reducedMotion || prefersReducedMotion; + const dragYRef = useRef(0); + const resetAnimationRef = useRef(); + 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 = ( - - {open && ( - <> - event.stopPropagation()} - data-gestures="ignore" - aria-hidden="true" - /> + const sheetElement = open ? ( + <> +
event.stopPropagation()} + data-gestures="ignore" + aria-hidden="true" + /> - 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; - }, - }} - > - event.stopPropagation()} - > - - {sheetContent} - - - - - )} - - ); + 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; + }, + }} + > +
event.stopPropagation()} + > + {sheetContent} +
+
+ + ) : 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. diff --git a/src/app/components/mobileSheetAnimation.css.ts b/src/app/components/mobileSheetAnimation.css.ts new file mode 100644 index 000000000..f1bdb7e00 --- /dev/null +++ b/src/app/components/mobileSheetAnimation.css.ts @@ -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', + }, + }, +}); diff --git a/src/app/components/mobileSheetAnimation.ts b/src/app/components/mobileSheetAnimation.ts new file mode 100644 index 000000000..85f9c0baa --- /dev/null +++ b/src/app/components/mobileSheetAnimation.ts @@ -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, + }; +} diff --git a/src/app/components/mobileSheetAnimationConstants.ts b/src/app/components/mobileSheetAnimationConstants.ts new file mode 100644 index 000000000..968c003bd --- /dev/null +++ b/src/app/components/mobileSheetAnimationConstants.ts @@ -0,0 +1,2 @@ +export const MOBILE_SHEET_DURATION_MS = 240; +export const MOBILE_SHEET_EASING = 'cubic-bezier(0.32, 0.72, 0, 1)'; diff --git a/src/app/features/room/message/styles.css.ts b/src/app/features/room/message/styles.css.ts index 13802b251..77a0cf7e9 100644 --- a/src/app/features/room/message/styles.css.ts +++ b/src/app/features/room/message/styles.css.ts @@ -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({