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

@@ -1,6 +1,6 @@
'use client'; 'use client';
import { ChangeEvent, FC, useRef, useState } from 'react'; import { ChangeEvent, FC, useEffect, useRef, useState } from 'react';
import { create, InstanceProps } from 'react-modal-promise'; import { create, InstanceProps } from 'react-modal-promise';
import { Search as SearchIcon, XCircle } from 'lucide-react'; import { Search as SearchIcon, XCircle } from 'lucide-react';
@@ -42,7 +42,6 @@ const Search: FC<InstanceProps<void>> = ({ onResolve }) => {
<div className='relative'> <div className='relative'>
<Input <Input
ref={inputRef} ref={inputRef}
autoFocus
placeholder='Start entering title...' placeholder='Start entering title...'
value={title} value={title}
onChange={handleChangeTitle} onChange={handleChangeTitle}
@@ -50,6 +49,7 @@ const Search: FC<InstanceProps<void>> = ({ onResolve }) => {
/> />
<Button <Button
variant='outline' variant='outline'
type='button'
size='icon' size='icon'
onClick={handleReset} onClick={handleReset}
disabled={!query.length} disabled={!query.length}
@@ -58,7 +58,7 @@ const Search: FC<InstanceProps<void>> = ({ onResolve }) => {
<XCircle /> <XCircle />
</Button> </Button>
</div> </div>
{Boolean(results?.pages.length) && ( {Boolean(results?.pages.length && results?.pages?.[0]?.results?.length) && (
<div className='mt-3'> <div className='mt-3'>
<List <List
pages={results!.pages as unknown as MovieDBResponse[]} pages={results!.pages as unknown as MovieDBResponse[]}
+86 -20
View File
@@ -4,6 +4,7 @@ import * as React from 'react';
import { useState } from 'react'; import { useState } from 'react';
import * as DialogPrimitive from '@radix-ui/react-dialog'; import * as DialogPrimitive from '@radix-ui/react-dialog';
import useScrollDelta from '@/hooks/useScrollDelta';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
const Dialog = DialogPrimitive.Root; const Dialog = DialogPrimitive.Root;
@@ -21,7 +22,7 @@ const DialogOverlay = React.forwardRef<
<DialogPrimitive.Overlay <DialogPrimitive.Overlay
ref={ref} ref={ref}
className={cn( 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 className
)} )}
{...props} {...props}
@@ -33,24 +34,87 @@ DialogOverlay.displayName = DialogPrimitive.Overlay.displayName;
const DialogContent = React.forwardRef< const DialogContent = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Content>, React.ElementRef<typeof DialogPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> & { onClose?: () => void }
>(({ className, children, ...props }, ref) => ( >(({ className, children, onClose, ...props }, ref) => {
<DialogPortal> const position = React.useRef(0);
<DialogOverlay> const close = React.useRef(false);
<DialogPrimitive.Content const wrapperRef = React.useRef<HTMLDivElement>(null);
ref={ref} const overlayRef = React.useRef<HTMLDivElement>(null);
{...props} const contentRef = React.useRef<HTMLDivElement | null>(null);
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', const { delta, onScroll } = useScrollDelta();
className
)} const handleTouchStart = (event: React.TouchEvent<HTMLDivElement>) => {
> position.current = event.touches[0].clientY;
{children} };
<div className='absolute top-full block w-10 h-[20vh] pointer-events-none' />
</DialogPrimitive.Content> const handleTouchMove = (event: React.TouchEvent<HTMLDivElement>) => {
</DialogOverlay> const threshold = 150;
</DialogPortal>
)); 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; DialogContent.displayName = DialogPrimitive.Content.displayName;
const DialogHeader = ({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) => ( const DialogHeader = ({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) => (
@@ -97,7 +161,9 @@ const Modal: React.FC<{
return ( return (
<Dialog open={open} onOpenChange={handleClose}> <Dialog open={open} onOpenChange={handleClose}>
<DialogContent className={className}>{children}</DialogContent> <DialogContent forceMount onClose={handleClose} className={className}>
{children}
</DialogContent>
</Dialog> </Dialog>
); );
}; };
+27
View File
@@ -0,0 +1,27 @@
import { useCallback, useRef } from 'react';
export default function useScrollDelta() {
const lastKnownScrollPosition = useRef(0);
const deltaY = useRef(0);
return {
delta: deltaY,
onScroll: useCallback((scroll: React.UIEvent<HTMLDivElement, UIEvent>) => {
let ticking = false;
if (!ticking) {
window.requestAnimationFrame(() => {
const top = (scroll.target as HTMLDivElement).scrollTop;
deltaY.current = top - lastKnownScrollPosition.current;
lastKnownScrollPosition.current = top;
ticking = false;
});
ticking = true;
}
}, [])
};
}