feat: add mobile swipe-back navigation and gestures

- Implement swipe-to-close for mobile modals and dialogs
- Add PageTransition component with push/pop animations
- Create navigation store for managing navigation state
- Add useNavigationDirection hook for animation direction
- Implement useSwipeToClose hook for swipe gestures
- Add prefers-reduced-motion accessibility support
- Create FileBrowserSheet component for mobile file browser
- Add comprehensive tests for swipe behavior and navigation
- Update FileBrowser, Dialog, and BackButton components
- Configure Vite for proper CSS handling
This commit is contained in:
Chris Scott committed 2026-04-15 12:44:33 -04:00
1 parent 46731210e8
commit 9d94c333b4
23 files changed
+1684 -161

No files matched your search

@@ -0,0 +1,68 @@
import { useRef, useEffect, useState, type ReactNode } from 'react'
import { useLocation } from 'react-router-dom'
import { useNavigationStore } from '@/stores/navigationStore'
import { useSwipeBack, useMobile } from '@/hooks/useMobile'
interface PageTransitionProps {
children: ReactNode
}
export function PageTransition({ children }: PageTransitionProps) {
const isMobile = useMobile()
const location = useLocation()
const pageRef = useRef<HTMLDivElement>(null)
const direction = useNavigationStore((s) => s.direction)
const phase = useNavigationStore((s) => s.phase)
const { bindSwipe, pageStyles, underlayStyles, isSwiping } = useSwipeBack()
useEffect(() => {
return bindSwipe(pageRef.current)
}, [bindSwipe])
const isAnimating = isMobile && phase === 'entering' && direction !== 'none'
const isActive = isSwiping || isAnimating
const animationClass = isAnimating
? direction === 'push'
? 'page-slide-in-push'
: 'page-slide-in-pop'
: ''
const [reducedMotion, setReducedMotion] = useState(false)
useEffect(() => {
const mql = window.matchMedia('(prefers-reduced-motion: reduce)')
setReducedMotion(mql.matches)
const handler = (e: MediaQueryListEvent) => setReducedMotion(e.matches)
mql.addEventListener('change', handler)
return () => mql.removeEventListener('change', handler)
}, [])
return (
<>
{isMobile && isActive && !reducedMotion && (
<div
className="fixed inset-0 bg-black pointer-events-none"
style={{
zIndex: 40,
...underlayStyles,
}}
/>
)}
<div
key={location.pathname}
ref={pageRef}
className={reducedMotion ? '' : animationClass}
style={{
position: 'relative',
zIndex: isActive ? 50 : undefined,
minHeight: '100dvh',
...(isSwiping && !reducedMotion ? pageStyles : {}),
willChange: isActive ? 'transform' : undefined,
}}
>
{children}
</div>
</>
)
}
+6 -2
View File
@@ -9,12 +9,16 @@ interface BackButtonProps {
export function BackButton({ to = "/", className = "" }: BackButtonProps) {
const navigate = useNavigate();
const handleBack = () => {
navigate(to);
};
return (
<button
onClick={() => navigate(to)}
onClick={handleBack}
className={`text-zinc-400 hover:text-zinc-100 transition-all duration-200 hover:scale-105 text-sm md:text-md border border-zinc-700 rounded-md px-3 py-1.5 hover ${className}`}
>
<ArrowLeft className="w-4 h-4" />
</button>
);
}
}
+92 -1
View File
@@ -1,4 +1,4 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import {
Dialog,
@@ -131,4 +131,95 @@ describe("DialogContent", () => {
);
expect(screen.getByText("Test Child Content")).toBeInTheDocument();
});
it("accepts mobileSwipeToClose prop without breaking rendering", () => {
const onOpenChange = vi.fn();
render(
<Dialog open onOpenChange={onOpenChange}>
<DialogContent mobileFullscreen mobileSwipeToClose data-testid="dialog-content">
<DialogHeader>
<DialogTitle>Swipe Dialog</DialogTitle>
</DialogHeader>
</DialogContent>
</Dialog>
);
const content = screen.getByTestId("dialog-content");
expect(content).toBeInTheDocument();
expect(content).toHaveClass("inset-0");
});
it("applies safe-area padding when mobileSwipeToClose is used with mobileFullscreen", () => {
render(
<Dialog open>
<DialogContent mobileFullscreen mobileSwipeToClose data-testid="dialog-content">
<DialogHeader>
<DialogTitle>Test Dialog</DialogTitle>
</DialogHeader>
</DialogContent>
</Dialog>
);
const content = screen.getByTestId("dialog-content");
expect(content).toHaveStyle({ paddingTop: "env(safe-area-inset-top, 0px)" });
});
it('renders hidden close trigger when mobileSwipeToClose is enabled', () => {
const onOpenChange = vi.fn();
render(
<Dialog open onOpenChange={onOpenChange}>
<DialogContent mobileFullscreen mobileSwipeToClose>
<DialogHeader>
<DialogTitle>Swipe Dialog</DialogTitle>
</DialogHeader>
</DialogContent>
</Dialog>
);
const closeTrigger = document.querySelector('[data-swipe-close-trigger]');
expect(closeTrigger).toBeInTheDocument();
});
it('closes dialog when hidden close trigger is activated', () => {
const onOpenChange = vi.fn();
render(
<Dialog open onOpenChange={onOpenChange}>
<DialogContent mobileFullscreen mobileSwipeToClose>
<DialogHeader>
<DialogTitle>Swipe Dialog</DialogTitle>
</DialogHeader>
</DialogContent>
</Dialog>
);
const closeTrigger = document.querySelector('[data-swipe-close-trigger]') as HTMLButtonElement | null;
expect(closeTrigger).toBeInTheDocument();
if (closeTrigger) {
closeTrigger.click();
expect(onOpenChange).toHaveBeenCalledWith(false);
}
});
it('binds swipe handler when mobileSwipeToClose and mobileFullscreen are enabled', () => {
const onOpenChange = vi.fn();
render(
<Dialog open onOpenChange={onOpenChange}>
<DialogContent mobileFullscreen mobileSwipeToClose data-testid="swipe-dialog">
<DialogHeader>
<DialogTitle>Swipe Dialog</DialogTitle>
</DialogHeader>
</DialogContent>
</Dialog>
);
const content = screen.getByTestId('swipe-dialog');
const closeTrigger = document.querySelector('[data-swipe-close-trigger]') as HTMLButtonElement | null;
expect(content).toBeInTheDocument();
expect(closeTrigger).toBeInTheDocument();
const clickSpy = vi.spyOn(closeTrigger as HTMLButtonElement, 'click');
closeTrigger?.click();
expect(clickSpy).toHaveBeenCalled();
expect(onOpenChange).toHaveBeenCalledWith(false);
});
});
+42 -4
View File
@@ -3,6 +3,7 @@ import * as DialogPrimitive from "@radix-ui/react-dialog"
import { X } from "lucide-react"
import { cn } from "@/lib/utils"
import { useSwipeToClose } from '@/hooks/useMobile'
const Dialog = DialogPrimitive.Root
@@ -28,24 +29,58 @@ const DialogOverlay = React.forwardRef<
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName
interface DialogContentProps
extends React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> {
extends Omit<React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>, 'onOpenChange'> {
hideCloseButton?: boolean
fullscreen?: boolean
mobileFullscreen?: boolean
mobileSwipeToClose?: boolean
onOpenChange?: (open: boolean) => void
overlayClassName?: string
}
const DialogContent = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Content>,
DialogContentProps
>(({ className, children, hideCloseButton, fullscreen, mobileFullscreen, overlayClassName, ...props }, ref) => {
>(({ className, children, hideCloseButton, fullscreen, mobileFullscreen, mobileSwipeToClose, overlayClassName, ...props }, ref) => {
const isMobileFullscreenMode = fullscreen || mobileFullscreen
const swipeContainerRef = React.useRef<HTMLDivElement>(null)
const contentRef = React.useRef<HTMLDivElement>(null)
const closeTriggerRef = React.useRef<HTMLButtonElement>(null)
const combinedRef = React.useCallback((node: HTMLDivElement | null) => {
contentRef.current = node
swipeContainerRef.current = node
if (typeof ref === 'function') {
ref(node)
} else if (ref) {
ref.current = node
}
}, [ref])
const [isMobile, setIsMobile] = React.useState(() => typeof window !== 'undefined' ? window.innerWidth < 768 : false)
React.useEffect(() => {
const check = () => setIsMobile(window.innerWidth < 768)
window.addEventListener('resize', check)
return () => window.removeEventListener('resize', check)
}, [])
const { bind: swipeBind } = useSwipeToClose(
() => closeTriggerRef.current?.click(),
{ enabled: isMobileFullscreenMode && mobileSwipeToClose === true && isMobile }
)
React.useEffect(() => {
if (isMobileFullscreenMode && mobileSwipeToClose && isMobile) {
return swipeBind(swipeContainerRef.current)
}
return undefined
}, [isMobileFullscreenMode, mobileSwipeToClose, isMobile, swipeBind])
return (
<DialogPortal>
{!fullscreen && <DialogOverlay className={overlayClassName} />}
<DialogPrimitive.Content
ref={ref}
ref={combinedRef}
autoFocus={false}
aria-describedby={undefined}
className={cn(
@@ -53,7 +88,7 @@ const DialogContent = React.forwardRef<
fullscreen
? "inset-0 w-full h-full max-w-none max-h-none p-0 rounded-none"
: mobileFullscreen
? "inset-0 w-full h-full max-w-none max-h-none p-0 rounded-none sm:inset-auto sm:left-[50%] sm:bottom-auto sm:w-[90%] sm:max-w-lg sm:translate-x-[-50%] sm:translate-y-0 sm:p-6 sm:rounded-lg sm:top-[8%]"
? "inset-0 w-full h-full max-w-none max-h-none p-0 rounded-none sm:inset-auto sm:left-[50%] sm:bottom-auto sm:w-[90%] sm:max-w-lg sm:translate-x-[-50%] sm:translate-y-0 sm:rounded-lg sm:top-[8%] sm:p-6"
: "left-[50%] top-[50%] w-[90%] max-w-lg translate-x-[-50%] translate-y-[-50%] p-6 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg",
className
)}
@@ -63,6 +98,9 @@ const DialogContent = React.forwardRef<
{...props}
>
{children}
{mobileSwipeToClose && isMobileFullscreenMode && (
<DialogPrimitive.Close ref={closeTriggerRef} className="sr-only" data-swipe-close-trigger />
)}
{!hideCloseButton && !fullscreen && (
<DialogPrimitive.Close
className="absolute right-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground"