mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 00:51:17 -08:00
fix: scrolling, closing, and animations in bottom sheets
This commit is contained in:
1 parent
f5650a3a65
commit
a5ca9dfd04
4 files changed
+78
-9
No files matched your search
@@ -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
|
||||
|
||||
@@ -54,7 +54,8 @@ export function ResponsiveMenu({
|
||||
fallbackFocus: () => document.body,
|
||||
returnFocusOnDeactivate,
|
||||
onDeactivate: requestClose,
|
||||
clickOutsideDeactivates: true,
|
||||
clickOutsideDeactivates: !isMobile,
|
||||
allowOutsideClick: isMobile,
|
||||
isKeyForward,
|
||||
isKeyBackward,
|
||||
escapeDeactivates: stopPropagation,
|
||||
|
||||
@@ -116,6 +116,7 @@ export const MessageMobileDragHandle = style({
|
||||
paddingTop: '6px',
|
||||
justifyContent: 'center',
|
||||
zIndex: 10,
|
||||
pointerEvents: 'none',
|
||||
});
|
||||
|
||||
export const MessageMobileDragIndicator = style({
|
||||
@@ -124,6 +125,7 @@ export const MessageMobileDragIndicator = style({
|
||||
borderRadius: '2px',
|
||||
backgroundColor: color.SurfaceVariant.OnContainer,
|
||||
opacity: 0.5,
|
||||
pointerEvents: 'auto',
|
||||
});
|
||||
|
||||
export const BubbleAvatarBase = style({
|
||||
|
||||
@@ -39,6 +39,8 @@ import type { PinReadMarker } from '$features/room/RoomViewHeader';
|
||||
import * as css from './RoomPinMenu.css';
|
||||
import { CustomAccountDataEvent } from '$types/matrix/accountData';
|
||||
import { EventType } from '$types/matrix-sdk';
|
||||
import { ScreenSize, useScreenSizeOptionally } from '$hooks/useScreenSize';
|
||||
import { useMobileSheetClose } from '$components/MobileSwipeDownModal';
|
||||
|
||||
const log = createLogger('RoomPinMenu');
|
||||
|
||||
@@ -172,6 +174,8 @@ type RoomPinMenuProps = {
|
||||
|
||||
export const RoomPinMenu = forwardRef<HTMLDivElement, RoomPinMenuProps>(
|
||||
({ room, requestClose, currentHash }, ref) => {
|
||||
const isMobile = useScreenSizeOptionally() === ScreenSize.Mobile;
|
||||
const mobileSheetClose = useMobileSheetClose();
|
||||
const mx = useMatrixClient();
|
||||
const userId = mx.getUserId()!;
|
||||
const powerLevels = usePowerLevelsContext();
|
||||
@@ -214,20 +218,39 @@ export const RoomPinMenu = forwardRef<HTMLDivElement, RoomPinMenuProps>(
|
||||
};
|
||||
|
||||
return (
|
||||
<Menu ref={ref} className={css.PinMenu}>
|
||||
<Menu
|
||||
ref={ref}
|
||||
className={css.PinMenu}
|
||||
>
|
||||
<Box grow="Yes" direction="Column">
|
||||
<Header className={css.PinMenuHeader} size="500">
|
||||
<Box grow="Yes">
|
||||
<Text size="H5">Pinned Messages</Text>
|
||||
</Box>
|
||||
<Box shrink="No">
|
||||
<IconButton size="300" onClick={requestClose} radii="300">
|
||||
<IconButton
|
||||
size="300"
|
||||
onClick={mobileSheetClose ?? requestClose}
|
||||
radii="300"
|
||||
>
|
||||
{composerIcon(X)}
|
||||
</IconButton>
|
||||
</Box>
|
||||
</Header>
|
||||
<Box grow="Yes">
|
||||
<Scroll ref={scrollRef} size="300" hideTrack visibility="Hover">
|
||||
<Scroll
|
||||
ref={scrollRef}
|
||||
size="300"
|
||||
hideTrack
|
||||
visibility="Hover"
|
||||
style={{
|
||||
minHeight: 0,
|
||||
flex: isMobile ? '0 1 auto' : 1,
|
||||
maxHeight: isMobile ? 'calc(85vh - 4rem)' : undefined,
|
||||
overflowY: 'auto',
|
||||
touchAction: 'pan-y',
|
||||
}}
|
||||
>
|
||||
<Box className={css.PinMenuContent} direction="Column" gap="100">
|
||||
{sortedPinnedEvent.length > 0 ? (
|
||||
<div
|
||||
|
||||
Reference in new issue
Block a user