feat: pull to close modal window

This commit is contained in:
ner committed 2024-04-03 20:20:12 +03:00
1 parent 5f5a7b9d87
commit a903752f9d
3 files changed
+116 -23

No files matched your search

+86 -20
View File
@@ -4,6 +4,7 @@ import * as React from 'react';
import { useState } from 'react';
import * as DialogPrimitive from '@radix-ui/react-dialog';
import useScrollDelta from '@/hooks/useScrollDelta';
import { cn } from '@/lib/utils';
const Dialog = DialogPrimitive.Root;
@@ -21,7 +22,7 @@ const DialogOverlay = React.forwardRef<
<DialogPrimitive.Overlay
ref={ref}
className={cn(
'fixed inset-0 z-50 overflow-auto bg-black/50 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0',
'fixed inset-0 z-50 overflow-auto data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0',
className
)}
{...props}
@@ -33,24 +34,87 @@ DialogOverlay.displayName = DialogPrimitive.Overlay.displayName;
const DialogContent = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>
>(({ className, children, ...props }, ref) => (
<DialogPortal>
<DialogOverlay>
<DialogPrimitive.Content
ref={ref}
{...props}
className={cn(
'absolute top-[20vh] max-w-[1000px] w-[calc(100%-24px)] left-[50%] rounded-xl translate-x-[-50%] gap-4 shadow-lg data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=open]:slide-in-from-left-1/2 bg-background',
className
)}
>
{children}
<div className='absolute top-full block w-10 h-[20vh] pointer-events-none' />
</DialogPrimitive.Content>
</DialogOverlay>
</DialogPortal>
));
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> & { onClose?: () => void }
>(({ className, children, onClose, ...props }, ref) => {
const position = React.useRef(0);
const close = React.useRef(false);
const wrapperRef = React.useRef<HTMLDivElement>(null);
const overlayRef = React.useRef<HTMLDivElement>(null);
const contentRef = React.useRef<HTMLDivElement | null>(null);
const { delta, onScroll } = useScrollDelta();
const handleTouchStart = (event: React.TouchEvent<HTMLDivElement>) => {
position.current = event.touches[0].clientY;
};
const handleTouchMove = (event: React.TouchEvent<HTMLDivElement>) => {
const threshold = 150;
const { clientY } = event.touches[0];
const content = contentRef.current;
const overlay = overlayRef.current;
const wrapper = wrapperRef.current;
const diff = clientY - position.current;
const top = overlay?.scrollTop || 0;
const isTop = top === 0;
const canBeClosed = diff > threshold;
close.current = canBeClosed && isTop;
if (isTop && content && wrapper) {
const opacity = `${1 - (diff * 100) / threshold / 100}`;
content.style.opacity = opacity;
wrapper.style.opacity = opacity;
content.style.transform = `translate(-50%, ${diff}px)`;
}
if (top <= 0 && delta.current < 0 && canBeClosed) onClose?.();
};
const handleTouchEnd = () => {
if (close.current) return onClose?.();
close.current = false;
if (!contentRef.current || !wrapperRef.current) return;
contentRef.current.style.opacity = '1';
wrapperRef.current.style.opacity = '1';
contentRef.current.style.transform = 'translate(-50%, 0)';
};
const handleRef = (element: HTMLDivElement | null) => {
if (!element) return;
(ref as (element: HTMLElement | null) => void)?.(element);
contentRef.current = element;
};
return (
<DialogPortal>
<div className='fixed inset-0 w-full h-full bg-black/50 z-50' ref={wrapperRef}>
<DialogOverlay ref={overlayRef} onScroll={onScroll}>
<DialogPrimitive.Content
onTouchStart={handleTouchStart}
onTouchMove={handleTouchMove}
onTouchEnd={handleTouchEnd}
ref={handleRef}
{...props}
className={cn(
'absolute top-[20vh] max-w-[1000px] w-[calc(100%-24px)] left-[50%] rounded-xl translate-x-[-50%] gap-4 shadow-lg data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=open]:slide-in-from-left-1/2 bg-background',
className
)}
>
{children}
<div className='absolute top-full block w-10 h-[20vh] pointer-events-none' />
</DialogPrimitive.Content>
</DialogOverlay>
</div>
</DialogPortal>
);
});
DialogContent.displayName = DialogPrimitive.Content.displayName;
const DialogHeader = ({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) => (
@@ -97,7 +161,9 @@ const Modal: React.FC<{
return (
<Dialog open={open} onOpenChange={handleClose}>
<DialogContent className={className}>{children}</DialogContent>
<DialogContent forceMount onClose={handleClose} className={className}>
{children}
</DialogContent>
</Dialog>
);
};